Dynamic array based on segments of exponentially growing size. More...
Go to the source code of this file.
Data Structures | |
| struct | tommy_array_struct |
| Array container type. More... | |
Macros | |
| #define | TOMMY_ARRAY_BIT 6 |
| Initial and minimal size of the array expressed as a power of 2. More... | |
Typedefs | |
| typedef struct tommy_array_struct | tommy_array |
| Array container type. More... | |
Functions | |
| TOMMY_API void | tommy_array_init (tommy_array *array) |
| Initializes the array. More... | |
| TOMMY_API void | tommy_array_done (tommy_array *array) |
| Deinitializes the array. More... | |
| TOMMY_API void | tommy_array_reserve (tommy_array *array, tommy_size_t size) |
| Allocates space for at least the specified number of elements. More... | |
| void | tommy_array_grow (tommy_array *array, tommy_size_t size) |
| Grows the size up to the specified value. More... | |
| TOMMY_API void | tommy_array_resize (tommy_array *array, tommy_size_t size) |
| Changes the initialized size, preserving the common prefix. More... | |
| void | tommy_array_clear (tommy_array *array) |
| Removes all elements, preserving the allocated capacity. More... | |
| TOMMY_API void | tommy_array_shrink (tommy_array *array) |
| Releases unused allocated memory. More... | |
| void ** | tommy_array_ref (tommy_array *array, tommy_size_t pos) |
| Gets a reference to the element at the specified position. More... | |
| void | tommy_array_set (tommy_array *array, tommy_size_t pos, void *element) |
| Sets the element at the specified position. More... | |
| void * | tommy_array_get (tommy_array *array, tommy_size_t pos) |
| Gets the element at the specified position. More... | |
| void * | tommy_array_tail (tommy_array *array) |
| Gets the last element. More... | |
| void | tommy_array_insert_tail (tommy_array *array, void *element) |
| Grows and inserts a new element at the end of the array. More... | |
| void * | tommy_array_remove_tail (tommy_array *array) |
| Removes and returns the last element. More... | |
| tommy_bool_t | tommy_array_empty (const tommy_array *array) |
| Checks whether the array is empty. More... | |
| tommy_size_t | tommy_array_size (const tommy_array *array) |
| Gets the initialized size of the array. More... | |
| tommy_size_t | tommy_array_capacity (const tommy_array *array) |
| Gets the number of elements that fit without further allocation. More... | |
| void | tommy_array_swap (tommy_array *first, tommy_array *second) |
| Exchanges two initialized arrays without copying their elements. More... | |
| TOMMY_API void | tommy_array_foreach (tommy_array *array, tommy_foreach_func *func) |
| Calls the specified function for each element in the array. More... | |
| TOMMY_API void | tommy_array_foreach_arg (tommy_array *array, tommy_foreach_arg_func *func, void *arg) |
| Calls the specified function with an argument for each element in the array. More... | |
| TOMMY_API tommy_size_t | tommy_array_memory_usage (const tommy_array *array) |
| Gets the size of allocated memory. More... | |
Dynamic array based on segments of exponentially growing size.
This array is able to grow dynamically upon request, without any reallocation.
The grow operation involves an allocation of a new array segment, without reallocating the already used memory, and thus not increasing the heap fragmentation. This also implies that the address of the stored elements never changes.
Allocated segments grow in size exponentially.
| #define TOMMY_ARRAY_BIT 6 |
Initial and minimal size of the array expressed as a power of 2.
The initial size is 2^TOMMY_ARRAY_BIT.
| typedef struct tommy_array_struct tommy_array |
Array container type.
| TOMMY_API void tommy_array_init | ( | tommy_array * | array | ) |
Initializes the array.
| array | The array. |
| TOMMY_API void tommy_array_done | ( | tommy_array * | array | ) |
Deinitializes the array.
| array | The array. |
| TOMMY_API void tommy_array_reserve | ( | tommy_array * | array, |
| tommy_size_t | size | ||
| ) |
Allocates space for at least the specified number of elements.
The initialized size and existing elements are unchanged. Existing element references remain valid.
| array | The array. |
| size | Number of elements to reserve space for. |
| void tommy_array_grow | ( | tommy_array * | array, |
| tommy_size_t | size | ||
| ) |
Grows the size up to the specified value.
All the new elements in the array are initialized with the 0 value.
| array | The array. |
| size | New size of the array. |
| TOMMY_API void tommy_array_resize | ( | tommy_array * | array, |
| tommy_size_t | size | ||
| ) |
Changes the initialized size, preserving the common prefix.
New elements are initialized to zero. Reducing the size does not release allocated capacity.
| array | The array. |
| size | New size of the array. |
| void tommy_array_clear | ( | tommy_array * | array | ) |
Removes all elements, preserving the allocated capacity.
Call tommy_array_shrink() after clearing to restore the initial capacity. Pointed-to elements are not freed. The array remains initialized and can be reused immediately.
| array | The array. |
| TOMMY_API void tommy_array_shrink | ( | tommy_array * | array | ) |
Releases unused allocated memory.
Preserves the size, values, and addresses of existing elements.
| array | The array. |
| void ** tommy_array_ref | ( | tommy_array * | array, |
| tommy_size_t | pos | ||
| ) |
Gets a reference to the element at the specified position.
You must be sure that space for this position is already allocated by calling tommy_array_grow().
| array | The array. |
| pos | Position of the element. |
| void tommy_array_set | ( | tommy_array * | array, |
| tommy_size_t | pos, | ||
| void * | element | ||
| ) |
Sets the element at the specified position.
You must be sure that space for this position is already allocated by calling tommy_array_grow().
| array | The array. |
| pos | Position of the element. |
| element | Element to set. |
| void * tommy_array_get | ( | tommy_array * | array, |
| tommy_size_t | pos | ||
| ) |
Gets the element at the specified position.
You must be sure that space for this position is already allocated by calling tommy_array_grow().
| array | The array. |
| pos | Position of the element. |
| void * tommy_array_tail | ( | tommy_array * | array | ) |
Gets the last element.
The array must not be empty.
| array | The array. |
| void tommy_array_insert_tail | ( | tommy_array * | array, |
| void * | element | ||
| ) |
Grows and inserts a new element at the end of the array.
| array | The array. |
| element | Element to insert. |
| void * tommy_array_remove_tail | ( | tommy_array * | array | ) |
Removes and returns the last element.
The array must not be empty. The removed slot is cleared, and allocated capacity is preserved.
| array | The array. |
| tommy_bool_t tommy_array_empty | ( | const tommy_array * | array | ) |
Checks whether the array is empty.
| array | The array. |
| tommy_size_t tommy_array_size | ( | const tommy_array * | array | ) |
Gets the initialized size of the array.
| array | The array. |
| tommy_size_t tommy_array_capacity | ( | const tommy_array * | array | ) |
Gets the number of elements that fit without further allocation.
| array | The array. |
| void tommy_array_swap | ( | tommy_array * | first, |
| tommy_array * | second | ||
| ) |
Exchanges two initialized arrays without copying their elements.
Element references remain valid and belong to the other array. Passing the same array twice has no effect.
| first | The first array. |
| second | The second array. |
| TOMMY_API void tommy_array_foreach | ( | tommy_array * | array, |
| tommy_foreach_func * | func | ||
| ) |
Calls the specified function for each element in the array.
You cannot add or remove elements, nor change the size of the array, from inside the callback.
If the callback frees the elements, the array retains dangling pointers. Call tommy_array_clear() before reusing it, or tommy_array_done() to release its storage.
| array | The array. |
| func | Function to call with each element. |
| TOMMY_API void tommy_array_foreach_arg | ( | tommy_array * | array, |
| tommy_foreach_arg_func * | func, | ||
| void * | arg | ||
| ) |
Calls the specified function with an argument for each element in the array.
| array | The array. |
| func | Function to call with each element. |
| arg | Argument to pass to the function. |
| TOMMY_API tommy_size_t tommy_array_memory_usage | ( | const tommy_array * | array | ) |
Gets the size of allocated memory.
| array | The array. |