1 /* a generic DYNamic ARray implementation. */
3 /* Copyright (c) 2004-2013. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/sysdep.h"
13 #include "xbt/dynar.h"
14 #include <sys/types.h>
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_dyn, xbt, "Dynamic arrays");
18 static XBT_INLINE void _sanity_check_dynar(xbt_dynar_t dynar)
20 xbt_assert(dynar, "dynar is NULL");
23 static XBT_INLINE void _sanity_check_idx(int idx)
25 xbt_assert(idx >= 0, "dynar idx(=%d) < 0", (int) (idx));
28 static XBT_INLINE void _check_inbound_idx(xbt_dynar_t dynar, int idx)
30 if (idx < 0 || idx >= dynar->used) {
31 THROWF(bound_error, idx,
32 "dynar is not that long. You asked %d, but it's only %lu long",
33 (int) (idx), (unsigned long) dynar->used);
37 static XBT_INLINE void _check_populated_dynar(xbt_dynar_t dynar)
39 if (dynar->used == 0) {
40 THROWF(bound_error, 0, "dynar %p is empty", dynar);
45 void _xbt_dynar_resize(xbt_dynar_t dynar, unsigned long new_size)
47 if (new_size != dynar->size) {
48 dynar->size = new_size;
49 dynar->data = xbt_realloc(dynar->data, new_size * dynar->elmsize);
54 void _xbt_dynar_expand(xbt_dynar_t const dynar, const unsigned long nb)
56 const unsigned long old_size = dynar->size;
59 const unsigned long expand = 2 * (old_size + 1);
60 _xbt_dynar_resize(dynar, (nb > expand ? nb : expand));
61 XBT_DEBUG("expand %p from %lu to %lu elements",
62 dynar, old_size, dynar->size);
67 void *_xbt_dynar_elm(const xbt_dynar_t dynar, const unsigned long idx)
69 char *const data = (char *) dynar->data;
70 const unsigned long elmsize = dynar->elmsize;
72 return data + idx * elmsize;
77 _xbt_dynar_get_elm(void *const dst,
78 const xbt_dynar_t dynar, const unsigned long idx)
80 void *const elm = _xbt_dynar_elm(dynar, idx);
82 memcpy(dst, elm, dynar->elmsize);
87 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
88 const unsigned long idx, const void *const src)
90 void *const elm = _xbt_dynar_elm(dynar, idx);
91 const unsigned long elmsize = dynar->elmsize;
93 memcpy(elm, src, elmsize);
96 void xbt_dynar_dump(xbt_dynar_t dynar)
98 XBT_INFO("Dynar dump: size=%lu; used=%lu; elmsize=%lu; data=%p; free_f=%p",
99 dynar->size, dynar->used, dynar->elmsize, dynar->data,
103 /** @brief Constructor
105 * \param elmsize size of each element in the dynar
106 * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
108 * Creates a new dynar. If a free_func is provided, the elements have to be
109 * pointer of pointer. That is to say that dynars can contain either base
110 * types (int, char, double, etc) or pointer of pointers (struct **).
113 xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t const free_f)
116 xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t, 1);
120 dynar->elmsize = elmsize;
122 dynar->free_f = free_f;
127 /** @brief Destructor of the structure not touching to the content
129 * \param dynar poor victim
131 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
132 * is not touched (the \a free_f function is not used)
134 void xbt_dynar_free_container(xbt_dynar_t * dynar)
136 if (dynar && *dynar) {
137 xbt_dynar_t d = *dynar;
144 /** @brief Frees the content and set the size to 0
146 * \param dynar who to squeeze
148 XBT_INLINE void xbt_dynar_reset(xbt_dynar_t const dynar)
150 _sanity_check_dynar(dynar);
152 XBT_CDEBUG(xbt_dyn, "Reset the dynar %p", (void *) dynar);
154 xbt_dynar_map(dynar, dynar->free_f);
159 /** @brief Merge dynar d2 into d1
161 * \param d1 dynar to keep
162 * \param d2 dynar to merge into d1. This dynar is free at end.
164 void xbt_dynar_merge(xbt_dynar_t *d1, xbt_dynar_t *d2)
166 if((*d1)->elmsize != (*d2)->elmsize)
167 xbt_die("Element size must are not equal");
169 const unsigned long elmsize = (*d1)->elmsize;
171 void *ptr = _xbt_dynar_elm((*d2), 0);
172 _xbt_dynar_resize(*d1, (*d1)->size + (*d2)->size);
173 void *elm = _xbt_dynar_elm((*d1), (*d1)->used);
175 memcpy(elm, ptr, ((*d2)->size)*elmsize);
176 (*d1)->used += (*d2)->used;
182 * \brief Shrink the dynar by removing empty slots at the end of the internal array
183 * \param dynar a dynar
184 * \param empty_slots_wanted number of empty slots you want to keep at the end of the
185 * internal array for further insertions
187 * Reduces the internal array size of the dynar to the number of elements plus
188 * \a empty_slots_wanted.
189 * After removing elements from the dynar, you can call this function to make
190 * the dynar use less memory.
191 * Set \a empty_slots_wanted to zero to reduce the dynar internal array as much
193 * Note that if \a empty_slots_wanted is greater than the array size, the internal
194 * array is expanded instead of shriked.
196 void xbt_dynar_shrink(xbt_dynar_t dynar, int empty_slots_wanted)
198 _xbt_dynar_resize(dynar, dynar->used + empty_slots_wanted);
201 /** @brief Destructor
203 * \param dynar poor victim
205 * kilkil a dynar and its content
208 XBT_INLINE void xbt_dynar_free(xbt_dynar_t * dynar)
210 if (dynar && *dynar) {
211 xbt_dynar_reset(*dynar);
212 xbt_dynar_free_container(dynar);
216 /** \brief free a dynar passed as void* (handy to store dynar in dynars or dict) */
217 void xbt_dynar_free_voidp(void *d)
219 xbt_dynar_t dynar = (xbt_dynar_t)d;
220 xbt_dynar_free(&dynar);
223 /** @brief Count of dynar's elements
225 * \param dynar the dynar we want to mesure
227 XBT_INLINE unsigned long xbt_dynar_length(const xbt_dynar_t dynar)
229 return (dynar ? (unsigned long) dynar->used : (unsigned long) 0);
232 /**@brief check if a dynar is empty
234 *\param dynar the dynat we want to check
237 XBT_INLINE int xbt_dynar_is_empty(const xbt_dynar_t dynar)
239 return (xbt_dynar_length(dynar) == 0);
242 /** @brief Retrieve a copy of the Nth element of a dynar.
244 * \param dynar information dealer
245 * \param idx index of the slot we want to retrieve
246 * \param[out] dst where to put the result to.
249 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
250 const unsigned long idx, void *const dst)
252 _sanity_check_dynar(dynar);
253 _check_inbound_idx(dynar, idx);
255 _xbt_dynar_get_elm(dst, dynar, idx);
258 /** @brief Retrieve a pointer to the Nth element of a dynar.
260 * \param dynar information dealer
261 * \param idx index of the slot we want to retrieve
262 * \return the \a idx-th element of \a dynar.
264 * \warning The returned value is the actual content of the dynar.
265 * Make a copy before fooling with it.
267 XBT_INLINE void *xbt_dynar_get_ptr(const xbt_dynar_t dynar,
268 const unsigned long idx)
272 _sanity_check_dynar(dynar);
273 _check_inbound_idx(dynar, idx);
275 res = _xbt_dynar_elm(dynar, idx);
279 XBT_INLINE void *xbt_dynar_set_at_ptr(const xbt_dynar_t dynar,
280 const unsigned long idx)
282 _sanity_check_dynar(dynar);
284 if (idx >= dynar->used) {
285 _xbt_dynar_expand(dynar, idx + 1);
286 if (idx > dynar->used) {
287 memset(_xbt_dynar_elm(dynar, dynar->used), 0,
288 (idx - dynar->used) * dynar->elmsize);
290 dynar->used = idx + 1;
292 return _xbt_dynar_elm(dynar, idx);
295 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value at this position is NOT freed
297 * \param dynar information dealer
298 * \param idx index of the slot we want to modify
299 * \param src What will be feeded to the dynar
301 * If you want to free the previous content, use xbt_dynar_replace().
303 XBT_INLINE void xbt_dynar_set(xbt_dynar_t dynar, const int idx,
304 const void *const src)
307 memcpy(xbt_dynar_set_at_ptr(dynar, idx), src, dynar->elmsize);
310 /** @brief Set the Nth element of a dynar (expanded if needed). Previous value is freed
316 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
317 * free the previous value at this position. If you don't want to free the
318 * previous content, use xbt_dynar_set().
321 xbt_dynar_replace(xbt_dynar_t dynar,
322 const unsigned long idx, const void *const object)
324 _sanity_check_dynar(dynar);
326 if (idx < dynar->used && dynar->free_f) {
327 void *const old_object = _xbt_dynar_elm(dynar, idx);
329 dynar->free_f(old_object);
332 xbt_dynar_set(dynar, idx, object);
335 /** @brief Make room for a new element, and return a pointer to it
337 * You can then use regular affectation to set its value instead of relying
338 * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
340 void *xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar, const int idx)
343 unsigned long old_used;
344 unsigned long new_used;
347 _sanity_check_dynar(dynar);
348 _sanity_check_idx(idx);
350 old_used = dynar->used;
351 new_used = old_used + 1;
353 _xbt_dynar_expand(dynar, new_used);
355 nb_shift = old_used - idx;
358 memmove(_xbt_dynar_elm(dynar, idx + 1),
359 _xbt_dynar_elm(dynar, idx), nb_shift * dynar->elmsize);
362 dynar->used = new_used;
363 res = _xbt_dynar_elm(dynar, idx);
367 /** @brief Set the Nth dynar's element, expanding the dynar and sliding the previous values to the right
369 * Set the Nth element of a dynar, expanding the dynar if needed, and
370 * moving the previously existing value and all subsequent ones to one
371 * position right in the dynar.
374 xbt_dynar_insert_at(xbt_dynar_t const dynar,
375 const int idx, const void *const src)
378 /* checks done in xbt_dynar_insert_at_ptr */
379 memcpy(xbt_dynar_insert_at_ptr(dynar, idx), src, dynar->elmsize);
382 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
384 * Get the Nth element of a dynar, removing it from the dynar and moving
385 * all subsequent values to one position left in the dynar.
387 * If the object argument of this function is a non-null pointer, the removed
388 * element is copied to this address. If not, the element is freed using the
389 * free_f function passed at dynar creation.
392 xbt_dynar_remove_at(xbt_dynar_t const dynar,
393 const int idx, void *const object)
395 unsigned long nb_shift;
396 unsigned long offset;
398 _sanity_check_dynar(dynar);
399 _check_inbound_idx(dynar, idx);
402 _xbt_dynar_get_elm(object, dynar, idx);
403 } else if (dynar->free_f) {
404 dynar->free_f(_xbt_dynar_elm(dynar, idx));
407 nb_shift = dynar->used - 1 - idx;
410 offset = nb_shift * dynar->elmsize;
411 memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + 1),
418 /** @brief Remove a slice of the dynar, sliding the rest of the values to the left
420 * This function removes an n-sized slice that starts at element idx. It is equivalent
421 * to xbt_dynar_remove_at with a NULL object argument if n equals to 1.
423 * Each of the removed elements is freed using the free_f function passed at dynar
427 xbt_dynar_remove_n_at(xbt_dynar_t const dynar,
428 const unsigned int n, const int idx)
430 unsigned long nb_shift;
431 unsigned long offset;
436 _sanity_check_dynar(dynar);
437 _check_inbound_idx(dynar, idx);
438 _check_inbound_idx(dynar, idx + n - 1);
441 for (cur = idx; cur < idx + n; cur++) {
442 dynar->free_f(_xbt_dynar_elm(dynar, cur));
446 nb_shift = dynar->used - n - idx;
449 offset = nb_shift * dynar->elmsize;
450 memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + n),
457 /** @brief Returns the position of the element in the dynar
459 * Beware that if your dynar contains pointed values (such as strings) instead
460 * of scalar, this function compares the pointer value, not what's pointed. The only
461 * solution to search for a pointed value is then to write the foreach loop yourself:
463 * signed int position = -1;
464 * xbt_dynar_foreach(dynar, iter, elem) {
465 * if (!memcmp(elem, searched_element, sizeof(*elem))) {
472 * Raises not_found_error if not found. If you have less than 2 millions elements,
473 * you probably want to use #xbt_dynar_search_or_negative() instead, so that you
474 * don't have to TRY/CATCH on element not found.
476 unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem)
480 for (it = 0; it < dynar->used; it++)
481 if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
485 THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem,
489 /** @brief Returns the position of the element in the dynar (or -1 if not found)
491 * Beware that if your dynar contains pointed values (such as
492 * strings) instead of scalar, this function is probably not what you
493 * want. Check the documentation of xbt_dynar_search() for more info.
495 * Note that usually, the dynar indices are unsigned integers. If you have more
496 * than 2 million elements in your dynar, this very function will not work (but the other will).
498 signed int xbt_dynar_search_or_negative(xbt_dynar_t const dynar, void *const elem)
502 for (it = 0; it < dynar->used; it++)
503 if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) {
510 /** @brief Returns a boolean indicating whether the element is part of the dynar
512 * Beware that if your dynar contains pointed values (such as
513 * strings) instead of scalar, this function is probably not what you
514 * want. Check the documentation of xbt_dynar_search() for more info.
516 int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem)
522 xbt_dynar_search(dynar, elem);
525 if (e.category == not_found_error) {
534 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
536 * You can then use regular affectation to set its value instead of relying
537 * on the slow memcpy. This is what xbt_dynar_push_as() does.
539 XBT_INLINE void *xbt_dynar_push_ptr(xbt_dynar_t const dynar)
541 return xbt_dynar_insert_at_ptr(dynar, dynar->used);
544 /** @brief Add an element at the end of the dynar */
545 XBT_INLINE void xbt_dynar_push(xbt_dynar_t const dynar,
546 const void *const src)
548 /* checks done in xbt_dynar_insert_at_ptr */
549 memcpy(xbt_dynar_insert_at_ptr(dynar, dynar->used), src, dynar->elmsize);
552 /** @brief Mark the last dynar's element as unused and return a pointer to it.
554 * You can then use regular affectation to set its value instead of relying
555 * on the slow memcpy. This is what xbt_dynar_pop_as() does.
557 XBT_INLINE void *xbt_dynar_pop_ptr(xbt_dynar_t const dynar)
559 _check_populated_dynar(dynar);
560 XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar);
562 return _xbt_dynar_elm(dynar, dynar->used);
565 /** @brief Get and remove the last element of the dynar */
566 XBT_INLINE void xbt_dynar_pop(xbt_dynar_t const dynar, void *const dst)
569 /* sanity checks done by remove_at */
570 XBT_CDEBUG(xbt_dyn, "Pop %p", (void *) dynar);
571 xbt_dynar_remove_at(dynar, dynar->used - 1, dst);
574 /** @brief Add an element at the begining of the dynar.
576 * This is less efficient than xbt_dynar_push()
578 XBT_INLINE void xbt_dynar_unshift(xbt_dynar_t const dynar,
579 const void *const src)
582 /* sanity checks done by insert_at */
583 xbt_dynar_insert_at(dynar, 0, src);
586 /** @brief Get and remove the first element of the dynar.
588 * This is less efficient than xbt_dynar_pop()
590 XBT_INLINE void xbt_dynar_shift(xbt_dynar_t const dynar, void *const dst)
593 /* sanity checks done by remove_at */
594 xbt_dynar_remove_at(dynar, 0, dst);
597 /** @brief Apply a function to each member of a dynar
599 * The mapped function may change the value of the element itself,
600 * but should not mess with the structure of the dynar.
602 XBT_INLINE void xbt_dynar_map(const xbt_dynar_t dynar,
603 void_f_pvoid_t const op)
605 char *const data = (char *) dynar->data;
606 const unsigned long elmsize = dynar->elmsize;
607 const unsigned long used = dynar->used;
610 _sanity_check_dynar(dynar);
612 for (i = 0; i < used; i++) {
613 char* elm = (char*) data + i * elmsize;
619 /** @brief Removes and free the entry pointed by the cursor
621 * This function can be used while traversing without problem.
623 XBT_INLINE void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
624 unsigned int *const cursor)
627 xbt_dynar_remove_at(dynar, (*cursor)--, NULL);
630 /** @brief Sorts a dynar according to the function <tt>compar_fn</tt>
632 * \param dynar the dynar to sort
633 * \param compar_fn comparison function of type (int (compar_fn*) (void*) (void*)).
635 * Remark: if the elements stored in the dynar are structures, the compar_fn
636 * function has to retrieve the field to sort first.
638 XBT_INLINE void xbt_dynar_sort(xbt_dynar_t dynar,
639 int_f_cpvoid_cpvoid_t compar_fn)
641 qsort(dynar->data, dynar->used, dynar->elmsize, compar_fn);
644 /** @brief Sorts a dynar according to their color assuming elements can have only three colors.
645 * Since there are only three colors, it is linear and much faster than a classical sort.
646 * See for example http://en.wikipedia.org/wiki/Dutch_national_flag_problem
648 * \param dynar the dynar to sort
649 * \param color the color function of type (int (compar_fn*) (void*) (void*)). The return value of color is assumed to be 0, 1, or 2.
651 * At the end of the call, elements with color 0 are at the beginning of the dynar, elements with color 2 are at the end and elements with color 1 are in the middle.
653 * Remark: if the elements stored in the dynar are structures, the color
654 * function has to retrieve the field to sort first.
656 XBT_PUBLIC(void) xbt_dynar_three_way_partition(xbt_dynar_t const dynar,
660 unsigned long int p = -1;
661 unsigned long int q = dynar->used;
662 const unsigned long elmsize = dynar->elmsize;
663 void *tmp = xbt_malloc(elmsize);
666 for (i = 0; i < q;) {
667 void *elmi = _xbt_dynar_elm(dynar, i);
668 int colori = color(elmi);
674 elm = _xbt_dynar_elm(dynar, ++p);
676 } else { /* colori == 2 */
677 elm = _xbt_dynar_elm(dynar, --q);
680 memcpy(tmp, elm, elmsize);
681 memcpy(elm, elmi, elmsize);
682 memcpy(elmi, tmp, elmsize);
689 /** @brief Transform a dynar into a NULL terminated array.
690 * The dynar won't be usable afterwards.
691 * \param dynar the dynar to transform
693 XBT_INLINE void * xbt_dynar_to_array (xbt_dynar_t dynar)
696 xbt_dynar_shrink(dynar, 1);
697 memset(xbt_dynar_push_ptr(dynar), 0, dynar->elmsize);
704 * Return 0 if d1 and d2 are equal and 1 if not equal
706 int xbt_dynar_compare(xbt_dynar_t d1, xbt_dynar_t d2,
707 int(*compar)(const void *, const void *))
711 if((!d1) && (!d2)) return 0;
714 XBT_DEBUG("NULL dynar d1=%p d2=%p",d1,d2);
718 if((d1->elmsize)!=(d2->elmsize))
720 XBT_DEBUG("Size of elmsize d1=%lu d2=%lu",d1->elmsize,d2->elmsize);
724 if(xbt_dynar_length(d1) != xbt_dynar_length(d2))
726 XBT_DEBUG("Size of dynar d1=%lu d2=%lu",xbt_dynar_length(d1),xbt_dynar_length(d2));
731 size = xbt_dynar_length(d1);
734 void *data1 = xbt_dynar_get_as(d1, i, void *);
735 void *data2 = xbt_dynar_get_as(d2, i, void *);
736 XBT_DEBUG("link[%d] d1=%p d2=%p",i,data1,data2);
737 if(compar(data1,data2)){
750 XBT_TEST_SUITE("dynar", "Dynar data container");
751 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_dyn);
753 XBT_TEST_UNIT("int", test_dynar_int, "Dynars of integers")
755 /* Vars_decl [doxygen cruft] */
761 xbt_test_add("==== Traverse the empty dynar");
762 d = xbt_dynar_new(sizeof(int), NULL);
763 xbt_dynar_foreach(d, cursor, i) {
764 xbt_die( "Damnit, there is something in the empty dynar");
766 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
767 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
768 /* in your code is naturally the way to go outside a regression test */
771 ("==== Push %d int, set them again 3 times, traverse them, shift them",
773 /* Populate_ints [doxygen cruft] */
774 /* 1. Populate the dynar */
775 d = xbt_dynar_new(sizeof(int), NULL);
776 for (cpt = 0; cpt < NB_ELEM; cpt++) {
777 xbt_dynar_push_as(d, int, cpt); /* This is faster (and possible only with scalars) */
778 /* xbt_dynar_push(d,&cpt); This would also work */
779 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
782 /* 2. Traverse manually the dynar */
783 for (cursor = 0; cursor < NB_ELEM; cursor++) {
784 iptr = xbt_dynar_get_ptr(d, cursor);
785 xbt_test_assert(cursor == *iptr,
786 "The retrieved value is not the same than the injected one (%u!=%d)",
790 /* 3. Traverse the dynar using the neat macro to that extend */
791 xbt_dynar_foreach(d, cursor, cpt) {
792 xbt_test_assert(cursor == cpt,
793 "The retrieved value is not the same than the injected one (%u!=%d)",
796 /* end_of_traversal */
798 for (cpt = 0; cpt < NB_ELEM; cpt++)
799 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
801 for (cpt = 0; cpt < NB_ELEM; cpt++)
802 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
803 /* xbt_dynar_set(d,cpt,&cpt); */
805 for (cpt = 0; cpt < NB_ELEM; cpt++)
806 *(int *) xbt_dynar_get_ptr(d, cpt) = cpt;
809 xbt_dynar_foreach(d, cursor, i) {
810 xbt_test_assert(i == cpt,
811 "The retrieved value is not the same than the injected one (%d!=%d)",
815 xbt_test_assert(cpt == NB_ELEM,
816 "Cannot retrieve my %d values. Last got one is %d",
819 /* shifting [doxygen cruft] */
820 /* 4. Shift all the values */
821 for (cpt = 0; cpt < NB_ELEM; cpt++) {
822 xbt_dynar_shift(d, &i);
823 xbt_test_assert(i == cpt,
824 "The retrieved value is not the same than the injected one (%d!=%d)",
826 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
829 /* 5. Free the resources */
830 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
831 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
832 /* in your code is naturally the way to go outside a regression test */
834 xbt_test_add("==== Unshift/pop %d int", NB_ELEM);
835 d = xbt_dynar_new(sizeof(int), NULL);
836 for (cpt = 0; cpt < NB_ELEM; cpt++) {
837 xbt_dynar_unshift(d, &cpt);
838 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
840 for (cpt = 0; cpt < NB_ELEM; cpt++) {
841 i = xbt_dynar_pop_as(d, int);
842 xbt_test_assert(i == cpt,
843 "The retrieved value is not the same than the injected one (%d!=%d)",
845 xbt_test_log("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
847 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
848 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
849 /* in your code is naturally the way to go outside a regression test */
853 ("==== Push %d int, insert 1000 int in the middle, shift everything",
855 d = xbt_dynar_new(sizeof(int), NULL);
856 for (cpt = 0; cpt < NB_ELEM; cpt++) {
857 xbt_dynar_push_as(d, int, cpt);
858 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
860 for (cpt = 0; cpt < NB_ELEM/5; cpt++) {
861 xbt_dynar_insert_at_as(d, NB_ELEM/2, int, cpt);
862 XBT_DEBUG("Push %d, length=%lu", cpt, xbt_dynar_length(d));
865 for (cpt = 0; cpt < NB_ELEM/2; cpt++) {
866 xbt_dynar_shift(d, &i);
867 xbt_test_assert(i == cpt,
868 "The retrieved value is not the same than the injected one at the begining (%d!=%d)",
870 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
872 for (cpt = 999; cpt >= 0; cpt--) {
873 xbt_dynar_shift(d, &i);
874 xbt_test_assert(i == cpt,
875 "The retrieved value is not the same than the injected one in the middle (%d!=%d)",
878 for (cpt = 2500; cpt < NB_ELEM; cpt++) {
879 xbt_dynar_shift(d, &i);
880 xbt_test_assert(i == cpt,
881 "The retrieved value is not the same than the injected one at the end (%d!=%d)",
884 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
885 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
886 /* in your code is naturally the way to go outside a regression test */
888 xbt_test_add("==== Push %d int, remove 2000-4000. free the rest",
890 d = xbt_dynar_new(sizeof(int), NULL);
891 for (cpt = 0; cpt < NB_ELEM; cpt++)
892 xbt_dynar_push_as(d, int, cpt);
894 for (cpt = 2000; cpt < 4000; cpt++) {
895 xbt_dynar_remove_at(d, 2000, &i);
896 xbt_test_assert(i == cpt,
897 "Remove a bad value. Got %d, expected %d", i, cpt);
898 XBT_DEBUG("remove %d, length=%lu", cpt, xbt_dynar_length(d));
900 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
901 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
902 /* in your code is naturally the way to go outside a regression test */
905 /*******************************************************************************/
906 /*******************************************************************************/
907 /*******************************************************************************/
908 XBT_TEST_UNIT("insert",test_dynar_insert,"Using the xbt_dynar_insert and xbt_dynar_remove functions")
910 xbt_dynar_t d = xbt_dynar_new(sizeof(unsigned int), NULL);
914 xbt_test_add("==== Insert %d int, traverse them, remove them",NB_ELEM);
915 /* Populate_ints [doxygen cruft] */
916 /* 1. Populate the dynar */
917 for (cpt = 0; cpt < NB_ELEM; cpt++) {
918 xbt_dynar_insert_at(d, cpt, &cpt);
919 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
922 /* 3. Traverse the dynar */
923 xbt_dynar_foreach(d, cursor, cpt) {
924 xbt_test_assert(cursor == cpt,
925 "The retrieved value is not the same than the injected one (%u!=%d)",
928 /* end_of_traversal */
930 /* Re-fill with the same values using set_as (and re-verify) */
931 for (cpt = 0; cpt < NB_ELEM; cpt++)
932 xbt_dynar_set_as(d, cpt, int, cpt);
933 xbt_dynar_foreach(d, cursor, cpt)
934 xbt_test_assert(cursor == cpt,
935 "The retrieved value is not the same than the injected one (%u!=%d)",
938 for (cpt = 0; cpt < NB_ELEM; cpt++) {
940 xbt_dynar_remove_at(d,0,&val);
941 xbt_test_assert(cpt == val,
942 "The retrieved value is not the same than the injected one (%u!=%d)",
945 xbt_test_assert(xbt_dynar_is_empty(d),
946 "There is still %lu elements in the dynar after removing everything",
947 xbt_dynar_length(d));
950 /* ********************* */
951 xbt_test_add("==== Insert %d int in reverse order, traverse them, remove them",NB_ELEM);
952 d = xbt_dynar_new(sizeof(int), NULL);
953 for (cpt = NB_ELEM-1; cpt >=0; cpt--) {
954 xbt_dynar_replace(d, cpt, &cpt);
955 xbt_test_log("Push %d, length=%lu", cpt, xbt_dynar_length(d));
958 /* 3. Traverse the dynar */
959 xbt_dynar_foreach(d, cursor, cpt) {
960 xbt_test_assert(cursor == cpt,
961 "The retrieved value is not the same than the injected one (%u!=%d)",
964 /* end_of_traversal */
966 for (cpt =NB_ELEM-1; cpt >=0; cpt--) {
968 xbt_dynar_remove_at(d,xbt_dynar_length(d)-1,&val);
969 xbt_test_assert(cpt == val,
970 "The retrieved value is not the same than the injected one (%u!=%d)",
973 xbt_test_assert(xbt_dynar_is_empty(d),
974 "There is still %lu elements in the dynar after removing everything",
975 xbt_dynar_length(d));
979 /*******************************************************************************/
980 /*******************************************************************************/
981 /*******************************************************************************/
982 XBT_TEST_UNIT("double", test_dynar_double, "Dynars of doubles")
989 xbt_test_add("==== Traverse the empty dynar");
990 d = xbt_dynar_new(sizeof(int), NULL);
991 xbt_dynar_foreach(d, cursor, cpt) {
992 xbt_test_assert(FALSE,
993 "Damnit, there is something in the empty dynar");
995 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
996 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
997 /* in your code is naturally the way to go outside a regression test */
999 xbt_test_add("==== Push/shift 5000 doubles");
1000 d = xbt_dynar_new(sizeof(double), NULL);
1001 for (cpt = 0; cpt < 5000; cpt++) {
1003 xbt_dynar_push(d, &d1);
1005 xbt_dynar_foreach(d, cursor, d2) {
1006 d1 = (double) cursor;
1007 xbt_test_assert(d1 == d2,
1008 "The retrieved value is not the same than the injected one (%f!=%f)",
1011 for (cpt = 0; cpt < 5000; cpt++) {
1013 xbt_dynar_shift(d, &d2);
1014 xbt_test_assert(d1 == d2,
1015 "The retrieved value is not the same than the injected one (%f!=%f)",
1018 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1019 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1020 /* in your code is naturally the way to go outside a regression test */
1022 xbt_test_add("==== Unshift/pop 5000 doubles");
1023 d = xbt_dynar_new(sizeof(double), NULL);
1024 for (cpt = 0; cpt < 5000; cpt++) {
1026 xbt_dynar_unshift(d, &d1);
1028 for (cpt = 0; cpt < 5000; cpt++) {
1030 xbt_dynar_pop(d, &d2);
1031 xbt_test_assert(d1 == d2,
1032 "The retrieved value is not the same than the injected one (%f!=%f)",
1035 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1036 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1037 /* in your code is naturally the way to go outside a regression test */
1042 ("==== Push 5000 doubles, insert 1000 doubles in the middle, shift everything");
1043 d = xbt_dynar_new(sizeof(double), NULL);
1044 for (cpt = 0; cpt < 5000; cpt++) {
1046 xbt_dynar_push(d, &d1);
1048 for (cpt = 0; cpt < 1000; cpt++) {
1050 xbt_dynar_insert_at(d, 2500, &d1);
1053 for (cpt = 0; cpt < 2500; cpt++) {
1055 xbt_dynar_shift(d, &d2);
1056 xbt_test_assert(d1 == d2,
1057 "The retrieved value is not the same than the injected one at the begining (%f!=%f)",
1059 XBT_DEBUG("Pop %d, length=%lu", cpt, xbt_dynar_length(d));
1061 for (cpt = 999; cpt >= 0; cpt--) {
1063 xbt_dynar_shift(d, &d2);
1064 xbt_test_assert(d1 == d2,
1065 "The retrieved value is not the same than the injected one in the middle (%f!=%f)",
1068 for (cpt = 2500; cpt < 5000; cpt++) {
1070 xbt_dynar_shift(d, &d2);
1071 xbt_test_assert(d1 == d2,
1072 "The retrieved value is not the same than the injected one at the end (%f!=%f)",
1075 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1076 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1077 /* in your code is naturally the way to go outside a regression test */
1080 xbt_test_add("==== Push 5000 double, remove 2000-4000. free the rest");
1081 d = xbt_dynar_new(sizeof(double), NULL);
1082 for (cpt = 0; cpt < 5000; cpt++) {
1084 xbt_dynar_push(d, &d1);
1086 for (cpt = 2000; cpt < 4000; cpt++) {
1088 xbt_dynar_remove_at(d, 2000, &d2);
1089 xbt_test_assert(d1 == d2,
1090 "Remove a bad value. Got %f, expected %f", d2, d1);
1092 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1093 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1094 /* in your code is naturally the way to go outside a regression test */
1098 /* doxygen_string_cruft */
1100 /*******************************************************************************/
1101 /*******************************************************************************/
1102 /*******************************************************************************/
1103 XBT_TEST_UNIT("string", test_dynar_string, "Dynars of strings")
1111 xbt_test_add("==== Traverse the empty dynar");
1112 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1113 xbt_dynar_foreach(d, iter, s1) {
1114 xbt_test_assert(FALSE,
1115 "Damnit, there is something in the empty dynar");
1117 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1118 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1119 /* in your code is naturally the way to go outside a regression test */
1121 xbt_test_add("==== Push %d strings, set them again 3 times, shift them",
1123 /* Populate_str [doxygen cruft] */
1124 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1125 /* 1. Populate the dynar */
1126 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1127 sprintf(buf, "%d", cpt);
1129 xbt_dynar_push(d, &s1);
1131 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1132 sprintf(buf, "%d", cpt);
1134 xbt_dynar_replace(d, cpt, &s1);
1136 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1137 sprintf(buf, "%d", cpt);
1139 xbt_dynar_replace(d, cpt, &s1);
1141 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1142 sprintf(buf, "%d", cpt);
1144 xbt_dynar_replace(d, cpt, &s1);
1146 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1147 sprintf(buf, "%d", cpt);
1148 xbt_dynar_shift(d, &s2);
1149 xbt_test_assert(!strcmp(buf, s2),
1150 "The retrieved value is not the same than the injected one (%s!=%s)",
1154 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1155 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1156 /* in your code is naturally the way to go outside a regression test */
1158 xbt_test_add("==== Unshift, traverse and pop %d strings", NB_ELEM);
1159 d = xbt_dynar_new(sizeof(char **), &xbt_free_ref);
1160 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1161 sprintf(buf, "%d", cpt);
1163 xbt_dynar_unshift(d, &s1);
1165 /* 2. Traverse the dynar with the macro */
1166 xbt_dynar_foreach(d, iter, s1) {
1167 sprintf(buf, "%u", NB_ELEM - iter - 1);
1168 xbt_test_assert(!strcmp(buf, s1),
1169 "The retrieved value is not the same than the injected one (%s!=%s)",
1172 /* 3. Traverse the dynar with the macro */
1173 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1174 sprintf(buf, "%d", cpt);
1175 xbt_dynar_pop(d, &s2);
1176 xbt_test_assert(!strcmp(buf, s2),
1177 "The retrieved value is not the same than the injected one (%s!=%s)",
1181 /* 4. Free the resources */
1182 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1183 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1184 /* in your code is naturally the way to go outside a regression test */
1188 ("==== Push %d strings, insert %d strings in the middle, shift everything",
1189 NB_ELEM, NB_ELEM / 5);
1190 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1191 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1192 sprintf(buf, "%d", cpt);
1194 xbt_dynar_push(d, &s1);
1196 for (cpt = 0; cpt < NB_ELEM / 5; cpt++) {
1197 sprintf(buf, "%d", cpt);
1199 xbt_dynar_insert_at(d, NB_ELEM / 2, &s1);
1202 for (cpt = 0; cpt < NB_ELEM / 2; cpt++) {
1203 sprintf(buf, "%d", cpt);
1204 xbt_dynar_shift(d, &s2);
1205 xbt_test_assert(!strcmp(buf, s2),
1206 "The retrieved value is not the same than the injected one at the begining (%s!=%s)",
1210 for (cpt = (NB_ELEM / 5) - 1; cpt >= 0; cpt--) {
1211 sprintf(buf, "%d", cpt);
1212 xbt_dynar_shift(d, &s2);
1213 xbt_test_assert(!strcmp(buf, s2),
1214 "The retrieved value is not the same than the injected one in the middle (%s!=%s)",
1218 for (cpt = NB_ELEM / 2; cpt < NB_ELEM; cpt++) {
1219 sprintf(buf, "%d", cpt);
1220 xbt_dynar_shift(d, &s2);
1221 xbt_test_assert(!strcmp(buf, s2),
1222 "The retrieved value is not the same than the injected one at the end (%s!=%s)",
1226 xbt_dynar_free(&d); /* This code is used both as example and as regression test, so we try to */
1227 xbt_dynar_free(&d); /* free the struct twice here to check that it's ok, but freeing it only once */
1228 /* in your code is naturally the way to go outside a regression test */
1231 xbt_test_add("==== Push %d strings, remove %d-%d. free the rest",
1232 NB_ELEM, 2 * (NB_ELEM / 5), 4 * (NB_ELEM / 5));
1233 d = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
1234 for (cpt = 0; cpt < NB_ELEM; cpt++) {
1235 sprintf(buf, "%d", cpt);
1237 xbt_dynar_push(d, &s1);
1239 for (cpt = 2 * (NB_ELEM / 5); cpt < 4 * (NB_ELEM / 5); cpt++) {
1240 sprintf(buf, "%d", cpt);
1241 xbt_dynar_remove_at(d, 2 * (NB_ELEM / 5), &s2);
1242 xbt_test_assert(!strcmp(buf, s2),
1243 "Remove a bad value. Got %s, expected %s", s2, buf);
1246 xbt_dynar_free(&d); /* end_of_doxygen */
1248 #endif /* SIMGRID_TEST */