libstd v0.1.0
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
vector.h File Reference
#include <stdbool.h>
#include <stddef.h>
Include dependency graph for vector.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  VectorNode
 Node structure representing an element in the vector. More...
 
struct  Vector
 Structure representing a generic linked-list-backed Vector. More...
 

Typedefs

typedef struct VectorNode VectorNode
 Node structure representing an element in the vector.
 
typedef struct Vector Vector
 Structure representing a generic linked-list-backed Vector.
 

Functions

void vector_init (Vector *vector, size_t element_size)
 Initializes an empty Vector.
 
void vector_destroy (Vector *vector)
 Destroys the Vector and frees all allocated nodes and their associated data.
 
bool vector_insert (Vector *vector, const void *data)
 Appends (inserts) a new element to the end of the vector.
 
bool vector_remove (Vector *vector, size_t index)
 Removes an element at a specific index from the vector.
 
bool vector_update (Vector *vector, size_t index, const void *new_data)
 Updates the element data at a specific index in the vector.
 
void * vector_get (Vector *vector, size_t index)
 Retrieves a pointer to the element at a specific index.
 
int vector_search (Vector *vector, int(*cmp_fn)(void *, void *), void *key)
 Searches for an element in the vector using a custom comparison function.
 
int vector_size (const Vector *vector)
 Retrieves the total number of elements currently stored in the vector.
 
bool vector_sort (Vector *vector, int(*cmp_fn)(void *, void *))
 Sorts the elements in the vector using a comparison function.
 

Typedef Documentation

◆ Vector

typedef struct Vector Vector

Structure representing a generic linked-list-backed Vector.

◆ VectorNode

typedef struct VectorNode VectorNode

Node structure representing an element in the vector.

Function Documentation

◆ vector_destroy()

void vector_destroy ( Vector vector)

Destroys the Vector and frees all allocated nodes and their associated data.

Parameters
vectorPointer to the Vector to destroy.

◆ vector_get()

void * vector_get ( Vector vector,
size_t  index 
)

Retrieves a pointer to the element at a specific index.

Parameters
vectorPointer to the Vector.
indexZero-based position of the element to retrieve.
Returns
Pointer to the stored data, or NULL if index is out of bounds.

◆ vector_init()

void vector_init ( Vector vector,
size_t  element_size 
)

Initializes an empty Vector.

Parameters
vectorPointer to the Vector structure to initialize.
element_sizeSize of each element in bytes.

◆ vector_insert()

bool vector_insert ( Vector vector,
const void *  data 
)

Appends (inserts) a new element to the end of the vector.

Parameters
vectorPointer to the Vector.
dataPointer to the element data to insert.
Returns
true if insertion was successful, or false on memory allocation failure.

◆ vector_remove()

bool vector_remove ( Vector vector,
size_t  index 
)

Removes an element at a specific index from the vector.

Parameters
vectorPointer to the Vector.
indexZero-based position of the element to remove.
Returns
true if the element was successfully removed, or false if index is out of bounds.

◆ vector_search()

int vector_search ( Vector vector,
int(*)(void *, void *)  cmp_fn,
void *  key 
)

Searches for an element in the vector using a custom comparison function.

Parameters
vectorPointer to the Vector.
cmp_fnPointer to the comparison function (should return 0 when elements match).
keyPointer to the value/key being searched for.
Returns
Zero-based index of the element if found, or -1 if not found.

◆ vector_size()

int vector_size ( const Vector vector)

Retrieves the total number of elements currently stored in the vector.

Parameters
vectorPointer to the Vector.
Returns
Total number of elements.

◆ vector_sort()

bool vector_sort ( Vector vector,
int(*)(void *, void *)  cmp_fn 
)

Sorts the elements in the vector using a comparison function.

Parameters
vectorPointer to the Vector.
cmp_fnPointer to the comparison function defining the sort order (e.g., returns < 0 if a < b, 0 if a == b, > 0 if a > b).
Returns
true if sorting succeeded, or false if the operation failed.

◆ vector_update()

bool vector_update ( Vector vector,
size_t  index,
const void *  new_data 
)

Updates the element data at a specific index in the vector.

Parameters
vectorPointer to the Vector.
indexZero-based position of the element to update.
new_dataPointer to the new data to be copied into the vector.
Returns
true if the element was successfully updated, or false if index is out of bounds.