|
solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
|
Dynamic array implementation with automatic resizing. More...
#include <stdbool.h>#include <stddef.h>#include <stdint.h>Go to the source code of this file.
Classes | |
| struct | dynarray_t |
Macros | |
| #define | DYNARRAY_GROWTH_NUMERATOR 3 |
| #define | DYNARRAY_INITIAL_CAPACITY 8 |
| #define | DYNARRAY_SHRINK_THRESHOLD 4 |
Functions | |
| bool | dynarray_init (dynarray_t *arr, size_t element_size, size_t initial_capacity) |
| void | dynarray_free (dynarray_t *arr) |
| bool | dynarray_push (dynarray_t *arr, const void *element) |
| bool | dynarray_pop (dynarray_t *arr, void *out_element) |
| void * | dynarray_get (const dynarray_t *arr, size_t index) |
| bool | dynarray_set (dynarray_t *arr, size_t index, const void *element) |
| bool | dynarray_reserve (dynarray_t *arr, size_t new_capacity) |
| bool | dynarray_shrink_to_fit (dynarray_t *arr) |
| void | dynarray_clear (dynarray_t *arr) |
Dynamic array implementation with automatic resizing.
Definition in file dynarray.h.
| #define DYNARRAY_GROWTH_NUMERATOR 3 |
Growth factor for capacity expansion (1.5x). Balances memory vs reallocation frequency.
Definition at line 18 of file dynarray.h.
| #define DYNARRAY_INITIAL_CAPACITY 8 |
Initial capacity when array is first created.
Definition at line 22 of file dynarray.h.
| #define DYNARRAY_SHRINK_THRESHOLD 4 |
Threshold for shrinking: shrink when size < capacity/4.
Definition at line 25 of file dynarray.h.
| void dynarray_clear | ( | dynarray_t * | arr | ) |
Clears all elements from the array. Does not free the underlying buffer (capacity remains unchanged).
| arr | Pointer to the array. |
Definition at line 184 of file dynarray.c.
References dynarray_clear(), and dynarray_t::size.
Referenced by dynarray_clear().
| void dynarray_free | ( | dynarray_t * | arr | ) |
Frees all resources associated with the dynamic array.
| arr | Pointer to the array to free. Safe to pass NULL. |
Definition at line 63 of file dynarray.c.
References dynarray_t::data, and dynarray_free().
Referenced by dynarray_free().
| void * dynarray_get | ( | const dynarray_t * | arr, |
| size_t | index | ||
| ) |
Gets a pointer to the element at the specified index.
| arr | Pointer to the array. |
| index | Index of the element. |
Definition at line 124 of file dynarray.c.
References dynarray_t::data, dynarray_get(), dynarray_t::element_size, and dynarray_t::size.
Referenced by dynarray_get().
| bool dynarray_init | ( | dynarray_t * | arr, |
| size_t | element_size, | ||
| size_t | initial_capacity | ||
| ) |
Initializes a new dynamic array.
| arr | Pointer to the array structure to initialize. |
| element_size | Size of each element in bytes. |
| initial_capacity | Initial capacity (0 uses default). |
Definition at line 34 of file dynarray.c.
References dynarray_t::data, dynarray_init(), and DYNARRAY_INITIAL_CAPACITY.
Referenced by dynarray_init().
| bool dynarray_pop | ( | dynarray_t * | arr, |
| void * | out_element | ||
| ) |
Removes and returns the last element from the array.
| arr | Pointer to the array. |
| out_element | Pointer to store the popped element (can be NULL if value not needed). |
Definition at line 97 of file dynarray.c.
References dynarray_t::capacity, dynarray_t::data, DYNARRAY_INITIAL_CAPACITY, dynarray_pop(), dynarray_reserve(), DYNARRAY_SHRINK_THRESHOLD, dynarray_t::element_size, and dynarray_t::size.
Referenced by dynarray_pop().
| bool dynarray_push | ( | dynarray_t * | arr, |
| const void * | element | ||
| ) |
Appends an element to the end of the array. Grows the array automatically if needed.
| arr | Pointer to the array. |
| element | Pointer to the element to append. |
Definition at line 72 of file dynarray.c.
References dynarray_t::capacity, dynarray_t::data, dynarray_push(), dynarray_reserve(), dynarray_t::element_size, and dynarray_t::size.
Referenced by dynarray_push().
| bool dynarray_reserve | ( | dynarray_t * | arr, |
| size_t | new_capacity | ||
| ) |
Reserves capacity for at least the specified number of elements. Does not shrink if current capacity is already sufficient.
| arr | Pointer to the array. |
| new_capacity | Desired minimum capacity. |
Definition at line 142 of file dynarray.c.
References dynarray_t::capacity, dynarray_t::data, dynarray_reserve(), dynarray_t::element_size, and dynarray_t::size.
Referenced by dynarray_pop(), dynarray_push(), dynarray_reserve(), and dynarray_shrink_to_fit().
| bool dynarray_set | ( | dynarray_t * | arr, |
| size_t | index, | ||
| const void * | element | ||
| ) |
Sets the element at the specified index.
| arr | Pointer to the array. |
| index | Index of the element. |
| element | Pointer to the new element value. |
Definition at line 131 of file dynarray.c.
References dynarray_t::data, dynarray_set(), dynarray_t::element_size, and dynarray_t::size.
Referenced by dynarray_set().
| bool dynarray_shrink_to_fit | ( | dynarray_t * | arr | ) |
Shrinks the array capacity to fit the current size exactly. Useful to reclaim memory after many removals.
| arr | Pointer to the array. |
Definition at line 173 of file dynarray.c.
References DYNARRAY_INITIAL_CAPACITY, dynarray_reserve(), dynarray_shrink_to_fit(), and dynarray_t::size.
Referenced by dynarray_shrink_to_fit().