Functions for adding, removing, and clearing elements.
More...
|
DA_DEF void | da_push (da_array arr, const void *element) |
| Appends an element to the end of the array.
|
|
DA_DEF void | da_insert (da_array arr, int index, const void *element) |
| Inserts an element at the specified index.
|
|
DA_DEF void | da_remove (da_array arr, int index, void *out) |
| Removes and optionally returns an element at the specified index.
|
|
DA_DEF void | da_pop (da_array arr, void *out) |
| Removes and optionally returns the last element.
|
|
DA_DEF void | da_clear (da_array arr) |
| Removes all elements from the array.
|
|
DA_DEF void * | da_peek (da_array arr) |
| Gets a pointer to the last element without removing it.
|
|
DA_DEF void * | da_peek_first (da_array arr) |
| Gets a pointer to the first element without removing it.
|
|
Functions for adding, removing, and clearing elements.
◆ da_clear()
Removes all elements from the array.
- Parameters
-
arr | Array to clear (must not be NULL) |
- Note
- Sets length to 0 but preserves capacity
-
Does not free allocated memory (use da_resize(arr, 0) for that)
DA_DEF void da_clear(da_array arr)
Removes all elements from the array.
Definition dynamic_array.h:1686
DA_DEF int da_length(da_array arr)
Gets the current number of elements in the array.
Definition dynamic_array.h:1700
◆ da_insert()
DA_DEF void da_insert |
( |
da_array |
arr, |
|
|
int |
index, |
|
|
const void * |
element |
|
) |
| |
Inserts an element at the specified index.
- Parameters
-
arr | Array to modify (must not be NULL) |
index | Position to insert at (must be >= 0 and <= length) |
element | Pointer to element data to copy (must not be NULL) |
- Note
- Shifts all elements at and after index to the right
-
Automatically grows array capacity if needed
-
index == length is equivalent to da_push()
-
Asserts on out-of-bounds index or NULL parameters
int value = 42;
DA_DEF void da_insert(da_array arr, int index, const void *element)
Inserts an element at the specified index.
Definition dynamic_array.h:1609
◆ da_peek()
Gets a pointer to the last element without removing it.
- Parameters
-
arr | Array to peek (must not be NULL and not empty) |
- Returns
- Pointer to the last element
- Note
- Asserts if array is empty
-
Returned pointer is valid until array is modified or released
-
Useful for stack-like access patterns
int* last_ptr = (
int*)
da_peek(arr);
printf("Last element: %d\n", *last_ptr);
DA_DEF void * da_peek(da_array arr)
Gets a pointer to the last element without removing it.
Definition dynamic_array.h:1994
◆ da_peek_first()
DA_DEF void * da_peek_first |
( |
da_array |
arr | ) |
|
Gets a pointer to the first element without removing it.
- Parameters
-
arr | Array to peek (must not be NULL and not empty) |
- Returns
- Pointer to the first element
- Note
- Asserts if array is empty
-
Returned pointer is valid until array is modified or released
-
Useful for queue-like access patterns
printf("First element: %d\n", *first_ptr);
DA_DEF void * da_peek_first(da_array arr)
Gets a pointer to the first element without removing it.
Definition dynamic_array.h:2000
◆ da_pop()
DA_DEF void da_pop |
( |
da_array |
arr, |
|
|
void * |
out |
|
) |
| |
Removes and optionally returns the last element.
- Parameters
-
arr | Array to modify (must not be NULL) |
out | Optional pointer to store popped element (can be NULL) |
- Note
- Asserts if array is empty
-
If out is NULL, element is discarded
-
Does not shrink capacity
int popped;
DA_DEF void da_pop(da_array arr, void *out)
Removes and optionally returns the last element.
Definition dynamic_array.h:1669
◆ da_push()
DA_DEF void da_push |
( |
da_array |
arr, |
|
|
const void * |
element |
|
) |
| |
Appends an element to the end of the array.
- Parameters
-
arr | Array to modify (must not be NULL) |
element | Pointer to element data to copy (must not be NULL) |
- Note
- Automatically grows array capacity if needed
-
Asserts on allocation failure or NULL parameters
-
Uses configured growth strategy (DA_GROWTH)
int value = 42;
DA_DEF void da_push(da_array arr, const void *element)
Appends an element to the end of the array.
Definition dynamic_array.h:1587
◆ da_remove()
DA_DEF void da_remove |
( |
da_array |
arr, |
|
|
int |
index, |
|
|
void * |
out |
|
) |
| |
Removes and optionally returns an element at the specified index.
- Parameters
-
arr | Array to modify (must not be NULL) |
index | Position to remove from (must be >= 0 and < length) |
out | Optional pointer to store removed element (can be NULL) |
- Note
- Shifts all elements after index to the left
-
Does not shrink capacity
-
Asserts on out-of-bounds index
int removed;
DA_DEF void da_remove(da_array arr, int index, void *out)
Removes and optionally returns an element at the specified index.
Definition dynamic_array.h:1642