libamxc
1.10.3
C Generic Data Containers
|
The Ambiorix Dynamic Bucket Array. More...
Modules | |
Array iterators | |
Data Structures | |
struct | _amxc_array |
The array structure. More... | |
Typedefs | |
typedef struct _amxc_array | amxc_array_t |
The array structure. More... | |
typedef void(* | amxc_array_it_delete_t) (amxc_array_it_t *it) |
Definition of the array item delete callback function. More... | |
Functions | |
int8_t | amxc_array_new (amxc_array_t **array, const size_t items) |
Allocates an array. More... | |
void | amxc_array_delete (amxc_array_t **array, const amxc_array_it_delete_t func) |
Frees the previously allocated array. More... | |
int | amxc_array_init (amxc_array_t *const array, const size_t items) |
Initializes an array. More... | |
void | amxc_array_clean (amxc_array_t *const array, amxc_array_it_delete_t func) |
Removes all items from the array. More... | |
int | amxc_array_grow (amxc_array_t *const array, const size_t items) |
Expands the array. More... | |
int | amxc_array_shrink (amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func) |
Shrinks the array. More... | |
int | amxc_array_shift_right (amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func) |
Shift all items to the right in the array. More... | |
int | amxc_array_shift_left (amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func) |
Shift all items to the left in the array. More... | |
bool | amxc_array_is_empty (const amxc_array_t *const array) |
Checks that the array is empty. More... | |
size_t | amxc_array_size (const amxc_array_t *const array) |
Calculates the number of used items in the array. More... | |
amxc_array_it_t * | amxc_array_append_data (amxc_array_t *const array, void *data) |
Adds an item after the last used item in the array. More... | |
amxc_array_it_t * | amxc_array_prepend_data (amxc_array_t *const array, void *data) |
Adds an item before the first used item in the array. More... | |
amxc_array_it_t * | amxc_array_get_at (const amxc_array_t *const array, const unsigned int index) |
Gets the item iterator for the given index. More... | |
amxc_array_it_t * | amxc_array_set_data_at (amxc_array_t *const array, const unsigned int index, void *data) |
Sets data at the given index. More... | |
amxc_array_it_t * | amxc_array_get_first (const amxc_array_t *const array) |
Gets the item iterator of the first used item in the array. More... | |
amxc_array_it_t * | amxc_array_get_first_free (const amxc_array_t *const array) |
Gets the first free position in the array. More... | |
amxc_array_it_t * | amxc_array_get_last (const amxc_array_t *const array) |
Gets the item iterator of the last used item in the array. More... | |
amxc_array_it_t * | amxc_array_get_last_free (const amxc_array_t *const array) |
Gets the last free position in the array. More... | |
void * | amxc_array_take_first_data (amxc_array_t *const array) |
Takes the data pointer from the first used item in the array. More... | |
void * | amxc_array_take_last_data (amxc_array_t *const array) |
Takes the data pointer from the last used item in the array. More... | |
AMXC_INLINE size_t | amxc_array_capacity (const amxc_array_t *const array) |
Gets the capacity of the array. More... | |
AMXC_INLINE void * | amxc_array_get_data_at (const amxc_array_t *const array, const unsigned int index) |
Gets the data pointer of the item at the given index. More... | |
int | amxc_array_sort (amxc_array_t *const array, amxc_array_it_cmp_t cmp) |
Sorts the content of the array. More... | |
The Ambiorix Dynamic Bucket Array.
A bucket is basically a pointer to a memory block (like a structure). This array implementation provides an array of pointers (aka buckets), the allocated memory for the buckets is a sequential memory block.
A bucket is considered empty when the pointer is NULL.
The array will grow dynamically when items are added to the end.
The items (or buckets) are defined using amxc_array_it_t structure. This structure contains the pointer to the data and the pointer to the beginning of the array.
To get the number of used buckets in the array use amxc_array_size.
If you only want to loop over the used buckets (with a non NULL data pointer), use the functions amxc_array_get_first, amxc_array_it_get_next
typedef void(* amxc_array_it_delete_t) (amxc_array_it_t *it) |
Definition of the array item delete callback function.
A pointer to a delete function is used in the following functions: amxc_array_delete, amxc_array_clean, amxc_array_shrink, amxc_array_shift_right, amxc_array_shift_left.
When an array contains pointers to heap allocated memory blocks, that are possibly nowhere else referenced, this may lead to memory leaks. Providing a callback function to free the memory is easier and more efficient then first iterating over the array freeing up and then call one of the previously mentioned functions.
Definition at line 194 of file amxc_array.h.
typedef struct _amxc_array amxc_array_t |
The array structure.
The number of available items in the allocated buffer is tracked in the items member of this structure. C does not provide any standard way to get the size of an allocated memory block.
The structure also contains the first_used and last_used members. These are mainly used to be able to add data after the last taken position or to add data before the first taken position.
Alternatively the functions inserting data after or before can iterate over the array start from the end or from the beginning, this could have some significant impact on the performance of these functions.
Although the members of this structure are publicly available it is not recommended to access them directly, use the provided functions.
amxc_array_it_t* amxc_array_append_data | ( | amxc_array_t *const | array, |
void * | data | ||
) |
Adds an item after the last used item in the array.
The array grows if the item can not be added to the array.
array | a pointer to the array structure |
data | a pointer to the data that is added to the array |
Definition at line 429 of file amxc_array.c.
AMXC_INLINE size_t amxc_array_capacity | ( | const amxc_array_t *const | array | ) |
Gets the capacity of the array.
The capacity of an array is the number of items that can be stored in the array.
array | pointer to the array structure |
Definition at line 694 of file amxc_array.h.
void amxc_array_clean | ( | amxc_array_t *const | array, |
amxc_array_it_delete_t | func | ||
) |
Removes all items from the array.
Removes all items from the array. If a delete function is provided, it is called for each used item in the array.
array | a pointer to the array structure |
func | a pointer to a function that is called to free each used item in the array |
Definition at line 261 of file amxc_array.c.
void amxc_array_delete | ( | amxc_array_t ** | array, |
const amxc_array_it_delete_t | func | ||
) |
Frees the previously allocated array.
Removes all items from the array, if a delete function is provided, it is called for each used item in the array.
Frees the allocated memory and sets the pointer to the array to NULL.
array | a pointer to the location where the pointer to the array is stored |
func | a pointer to a function that is called to free each item in the array |
Definition at line 213 of file amxc_array.c.
amxc_array_it_t* amxc_array_get_at | ( | const amxc_array_t *const | array, |
const unsigned int | index | ||
) |
Gets the item iterator for the given index.
array | a pointer to the array structure |
index | the position in the array for which the iterator has to be returned |
Definition at line 504 of file amxc_array.c.
AMXC_INLINE void* amxc_array_get_data_at | ( | const amxc_array_t *const | array, |
const unsigned int | index | ||
) |
Gets the data pointer of the item at the given index.
array | pointer to the array structure |
index | the index of the item |
Definition at line 711 of file amxc_array.h.
amxc_array_it_t* amxc_array_get_first | ( | const amxc_array_t *const | array | ) |
Gets the item iterator of the first used item in the array.
array | a pointer to the array structure |
Definition at line 517 of file amxc_array.c.
amxc_array_it_t* amxc_array_get_first_free | ( | const amxc_array_t *const | array | ) |
Gets the first free position in the array.
array | a pointer to the array structure |
Definition at line 529 of file amxc_array.c.
amxc_array_it_t* amxc_array_get_last | ( | const amxc_array_t *const | array | ) |
Gets the item iterator of the last used item in the array.
array | a pointer to the array structure |
Definition at line 546 of file amxc_array.c.
amxc_array_it_t* amxc_array_get_last_free | ( | const amxc_array_t *const | array | ) |
Gets the last free position in the array.
array | a pointer to the array structure |
Definition at line 558 of file amxc_array.c.
int amxc_array_grow | ( | amxc_array_t *const | array, |
const size_t | items | ||
) |
Expands the array.
Expands the array by the given number of items. Extra memory is allocated to be able to store the number of items requested.
array | a pointer to the array structure |
items | the number of items the array has to grow |
Definition at line 280 of file amxc_array.c.
int amxc_array_init | ( | amxc_array_t *const | array, |
const size_t | items | ||
) |
Initializes an array.
Initializes the array structure. Memory is allocated from the heap to be able to store the number of items requested. All the items are initialized to NULL. This function is typically called for arrays that are on the stack. Allocating and initializing an array on the heap can be done using amxc_array_new
array | a pointer to the array structure. |
items | the size of the array in number of items |
Definition at line 231 of file amxc_array.c.
bool amxc_array_is_empty | ( | const amxc_array_t *const | array | ) |
Checks that the array is empty.
An array is empty if none of the items are used.
array | a pointer to the array structure |
Definition at line 399 of file amxc_array.c.
int8_t amxc_array_new | ( | amxc_array_t ** | array, |
const size_t | items | ||
) |
Allocates an array.
Allocates and initializes memory to store an array. This function allocates memory from the heap, if an array is on the stack, it can be initialized using the function amxc_array_init
The size of the array is not fixed and can be changed with the functions amxc_array_grow or amxc_array_shrink
The size of the array is expressed in number of items that can be stored in the array. All items in the allocated array are set to NULL.
array | a pointer to the location where the pointer to the new array can be stored |
items | the size of the array in number of items |
Definition at line 179 of file amxc_array.c.
amxc_array_it_t* amxc_array_prepend_data | ( | amxc_array_t *const | array, |
void * | data | ||
) |
Adds an item before the first used item in the array.
The array grows if the item can not be added to the array.
array | a pointer to the array structure |
data | a pointer to the data that is added to the array |
Definition at line 449 of file amxc_array.c.
amxc_array_it_t* amxc_array_set_data_at | ( | amxc_array_t *const | array, |
const unsigned int | index, | ||
void * | data | ||
) |
Sets data at the given index.
array | a pointer to the array structure |
index | position in the array where the data has to be set |
data | pointer to the data that has to be set in the array |
Definition at line 474 of file amxc_array.c.
int amxc_array_shift_left | ( | amxc_array_t *const | array, |
const size_t | items, | ||
amxc_array_it_delete_t | func | ||
) |
Shift all items to the left in the array.
Moves all items in the array to the left with the given number of items. Items that are moved out of the array are removed from the array. A function can be provided that is called for each used item that is removed from the array.
E.g. Shifting the array to the left by 3 items, moves the item on index 3 to index 0, the item on index 4 to index 1, ...
The items at the end of the array are reinitialized to NULL.
array | a pointer to the array structure |
items | the number of items that needs to be shifted |
func | a pointer to a function that is called to free each used item in the array, that is moved out of the array. |
Definition at line 361 of file amxc_array.c.
int amxc_array_shift_right | ( | amxc_array_t *const | array, |
const size_t | items, | ||
amxc_array_it_delete_t | func | ||
) |
Shift all items to the right in the array.
Moves all items in the array to the right with the given number of items. Items that are moved out of the array are removed from the array. A function can be provided that is called for each used item that is removed from the array.
E.g. Shifting the array to the right by 3 items, moves the item on index 0 to index 3, the item on index 1 to index 4, ...
The items in the beginning of the array are reinitialized to NULL.
array | a pointer to the array structure |
items | the number of items that needs to be shifted |
func | a pointer to a function that is called to free each used item in the array, that is moved out of the array. |
Definition at line 323 of file amxc_array.c.
int amxc_array_shrink | ( | amxc_array_t *const | array, |
const size_t | items, | ||
amxc_array_it_delete_t | func | ||
) |
Shrinks the array.
Shrinks the array by the given number of items. Memory is freed. If a delete function is provided it is called for each used item that is removed from the array.
array | a pointer to the array structure |
items | the number of items the array has to shrink |
func | pointer to a function that is called to free each used item in the array |
Definition at line 298 of file amxc_array.c.
size_t amxc_array_size | ( | const amxc_array_t *const | array | ) |
Calculates the number of used items in the array.
Loops over all items in the array and counts the used items. The size of the array is expressed in used items.
array | a pointer to the array structure |
Definition at line 415 of file amxc_array.c.
int amxc_array_sort | ( | amxc_array_t *const | array, |
amxc_array_it_cmp_t | cmp | ||
) |
Sorts the content of the array.
The smallest items according to the compare functions are at the beginning of the array, empty buckets are at the end.
array | the array that will be sorted |
cmp | array iterator content compare function |
Definition at line 596 of file amxc_array.c.
void* amxc_array_take_first_data | ( | amxc_array_t *const | array | ) |
Takes the data pointer from the first used item in the array.
array | a pointer to the array structure |
Definition at line 576 of file amxc_array.c.
void* amxc_array_take_last_data | ( | amxc_array_t *const | array | ) |
Takes the data pointer from the last used item in the array.
array | a pointer to the array structure |
Definition at line 586 of file amxc_array.c.