Data Structures | Macros | Typedefs | Functions
tommyarrayblk.h File Reference

Dynamic array based on fixed-size blocks. More...

Go to the source code of this file.

Data Structures

struct  tommy_arrayblk_struct
 Array container type. More...
 

Macros

#define TOMMY_ARRAYBLK_BIT   6
 Initial and minimal capacity of the block directory expressed as a power of 2. More...
 
#define TOMMY_ARRAYBLK_SIZE   (4 * 1024)
 Elements for each block. More...
 

Typedefs

typedef struct tommy_arrayblk_struct tommy_arrayblk
 Array container type. More...
 

Functions

TOMMY_API void tommy_arrayblk_init (tommy_arrayblk *array)
 Initializes the array. More...
 
TOMMY_API void tommy_arrayblk_done (tommy_arrayblk *array)
 Deinitializes the array. More...
 
TOMMY_API void tommy_arrayblk_reserve (tommy_arrayblk *array, tommy_size_t size)
 Allocates space for at least the specified number of elements. More...
 
void tommy_arrayblk_grow (tommy_arrayblk *array, tommy_size_t size)
 Grows the size up to the specified value. More...
 
TOMMY_API void tommy_arrayblk_resize (tommy_arrayblk *array, tommy_size_t size)
 Changes the initialized size, preserving the common prefix. More...
 
void tommy_arrayblk_clear (tommy_arrayblk *array)
 Removes all elements, preserving the allocated capacity. More...
 
TOMMY_API void tommy_arrayblk_shrink (tommy_arrayblk *array)
 Releases unused allocated memory. More...
 
void ** tommy_arrayblk_ref (tommy_arrayblk *array, tommy_size_t pos)
 Gets a reference to the element at the specified position. More...
 
void tommy_arrayblk_set (tommy_arrayblk *array, tommy_size_t pos, void *element)
 Sets the element at the specified position. More...
 
void * tommy_arrayblk_get (tommy_arrayblk *array, tommy_size_t pos)
 Gets the element at the specified position. More...
 
void * tommy_arrayblk_tail (tommy_arrayblk *array)
 Gets the last element. More...
 
void tommy_arrayblk_insert_tail (tommy_arrayblk *array, void *element)
 Grows and inserts a new element at the end of the array. More...
 
void * tommy_arrayblk_remove_tail (tommy_arrayblk *array)
 Removes and returns the last element. More...
 
tommy_bool_t tommy_arrayblk_empty (const tommy_arrayblk *array)
 Checks whether the array is empty. More...
 
tommy_size_t tommy_arrayblk_size (const tommy_arrayblk *array)
 Gets the initialized size of the array. More...
 
tommy_size_t tommy_arrayblk_capacity (const tommy_arrayblk *array)
 Gets the number of elements that fit without further allocation. More...
 
void tommy_arrayblk_swap (tommy_arrayblk *first, tommy_arrayblk *second)
 Exchanges two initialized arrays without copying their elements. More...
 
TOMMY_API void tommy_arrayblk_foreach (tommy_arrayblk *array, tommy_foreach_func *func)
 Calls the specified function for each element in the array. More...
 
