O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1#pragma once
2
3/* ************************************************************************** */
4/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
5/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
6/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
7/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
8/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
9/* */
17/* ************************************************************************** */
18
19#include <stdbool.h> // bool
20#include <stddef.h> // size_t
21
23typedef struct array
24{
25 void* start;
26 size_t type_size;
27 size_t count;
28 size_t capacity;
29} array_t;
30
33array_t array_new(size_t type_size);
34void array_clear(array_t* self);
35void array_clear_f(array_t* self, void (*cleanup)(void*));
36
38#define ArrayNew(Type) array_new(sizeof(Type))
39
55#define Array __attribute__((cleanup(array_clear))) array_t
57
60bool array_push_back(array_t* self, const void* element);
61bool array_push_back_n(array_t* self, const void* elements, size_t count);
62
63bool array_pop_back(array_t* self, void* destination);
64bool array_pop_back_n(array_t* self, void* destination, size_t count);
65
66bool array_pop_front(array_t* self, void* destination);
67bool array_pop_front_n(array_t* self, void* destination, size_t count);
69
72void* array_first(const array_t* self);
73void* array_last(const array_t* self);
74void* array_get(const array_t* self, size_t index);
75void* array_end(const array_t* self);
77
80bool array_is_empty(const array_t* self);
81
82size_t array_count(const array_t* self);
83bool array_reserve(array_t* self, size_t count);
84bool array_trim(array_t* self);
86
89void array_iter(const array_t* self, void (*function)(void*));
90
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)
99
104#define array_enumerate(TYPE, ARRAY, ELEMENT, INDEX) \
105 for (*(INDEX) = 0; *(INDEX) < array_count(ARRAY) \
106 && ((*(ELEMENT) = *(TYPE*)array_get(ARRAY, *(INDEX))), true); \
107 (*(INDEX))++)
108
void * array_first(const array_t *self)
Pointer to the first element of the array.
Definition getters.c:36
bool array_trim(array_t *self)
Resizes the underlying storage to fit exactly the current elements count.
Definition memory.c:65
void array_iter(const array_t *self, void(*function)(void *))
Call function on each element.
Definition iter.c:21
bool array_pop_back_n(array_t *self, void *destination, size_t count)
Moves to destination the n last elements of the array.
Definition push_pop.c:53
bool array_is_empty(const array_t *self)
True if the array contains no element.
Definition getters.c:64
bool array_push_back_n(array_t *self, const void *elements, size_t count)
Adds count elements at the end of the array.
Definition push_pop.c:28
bool array_push_back(array_t *self, const void *element)
Adds one element at the end of the array.
Definition push_pop.c:43
bool array_pop_back(array_t *self, void *destination)
Moves to destination the last element of the array.
Definition push_pop.c:68
void * array_last(const array_t *self)
Pointer of the last element of the array.
Definition getters.c:45
void * array_end(const array_t *self)
One-past-the-end pointer of the array.
Definition getters.c:25
bool array_pop_front(array_t *self, void *destination)
Moves to destination the first element of self.
Definition push_pop.c:93
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.
Definition memory.c:47
void * array_get(const array_t *self, size_t index)
Pointer of the element at position index of the array.
Definition getters.c:56
size_t array_count(const array_t *self)
Number of elements stored.
Definition getters.c:70
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.
Definition push_pop.c:77
O2S array implementation.
Definition array.h:24
void * start
Underlying storage.
Definition array.h:25
size_t type_size
Size in bytes of a single element.
Definition array.h:26
size_t count
Number of elements currently stored.
Definition array.h:27
size_t capacity
Number of elements that can fit in the storage.
Definition array.h:28