dd_dynamic_array.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef DD_DYNAMIC_ARRAY_H
  2. #define DD_DYNAMIC_ARRAY_H
  3. /* a dynamic array
  4. * elements have a custom size (specified inside an init function)
  5. * new elements can be added
  6. */
  7. /* Dynamic array struct */
  8. struct dd_dynamic_array {
  9. void *array;
  10. unsigned int elements;
  11. unsigned int array_size;
  12. unsigned int element_size;
  13. };
  14. /* Init functions
  15. * init : init empty array
  16. * inita : init array with fixed array size
  17. */
  18. int dd_da_init (struct dd_dynamic_array *da, int el_size);
  19. int dd_da_inita(struct dd_dynamic_array *da, int el_size, int ar_size);
  20. /* Add functions
  21. * add : adds one element to array (based on element_size)
  22. * adda : adds an array of elements
  23. * add_first : adds element at first place in the array, pushing everything back
  24. */
  25. int dd_da_add (struct dd_dynamic_array *da, void *data);
  26. int dd_da_adda(struct dd_dynamic_array *da, void *data, unsigned int ar_size);
  27. int dd_da_add_first(struct dd_dynamic_array *da, void *data);
  28. /* remove functions */
  29. int dd_da_pop(struct dd_dynamic_array *da);
  30. int dd_da_remove(struct dd_dynamic_array *da, unsigned int element);
  31. /* Clean
  32. * responsible on freeing any memory that is allocated
  33. * should be called once for every _init* function
  34. * (when array is no longer needed)
  35. * a dynamic array that is cleaned is left undefined,
  36. * it can be reused with another init function
  37. */
  38. void dd_da_free(struct dd_dynamic_array *da);
  39. /* Get element of the array */
  40. void *dd_da_get(struct dd_dynamic_array *da, unsigned int element);
  41. #endif