TOMMY_API void tommy_arrayblk_foreach_arg (tommy_arrayblk *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_arrayblk_memory_usage (const tommy_arrayblk *array)
 Gets the size of allocated memory. More...
 

Detailed Description

Dynamic array based on fixed-size blocks.

This array is able to grow dynamically without reallocating stored elements.

Growth allocates new blocks without moving existing ones. The directory of block pointers may be reallocated, but element addresses never change.

Allocated blocks are always of the same fixed size of 4 Ki pointers.

Macro Definition Documentation

◆ TOMMY_ARRAYBLK_BIT

#define TOMMY_ARRAYBLK_BIT   6

Initial and minimal capacity of the block directory expressed as a power of 2.

◆ TOMMY_ARRAYBLK_SIZE

#define TOMMY_ARRAYBLK_SIZE   (4 * 1024)

Elements for each block.

Typedef Documentation

◆ tommy_arrayblk

Array container type.

Note
Don't use internal fields directly, but access the container only using functions.

Function Documentation

◆ tommy_arrayblk_init()

TOMMY_API void tommy_arrayblk_init ( tommy_arrayblk *  array)

Initializes the array.

Parameters
arrayThe array.

◆ tommy_arrayblk_done()

TOMMY_API void tommy_arrayblk_done ( tommy_arrayblk *  array)

Deinitializes the array.

Parameters
arrayThe array.

◆ tommy_arrayblk_reserve()

TOMMY_API void tommy_arrayblk_reserve ( tommy_arrayblk *  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.

Parameters
arrayThe array.
sizeNumber of elements to reserve space for.

◆ tommy_arrayblk_grow()

void tommy_arrayblk_grow ( tommy_arrayblk *  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.

Parameters
arrayThe array.
sizeNew size of the array.

◆ tommy_arrayblk_resize()

TOMMY_API void tommy_arrayblk_resize ( tommy_arrayblk *  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.

Parameters
arrayThe array.
sizeNew size of the array.

◆ tommy_arrayblk_clear()

void tommy_arrayblk_clear ( tommy_arrayblk *  array)

Removes all elements, preserving the allocated capacity.

Call tommy_arrayblk_shrink() after clearing to restore the initial capacity. Pointed-to elements are not freed. The array remains initialized and can be reused immediately.

Parameters
arrayThe array.

◆ tommy_arrayblk_shrink()

TOMMY_API void tommy_arrayblk_shrink ( tommy_arrayblk *  array)

Releases unused allocated memory.

Preserves the size, values, and addresses of existing elements.

Parameters
arrayThe array.

◆ tommy_arrayblk_ref()

void ** tommy_arrayblk_ref ( tommy_arrayblk *  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_arrayblk_grow().

Parameters
arrayThe array.
posPosition of the element.
Returns
Pointer to the slot at the specified position.

◆ tommy_arrayblk_set()

void tommy_arrayblk_set ( tommy_arrayblk *  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_arrayblk_grow().

Parameters
arrayThe array.
posPosition of the element.
elementElement to set.

◆ tommy_arrayblk_get()

void * tommy_arrayblk_get ( tommy_arrayblk *  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_arrayblk_grow().

Parameters
arrayThe array.
posPosition of the element.
Returns
The element at the specified position.

◆ tommy_arrayblk_tail()

void * tommy_arrayblk_tail ( tommy_arrayblk *  array)

Gets the last element.

The array must not be empty.

Parameters
arrayThe array.
Returns
The last element.

◆ tommy_arrayblk_insert_tail()

void tommy_arrayblk_insert_tail ( tommy_arrayblk *  array,
void *  element 
)

Grows and inserts a new element at the end of the array.

Parameters
arrayThe array.
elementElement to insert.

◆ tommy_arrayblk_remove_tail()

void * tommy_arrayblk_remove_tail ( tommy_arrayblk *  array)

Removes and returns the last element.

The array must not be empty. The removed slot is cleared, and allocated capacity is preserved.

Parameters
arrayThe array.
Returns
The removed element.

◆ tommy_arrayblk_empty()

tommy_bool_t tommy_arrayblk_empty ( const tommy_arrayblk *  array)

Checks whether the array is empty.

Parameters
arrayThe array.
Returns
Nonzero if the array is empty, 0 otherwise.

◆ tommy_arrayblk_size()

tommy_size_t tommy_arrayblk_size ( const tommy_arrayblk *  array)

Gets the initialized size of the array.

Parameters
arrayThe array.
Returns
The initialized size of the array.

◆ tommy_arrayblk_capacity()

tommy_size_t tommy_arrayblk_capacity ( const tommy_arrayblk *  array)

Gets the number of elements that fit without further allocation.

Parameters
arrayThe array.
Returns
The capacity of the array.

◆ tommy_arrayblk_swap()

void tommy_arrayblk_swap ( tommy_arrayblk *  first,
tommy_arrayblk *  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.

Parameters
firstThe first array.
secondThe second array.
Note
This operation is O(1).

◆ tommy_arrayblk_foreach()

TOMMY_API void tommy_arrayblk_foreach ( tommy_arrayblk *  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_arrayblk_clear() before reusing it, or tommy_arrayblk_done() to release its storage.

Parameters
arrayThe array.
funcFunction to call with each element.

◆ tommy_arrayblk_foreach_arg()

TOMMY_API void tommy_arrayblk_foreach_arg ( tommy_arrayblk *  array,
tommy_foreach_arg_func *  func,
void *  arg 
)

Calls the specified function with an argument for each element in the array.

Parameters
arrayThe array.
funcFunction to call with each element.
argArgument to pass to the function.

◆ tommy_arrayblk_memory_usage()

TOMMY_API tommy_size_t tommy_arrayblk_memory_usage ( const tommy_arrayblk *  array)

Gets the size of allocated memory.

Parameters
arrayThe array.
Returns
Total memory allocated by the array, in bytes.