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"
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dynar,tbx);
23 #define __sanity_check_dynar(dynar) \
26 #define __sanity_check_idx(idx) \
27 gras_assert1(idx >= 0, \
28 "dynar idx(=%d) < 0", \
30 #define __check_inbound_idx(dynar, idx) \
31 gras_assert2(idx < dynar->used, \
32 "dynar is not that long. You asked %d, but it's only %d long", \
34 #define __check_sloppy_inbound_idx(dynar, idx) \
35 gras_assert2(idx <= dynar->used, \
36 "dynar is not that long. You asked %d, but it's only %d long", \
38 #define __check_populated_dynar(dynar) \
39 gras_assert1(dynar->used, \
40 "dynar %p contains nothing",dynar)
45 _gras_clear_mem(void * const ptr,
46 const size_t length) {
47 memset(ptr, 0, length);
52 _gras_dynar_expand(gras_dynar_t * const dynar,
54 gras_error_t errcode = no_error;
55 const size_t old_size = dynar->size;
58 char * const old_data = dynar->data;
60 const size_t elmsize = dynar->elmsize;
61 const size_t old_length = old_size*elmsize;
63 const size_t used = dynar->used;
64 const size_t used_length = used*elmsize;
66 const size_t new_size = nb > (2*(old_size+1)) ? nb : (2*(old_size+1));
67 const size_t new_length = new_size*elmsize;
68 char * const new_data = calloc(1, elmsize*new_size);
70 DEBUG3("expend %p from %d to %d elements", dynar, old_size, nb);
75 memcpy(new_data, old_data, used_length);
76 _gras_clear_mem(old_data, old_length);
80 _gras_clear_mem(new_data + used_length, new_length - used_length);
82 dynar->size = new_size;
83 dynar->data = new_data;
91 _gras_dynar_elm(const gras_dynar_t * const dynar,
93 char * const data = dynar->data;
94 const size_t elmsize = dynar->elmsize;
96 return data + idx*elmsize;
101 _gras_dynar_get_elm(void * const dst,
102 const gras_dynar_t * const dynar,
104 void * const elm = _gras_dynar_elm(dynar, idx);
105 const size_t elmsize = dynar->elmsize;
107 memcpy(dst, elm, elmsize);
112 _gras_dynar_put_elm(const gras_dynar_t * const dynar,
114 const void * const src) {
115 void * const elm = _gras_dynar_elm(dynar, idx);
116 const size_t elmsize = dynar->elmsize;
118 memcpy(elm, src, elmsize);
123 * @whereto: pointer to where the dynar should be created
124 * @elm_size: size of each element in the dynar
125 * @free_func: function to call each time we want to get rid of an element (or NULL if nothing to do).
126 * @Returns: malloc_error or no_error
128 * Creates a new dynar. If a free_func is provided, the elements have to be
129 * pointer of pointer. That is to say that dynars can contain either base
130 * types (int, char, double, etc) or pointer of pointers (struct **).
133 gras_dynar_new(gras_dynar_t ** const p_dynar,
134 const size_t elmsize,
135 void_f_pvoid_t * const free_func) {
136 gras_error_t errcode = no_error;
137 gras_dynar_t *dynar = NULL;
139 if (!(dynar = calloc(1, sizeof(gras_dynar_t))))
144 dynar->elmsize = elmsize;
146 dynar->free = free_func;
154 * gras_dynar_free_container:
155 * @dynar: poor victim
157 * kilkil a dynar BUT NOT its content. Ie, the array is freed, but not what
158 * its contain points to.
161 gras_dynar_free_container(gras_dynar_t * const dynar) {
165 _gras_clear_mem(dynar->data, dynar->size);
169 _gras_clear_mem(dynar, sizeof(gras_dynar_t));
177 * @dynar: who to squeeze
179 * Frees the content and set the size to 0
182 gras_dynar_reset(gras_dynar_t * const dynar) {
184 __sanity_check_dynar(dynar);
186 DEBUG1("Reset the dynar %p",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 DEBUG1("Pop %p",dynar);
423 gras_dynar_remove_at(dynar, dynar->used-1, dst);
427 * gras_dynar_unshift:
430 * @Returns: malloc_error or no_error
432 * Add an element at the begining of the dynar (rather long, Use
433 * gras_dynar_push() when possible)
436 gras_dynar_unshift(gras_dynar_t * const dynar,
437 const void * const src) {
438 __sanity_check_dynar(dynar);
439 return gras_dynar_insert_at(dynar, 0, src);
447 * Get and remove the first element of the dynar (rather long, Use
448 * gras_dynar_pop() when possible)
451 gras_dynar_shift(gras_dynar_t * const dynar,
454 __sanity_check_dynar(dynar);
455 __check_populated_dynar(dynar);
456 gras_dynar_remove_at(dynar, 0, dst);
464 * Apply a function to each member of a dynar (this function may change the
465 * value of the element itself, but should not mess with the dynar).
468 gras_dynar_map(const gras_dynar_t * const dynar,
469 void_f_pvoid_t * const operator) {
471 __sanity_check_dynar(dynar);
475 const size_t used = dynar->used;
478 for (i = 0; i < used; i++) {
479 _gras_dynar_get_elm(elm, dynar, i);
488 * Put the cursor at the begining of the dynar. (actually, one step before
489 * the begining, so that you can iterate over the dynar with a for loop).
491 * Dynar cursor are as dumb as possible. If you insert or remove elements
492 * from the dynar between the creation and end, you'll fuck up your
497 gras_dynar_cursor_first(const gras_dynar_t * const dynar,
498 int * const cursor) {
500 __sanity_check_dynar(dynar);
501 DEBUG1("Set cursor on %p to the first position",dynar);
506 * gras_dynar_cursor_step:
508 * Move the cursor to the next value (and return true), or return false.
511 gras_dynar_cursor_step(const gras_dynar_t * const dynar,
512 int * const cursor) {
514 __sanity_check_dynar(dynar);
519 * gras_dynar_cursor_get:
521 * Get the current value of the cursor
524 gras_dynar_cursor_get(const gras_dynar_t * const dynar,
528 __sanity_check_dynar(dynar);
531 const int idx = *cursor;
533 if (idx >= dynar->used) {
534 DEBUG1("Cursor on %p already on last elem",dynar);
537 DEBUG2("Cash out cursor on %p at %d",dynar,idx);
539 _gras_dynar_get_elm(dst, dynar, idx);
546 * gras_dynar_cursor_rm:
550 * Remove (free) the entry pointed by the cursor, for use in the middle of a foreach
552 void gras_dynar_cursor_rm(gras_dynar_t * dynar,
553 int * const cursor) {
556 if (dynar->elmsize > sizeof(void*)) {
557 DEBUG0("Elements too big to fit into a pointer");
559 dst=malloc(dynar->elmsize);
560 gras_dynar_remove_at(dynar,(*cursor)--,dst);
564 DEBUG0("Ok, we dont care about the element when no free function");
565 gras_dynar_remove_at(dynar,(*cursor)--,NULL);
569 gras_dynar_remove_at(dynar,(*cursor)--,&dst);