3 /* a generic DYNamic ARray implementation. */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "portable.h" /* SIZEOF_MAX */
12 #include "xbt/sysdep.h"
14 #include "xbt/error.h"
15 #include "xbt/dynar.h"
16 #include <sys/types.h>
18 #include "xbt/dynar_private.h" /* type definition, which we share with the
19 code in charge of sending this across the net */
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,xbt,"Dynamic arrays");
24 #define __sanity_check_dynar(dynar) \
27 #define __sanity_check_idx(idx) \
28 xbt_assert1(idx >= 0, \
29 "dynar idx(=%d) < 0", \
31 #define __check_inbound_idx(dynar, idx) \
32 xbt_assert2(idx < dynar->used, \
33 "dynar is not that long. You asked %d, but it's only %lu long", \
34 (int) (idx), (unsigned long) dynar->used)
35 #define __check_sloppy_inbound_idx(dynar, idx) \
36 xbt_assert2(idx <= dynar->used, \
37 "dynar is not that long. You asked %d, but it's only %lu long", \
38 (int) (idx), (unsigned long) dynar->used)
39 #define __check_populated_dynar(dynar) \
40 xbt_assert1(dynar->used, \
41 "dynar %p contains nothing",(void*)dynar)
44 void _xbt_clear_mem(void * const ptr,
45 const unsigned long length) {
46 memset(ptr, 0, length);
51 _xbt_dynar_expand(xbt_dynar_t const dynar,
53 xbt_error_t errcode = no_error;
54 const unsigned long old_size = dynar->size;
57 char * const old_data = dynar->data;
59 const unsigned long elmsize = dynar->elmsize;
60 const unsigned long old_length = old_size*elmsize;
62 const unsigned long used = dynar->used;
63 const unsigned long used_length = used*elmsize;
65 const unsigned long new_size = nb > (2*(old_size+1)) ? nb : (2*(old_size+1));
66 const unsigned long new_length = new_size*elmsize;
67 char * const new_data = xbt_malloc0(elmsize*new_size);
69 DEBUG3("expend %p from %lu to %d elements", (void*)dynar, (unsigned long)old_size, nb);
72 memcpy(new_data, old_data, used_length);
73 _xbt_clear_mem(old_data, old_length);
77 _xbt_clear_mem(new_data + used_length, new_length - used_length);
79 dynar->size = new_size;
80 dynar->data = new_data;
88 _xbt_dynar_elm(const xbt_dynar_t dynar,
89 const unsigned long idx) {
90 char * const data = dynar->data;
91 const unsigned long elmsize = dynar->elmsize;
93 return data + idx*elmsize;
98 _xbt_dynar_get_elm(void * const dst,
99 const xbt_dynar_t dynar,
100 const unsigned long idx) {
101 void * const elm = _xbt_dynar_elm(dynar, idx);
102 const unsigned long elmsize = dynar->elmsize;
104 memcpy(dst, elm, elmsize);
109 _xbt_dynar_put_elm(const xbt_dynar_t dynar,
110 const unsigned long idx,
111 const void * const src) {
112 void * const elm = _xbt_dynar_elm(dynar, idx);
113 const unsigned long elmsize = dynar->elmsize;
115 memcpy(elm, src, elmsize);
118 /** @brief Constructor
120 * \param elmsize size of each element in the dynar
121 * \param free_f function to call each time we want to get rid of an element (or NULL if nothing to do).
123 * Creates a new dynar. If a free_func is provided, the elements have to be
124 * pointer of pointer. That is to say that dynars can contain either base
125 * types (int, char, double, etc) or pointer of pointers (struct **).
128 xbt_dynar_new(const unsigned long elmsize,
129 void_f_pvoid_t * const free_f) {
131 xbt_dynar_t dynar = xbt_new0(s_xbt_dynar_t,1);
135 dynar->elmsize = elmsize;
137 dynar->free_f = free_f;
142 /** @brief Destructor of the structure not touching to the content
144 * \param dynar poor victim
146 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but the content
147 * is not touched (the \a free_f function is not used)
150 xbt_dynar_free_container(xbt_dynar_t *dynar) {
151 if (dynar && *dynar) {
153 if ((*dynar)->data) {
154 _xbt_clear_mem((*dynar)->data, (*dynar)->size);
155 free((*dynar)->data);
158 _xbt_clear_mem(*dynar, sizeof(s_xbt_dynar_t));
165 /** @brief Frees the content and set the size to 0
167 * \param dynar who to squeeze
170 xbt_dynar_reset(xbt_dynar_t const dynar) {
172 __sanity_check_dynar(dynar);
174 DEBUG1("Reset the dynar %p",(void*)dynar);
176 xbt_dynar_map(dynar, dynar->free_f);
187 /** @brief Destructor
189 * \param dynar poor victim
191 * kilkil a dynar and its content
195 xbt_dynar_free(xbt_dynar_t * dynar) {
196 if (dynar && *dynar) {
197 xbt_dynar_reset(*dynar);
198 xbt_dynar_free_container(dynar);
202 /** @brief Count of dynar's elements
204 * \param dynar the dynar we want to mesure
207 xbt_dynar_length(const xbt_dynar_t dynar) {
208 return (dynar ? (unsigned long) dynar->used : (unsigned long)0);
211 /** @brief Retrieve a copy of the Nth element of a dynar.
213 * \param dynar information dealer
214 * \param idx index of the slot we want to retrive
215 * \param[out] dst where to put the result to.
218 xbt_dynar_get_cpy(const xbt_dynar_t dynar,
222 __sanity_check_dynar(dynar);
223 __sanity_check_idx(idx);
224 __check_inbound_idx(dynar, idx);
226 _xbt_dynar_get_elm(dst, dynar, idx);
229 /** @brief Retrieve a pointer to the Nth element of a dynar.
231 * \param dynar information dealer
232 * \param idx index of the slot we want to retrieve
233 * \return the \a idx-th element of \a dynar.
235 * \warning The returned value is the actual content of the dynar.
236 * Make a copy before fooling with it.
239 xbt_dynar_get_ptr(const xbt_dynar_t dynar,
242 __sanity_check_dynar(dynar);
243 __sanity_check_idx(idx);
244 __check_inbound_idx(dynar, idx);
246 return _xbt_dynar_elm(dynar, idx);
249 /** @brief Set the Nth element of a dynar (expended if needed). Previous value at this position is NOT freed
251 * \param dynar information dealer
252 * \param idx index of the slot we want to modify
253 * \param src What will be feeded to the dynar
255 * If you want to free the previous content, use xbt_dynar_replace().
258 xbt_dynar_set(xbt_dynar_t dynar,
260 const void * const src) {
262 __sanity_check_dynar(dynar);
263 __sanity_check_idx(idx);
265 _xbt_dynar_expand(dynar, idx+1);
267 if (idx >= dynar->used) {
271 _xbt_dynar_put_elm(dynar, idx, src);
274 /** @brief Set the Nth element of a dynar (expended if needed). Previous value is freed
280 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
281 * free the previous value at this position. If you don't want to free the
282 * previous content, use xbt_dynar_set().
285 xbt_dynar_replace(xbt_dynar_t dynar,
287 const void * const object) {
289 __sanity_check_dynar(dynar);
290 __sanity_check_idx(idx);
292 if (idx < dynar->used && dynar->free_f) {
293 void * const old_object = _xbt_dynar_elm(dynar, idx);
295 dynar->free_f(old_object);
298 xbt_dynar_set(dynar, idx, object);
301 /** @brief Make room for a new element, and return a pointer to it
303 * You can then use regular affectation to set its value instead of relying
304 * on the slow memcpy. This is what xbt_dynar_insert_at_as() does.
307 xbt_dynar_insert_at_ptr(xbt_dynar_t const dynar,
310 __sanity_check_dynar(dynar);
311 __sanity_check_idx(idx);
312 __check_sloppy_inbound_idx(dynar, idx);
315 const unsigned long old_used = dynar->used;
316 const unsigned long new_used = old_used + 1;
318 _xbt_dynar_expand(dynar, new_used);
321 const unsigned long nb_shift = old_used - idx;
324 memmove(_xbt_dynar_elm(dynar, idx+1),
325 _xbt_dynar_elm(dynar, idx),
326 nb_shift * dynar->elmsize);
329 dynar->used = new_used;
330 return _xbt_dynar_elm(dynar,idx);
334 /** @brief Set the Nth dynar's element, expending the dynar and sliding the previous values to the right
336 * Set the Nth element of a dynar, expanding the dynar if needed, and
337 * moving the previously existing value and all subsequent ones to one
338 * position right in the dynar.
341 xbt_dynar_insert_at(xbt_dynar_t const dynar,
343 const void * const src) {
345 /* checks done in xbt_dynar_insert_at_ptr */
346 memcpy(xbt_dynar_insert_at_ptr(dynar,idx),
351 /** @brief Remove the Nth dynar's element, sliding the previous values to the left
353 * Get the Nth element of a dynar, removing it from the dynar and moving
354 * all subsequent values to one position left in the dynar.
357 xbt_dynar_remove_at(xbt_dynar_t const dynar,
359 void * const object) {
361 __sanity_check_dynar(dynar);
362 __sanity_check_idx(idx);
363 __check_inbound_idx(dynar, idx);
366 _xbt_dynar_get_elm(object, dynar, idx);
369 const unsigned long old_used = dynar->used;
370 const unsigned long new_used = old_used - 1;
372 const unsigned long nb_shift = old_used-1 - idx;
373 const unsigned long elmsize = dynar->elmsize;
375 const unsigned long offset = nb_shift*elmsize;
377 void * const elm_src = _xbt_dynar_elm(dynar, idx+1);
378 void * const elm_dst = _xbt_dynar_elm(dynar, idx);
380 memmove(elm_dst, elm_src, offset);
382 dynar->used = new_used;
386 /** @brief Make room at the end of the dynar for a new element, and return a pointer to it.
388 * You can then use regular affectation to set its value instead of relying
389 * on the slow memcpy. This is what xbt_dynar_push_as() does.
392 xbt_dynar_push_ptr(xbt_dynar_t const dynar) {
393 return xbt_dynar_insert_at_ptr(dynar, dynar->used);
396 /** @brief Add an element at the end of the dynar */
398 xbt_dynar_push(xbt_dynar_t const dynar,
399 const void * const src) {
400 /* sanity checks done by insert_at */
401 xbt_dynar_insert_at(dynar, dynar->used, src);
404 /** @brief Mark the last dynar's element as unused and return a pointer to it.
406 * You can then use regular affectation to set its value instead of relying
407 * on the slow memcpy. This is what xbt_dynar_pop_as() does.
410 xbt_dynar_pop_ptr(xbt_dynar_t const dynar) {
412 __check_populated_dynar(dynar);
413 DEBUG1("Pop %p",(void*)dynar);
415 return _xbt_dynar_elm(dynar,dynar->used);
418 /** @brief Get and remove the last element of the dynar */
420 xbt_dynar_pop(xbt_dynar_t const dynar,
423 /* sanity checks done by remove_at */
424 DEBUG1("Pop %p",(void*)dynar);
425 xbt_dynar_remove_at(dynar, dynar->used-1, dst);
428 /** @brief Add an element at the begining of the dynar.
430 * This is less efficient than xbt_dynar_push()
433 xbt_dynar_unshift(xbt_dynar_t const dynar,
434 const void * const src) {
436 /* sanity checks done by insert_at */
437 xbt_dynar_insert_at(dynar, 0, src);
440 /** @brief Get and remove the first element of the dynar.
442 * This is less efficient than xbt_dynar_pop()
445 xbt_dynar_shift(xbt_dynar_t const dynar,
448 /* sanity checks done by remove_at */
449 xbt_dynar_remove_at(dynar, 0, dst);
452 /** @brief Apply a function to each member of a dynar
454 * The mapped function may change the value of the element itself,
455 * but should not mess with the structure of the dynar.
458 xbt_dynar_map(const xbt_dynar_t dynar,
459 void_f_pvoid_t * const operator) {
461 __sanity_check_dynar(dynar);
464 char elm[SIZEOF_MAX];
465 const unsigned long used = dynar->used;
468 for (i = 0; i < used; i++) {
469 _xbt_dynar_get_elm(elm, dynar, i);
475 /** @brief Put the cursor at the begining of the dynar.
477 * Actually, the cursor is set one step before the begining, so that you
478 * can iterate over the dynar with a for loop.
481 xbt_dynar_cursor_first(const xbt_dynar_t dynar,
482 int * const cursor) {
484 DEBUG1("Set cursor on %p to the first position",(void*)dynar);
488 /** @brief Move the cursor to the next value */
490 xbt_dynar_cursor_step(const xbt_dynar_t dynar,
491 int * const cursor) {
496 /** @brief Get the data currently pointed by the cursor */
498 xbt_dynar_cursor_get(const xbt_dynar_t dynar,
502 __sanity_check_dynar(dynar);
505 const int idx = *cursor;
507 if (idx >= dynar->used) {
508 DEBUG1("Cursor on %p already on last elem",(void*)dynar);
511 DEBUG2("Cash out cursor on %p at %d",(void*)dynar,idx);
513 _xbt_dynar_get_elm(dst, dynar, idx);
519 /** @brief Removes and free the entry pointed by the cursor
521 * This function can be used while traversing without problem.
523 void xbt_dynar_cursor_rm(xbt_dynar_t dynar,
524 int * const cursor) {
527 if (dynar->elmsize > sizeof(void*)) {
528 DEBUG0("Elements too big to fit into a pointer");
530 dst=xbt_malloc(dynar->elmsize);
531 xbt_dynar_remove_at(dynar,(*cursor)--,dst);
532 (dynar->free_f)(dst);
535 DEBUG0("Ok, we dont care about the element without free function");
536 xbt_dynar_remove_at(dynar,(*cursor)--,NULL);
540 xbt_dynar_remove_at(dynar,(*cursor)--,&dst);
542 (dynar->free_f)(dst);