3 /* a generic DYNamic ARray */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003 the OURAGAN project. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "gras_private.h"
14 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,GRAS);
24 #define __sanity_check_dynar(dynar) \
27 #define __sanity_check_idx(idx) \
28 gras_assert1(idx >= 0, \
29 "dynar idx(=%d) < 0", \
31 #define __check_inbound_idx(dynar, idx) \
32 gras_assert2(idx < dynar->used, \
33 "dynar is not that long. You asked %d, but it's only %d long", \
35 #define __check_sloppy_inbound_idx(dynar, idx) \
36 gras_assert2(idx <= dynar->used, \
37 "dynar is not that long. You asked %d, but it's only %d long", \
39 #define __check_populated_dynar(dynar) \
40 gras_assert0(dynar->used, \
41 "dynar contains nothing")
46 _gras_clear_mem(void * const ptr,
47 const size_t length) {
48 memset(ptr, 0, length);
53 _gras_dynar_expand(gras_dynar_t * const dynar,
55 gras_error_t errcode = no_error;
56 const size_t old_size = dynar->size;
59 char * const old_data = dynar->data;
61 const size_t elmsize = dynar->elmsize;
62 const size_t old_length = old_size*elmsize;
64 const size_t used = dynar->used;
65 const size_t used_length = used*elmsize;
67 const size_t new_size = nb > (2*(old_size+1)) ? nb : (2*(old_size+1));
68 const size_t new_length = new_size*elmsize;
69 char * const new_data = calloc(1, elmsize*new_size);
71 DEBUG3("expend %p from %d to %d elements", dynar, old_size, nb);
76 memcpy(new_data, old_data, used_length);
77 _gras_clear_mem(old_data, old_length);
81 _gras_clear_mem(new_data + used_length, new_length - used_length);
83 dynar->size = new_size;
84 dynar->data = new_data;
92 _gras_dynar_elm(const gras_dynar_t * const dynar,
94 char * const data = dynar->data;
95 const size_t elmsize = dynar->elmsize;
97 return data + idx*elmsize;
102 _gras_dynar_get_elm(void * const dst,
103 const gras_dynar_t * const dynar,
105 void * const elm = _gras_dynar_elm(dynar, idx);
106 const size_t elmsize = dynar->elmsize;
108 memcpy(dst, elm, elmsize);
113 _gras_dynar_put_elm(const gras_dynar_t * const dynar,
115 const void * const src) {
116 void * const elm = _gras_dynar_elm(dynar, idx);
117 const size_t elmsize = dynar->elmsize;
119 memcpy(elm, src, elmsize);
124 * @whereto: pointer to where the dynar should be created
125 * @elm_size: size of each element in the dynar
126 * @free_func: function to call each time we want to get rid of an element (or NULL if nothing to do).
127 * @Returns: malloc_error or no_error
129 * Creates a new dynar. If a free_func is provided, the elements have to be
130 * pointer of pointer. That is to say that dynars can contain either base
131 * types (int, char, double, etc) or pointer of pointers (struct **).
134 gras_dynar_new(gras_dynar_t ** const p_dynar,
135 const size_t elmsize,
136 void_f_pvoid_t * const free_func) {
137 gras_error_t errcode = no_error;
138 gras_dynar_t *dynar = NULL;
140 if (!(dynar = calloc(1, sizeof(gras_dynar_t))))
145 dynar->elmsize = elmsize;
147 dynar->free = free_func;
155 * gras_dynar_free_container:
156 * @dynar: poor victim
158 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but not what
159 * its contain points to.
162 gras_dynar_free_container(gras_dynar_t * const dynar) {
166 _gras_clear_mem(dynar->data, dynar->size);
170 _gras_clear_mem(dynar, sizeof(gras_dynar_t));
178 * @dynar: who to squeeze
180 * Frees the content and set the size to 0
183 gras_dynar_reset(gras_dynar_t * const dynar) {
185 __sanity_check_dynar(dynar);
188 gras_dynar_map(dynar, dynar->free);
192 _gras_clear_mem(dynar->data, dynar->size);
203 * @dynar: poor victim
205 * kilkil a dynar and its content
209 gras_dynar_free(gras_dynar_t * const dynar) {
211 gras_dynar_reset(dynar);
212 gras_dynar_free_container(dynar);
218 * @dynar: the dynar we want to mesure
220 * Returns the count of elements in a dynar
223 gras_dynar_length(const gras_dynar_t * const dynar) {
224 return (dynar ? dynar->used : (size_t)0);
229 * @dynar: information dealer
230 * @idx: index of the slot we want to retrive
231 * @dst: where to pu the result to.
233 * Retrieve the Nth element of a dynar. Warning, the returned value is the actual content of
234 * the dynar. Make a copy before fooling with it.
237 gras_dynar_get(const gras_dynar_t * const dynar,
241 __sanity_check_dynar(dynar);
242 __sanity_check_idx(idx);
243 __check_inbound_idx(dynar, idx);
245 _gras_dynar_get_elm(dst, dynar, idx);
252 * @src: What will be feeded to the dynar
253 * @Returns: malloc_error or no_error
255 * Set the Nth element of a dynar, expanding the dynar if needed, BUT NOT freeing
256 * the previous value at this position. If you want to free the previous content,
257 * use gras_dynar_remplace().
260 gras_dynar_set(gras_dynar_t * const dynar,
262 const void * const src) {
263 gras_error_t errcode = no_error;
265 __sanity_check_dynar(dynar);
266 __sanity_check_idx(idx);
268 TRY(_gras_dynar_expand(dynar, idx+1));
270 if (idx >= dynar->used) {
274 _gras_dynar_put_elm(dynar, idx, src);
280 * gras_dynar_remplace:
284 * @Returns: malloc_error or no_error
286 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
287 * free the previous value at this position. If you don't want to free the
288 * previous content, use gras_dynar_set().
291 gras_dynar_remplace(gras_dynar_t * const dynar,
293 const void * const object) {
294 gras_error_t errcode = no_error;
296 __sanity_check_dynar(dynar);
297 __sanity_check_idx(idx);
299 if (idx < dynar->used && dynar->free) {
300 void * const old_object = _gras_dynar_elm(dynar, idx);
302 dynar->free(old_object);
305 errcode = gras_dynar_set(dynar, idx, object);
311 * gras_dynar_insert_at:
314 * @src: What will be feeded to the dynar
315 * @Returns: malloc_error or no_error
317 * Set the Nth element of a dynar, expanding the dynar if needed, and
318 * moving the previously existing value and all subsequent ones to one
319 * position right in the dynar.
322 gras_dynar_insert_at(gras_dynar_t * const dynar,
324 const void * const src) {
325 gras_error_t errcode = no_error;
327 __sanity_check_dynar(dynar);
328 __sanity_check_idx(idx);
329 __check_sloppy_inbound_idx(dynar, idx);
332 const size_t old_used = dynar->used;
333 const size_t new_used = old_used + 1;
335 TRY(_gras_dynar_expand(dynar, new_used));
338 const size_t nb_shift = old_used - idx;
339 const size_t elmsize = dynar->elmsize;
341 const size_t offset = nb_shift*elmsize;
343 void * const elm_src = _gras_dynar_elm(dynar, idx);
344 void * const elm_dst = _gras_dynar_elm(dynar, idx+1);
346 memmove(elm_dst, elm_src, offset);
349 _gras_dynar_put_elm(dynar, idx, src);
350 dynar->used = new_used;
357 * gras_dynar_remove_at:
362 * Get the Nth element of a dynar, removing it from the dynar and moving
363 * all subsequent values to one position left in the dynar.
366 gras_dynar_remove_at(gras_dynar_t * const dynar,
368 void * const object) {
370 __sanity_check_dynar(dynar);
371 __sanity_check_idx(idx);
372 __check_inbound_idx(dynar, idx);
375 _gras_dynar_get_elm(object, dynar, idx);
378 const size_t old_used = dynar->used;
379 const size_t new_used = old_used - 1;
381 const size_t nb_shift = old_used-1 - idx;
382 const size_t elmsize = dynar->elmsize;
384 const size_t offset = nb_shift*elmsize;
386 void * const elm_src = _gras_dynar_elm(dynar, idx+1);
387 void * const elm_dst = _gras_dynar_elm(dynar, idx);
389 memmove(elm_dst, elm_src, offset);
391 dynar->used = new_used;
399 * @Returns: malloc_error or no_error
401 * Add an element at the end of the dynar
404 gras_dynar_push(gras_dynar_t * const dynar,
405 const void * const src) {
406 __sanity_check_dynar(dynar);
407 return gras_dynar_insert_at(dynar, dynar->used, src);
415 * Get and remove the last element of the dynar
418 gras_dynar_pop(gras_dynar_t * const dynar,
420 __sanity_check_dynar(dynar);
421 __check_populated_dynar(dynar);
422 gras_dynar_remove_at(dynar, dynar->used-1, dst);
426 * gras_dynar_unshift:
429 * @Returns: malloc_error or no_error
431 * Add an element at the begining of the dynar (rather long, Use
432 * gras_dynar_push() when possible)
435 gras_dynar_unshift(gras_dynar_t * const dynar,
436 const void * const src) {
437 __sanity_check_dynar(dynar);
438 return gras_dynar_insert_at(dynar, 0, src);
446 * Get and remove the first element of the dynar (rather long, Use
447 * gras_dynar_pop() when possible)
450 gras_dynar_shift(gras_dynar_t * const dynar,
453 __sanity_check_dynar(dynar);
454 __check_populated_dynar(dynar);
455 gras_dynar_remove_at(dynar, 0, dst);
463 * Apply a function to each member of a dynar (this function may change the
464 * value of the element itself, but should not mess with the dynar).
467 gras_dynar_map(const gras_dynar_t * const dynar,
468 void_f_pvoid_t * const operator) {
470 __sanity_check_dynar(dynar);
474 const size_t used = dynar->used;
477 for (i = 0; i < used; i++) {
478 _gras_dynar_get_elm(elm, dynar, i);
487 * Put the cursor at the begining of the dynar. (actually, one step before
488 * the begining, so that you can iterate over the dynar with a for loop).
490 * Dynar cursor are as dumb as possible. If you insert or remove elements
491 * from the dynar between the creation and end, you'll fuck up your
496 gras_dynar_cursor_first(const gras_dynar_t * const dynar,
497 int * const cursor) {
499 __sanity_check_dynar(dynar);
500 DEBUG1("Set cursor on %p to the first position",dynar);
505 * gras_dynar_cursor_step:
507 * Move the cursor to the next value (and return true), or return false.
510 gras_dynar_cursor_step(const gras_dynar_t * const dynar,
511 int * const cursor) {
513 __sanity_check_dynar(dynar);
518 * gras_dynar_cursor_get:
520 * Get the current value of the cursor
523 gras_dynar_cursor_get(const gras_dynar_t * const dynar,
527 __sanity_check_dynar(dynar);
530 const int idx = *cursor;
532 if (idx >= dynar->used) {
533 DEBUG1("Cursor on %p already on last elem",dynar);
536 DEBUG2("Cash out cursor on %p at %d",dynar,idx);
538 _gras_dynar_get_elm(dst, dynar, idx);
545 * gras_dynar_cursor_rm:
549 * Remove the entry pointed by the cursor, for use in the middle of a foreach
551 void gras_dynar_cursor_rm(gras_dynar_t * dynar,
552 int * const cursor) {
554 gras_dynar_remove_at(dynar,(*cursor)--,NULL);