Libft
Custom implementation of core libc functions with additional utility helpers.
Loading...
Searching...
No Matches
Vector API

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.
 

Detailed Description

Dynamic array utilities for contiguous fixed-size items.

Functions to initialize, grow, shrink, duplicate and manipulate dynamic arrays storing items contiguously in memory.

Function Documentation

◆ vector_adjust()

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.

Warning
vector must be initialized before calling this function.
Parameters
vectorPointer to an initialized vector (borrowed).
Returns
true on success, false on allocation failure.
Here is the call graph for this function:

◆ vector_clear()

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.

Warning
vector must be initialized before calling this function.
Parameters
vectorPointer to an initialized vector (borrowed).
delOptional callback applied to each stored item before clearing (borrowed, read-only).

◆ vector_dup()

bool vector_dup ( t_vector dst,
const t_vector src 
)

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.

Note
On success, dst is initialized and owns its internal storage. Caller must later release it with vector_free().
Warning
dst must NOT be initialized, or must be freed before calling this function.
dst and src must be different.
Parameters
dstDestination vector to initialize and fill (borrowed).
srcSource vector to duplicate (borrowed).
Returns
true on success, false on failure.
Here is the call graph for this function:

◆ vector_free()

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.

Warning
Does not free the t_vector struct itself, only its internal data.
Parameters
vectorPointer to the vector (borrowed).
item_freeOptional callback to free each item (can be NULL).
Here is the caller graph for this function:

◆ vector_grow()

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.

Warning
vector must be initialized before calling this function.
Parameters
vectorPointer to an initialized vector (borrowed).
Returns
true on success, false on overflow or allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vector_init()

bool vector_init ( t_vector vector,
size_t  item_size,
size_t  cap 
)

Initializes a vector with a given item size and initial capacity.

Note
Cannot fail when cap == 0.
Warning
vector must NOT already own allocated data. Call vector_free() first if needed.
Parameters
vectorPointer to the vector to initialize (borrowed, uninitialized).
item_sizeSize of each item, in bytes (must be > 0).
capInitial capacity, expressed in items.
Returns
true on success, false on invalid item_size, overflow, or allocation failure.
Here is the caller graph for this function:

◆ vector_insert()

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.

Warning
vector must be initialized before calling this function.
item must NOT point inside vector->data.
Parameters
vectorPointer to an initialized vector (borrowed).
indexInsertion index, in range [0, vector->len].
itemItem to insert (borrowed, not modified).
Returns
true on success, false on invalid index or allocation failure.
Here is the call graph for this function:

◆ vector_merge()

bool vector_merge ( t_vector dst,
const t_vector src,
size_t  index 
)

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.

Warning
dst and src must be initialized before calling this function.
dst and src must be different.
dst and src must use the same item_size.
Parameters
dstDestination vector receiving the inserted items (borrowed).
srcSource vector providing items to copy (borrowed).
indexInsertion index in dst, in range [0, dst->len].
Returns
true on success, false on invalid input, overflow, or allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vector_pop()

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.

Warning
vector must be initialized before calling this function.
Parameters
vectorPointer to an initialized vector (borrowed).
dstOptional destination buffer receiving the removed item (borrowed, can be NULL).
Returns
true on success, false if the vector is empty.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vector_push()

bool vector_push ( t_vector vector,
const void *  item 
)

Appends one item at the end of the vector.

Vector is automatically grown if necessary.

Warning
vector must be initialized before calling this function.
item must NOT point inside vector->data.
Parameters
vectorPointer to an initialized vector (borrowed).
itemItem to append (borrowed, not modified).
Returns
true on success, false on allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ vector_remove()

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.

Warning
vector must be initialized before calling this function.
Parameters
vectorPointer to an initialized vector (borrowed).
indexIndex of the item to remove.
dstOptional destination buffer receiving the removed item (borrowed, can be NULL).
Returns
true on success, false if index is out of bounds.
Here is the call graph for this function:

◆ vector_take()

void vector_take ( t_vector dst,
t_vector src 
)

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.

Warning
dst and src must be different.
Parameters
dstDestination vector receiving the transferred state (borrowed).
srcSource vector whose state is transferred (borrowed).