Kohi Game Engine
|
This files contains an implementation of a dynamic array. More...
#include "defines.h"
Go to the source code of this file.
Data Structures | |
struct | darray_header |
struct | darray_base |
struct | darray_iterator |
Macros | |
#define | DARRAY_DEFAULT_CAPACITY 1 |
The default darray capacity. More... | |
#define | DARRAY_RESIZE_FACTOR 2 |
The default resize factor (doubles on resize) More... | |
#define | darray_create(type) (type*)_darray_create(DARRAY_DEFAULT_CAPACITY, sizeof(type), 0) |
Creates a new darray of the given type with the default capacity. Performs a dynamic memory allocation. More... | |
#define | darray_create_with_allocator(type, allocator) _darray_create(DARRAY_DEFAULT_CAPACITY, sizeof(type), allocator) |
Creates a new darray of the given type with the default capacity. Performs a dynamic memory allocation. More... | |
#define | darray_reserve(type, capacity) _darray_create(capacity, sizeof(type), 0) |
Creates a new darray of the given type with the provided capacity. Performs a dynamic memory allocation. More... | |
#define | darray_reserve_with_allocator(type, capacity, allocator) _darray_create(capacity, sizeof(type), allocator) |
Creates a new darray of the given type with the provided capacity. Performs a dynamic memory allocation. More... | |
#define | darray_push(array, value) |
Pushes a new entry to the given array. Resizes if necessary. More... | |
#define | darray_insert_at(array, index, value) |
Inserts a copy of the given value into the supplied array at the given index. Triggers an array resize if required. More... | |
#define | darray_duplicate(type, array) (type*)_darray_duplicate(sizeof(type), array) |
Duplicates the given array to a completely fresh copy, including header data as well as actual data contained within. More... | |
#define | DARRAY_TYPE_NAMED(type, name) |
#define | DARRAY_TYPE(type) DARRAY_TYPE_NAMED(type, type) |
Typedefs | |
typedef struct darray_header | darray_header |
typedef struct darray_base | darray_base |
typedef struct darray_iterator | darray_iterator |
Functions | |
KAPI void * | _darray_create (u64 length, u64 stride, struct frame_allocator_int *frame_allocator) |
Creates a new darray of the given length and stride. Note that this performs a dynamic memory allocation. More... | |
KAPI void * | _darray_resize (void *array) |
Resizes the given array using internal resizing amounts. Causes a new allocation. More... | |
KAPI void * | _darray_push (void *array, const void *value_ptr) |
Pushes a new entry to the given array. Resizes if necessary. More... | |
KAPI void * | _darray_insert_at (void *array, u64 index, void *value_ptr) |
Inserts a copy of the given value into the supplied array at the given index. Triggers an array resize if required. More... | |
KAPI void * | _darray_duplicate (u64 stride, void *array) |
Duplicates the given array to a completely fresh copy, including header data as well as actual data contained within. More... | |
KAPI void | darray_destroy (void *array) |
Destroys the provided array, freeing any memory allocated by it. More... | |
KAPI void | darray_pop (void *array, void *dest) |
Pops an entry out of the array and places it into dest (if provided). More... | |
KAPI void * | darray_pop_at (void *array, u64 index, void *dest) |
Pops an entry out of the array at the given index and places it into dest (if provided). Brings in all entries after the popped index in by one. More... | |
KAPI void | darray_clear (void *array) |
Clears all entries from the array. Does not release any internally-allocated memory. More... | |
KAPI u64 | darray_capacity (void *array) |
Gets the given array's capacity. More... | |
KAPI u64 | darray_length (void *array) |
Gets the length (number of elements) in the given array. More... | |
KAPI u64 | darray_stride (void *array) |
Gets the stride (element size) of the given array. More... | |
KAPI void | darray_length_set (void *array, u64 value) |
Sets the length of the given array. This ensures the array has the required capacity to be able to set entries directly, for instance. Can trigger an internal reallocation. More... | |
KAPI void | _kdarray_init (u32 length, u32 stride, u32 capacity, struct frame_allocator_int *allocator, u32 *out_length, u32 *out_stride, u32 *out_capacity, void **block, struct frame_allocator_int **out_allocator) |
KAPI void | _kdarray_free (u32 *length, u32 *capacity, u32 *stride, void **block, struct frame_allocator_int **out_allocator) |
KAPI void | _kdarray_ensure_size (u32 required_length, u32 stride, u32 *out_capacity, struct frame_allocator_int *allocator, void **block, void **base_block) |
KAPI darray_iterator | darray_iterator_begin (darray_base *arr) |
KAPI darray_iterator | darray_iterator_rbegin (darray_base *arr) |
KAPI b8 | darray_iterator_end (const darray_iterator *it) |
KAPI void * | darray_iterator_value (const darray_iterator *it) |
KAPI void | darray_iterator_next (darray_iterator *it) |
KAPI void | darray_iterator_prev (darray_iterator *it) |
DARRAY_TYPE (b8) | |
DARRAY_TYPE (u8) | |
DARRAY_TYPE (u16) | |
DARRAY_TYPE (u32) | |
DARRAY_TYPE (u64) | |
DARRAY_TYPE (i8) | |
DARRAY_TYPE (i16) | |
DARRAY_TYPE (i32) | |
DARRAY_TYPE (i64) | |
DARRAY_TYPE (f32) | |
DARRAY_TYPE (f64) | |
DARRAY_TYPE_NAMED (const char *, string) | |
This files contains an implementation of a dynamic array.
Memory layout:
#define darray_create | ( | type | ) | (type*)_darray_create(DARRAY_DEFAULT_CAPACITY, sizeof(type), 0) |
Creates a new darray of the given type with the default capacity. Performs a dynamic memory allocation.
type | The type to be used to create the darray. |
#define darray_create_with_allocator | ( | type, | |
allocator | |||
) | _darray_create(DARRAY_DEFAULT_CAPACITY, sizeof(type), allocator) |
Creates a new darray of the given type with the default capacity. Performs a dynamic memory allocation.
type | The type to be used to create the darray. |
allocator | A pointer to a frame allocator. |
#define DARRAY_DEFAULT_CAPACITY 1 |
The default darray capacity.
#define darray_duplicate | ( | type, | |
array | |||
) | (type*)_darray_duplicate(sizeof(type), array) |
Duplicates the given array to a completely fresh copy, including header data as well as actual data contained within.
Performs a dynamic memory allocation.
type | The type to be used to duplicate the darray. Used for size verification. |
array | The array to be duplicated. |
#define darray_insert_at | ( | array, | |
index, | |||
value | |||
) |
Inserts a copy of the given value into the supplied array at the given index. Triggers an array resize if required.
array | The array to insert into. |
index | The index to insert at. |
value_ptr | A pointer holding the value to be inserted. |
#define darray_push | ( | array, | |
value | |||
) |
Pushes a new entry to the given array. Resizes if necessary.
array | The array to be pushed to. |
value | The value to be pushed. A copy of this value is taken. |
#define darray_reserve | ( | type, | |
capacity | |||
) | _darray_create(capacity, sizeof(type), 0) |
Creates a new darray of the given type with the provided capacity. Performs a dynamic memory allocation.
type | The type to be used to create the darray. |
capacity | The number of elements the darray can initially hold (can be resized). |
#define darray_reserve_with_allocator | ( | type, | |
capacity, | |||
allocator | |||
) | _darray_create(capacity, sizeof(type), allocator) |
Creates a new darray of the given type with the provided capacity. Performs a dynamic memory allocation.
type | The type to be used to create the darray. |
capacity | The number of elements the darray can initially hold (can be resized). |
allocator | A pointer to a frame allocator. |
#define DARRAY_RESIZE_FACTOR 2 |
The default resize factor (doubles on resize)
#define DARRAY_TYPE | ( | type | ) | DARRAY_TYPE_NAMED(type, type) |
#define DARRAY_TYPE_NAMED | ( | type, | |
name | |||
) |
typedef struct darray_base darray_base |
typedef struct darray_header darray_header |
typedef struct darray_iterator darray_iterator |
KAPI void* _darray_create | ( | u64 | length, |
u64 | stride, | ||
struct frame_allocator_int * | frame_allocator | ||
) |
Creates a new darray of the given length and stride. Note that this performs a dynamic memory allocation.
length | The default number of elements in the array. |
stride | The size of each array element. |
Duplicates the given array to a completely fresh copy, including header data as well as actual data contained within.
Performs a dynamic memory allocation.
type | The type to be used to duplicate the darray. Used for size verification. |
array | The array to be duplicated. |
Inserts a copy of the given value into the supplied array at the given index. Triggers an array resize if required.
array | The array to insert into. |
index | The index to insert at. |
value_ptr | A pointer holding the value to be inserted. |
KAPI void* _darray_push | ( | void * | array, |
const void * | value_ptr | ||
) |
Pushes a new entry to the given array. Resizes if necessary.
array | The array to be pushed to. |
value_ptr | A pointer to the value to be pushed. A copy of this value is taken. |
KAPI void* _darray_resize | ( | void * | array | ) |
Resizes the given array using internal resizing amounts. Causes a new allocation.
array | The array to be resized. |
KAPI void _kdarray_ensure_size | ( | u32 | required_length, |
u32 | stride, | ||
u32 * | out_capacity, | ||
struct frame_allocator_int * | allocator, | ||
void ** | block, | ||
void ** | base_block | ||
) |
KAPI void _kdarray_free | ( | u32 * | length, |
u32 * | capacity, | ||
u32 * | stride, | ||
void ** | block, | ||
struct frame_allocator_int ** | out_allocator | ||
) |
KAPI void _kdarray_init | ( | u32 | length, |
u32 | stride, | ||
u32 | capacity, | ||
struct frame_allocator_int * | allocator, | ||
u32 * | out_length, | ||
u32 * | out_stride, | ||
u32 * | out_capacity, | ||
void ** | block, | ||
struct frame_allocator_int ** | out_allocator | ||
) |
NEW DARRAY
Gets the given array's capacity.
array | The array whose capacity to retrieve. |
KAPI void darray_clear | ( | void * | array | ) |
Clears all entries from the array. Does not release any internally-allocated memory.
array | The array to be cleared. |
KAPI void darray_destroy | ( | void * | array | ) |
Destroys the provided array, freeing any memory allocated by it.
array | The array to be destroyed. |
KAPI darray_iterator darray_iterator_begin | ( | darray_base * | arr | ) |
KAPI b8 darray_iterator_end | ( | const darray_iterator * | it | ) |
KAPI void darray_iterator_next | ( | darray_iterator * | it | ) |
KAPI void darray_iterator_prev | ( | darray_iterator * | it | ) |
KAPI darray_iterator darray_iterator_rbegin | ( | darray_base * | arr | ) |
KAPI void* darray_iterator_value | ( | const darray_iterator * | it | ) |
Gets the length (number of elements) in the given array.
array | The array to obtain the length of. |
Sets the length of the given array. This ensures the array has the required capacity to be able to set entries directly, for instance. Can trigger an internal reallocation.
array | The array to set the length of. |
value | The length to set the array to. |
KAPI void darray_pop | ( | void * | array, |
void * | dest | ||
) |
Pops an entry out of the array and places it into dest (if provided).
array | The array to pop from. |
dest | A pointer to hold the popped value. Optional. |
Pops an entry out of the array at the given index and places it into dest (if provided). Brings in all entries after the popped index in by one.
array | The array to pop from. |
index | The index to pop from. |
dest | A pointer to hold the popped value. Optional. |
Gets the stride (element size) of the given array.
array | The array to obtain the stride of. |
DARRAY_TYPE | ( | b8 | ) |
DARRAY_TYPE | ( | f32 | ) |
DARRAY_TYPE | ( | f64 | ) |
DARRAY_TYPE | ( | i16 | ) |
DARRAY_TYPE | ( | i32 | ) |
DARRAY_TYPE | ( | i64 | ) |
DARRAY_TYPE | ( | i8 | ) |
DARRAY_TYPE | ( | u16 | ) |
DARRAY_TYPE | ( | u32 | ) |
DARRAY_TYPE | ( | u64 | ) |
DARRAY_TYPE | ( | u8 | ) |
DARRAY_TYPE_NAMED | ( | const char * | , |
string | |||
) |