dd_dynamic_array.h 1.4 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. */
  24. int dd_da_add (struct dd_dynamic_array *da, void *data);
  25. int dd_da_adda(struct dd_dynamic_array *da, void *data, unsigned int ar_size);
  26. /* remove functions */
  27. int dd_da_pop(struct dd_dynamic_array *da);
  28. int dd_da_remove(struct dd_dynamic_array *da, unsigned int element);
  29. /* Clean
  30. * responsible on freeing any memory that is allocated
  31. * should be called once for every _init* function
  32. * (when array is no longer needed)
  33. * a dynamic array that is cleaned is left undefined,
  34. * it can be reused with another init function
  35. */
  36. void dd_da_free(struct dd_dynamic_array *da);
  37. /* Get element of the array */
  38. void *dd_da_get(struct dd_dynamic_array *da, unsigned int element);
  39. void dd_da_empty(struct dd_dynamic_array *da);
  40. void dd_da_copy(struct dd_dynamic_array *dest, struct dd_dynamic_array *src);
  41. #endif