38#define ArrayNew(Type) array_new(sizeof(Type))
55#define Array __attribute__((cleanup(array_clear))) array_t
95#define array_foreach(TYPE, ARRAY, ELEMENT) \
96 for (char* _pointer = (char*)array_first(ARRAY); \
97 _pointer <= (char*)array_last(ARRAY) && ((*(ELEMENT) = *(TYPE*)_pointer), true); \
98 _pointer += (ARRAY)->type_size)
104#define array_enumerate(TYPE, ARRAY, ELEMENT, INDEX) \
105 for (*(INDEX) = 0; *(INDEX) < array_count(ARRAY) \
106 && ((*(ELEMENT) = *(TYPE*)array_get(ARRAY, *(INDEX))), true); \
void * array_first(const array_t *self)
Pointer to the first element of the array.
bool array_trim(array_t *self)
Resizes the underlying storage to fit exactly the current elements count.
void array_iter(const array_t *self, void(*function)(void *))
Call function on each element.
bool array_pop_back_n(array_t *self, void *destination, size_t count)
Moves to destination the n last elements of the array.
bool array_is_empty(const array_t *self)
True if the array contains no element.
bool array_push_back_n(array_t *self, const void *elements, size_t count)
Adds count elements at the end of the array.
bool array_push_back(array_t *self, const void *element)
Adds one element at the end of the array.
bool array_pop_back(array_t *self, void *destination)
Moves to destination the last element of the array.
void * array_last(const array_t *self)
Pointer of the last element of the array.
void * array_end(const array_t *self)
One-past-the-end pointer of the array.
bool array_pop_front(array_t *self, void *destination)
Moves to destination the first element of self.
void array_clear(array_t *self)
Clears properly the array.
bool array_reserve(array_t *self, size_t count)
Ensures the array has enough capacity to fit count new elements, reallocating if needed.
void * array_get(const array_t *self, size_t index)
Pointer of the element at position index of the array.
size_t array_count(const array_t *self)
Number of elements stored.
array_t array_new(size_t type_size)
Contructs an empty array, no memory is allocated.
void array_clear_f(array_t *self, void(*cleanup)(void *))
Release the resources of each elements before clearing the array.
bool array_pop_front_n(array_t *self, void *destination, size_t count)
Moves to destination the count first elements of self.
O2S array implementation.
void * start
Underlying storage.
size_t type_size
Size in bytes of a single element.
size_t count
Number of elements currently stored.
size_t capacity
Number of elements that can fit in the storage.