|
Libft
Custom implementation of core libc functions with additional utility helpers.
|
Dynamic array utilities for contiguous fixed-size items. More...
Functions | |
| bool | vector_init (t_vector *vector, size_t item_size, size_t cap) |
| Initializes a vector with a given item size and initial capacity. | |
| bool | vector_grow (t_vector *vector) |
| Grows vector capacity, usually by doubling it. | |
| bool | vector_adjust (t_vector *vector) |
| Shrinks vector capacity to match its current length. | |
| bool | vector_dup (t_vector *dst, const t_vector *src) |
| Duplicates a vector into another one. | |
| void | vector_free (t_vector *vector, void(*item_free)(void *item)) |
| Frees the vector's internal storage. | |
| void | vector_clear (t_vector *vector, void(*del)(void *)) |
| Removes all items from a vector without freeing its storage. | |
| bool | vector_push (t_vector *vector, const void *item) |
| Appends one item at the end of the vector. | |
| bool | vector_pop (t_vector *vector, void *dst) |
| Removes the last item from the vector. | |
| bool | vector_insert (t_vector *vector, size_t index, const void *item) |
| Inserts one item at a specific index. | |
| bool | vector_remove (t_vector *vector, size_t index, void *dst) |
| Removes one item at a specific index. | |
| void | vector_take (t_vector *dst, t_vector *src) |
| Transfers a vector state into another one without copying items. | |
| bool | vector_merge (t_vector *dst, const t_vector *src, size_t index) |
| Inserts all items from src into dst at a specific index. | |
Dynamic array utilities for contiguous fixed-size items.
Functions to initialize, grow, shrink, duplicate and manipulate dynamic arrays storing items contiguously in memory.
| bool vector_adjust | ( | t_vector * | vector | ) |
Shrinks vector capacity to match its current length.
Existing items are preserved. If vector length is 0, internal storage is freed.
| vector | Pointer to an initialized vector (borrowed). |

| void vector_clear | ( | t_vector * | vector, |
| void(*)(void *) | del | ||
| ) |
Removes all items from a vector without freeing its storage.
If del is not NULL, it is called once for each stored item before the vector length is reset to 0. The callback receives a pointer to the item slot inside the vector storage.
vector must be initialized before calling this function.| vector | Pointer to an initialized vector (borrowed). |
| del | Optional callback applied to each stored item before clearing (borrowed, read-only). |
Duplicates a vector into another one.
A new internal storage is allocated for dst. Items are copied byte-for-byte; item payloads themselves are not deep-copied.
| dst | Destination vector to initialize and fill (borrowed). |
| src | Source vector to duplicate (borrowed). |

| void vector_free | ( | t_vector * | vector, |
| void(*)(void *item) | item_free | ||
| ) |
Frees the vector's internal storage.
Calls item_free on each stored item when provided. Frees vector->data only when vector owns its storage, that is when vector->cap is greater than 0. Then sets vector->data to NULL and vector->len and vector->cap to 0.
| vector | Pointer to the vector (borrowed). |
| item_free | Optional callback to free each item (can be NULL). |

| bool vector_grow | ( | t_vector * | vector | ) |
Grows vector capacity, usually by doubling it.
Existing items are preserved. If current capacity is 0, grows to VECTOR_INIT_CAP.
| vector | Pointer to an initialized vector (borrowed). |


| bool vector_init | ( | t_vector * | vector, |
| size_t | item_size, | ||
| size_t | cap | ||
| ) |
Initializes a vector with a given item size and initial capacity.
| vector | Pointer to the vector to initialize (borrowed, uninitialized). |
| item_size | Size of each item, in bytes (must be > 0). |
| cap | Initial capacity, expressed in items. |

| bool vector_insert | ( | t_vector * | vector, |
| size_t | index, | ||
| const void * | item | ||
| ) |
Inserts one item at a specific index.
Existing items at and after index are shifted to the right. Vector is automatically grown if necessary.
| vector | Pointer to an initialized vector (borrowed). |
| index | Insertion index, in range [0, vector->len]. |
| item | Item to insert (borrowed, not modified). |

Inserts all items from src into dst at a specific index.
Items are copied byte-for-byte from src into dst. Source and destination storages remain independent after the merge.
| dst | Destination vector receiving the inserted items (borrowed). |
| src | Source vector providing items to copy (borrowed). |
| index | Insertion index in dst, in range [0, dst->len]. |


| bool vector_pop | ( | t_vector * | vector, |
| void * | dst | ||
| ) |
Removes the last item from the vector.
If dst is not NULL, the removed item is copied there before removal.
| vector | Pointer to an initialized vector (borrowed). |
| dst | Optional destination buffer receiving the removed item (borrowed, can be NULL). |


| bool vector_push | ( | t_vector * | vector, |
| const void * | item | ||
| ) |
Appends one item at the end of the vector.
Vector is automatically grown if necessary.
| vector | Pointer to an initialized vector (borrowed). |
| item | Item to append (borrowed, not modified). |


| bool vector_remove | ( | t_vector * | vector, |
| size_t | index, | ||
| void * | dst | ||
| ) |
Removes one item at a specific index.
Items after index are shifted left to fill the gap. If dst is not NULL, the removed item is copied there before removal.
| vector | Pointer to an initialized vector (borrowed). |
| index | Index of the item to remove. |
| dst | Optional destination buffer receiving the removed item (borrowed, can be NULL). |

Transfers a vector state into another one without copying items.
After the transfer, dst receives the previous state of src. Then src keeps its data pointer and len, but no longer owns the storage because src->cap is set to 0.
dst and src must be different.| dst | Destination vector receiving the transferred state (borrowed). |
| src | Source vector whose state is transferred (borrowed). |