libref_array
0.1.2
|
Typedefs | |
typedef void(* | ref_array_fn )(void *elem, ref_array_del_enum type, void *data) |
Element cleanup callback. | |
Enumerations | |
enum | ref_array_del_enum |
Enumeration of the delete modes. More... | |
Functions | |
int | ref_array_create (struct ref_array **ra, size_t elem, uint32_t grow_by, ref_array_fn cb, void *data) |
Create referenced array. | |
struct ref_array * | ref_array_getref (struct ref_array *ra) |
Get new reference to an array. | |
void | ref_array_destroy (struct ref_array *ra) |
Delete the array. | |
int | ref_array_append (struct ref_array *ra, void *element) |
Add new element to the array. | |
void * | ref_array_get (struct ref_array *ra, uint32_t idx, void *acptr) |
Get element data. | |
int | ref_array_getlen (struct ref_array *ra, uint32_t *len) |
Get array length. | |
uint32_t | ref_array_len (struct ref_array *ra) |
Array length. | |
int | ref_array_insert (struct ref_array *ra, uint32_t idx, void *element) |
Insert a new element into the array. | |
int | ref_array_replace (struct ref_array *ra, uint32_t idx, void *element) |
Replace element in the array. | |
int | ref_array_remove (struct ref_array *ra, uint32_t idx) |
Remove element from the array. | |
int | ref_array_swap (struct ref_array *ra, uint32_t idx1, uint32_t idx2) |
Swap two elements in the array. | |
void | ref_array_reset (struct ref_array *ra) |
Reset array. |
typedef void(* ref_array_fn)(void *elem, ref_array_del_enum type, void *data) |
Element cleanup callback.
Callback that can be provided by a caller to free data when the storage is actually destroyed.
enum ref_array_del_enum |
Enumeration of the delete modes.
When the array is destroyed each element of the array most likely needs to be freed. Same is true when an element is removed from the array. However the caller might need to do different things with the data depending on whether the array is destroyed or whether the element is removed. This enumeration defines constants that you used to indicate which operation was performed.
int ref_array_append | ( | struct ref_array * | ra, |
void * | element | ||
) |
Add new element to the array.
Appends an element to the end of the array.
[in] | ra | Existing array object. |
[in] | element | Pointer to data. The number of bytes defined at the array creation as the array size will be copied into the array element. |
int ref_array_create | ( | struct ref_array ** | ra, |
size_t | elem, | ||
uint32_t | grow_by, | ||
ref_array_fn | cb, | ||
void * | data | ||
) |
Create referenced array.
[out] | ra | Newly created array object. |
[in] | elem | Element size in bytes. |
[in] | grow_by | Defines how many elements should be allocated together as one chunk. |
[in] | cb | Cleanup callback. |
[in] | data | Caller supplied data passed to cleanup callback. |
void ref_array_destroy | ( | struct ref_array * | ra | ) |
Delete the array.
[in] | ra | Existing array object or a reference. |
void* ref_array_get | ( | struct ref_array * | ra, |
uint32_t | idx, | ||
void * | acptr | ||
) |
Get element data.
Retrieves data from the array element.
[in] | ra | Existing array object. |
[in] | idx | Index of the array element. |
[in] | acptr | Pointer to the memory where the element's data will be copied. Can be NULL. In this case nothing is copied. |
int ref_array_getlen | ( | struct ref_array * | ra, |
uint32_t * | len | ||
) |
Get array length.
Determines length of the array.
[in] | ra | Existing array object. |
[out] | len | Variable will receive the length of the array. |
struct ref_array* ref_array_getref | ( | struct ref_array * | ra | ) | [read] |
Get new reference to an array.
[in] | ra | Existing array object. |
int ref_array_insert | ( | struct ref_array * | ra, |
uint32_t | idx, | ||
void * | element | ||
) |
Insert a new element into the array.
Inserts an element into the array. If idx is 0 the element will be added to the beginning of the array, if idx is 1 the element will be added after the first element of the array and so on. If index is greater than the number of elements in the array the function returns error.
[in] | ra | Existing array object. |
[in] | idx | Index of the array element. |
[in] | element | Pointer to data. The number of bytes defined at the array creation as the array size will be copied into the array element. |
uint32_t ref_array_len | ( | struct ref_array * | ra | ) |
Array length.
Alternative function to get length.
[in] | ra | Existing array object. |
int ref_array_remove | ( | struct ref_array * | ra, |
uint32_t | idx | ||
) |
Remove element from the array.
The element is removed and the length is decreased by 1. If index is greater than the number of elements in the array the function returns error.
[in] | ra | Existing array object. |
[in] | idx | Index of the array element. |
int ref_array_replace | ( | struct ref_array * | ra, |
uint32_t | idx, | ||
void * | element | ||
) |
Replace element in the array.
Replace an element of the array identified by index with a new value. If index is greater than the number of elements in the array the function returns error.
[in] | ra | Existing array object. |
[in] | idx | Index of the array element. |
[in] | element | Pointer to data. The number of bytes defined at the array creation as the array size will be copied into the array element. |
void ref_array_reset | ( | struct ref_array * | ra | ) |
Reset array.
Function clears all contents without destroying the object. The delete callback will be called for every element of the array from the beginning to the end passing in REF_ARRAY_DESTROY value. All the storage for the array will be deallocated. After the call the array will be empty as if just created.
[in] | ra | Existing array object. No return value. |
int ref_array_swap | ( | struct ref_array * | ra, |
uint32_t | idx1, | ||
uint32_t | idx2 | ||
) |
Swap two elements in the array.
If any of the indexes is greater than the number of elements in the array the function returns error.
[in] | ra | Existing array object. |
[in] | idx1 | Index of the array element. |
[in] | idx2 | Index of the array element. |