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_assert0(dynar->used, \
40 "dynar contains nothing")
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);
187 gras_dynar_map(dynar, dynar->free);
191 _gras_clear_mem(dynar->data, dynar->size);
202 * @dynar: poor victim
204 * kilkil a dynar and its content
208 gras_dynar_free(gras_dynar_t * const dynar) {
210 gras_dynar_reset(dynar);
211 gras_dynar_free_container(dynar);
217 * @dynar: the dynar we want to mesure
219 * Returns the count of elements in a dynar
222 gras_dynar_length(const gras_dynar_t * const dynar) {
223 return (dynar ? dynar->used : (size_t)0);
228 * @dynar: information dealer
229 * @idx: index of the slot we want to retrive
230 * @dst: where to pu the result to.
232 * Retrieve the Nth element of a dynar. Warning, the returned value is the actual content of
233 * the dynar. Make a copy before fooling with it.
236 gras_dynar_get(const gras_dynar_t * const dynar,
240 __sanity_check_dynar(dynar);
241 __sanity_check_idx(idx);
242 __check_inbound_idx(dynar, idx);
244 _gras_dynar_get_elm(dst, dynar, idx);
251 * @src: What will be feeded to the dynar
252 * @Returns: malloc_error or no_error
254 * Set the Nth element of a dynar, expanding the dynar if needed, BUT NOT freeing
255 * the previous value at this position. If you want to free the previous content,
256 * use gras_dynar_remplace().
259 gras_dynar_set(gras_dynar_t * const dynar,
261 const void * const src) {
262 gras_error_t errcode = no_error;
264 __sanity_check_dynar(dynar);
265 __sanity_check_idx(idx);
267 TRY(_gras_dynar_expand(dynar, idx+1));
269 if (idx >= dynar->used) {
273 _gras_dynar_put_elm(dynar, idx, src);
279 * gras_dynar_remplace:
283 * @Returns: malloc_error or no_error
285 * Set the Nth element of a dynar, expanding the dynar if needed, AND DO
286 * free the previous value at this position. If you don't want to free the
287 * previous content, use gras_dynar_set().
290 gras_dynar_remplace(gras_dynar_t * const dynar,
292 const void * const object) {
293 gras_error_t errcode = no_error;
295 __sanity_check_dynar(dynar);
296 __sanity_check_idx(idx);
298 if (idx < dynar->used && dynar->free) {
299 void * const old_object = _gras_dynar_elm(dynar, idx);
301 dynar->free(old_object);
304 errcode = gras_dynar_set(dynar, idx, object);
310 * gras_dynar_insert_at:
313 * @src: What will be feeded to the dynar
314 * @Returns: malloc_error or no_error
316 * Set the Nth element of a dynar, expanding the dynar if needed, and
317 * moving the previously existing value and all subsequent ones to one
318 * position right in the dynar.
321 gras_dynar_insert_at(gras_dynar_t * const dynar,
323 const void * const src) {
324 gras_error_t errcode = no_error;
326 __sanity_check_dynar(dynar);
327 __sanity_check_idx(idx);
328 __check_sloppy_inbound_idx(dynar, idx);
331 const size_t old_used = dynar->used;
332 const size_t new_used = old_used + 1;
334 TRY(_gras_dynar_expand(dynar, new_used));
337 const size_t nb_shift = old_used - idx;
338 const size_t elmsize = dynar->elmsize;
340 const size_t offset = nb_shift*elmsize;
342 void * const elm_src = _gras_dynar_elm(dynar, idx);
343 void * const elm_dst = _gras_dynar_elm(dynar, idx+1);
345 memmove(elm_dst, elm_src, offset);
348 _gras_dynar_put_elm(dynar, idx, src);
349 dynar->used = new_used;
356 * gras_dynar_remove_at:
361 * Get the Nth element of a dynar, removing it from the dynar and moving
362 * all subsequent values to one position left in the dynar.
365 gras_dynar_remove_at(gras_dynar_t * const dynar,
367 void * const object) {
369 __sanity_check_dynar(dynar);
370 __sanity_check_idx(idx);
371 __check_inbound_idx(dynar, idx);
374 _gras_dynar_get_elm(object, dynar, idx);
377 const size_t old_used = dynar->used;
378 const size_t new_used = old_used - 1;
380 const size_t nb_shift = old_used-1 - idx;
381 const size_t elmsize = dynar->elmsize;
383 const size_t offset = nb_shift*elmsize;
385 void * const elm_src = _gras_dynar_elm(dynar, idx+1);
386 void * const elm_dst = _gras_dynar_elm(dynar, idx);
388 memmove(elm_dst, elm_src, offset);
390 dynar->used = new_used;
398 * @Returns: malloc_error or no_error
400 * Add an element at the end of the dynar
403 gras_dynar_push(gras_dynar_t * const dynar,
404 const void * const src) {
405 __sanity_check_dynar(dynar);
406 return gras_dynar_insert_at(dynar, dynar->used, src);
414 * Get and remove the last element of the dynar
417 gras_dynar_pop(gras_dynar_t * const dynar,
419 __sanity_check_dynar(dynar);
420 __check_populated_dynar(dynar);
421 gras_dynar_remove_at(dynar, dynar->used-1, dst);
425 * gras_dynar_unshift:
428 * @Returns: malloc_error or no_error
430 * Add an element at the begining of the dynar (rather long, Use
431 * gras_dynar_push() when possible)
434 gras_dynar_unshift(gras_dynar_t * const dynar,
435 const void * const src) {
436 __sanity_check_dynar(dynar);
437 return gras_dynar_insert_at(dynar, 0, src);
445 * Get and remove the first element of the dynar (rather long, Use
446 * gras_dynar_pop() when possible)
449 gras_dynar_shift(gras_dynar_t * const dynar,
452 __sanity_check_dynar(dynar);
453 __check_populated_dynar(dynar);
454 gras_dynar_remove_at(dynar, 0, dst);
462 * Apply a function to each member of a dynar (this function may change the
463 * value of the element itself, but should not mess with the dynar).
466 gras_dynar_map(const gras_dynar_t * const dynar,
467 void_f_pvoid_t * const operator) {
469 __sanity_check_dynar(dynar);
473 const size_t used = dynar->used;
476 for (i = 0; i < used; i++) {
477 _gras_dynar_get_elm(elm, dynar, i);
486 * Put the cursor at the begining of the dynar. (actually, one step before
487 * the begining, so that you can iterate over the dynar with a for loop).
489 * Dynar cursor are as dumb as possible. If you insert or remove elements
490 * from the dynar between the creation and end, you'll fuck up your
495 gras_dynar_cursor_first(const gras_dynar_t * const dynar,
496 int * const cursor) {
498 __sanity_check_dynar(dynar);
499 DEBUG1("Set cursor on %p to the first position",dynar);
504 * gras_dynar_cursor_step:
506 * Move the cursor to the next value (and return true), or return false.
509 gras_dynar_cursor_step(const gras_dynar_t * const dynar,
510 int * const cursor) {
512 __sanity_check_dynar(dynar);
517 * gras_dynar_cursor_get:
519 * Get the current value of the cursor
522 gras_dynar_cursor_get(const gras_dynar_t * const dynar,
526 __sanity_check_dynar(dynar);
529 const int idx = *cursor;
531 if (idx >= dynar->used) {
532 DEBUG1("Cursor on %p already on last elem",dynar);
535 DEBUG2("Cash out cursor on %p at %d",dynar,idx);
537 _gras_dynar_get_elm(dst, dynar, idx);
544 * gras_dynar_cursor_rm:
548 * Remove (free) the entry pointed by the cursor, for use in the middle of a foreach
550 void gras_dynar_cursor_rm(gras_dynar_t * dynar,
551 int * const cursor) {
554 gras_dynar_remove_at(dynar,(*cursor)--,&dst);