/* a generic DYNamic ARray implementation. */
-/* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2004-2015. The SimGrid Team.
* All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn, xbt, "Dynamic arrays");
-static XBT_INLINE void _sanity_check_dynar(xbt_dynar_t dynar)
+static inline void _sanity_check_dynar(xbt_dynar_t dynar)
{
xbt_assert(dynar, "dynar is NULL");
}
-static XBT_INLINE void _sanity_check_idx(int idx)
+static inline void _sanity_check_idx(int idx)
{
xbt_assert(idx >= 0, "dynar idx(=%d) < 0", (int) (idx));
}
-static XBT_INLINE void _check_inbound_idx(xbt_dynar_t dynar, int idx)
+static inline void _check_inbound_idx(xbt_dynar_t dynar, int idx)
{
- if (idx < 0 || idx >= dynar->used) {
+ if (idx < 0 || idx >= (int)dynar->used) {
THROWF(bound_error, idx,
"dynar is not that long. You asked %d, but it's only %lu long",
(int) (idx), (unsigned long) dynar->used);
}
}
-static XBT_INLINE void _check_populated_dynar(xbt_dynar_t dynar)
+static inline void _check_populated_dynar(xbt_dynar_t dynar)
{
if (dynar->used == 0) {
THROWF(bound_error, 0, "dynar %p is empty", dynar);
}
}
-static XBT_INLINE
+static inline
void _xbt_dynar_resize(xbt_dynar_t dynar, unsigned long new_size)
{
if (new_size != dynar->size) {
}
}
-static XBT_INLINE
+static inline
void _xbt_dynar_expand(xbt_dynar_t const dynar, const unsigned long nb)
{
const unsigned long old_size = dynar->size;
}
}
-static XBT_INLINE
+static inline
void *_xbt_dynar_elm(const xbt_dynar_t dynar, const unsigned long idx)
{
char *const data = (char *) dynar->data;
return data + idx * elmsize;
}
-static XBT_INLINE
+static inline
void
_xbt_dynar_get_elm(void *const dst,
const xbt_dynar_t dynar, const unsigned long idx)
memcpy(dst, elm, dynar->elmsize);
}
-static XBT_INLINE
- void
-_xbt_dynar_put_elm(const xbt_dynar_t dynar,
- const unsigned long idx, const void *const src)
-{
- void *const elm = _xbt_dynar_elm(dynar, idx);
- const unsigned long elmsize = dynar->elmsize;
-
- memcpy(elm, src, elmsize);
-}
-
void xbt_dynar_dump(xbt_dynar_t dynar)
{
XBT_INFO("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
*
* \param dynar who to squeeze
*/
-XBT_INLINE void xbt_dynar_reset(xbt_dynar_t const dynar)
+inline void xbt_dynar_reset(xbt_dynar_t const dynar)
{
_sanity_check_dynar(dynar);
- XBT_DEBUG("Reset the dynar %p", (void *) dynar);
+ XBT_CDEBUG(xbt_dyn, "Reset the dynar %p", (void *) dynar);
if (dynar->free_f) {
xbt_dynar_map(dynar, dynar->free_f);
}
* kilkil a dynar and its content
*/
-XBT_INLINE void xbt_dynar_free(xbt_dynar_t * dynar)
+inline void xbt_dynar_free(xbt_dynar_t * dynar)
{
if (dynar && *dynar) {
xbt_dynar_reset(*dynar);
*
* \param dynar the dynar we want to mesure
*/
-XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
+inline unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
{
return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
}
*\param dynar the dynat we want to check
*/
-XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar)
+inline int xbt_dynar_is_empty(const xbt_dynar_t dynar)
{
return (xbt_dynar_length(dynar) == 0);
}
* \param idx index of the slot we want to retrieve
* \param[out] dst where to put the result to.
*/
-XBT_INLINE void
+inline void
xbt_dynar_get_cpy(const xbt_dynar_t dynar,
const unsigned long idx, void *const dst)
{
* \warning The returned value is the actual content of the dynar.
* Make a copy before fooling with it.
*/
-XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar,
+inline void *xbt_dynar_get_ptr(const xbt_dynar_t dynar,
const unsigned long idx)
{
return res;
}
-XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
+inline void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
const unsigned long idx)
{
_sanity_check_dynar(dynar);
*
* If you want to free the previous content, use xbt_dynar_replace().
*/
-XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx,
+inline void xbt_dynar_set(xbt_dynar_t dynar, const int idx,
const void *const src)
{
* moving the previously existing value and all subsequent ones to one
* position right in the dynar.
*/
-XBT_INLINE void
+inline void
xbt_dynar_insert_at(xbt_dynar_t const dynar,
const int idx, const void *const src)
{
return it;
}
- THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem,
- dynar);
+ THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem, dynar);
+ return -1; // Won't happen, just to please eclipse
}
/** @brief Returns the position of the element in the dynar (or -1 if not found)
* You can then use regular affectation to set its value instead of relying
* on the slow memcpy. This is what xbt_dynar_push_as() does.
*/
-XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar)
+inline void *xbt_dynar_push_ptr(xbt_dynar_t const dynar)
{
return xbt_dynar_insert_at_ptr(dynar, dynar->used);
}
/** @brief Add an element at the end of the dynar */
-XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar,
+inline void xbt_dynar_push(xbt_dynar_t const dynar,
const void *const src)
{
/* checks done in xbt_dynar_insert_at_ptr */
* You can then use regular affectation to set its value instead of relying
* on the slow memcpy. This is what xbt_dynar_pop_as() does.
*/
-XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar)
+inline void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar)
{
_check_populated_dynar(dynar);
- XBT_DEBUG("Pop %p", (void *) dynar);
+ XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar);
dynar->used--;
return _xbt_dynar_elm(dynar, dynar->used);
}
/** @brief Get and remove the last element of the dynar */
-XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst)
+inline void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst)
{
/* sanity checks done by remove_at */
- XBT_DEBUG("Pop %p", (void *) dynar);
+ XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar);
xbt_dynar_remove_at(dynar, dynar->used - 1, dst);
}
*
* This is less efficient than xbt_dynar_push()
*/
-XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar,
+inline void xbt_dynar_unshift(xbt_dynar_t const dynar,
const void *const src)
{
*
* This is less efficient than xbt_dynar_pop()
*/
-XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst)
+inline void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst)
{
/* sanity checks done by remove_at */
* The mapped function may change the value of the element itself,
* but should not mess with the structure of the dynar.
*/
-XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar,
+inline void xbt_dynar_map(const xbt_dynar_t dynar,
void_f_pvoid_t const op)
{
char *const data = (char *) dynar->data;
*
* This function can be used while traversing without problem.
*/
-XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
+inline void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
unsigned int *const cursor)
{
* Remark: if the elements stored in the dynar are structures, the compar_fn
* function has to retrieve the field to sort first.
*/
-XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar,
+inline void xbt_dynar_sort(xbt_dynar_t dynar,
int_f_cpvoid_cpvoid_t compar_fn)
{
-#ifdef HAVE_MERGESORT
- mergesort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
-#else
qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
-#endif
+}
+
+static int strcmp_voidp(const void *pa, const void *pb) {
+ return strcmp(*(const char **)pa, *(const char **)pb);
+}
+/** @brief Sorts a dynar of strings (ie, char* data) */
+xbt_dynar_t xbt_dynar_sort_strings(xbt_dynar_t dynar)
+{
+ xbt_dynar_sort(dynar, strcmp_voidp);
+ return dynar; // to enable functional uses
}
/** @brief Sorts a dynar according to their color assuming elements can have only three colors.
}
/** @brief Transform a dynar into a NULL terminated array.
- * The dynar won't be usable afterwards.
- * \param dynar the dynar to transform
+ *
+ * \param dynar the dynar to transform
+ * \return pointer to the first element of the array
+ *
+ * Note: The dynar won't be usable afterwards.
*/
-XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar)
+inline void *xbt_dynar_to_array(xbt_dynar_t dynar)
{
void *res;
xbt_dynar_shrink(dynar, 1);
return res;
}
-/*
- * Return 0 if d1 and d2 are equal and 1 if not equal
+/** @brief Compare two dynars
+ *
+ * \param d1 first dynar to compare
+ * \param d2 second dynar to compare
+ * \param compar function to use to compare elements
+ * \return 0 if d1 and d2 are equal and 1 if not equal
+ *
+ * d1 and d2 should be dynars of pointers. The compar function takes two
+ * elements and returns 0 when they are considered equal, and a value different
+ * of zero when they are considered different. Finally, d2 is destroyed
+ * afterwards.
*/
-XBT_INLINE int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
+int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
int(*compar)(const void *, const void *))
{
int i ;
xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
}
+ int* pi;
+ xbt_dynar_foreach_ptr(d, cursor, pi) {
+ *pi = 0;
+ }
+ xbt_dynar_foreach(d, cursor, i) {
+ xbt_test_assert(i == 0, "The value is not the same as the expected one.");
+ }
+ xbt_dynar_foreach_ptr(d, cursor, pi) {
+ *pi = 1;
+ }
+ xbt_dynar_foreach(d, cursor, i) {
+ xbt_test_assert(i == 1, "The value is not the same as the expected one.");
+ }
+
/* 5. Free the resources */
xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
/* 1. Populate the dynar */
for (cpt = 0; cpt < NB_ELEM; cpt++) {
sprintf(buf, "%d", cpt);
- s1 = strdup(buf);
+ s1 = xbt_strdup(buf);
xbt_dynar_push(d, &s1);
}
for (cpt = 0; cpt < NB_ELEM; cpt++) {
sprintf(buf, "%d", cpt);
- s1 = strdup(buf);
+ s1 = xbt_strdup(buf);
xbt_dynar_replace(d, cpt, &s1);
}
for (cpt = 0; cpt < NB_ELEM; cpt++) {
sprintf(buf, "%d", cpt);
- s1 = strdup(buf);
+ s1 = xbt_strdup(buf);
xbt_dynar_replace(d, cpt, &s1);
}
for (cpt = 0; cpt < NB_ELEM; cpt++) {
sprintf(buf, "%d", cpt);
- s1 = strdup(buf);
+ s1 = xbt_strdup(buf);
xbt_dynar_replace(d, cpt, &s1);
}
for (cpt = 0; cpt < NB_ELEM; cpt++) {
d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
for (cpt = 0; cpt < NB_ELEM; cpt++) {
sprintf(buf, "%d", cpt);
- s1 = strdup(buf);
+ s1 = xbt_strdup(buf);
xbt_dynar_unshift(d, &s1);
}
/* 2. Traverse the dynar with the macro */
d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
for (cpt = 0; cpt < NB_ELEM; cpt++) {
sprintf(buf, "%d", cpt);
- s1 = strdup(buf);
+ s1 = xbt_strdup(buf);
xbt_dynar_push(d, &s1);
}
for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
sprintf(buf, "%d", cpt);
- s1 = strdup(buf);
+ s1 = xbt_strdup(buf);
xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
}
d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
for (cpt = 0; cpt < NB_ELEM; cpt++) {
sprintf(buf, "%d", cpt);
- s1 = strdup(buf);
+ s1 = xbt_strdup(buf);
xbt_dynar_push(d, &s1);
}
for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {