dynvec.h file
Growable vector of element pointers.
Classes
- struct DynVec
- Dynamic vector of void* element pointers.
Typedefs
- using DynVecDestructor = void(*)(void*element)
- Callback that destroys a single vector element.
- using DynVecCopy = void*(*)(void*element)
- Callback that deep-copies a single vector element.
- using DynVecForEach = void*(*)(void*element)
- Callback invoked once per element (e.g.
- using DynVec = struct DynVec
- Dynamic vector of void* element pointers.
Functions
- static auto dynvec_new(const DynVecDestructor dtor) -> DynVec*
- Creates a vector with default initial capacity.
- static auto dynvec_with_capacity(const DynVecDestructor dtor, const size_t capacity) -> DynVec*
- Creates a vector with a chosen initial capacity.
- static void dynvec_set_destructor(DynVec* vec, const DynVecDestructor dtor)
- Sets the element destructor.
- static auto dynvec_destructor(const DynVec* vec) -> DynVecDestructor
- Returns the element destructor.
- static auto dynvec_length(const DynVec* vec) -> size_t
- Returns the number of elements.
- static auto dynvec_capacity(const DynVec* vec) -> size_t
- Returns the allocated capacity.
- static auto dynvec_empty(const DynVec* vec) -> int
- Returns whether the vector has no elements.
- static void dynvec_reserve(DynVec* vec, const size_t n)
- Grows capacity to at least
nif needed. - static void dynvec_grow(DynVec* vec)
- Doubles capacity when the vector is full.
- static void dynvec_maybe_destroy(DynVec* vec, const size_t i)
- Runs the destructor on elements[i] if one is set.
- static void dynvec_push(DynVec* vec, void* elem)
- Appends an element pointer, growing if needed.
- static void dynvec_insert(DynVec* vec, const size_t pos, void* elem)
- Inserts an element at position, shifting the tail right.
- static void dynvec_pop(DynVec* vec)
- Removes the last element, calling its destructor if set.
- static void dynvec_erase(DynVec* vec, const size_t pos)
- Erases the element at position, calling its destructor and shifting.
- static void dynvec_copy(DynVec* to, const DynVec* from)
- Copies elements from one vector to another.
- static void dynvec_free(DynVec* vec)
- Destroys all elements and frees the vector.
- static void dynvec_clear(DynVec* vec)
- Destroys all elements and sets length to 0.
- static auto dynvec_at_unsafe(const DynVec* vec, const size_t i) -> void*
- Returns elements[i] without bounds checking.
- static auto dynvec_at(const DynVec* vec, const size_t i) -> void*
- Returns elements[i], or NULL if out of range.
- static auto dynvec_index_of_unsafe(const DynVec* vec, const void* elem) -> size_t
- Finds the first index of element by pointer identity (unchecked).
- static auto dynvec_index_of(const DynVec* vec, const void* elem) -> size_t
- Finds the first index of element by pointer identity.
- static auto dynvec_begin(const DynVec* vec) -> void**
- Returns a pointer to the first element slot.
- static auto dynvec_end(const DynVec* vec) -> void**
- Returns a pointer one past the last element.
- static void dynvec_set_copy_func(DynVec* vec, const DynVecCopy copy_func)
- Sets the element deep-copy function.
- static auto dynvec_copy_func(const DynVec* vec) -> DynVecCopy
- Returns the element deep-copy function.
Defines
- #define dynvec_clib_free
- #define dynvec_clib_malloc
- #define dynvec_clib_realloc
- #define DYNVEC_INITIAL_CAPACITY
- #define FOR_EACH(vec, func, ...)
- Calls function for each element in vector.
Typedef documentation
typedef struct DynVec DynVec
Dynamic vector of void* element pointers.
Stores pointers only; callers own allocation unless a destructor is set. Optional copy_func enables deep copies in dynvec_copy.
Function documentation
static DynVec* dynvec_new(const DynVecDestructor dtor)
Creates a vector with default initial capacity.
| Parameters | |
|---|---|
| dtor | Optional element destructor, or NULL. |
| Returns | New vector, or NULL on allocation failure. |
static DynVec* dynvec_with_capacity(const DynVecDestructor dtor, const size_t capacity)
Creates a vector with a chosen initial capacity.
| Parameters | |
|---|---|
| dtor | Optional element destructor, or NULL. |
| capacity | Initial capacity in elements. |
| Returns | New vector, or NULL on allocation failure. |
static void dynvec_set_destructor(DynVec* vec, const DynVecDestructor dtor)
Sets the element destructor.
| Parameters | |
|---|---|
| vec | Vector to update. |
| dtor | New destructor, or NULL to clear. |
static DynVecDestructor dynvec_destructor(const DynVec* vec)
Returns the element destructor.
| Parameters | |
|---|---|
| vec | Vector to query. |
| Returns | Destructor pointer, or NULL if unset/invalid. |
static size_t dynvec_length(const DynVec* vec)
Returns the number of elements.
| Parameters | |
|---|---|
| vec | Vector to query (NULL-safe). |
| Returns | Length, or 0 if vec is NULL. |
static size_t dynvec_capacity(const DynVec* vec)
Returns the allocated capacity.
| Parameters | |
|---|---|
| vec | Vector to query (NULL-safe). |
| Returns | Capacity, or 0 if vec is NULL. |
static int dynvec_empty(const DynVec* vec)
Returns whether the vector has no elements.
| Parameters | |
|---|---|
| vec | Vector to query. |
| Returns | Non-zero if empty or vec is NULL. |
static void dynvec_reserve(DynVec* vec, const size_t n)
Grows capacity to at least n if needed.
| Parameters | |
|---|---|
| vec | Vector to reserve space in. |
| n | Minimum required capacity. |
static void dynvec_grow(DynVec* vec)
Doubles capacity when the vector is full.
| Parameters | |
|---|---|
| vec | Vector to grow. |
static void dynvec_maybe_destroy(DynVec* vec, const size_t i)
Runs the destructor on elements[i] if one is set.
| Parameters | |
|---|---|
| vec | Vector owning the element. |
| i | Element index. |
static void dynvec_push(DynVec* vec, void* elem)
Appends an element pointer, growing if needed.
| Parameters | |
|---|---|
| vec | Vector to modify. |
| elem | Non-NULL pointer to store. |
static void dynvec_insert(DynVec* vec, const size_t pos, void* elem)
Inserts an element at position, shifting the tail right.
| Parameters | |
|---|---|
| vec | Vector to modify. |
| pos | Insert index in [0, length]. |
| elem | Non-NULL pointer to insert. |
On grow failure the vector is left unchanged.
static void dynvec_pop(DynVec* vec)
Removes the last element, calling its destructor if set.
| Parameters | |
|---|---|
| vec | Vector to modify. |
static void dynvec_erase(DynVec* vec, const size_t pos)
Erases the element at position, calling its destructor and shifting.
| Parameters | |
|---|---|
| vec | Vector to modify. |
| pos | Index in [0, length). |
static void dynvec_copy(DynVec* to, const DynVec* from)
Copies elements from one vector to another.
| Parameters | |
|---|---|
| to | Destination vector. |
| from | Source vector. |
Uses to->copy_func for deep copy when set; otherwise shallow-copies pointers. Reserves capacity in to as needed.
static void dynvec_free(DynVec* vec)
Destroys all elements and frees the vector.
| Parameters | |
|---|---|
| vec | Vector to free (NULL-safe). |
static void dynvec_clear(DynVec* vec)
Destroys all elements and sets length to 0.
| Parameters | |
|---|---|
| vec | Vector to clear. |
Capacity and the elements array are kept.
static void* dynvec_at_unsafe(const DynVec* vec, const size_t i)
Returns elements[i] without bounds checking.
| Parameters | |
|---|---|
| vec | Vector to index. |
| i | Element index (must be in range). |
| Returns | Element pointer, or NULL if vec is NULL. |
static size_t dynvec_index_of_unsafe(const DynVec* vec, const void* elem)
Finds the first index of element by pointer identity (unchecked).
| Parameters | |
|---|---|
| vec | Non-NULL vector. |
| elem | Non-NULL pointer to search for. |
| Returns | Index if found, or (size_t)-1 if not found. |
static size_t dynvec_index_of(const DynVec* vec, const void* elem)
Finds the first index of element by pointer identity.
| Parameters | |
|---|---|
| vec | Vector to search. |
| elem | Pointer to search for. |
| Returns | Index if found, or (size_t)-1 if missing/invalid. |
static void** dynvec_begin(const DynVec* vec)
Returns a pointer to the first element slot.
| Parameters | |
|---|---|
| vec | Vector to query. |
| Returns | &elements[0], or NULL if empty/invalid. |
static void** dynvec_end(const DynVec* vec)
Returns a pointer one past the last element.
| Parameters | |
|---|---|
| vec | Vector to query. |
| Returns | &elements[length], or NULL if vec is NULL. |
static void dynvec_set_copy_func(DynVec* vec, const DynVecCopy copy_func)
Sets the element deep-copy function.
| Parameters | |
|---|---|
| vec | Vector to update. |
| copy_func | New copy function, or NULL to clear. |
static DynVecCopy dynvec_copy_func(const DynVec* vec)
Returns the element deep-copy function.
| Parameters | |
|---|---|
| vec | Vector to query. |
| Returns | Copy function, or NULL if unset/invalid. |