dynamic_array.h v0.3.1
Reference-counted dynamic arrays for C
|
Functions for querying and managing array size and capacity. More...
Functions | |
DA_DEF int | da_length (da_array arr) |
Gets the current number of elements in the array. | |
DA_DEF int | da_capacity (da_array arr) |
Gets the current allocated capacity of the array. | |
DA_DEF void | da_reserve (da_array arr, int new_capacity) |
Ensures the array has at least the specified capacity. | |
DA_DEF void | da_resize (da_array arr, int new_length) |
Changes the array length, growing or shrinking as needed. | |
DA_DEF void | da_trim (da_array arr, int new_capacity) |
Reduces the array's allocated capacity to a specific size. | |
DA_DEF void | da_append_array (da_array dest, da_array src) |
Appends all elements from source array to destination array. | |
DA_DEF da_array | da_concat (da_array arr1, da_array arr2) |
Creates a new array by concatenating two arrays. | |
DA_DEF void | da_append_raw (da_array arr, const void *data, int count) |
Appends raw C array data to the dynamic array. | |
DA_DEF void | da_fill (da_array arr, const void *element, int count) |
Fills the array with multiple copies of an element. | |
DA_DEF da_array | da_slice (da_array arr, int start, int end) |
Creates a new array containing elements from a range [start, end) | |
DA_DEF da_array | da_copy (da_array arr) |
Creates a complete copy of an existing array. | |
DA_DEF da_array | da_filter (da_array arr, int(*predicate)(const void *element, void *context), void *context) |
Creates a new array containing elements that pass a predicate test. | |
DA_DEF da_array | da_map (da_array arr, void(*mapper)(const void *src, void *dst, void *context), void *context) |
Creates a new array by transforming each element using a mapper function. | |
DA_DEF void | da_reduce (da_array arr, const void *initial, void *result, void(*reducer)(void *accumulator, const void *element, void *context), void *context) |
Reduces array to single value using accumulator function. | |
DA_DEF void | da_remove_range (da_array arr, int start, int count) |
Removes multiple consecutive elements from the array. | |
DA_DEF void | da_reverse (da_array arr) |
Reverses all elements in the array in place. | |
DA_DEF void | da_swap (da_array arr, int i, int j) |
Swaps two elements at the specified indices. | |
DA_DEF int | da_is_empty (da_array arr) |
Checks if the array is empty. | |
DA_DEF int | da_find_index (da_array arr, int(*predicate)(const void *element, void *context), void *context) |
Find index of first element matching predicate. | |
DA_DEF int | da_contains (da_array arr, int(*predicate)(const void *element, void *context), void *context) |
Check if array contains element matching predicate. | |
DA_DEF void | da_sort (da_array arr, int(*compare)(const void *a, const void *b, void *context), void *context) |
Sort array elements using comparison function. | |
Functions for querying and managing array size and capacity.
Appends all elements from source array to destination array.
dest | Destination array to append to (must not be NULL) |
src | Source array to read from (must not be NULL) |
DA_DEF void da_append_raw | ( | da_array | arr, |
const void * | data, | ||
int | count | ||
) |
Appends raw C array data to the dynamic array.
arr | Array to modify (must not be NULL) |
data | Pointer to raw data array (must not be NULL) |
count | Number of elements to append (must be >= 0) |
DA_DEF int da_capacity | ( | da_array | arr | ) |
Gets the current allocated capacity of the array.
arr | Array to query (must not be NULL) |
Creates a new array by concatenating two arrays.
arr1 | First array (must not be NULL) |
arr2 | Second array (must not be NULL) |
DA_DEF int da_contains | ( | da_array | arr, |
int(*)(const void *element, void *context) | predicate, | ||
void * | context | ||
) |
Check if array contains element matching predicate.
arr | Array to search (must not be NULL) |
predicate | Function to test elements (must not be NULL) |
context | Optional context passed to predicate (can be NULL) |
Creates a complete copy of an existing array.
arr | Source array to copy from (must not be NULL) |
DA_DEF void da_fill | ( | da_array | arr, |
const void * | element, | ||
int | count | ||
) |
Fills the array with multiple copies of an element.
arr | Array to modify (must not be NULL) |
element | Pointer to element to replicate (must not be NULL) |
count | Number of copies to add (must be >= 0) |
DA_DEF da_array da_filter | ( | da_array | arr, |
int(*)(const void *element, void *context) | predicate, | ||
void * | context | ||
) |
Creates a new array containing elements that pass a predicate test.
arr | Source array to filter (must not be NULL) |
predicate | Function that returns non-zero for elements to keep (must not be NULL) |
context | Optional context pointer passed to predicate function (can be NULL) |
DA_DEF int da_find_index | ( | da_array | arr, |
int(*)(const void *element, void *context) | predicate, | ||
void * | context | ||
) |
Find index of first element matching predicate.
arr | Array to search (must not be NULL) |
predicate | Function to test elements (must not be NULL) |
context | Optional context passed to predicate (can be NULL) |
DA_DEF int da_is_empty | ( | da_array | arr | ) |
Checks if the array is empty.
arr | Array to check (must not be NULL) |
DA_DEF int da_length | ( | da_array | arr | ) |
Gets the current number of elements in the array.
arr | Array to query (must not be NULL) |
DA_DEF da_array da_map | ( | da_array | arr, |
void(*)(const void *src, void *dst, void *context) | mapper, | ||
void * | context | ||
) |
Creates a new array by transforming each element using a mapper function.
arr | Source array to transform (must not be NULL) |
mapper | Function to transform elements (must not be NULL) |
context | Optional context pointer passed to mapper function (can be NULL) |
DA_DEF void da_reduce | ( | da_array | arr, |
const void * | initial, | ||
void * | result, | ||
void(*)(void *accumulator, const void *element, void *context) | reducer, | ||
void * | context | ||
) |
Reduces array to single value using accumulator function.
arr | Source array (must not be NULL) |
initial | Initial accumulator value (must not be NULL) |
result | Output buffer for final result (must not be NULL) |
reducer | Function that combines accumulator with each element |
context | Optional context passed to reducer function (can be NULL) |
DA_DEF void da_remove_range | ( | da_array | arr, |
int | start, | ||
int | count | ||
) |
Removes multiple consecutive elements from the array.
arr | Array to modify (must not be NULL) |
start | Starting index of range to remove (must be >= 0) |
count | Number of elements to remove (must be >= 0) |
DA_DEF void da_reserve | ( | da_array | arr, |
int | new_capacity | ||
) |
Ensures the array has at least the specified capacity.
arr | Array to modify (must not be NULL) |
new_capacity | Minimum capacity required (must be >= 0) |
DA_DEF void da_resize | ( | da_array | arr, |
int | new_length | ||
) |
Changes the array length, growing or shrinking as needed.
arr | Array to modify (must not be NULL) |
new_length | New length for the array (must be >= 0) |
DA_DEF void da_reverse | ( | da_array | arr | ) |
Reverses all elements in the array in place.
arr | Array to reverse (must not be NULL) |
Creates a new array containing elements from a range [start, end)
arr | Source array to slice from (must not be NULL) |
start | Starting index (inclusive, must be >= 0) |
end | Ending index (exclusive, must be >= start and <= length) |
DA_DEF void da_sort | ( | da_array | arr, |
int(*)(const void *a, const void *b, void *context) | compare, | ||
void * | context | ||
) |
Sort array elements using comparison function.
arr | Array to sort in-place (must not be NULL) |
compare | Comparison function (must not be NULL) |
context | Optional context passed to comparison function (can be NULL) |
DA_DEF void da_swap | ( | da_array | arr, |
int | i, | ||
int | j | ||
) |
Swaps two elements at the specified indices.
arr | Array to modify (must not be NULL) |
i | First index (must be >= 0 and < length) |
j | Second index (must be >= 0 and < length) |
DA_DEF void da_trim | ( | da_array | arr, |
int | new_capacity | ||
) |
Reduces the array's allocated capacity to a specific size.
arr | Array to modify (must not be NULL) |
new_capacity | New capacity for the array (must be >= length) |