solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
Classes | Macros | Functions
dynarray.h File Reference

Dynamic array implementation with automatic resizing. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for dynarray.h:
This graph shows which files directly or indirectly include this file:

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)
 

Detailed Description

Dynamic array implementation with automatic resizing.

Definition in file dynarray.h.

Macro Definition Documentation

◆ DYNARRAY_GROWTH_NUMERATOR

#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.

◆ DYNARRAY_INITIAL_CAPACITY

#define DYNARRAY_INITIAL_CAPACITY   8

Initial capacity when array is first created.

Definition at line 22 of file dynarray.h.

◆ DYNARRAY_SHRINK_THRESHOLD

#define DYNARRAY_SHRINK_THRESHOLD   4

Threshold for shrinking: shrink when size < capacity/4.

Definition at line 25 of file dynarray.h.

Function Documentation

◆ dynarray_clear()

void dynarray_clear ( dynarray_t arr)

Clears all elements from the array. Does not free the underlying buffer (capacity remains unchanged).

Parameters
arrPointer to the array.

Definition at line 184 of file dynarray.c.

References dynarray_clear(), and dynarray_t::size.

Referenced by dynarray_clear().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dynarray_free()

void dynarray_free ( dynarray_t arr)

Frees all resources associated with the dynamic array.

Parameters
arrPointer to the array to free. Safe to pass NULL.
Note
Array is left in an invalid state after this call.

Definition at line 63 of file dynarray.c.

References dynarray_t::data, and dynarray_free().

Referenced by dynarray_free().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dynarray_get()

void * dynarray_get ( const dynarray_t arr,
size_t  index 
)

Gets a pointer to the element at the specified index.

Parameters
arrPointer to the array.
indexIndex of the element.
Returns
Pointer to the element, or NULL if index is out of bounds.
Note
Returned pointer may be invalidated by operations that modify the array.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dynarray_init()

bool dynarray_init ( dynarray_t arr,
size_t  element_size,
size_t  initial_capacity 
)

Initializes a new dynamic array.

Parameters
arrPointer to the array structure to initialize.
element_sizeSize of each element in bytes.
initial_capacityInitial capacity (0 uses default).
Returns
true on success, false on allocation failure.
Note
Caller must call dynarray_free() when done.

Definition at line 34 of file dynarray.c.

References dynarray_t::data, dynarray_init(), and DYNARRAY_INITIAL_CAPACITY.

Referenced by dynarray_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dynarray_pop()

bool dynarray_pop ( dynarray_t arr,
void *  out_element 
)

Removes and returns the last element from the array.

Parameters
arrPointer to the array.
out_elementPointer to store the popped element (can be NULL if value not needed).
Returns
true on success, false if array is empty.
Note
May shrink the array if usage drops below threshold.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dynarray_push()

bool dynarray_push ( dynarray_t arr,
const void *  element 
)

Appends an element to the end of the array. Grows the array automatically if needed.

Parameters
arrPointer to the array.
elementPointer to the element to append.
Returns
true on success, false on allocation failure.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dynarray_reserve()

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.

Parameters
arrPointer to the array.
new_capacityDesired minimum capacity.
Returns
true on success, false on allocation failure.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dynarray_set()

bool dynarray_set ( dynarray_t arr,
size_t  index,
const void *  element 
)

Sets the element at the specified index.

Parameters
arrPointer to the array.
indexIndex of the element.
elementPointer to the new element value.
Returns
true on success, false if index is out of bounds.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dynarray_shrink_to_fit()

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.

Parameters
arrPointer to the array.
Returns
true on success, false on allocation failure.

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().

Here is the call graph for this function:
Here is the caller graph for this function: