![]() |
O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
|
Dynamic arrays. More...
#include <stdbool.h>
#include <stddef.h>
Go to the source code of this file.
Data Structures | |
struct | array_t |
O2S array implementation. More... |
Functions | |
Modifiers | |
bool | array_push_back (array_t *self, const void *element) |
Adds one element at the end of the array. | |
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_pop_back (array_t *self, void *destination) |
Moves to destination the last element of the array. | |
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_pop_front (array_t *self, void *destination) |
Moves to destination the first element of self . | |
bool | array_pop_front_n (array_t *self, void *destination, size_t count) |
Moves to destination the count first elements of self . | |
Element access | |
void * | array_first (const array_t *self) |
Pointer to the first element of the array. | |
void * | array_last (const array_t *self) |
Pointer of the last element of the array. | |
void * | array_get (const array_t *self, size_t index) |
Pointer of the element at position index of the array. | |
void * | array_end (const array_t *self) |
One-past-the-end pointer of the array. | |
Capacity | |
bool | array_is_empty (const array_t *self) |
True if the array contains no element. | |
size_t | array_count (const array_t *self) |
Number of elements stored. | |
bool | array_reserve (array_t *self, size_t count) |
Ensures the array has enough capacity to fit count new elements, reallocating if needed. | |
bool | array_trim (array_t *self) |
Resizes the underlying storage to fit exactly the current elements count. |
Initializing and destroying | |
#define | ArrayNew(Type) |
Convenient constructor wrapper. | |
#define | Array |
Automatically free the allocated storage when going out of scope. | |
array_t | array_new (size_t type_size) |
Contructs an empty array, no memory is allocated. | |
void | array_clear (array_t *self) |
Clears properly the array. | |
void | array_clear_f (array_t *self, void(*cleanup)(void *)) |
Release the resources of each elements before clearing the array. |
Iterators | |
#define | array_foreach(TYPE, ARRAY, ELEMENT) |
Iterate over the elements of the array. | |
#define | array_enumerate(TYPE, ARRAY, ELEMENT, INDEX) |
Iterate over the elements of the array, with the index. | |
void | array_iter (const array_t *self, void(*function)(void *)) |
Call function on each element. |
Dynamic arrays.
Here is the intended usage of arrays:
Definition in file array.h.
#define Array |
Automatically free the allocated storage when going out of scope.
In a situation where one wants to declare an array in a local scope, this "typedef" can be used for that array to release the allocated memory automatically when the variable goes out of scope.
It means this "typedef" can only be used like this:
#define array_enumerate | ( | TYPE, | |
ARRAY, | |||
ELEMENT, | |||
INDEX ) |
#define array_foreach | ( | TYPE, | |
ARRAY, | |||
ELEMENT ) |
void array_clear | ( | array_t * | self | ) |
Clears properly the array.
Frees the underlying storage, and leaves the array in a usable state
Definition at line 33 of file constructors_destructors.c.
void array_clear_f | ( | array_t * | self, |
void(* | cleanup )(void *) ) |
Release the resources of each elements before clearing the array.
In the case of an array storing elements that own resources, each element should be cleaned up before the array is emptied.
Definition at line 47 of file constructors_destructors.c.
void * array_end | ( | const array_t * | self | ) |
void * array_first | ( | const array_t * | self | ) |
void * array_get | ( | const array_t * | self, |
size_t | index ) |
void * array_last | ( | const array_t * | self | ) |
bool array_pop_back | ( | array_t * | self, |
void * | destination ) |
Moves to destination the last element of the array.
Definition at line 68 of file push_pop.c.
bool array_pop_back_n | ( | array_t * | self, |
void * | destination, | ||
size_t | count ) |
Moves to destination the n last elements of the array.
Destination can be NULL to just discard the elements.
Definition at line 53 of file push_pop.c.
bool array_pop_front | ( | array_t * | self, |
void * | destination ) |
Moves to destination
the first element of self
.
Definition at line 93 of file push_pop.c.
bool array_pop_front_n | ( | array_t * | self, |
void * | destination, | ||
size_t | count ) |
Moves to destination
the count
first elements of self
.
Definition at line 77 of file push_pop.c.
bool array_push_back | ( | array_t * | self, |
const void * | element ) |
Adds one element at the end of the array.
Definition at line 43 of file push_pop.c.
bool array_push_back_n | ( | array_t * | self, |
const void * | elements, | ||
size_t | count ) |
Adds count
elements at the end of the array.
Definition at line 28 of file push_pop.c.
bool array_reserve | ( | array_t * | self, |
size_t | count ) |