array.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef TOOLBOX_ARRAY_H
  2. #define TOOLBOX_ARRAY_H
  3. #include "toolbox/vptr.h"
  4. #include "toolbox/return_codes.h"
  5. #include <stdint.h>
  6. #include <stddef.h>
  7. #include <string.h>
  8. #ifdef TOOLBOX_TYPEDEF
  9. typedef struct array array_st;
  10. #endif
  11. struct array {
  12. size_t item_size;
  13. size_t size;
  14. size_t alloc_size;
  15. void **data;
  16. };
  17. /**
  18. * @brief Create dynamic array pointer
  19. *
  20. * @details Creates an pointer to an array with an allocated size of arr_size
  21. * and with item size of item_size
  22. *
  23. * @param arr_size The size to be pre-allocated
  24. * @param item_size The size of the array item in bytes
  25. *
  26. * @return return struct array *
  27. */
  28. __attribute__((__pure__))
  29. struct array *
  30. array_create(size_t arr_size, size_t item_size);
  31. RET_TYPE
  32. array_clear(struct array *self);
  33. RET_TYPE
  34. array_destroy(struct array **self);
  35. __attribute__((__pure__))
  36. size_t
  37. array_index_of(const struct array *self
  38. , const void *item);
  39. __attribute__((__pure__))
  40. _Bool
  41. array_contain(const struct array *self
  42. , const void *item);
  43. __attribute__((__pure__))
  44. size_t
  45. array_count(const struct array *self, const void *item);
  46. RET_TYPE
  47. array_set(struct array *self, const size_t p_index
  48. , const void *item);
  49. RET_TYPE
  50. array_insert(struct array *self, const size_t p_index
  51. , const void *item);
  52. RET_TYPE
  53. array_append(struct array *self, const void *item);
  54. RET_TYPE
  55. array_prepend(struct array *self, const void *item);
  56. RET_TYPE
  57. array_delete(struct array *self, const size_t p_index);
  58. RET_TYPE
  59. array_remove(struct array *self, const void *item);
  60. RET_TYPE
  61. array_for_each(struct array *self, void (*for_each)(void *item));
  62. #endif