My Project
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
marigold_dynamic_array.h File Reference
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
Include dependency graph for marigold_dynamic_array.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  dynamic_array
 

Typedefs

typedef struct dynamic_array dynamic_array
 

Functions

dynamic_arraydynamic_array_create (size_t item_size, unsigned int starting_capacity, unsigned char growth_factor, bool is_multithread_safe)
 Create and initialize a new dynamic array.
 
unsigned int dynamic_array_get_occupancy (const dynamic_array *array)
 Get the number of elements currently in the array.
 
unsigned int dynamic_array_get_capacity (const dynamic_array *array)
 Get the total capacity of the array.
 
void * dynamic_array_get_pointer_to_index (const dynamic_array *array, const unsigned int index)
 Get a pointer to the element at the specified index.
 
const void * dynamic_array_get_const_pointer_to_index (const dynamic_array *array, const unsigned int index)
 Get a const pointer to the element at the specified index.
 
bool dynamic_array_append (const dynamic_array *array, const void *element)
 Append an element to the end of the array.
 
bool dynamic_array_pop (const dynamic_array *array)
 Remove the last element from the array.
 
bool dynamic_array_remove (const dynamic_array *array, const unsigned int index)
 Remove an element at the specified index.
 
void dynamic_array_clear (const dynamic_array *array)
 Clear all elements from the array.
 
bool dynamic_array_is_empty (const dynamic_array *array)
 Check if the array is empty.
 
bool dynamic_array_increase_capacity (const dynamic_array *array, const unsigned int new_capacity)
 Reserve additional capacity without changing size.
 
bool dynamic_array_acquire (const dynamic_array *array)
 Increment the owner count for shared ownership.
 
bool dynamic_array_release (const dynamic_array *array)
 Decrement the owner count for shared ownership.
 
unsigned short dynamic_array_get_owner_count (const dynamic_array *array)
 Get the current owner count.
 
bool dynamic_array_is_thread_safe (const dynamic_array *array)
 Check if the array is thread-safe.
 
bool dynamic_array_clone (const dynamic_array *original_array, const dynamic_array *new_array)
 Creates a deep copy of the array.
 

Typedef Documentation

◆ dynamic_array

typedef struct dynamic_array dynamic_array

Function Documentation

◆ dynamic_array_acquire()

bool dynamic_array_acquire ( const dynamic_array array)

Increment the owner count for shared ownership.

Parameters
arrayPointer to the dynamic_array.
Returns
true on success, false if owner_count would overflow.

◆ dynamic_array_append()

bool dynamic_array_append ( const dynamic_array array,
const void *  element 
)

Append an element to the end of the array.

Automatically resizes if current_size equals current_capacity.

Parameters
arrayPointer to the dynamic_array.
elementPointer to the element to append.
Returns
true on success, false on allocation failure.

◆ dynamic_array_clear()

void dynamic_array_clear ( const dynamic_array array)

Clear all elements from the array.

Sets current_size to 0. Does not free the memory block.

Parameters
arrayPointer to the dynamic_array.

◆ dynamic_array_clone()

bool dynamic_array_clone ( const dynamic_array original_array,
const dynamic_array new_array 
)

Creates a deep copy of the array.

Parameters
arrayPointer to the copy being copied, and one to copy it onto. Must create your own struct instance.
Returns
true if creation is successful. False if it fails for any reason.

◆ dynamic_array_create()

dynamic_array * dynamic_array_create ( size_t  item_size,
unsigned int  starting_capacity,
unsigned char  growth_factor,
bool  is_multithread_safe 
)

Create and initialize a new dynamic array.

Creates a dynamic array with the specified item size and starting capacity. The growth_factor determines how the array expands when capacity is exceeded.

Parameters
item_sizeSize of each element in bytes.
starting_capacityInitial number of elements to allocate space for.
growth_factorGrowth strategy: 0 = linear (+starting_capacity), 1 = 1.5x, 2+ = multiplier (2 = 2x, 3 = 3x, etc.)
is_multithread_safeIf true, enables thread-safe operations.
Returns
Pointer to the initialized dynamic_array, or NULL on failure.

◆ dynamic_array_get_capacity()

unsigned int dynamic_array_get_capacity ( const dynamic_array array)

Get the total capacity of the array.

Parameters
arrayPointer to the dynamic_array.
Returns
Total capacity (current_capacity).

◆ dynamic_array_get_const_pointer_to_index()

const void * dynamic_array_get_const_pointer_to_index ( const dynamic_array array,
const unsigned int  index 
)

Get a const pointer to the element at the specified index.

Parameters
arrayPointer to the dynamic_array.
indexIndex of the element to access.
Returns
Const pointer to the element, or NULL if index is out of bounds.

◆ dynamic_array_get_occupancy()

unsigned int dynamic_array_get_occupancy ( const dynamic_array array)

Get the number of elements currently in the array.

Parameters
arrayPointer to the dynamic_array.
Returns
Number of elements (current_size).

◆ dynamic_array_get_owner_count()

unsigned short dynamic_array_get_owner_count ( const dynamic_array array)

Get the current owner count.

Parameters
arrayPointer to the dynamic_array.
Returns
Current owner_count value.

◆ dynamic_array_get_pointer_to_index()

void * dynamic_array_get_pointer_to_index ( const dynamic_array array,
const unsigned int  index 
)

Get a pointer to the element at the specified index.

Parameters
arrayPointer to the dynamic_array.
indexIndex of the element to access.
Returns
Pointer to the element, or NULL if index is out of bounds.

◆ dynamic_array_increase_capacity()

bool dynamic_array_increase_capacity ( const dynamic_array array,
const unsigned int  new_capacity 
)

Reserve additional capacity without changing size.

Parameters
arrayPointer to the dynamic_array.
new_capacityDesired capacity.
Returns
true on success, false on allocation failure.

◆ dynamic_array_is_empty()

bool dynamic_array_is_empty ( const dynamic_array array)

Check if the array is empty.

Parameters
arrayPointer to the dynamic_array.
Returns
true if current_size is 0, false otherwise.

◆ dynamic_array_is_thread_safe()

bool dynamic_array_is_thread_safe ( const dynamic_array array)

Check if the array is thread-safe.

Parameters
arrayPointer to the dynamic_array.
Returns
true if is_multithread_safe is set, false otherwise.

◆ dynamic_array_pop()

bool dynamic_array_pop ( const dynamic_array array)

Remove the last element from the array.

Decrements current_size. Does not free the memory block.

Parameters
arrayPointer to the dynamic_array.
Returns
true on success, false if array is empty.

◆ dynamic_array_release()

bool dynamic_array_release ( const dynamic_array array)

Decrement the owner count for shared ownership.

If owner_count reaches 0, the array is automatically destroyed.

Parameters
arrayPointer to the dynamic_array.
Returns
true if owner_count > 0 after decrement, false if destroyed.

◆ dynamic_array_remove()

bool dynamic_array_remove ( const dynamic_array array,
const unsigned int  index 
)

Remove an element at the specified index.

Shifts subsequent elements down to fill the gap.

Parameters
arrayPointer to the dynamic_array.
indexIndex of the element to remove.
Returns
true on success, false if index is out of bounds.