Array
Specifies the array structure. This structure works as a buffer, where the develop could insert the elements at the end and remove elements at any position.
- Author
Matheus T. dos Santos (tenoriomatheus0@gmail.com)
- Version
0.1.0
- Date
19/09/2021
- Copyright
Matheus T. dos Santos all rights reserved (c) 2021
Defines
-
BP_ARRAY_INVALID_INDEX
Value representing an invalid index
-
BP_ARRAY_INIT(array_)
Macro to initialize a bp_array.
- Parameters
-
array_ – buffer where the elements will be stored.
-
BP_ARRAY_START(array_, size_)
Macro to initialize a bp_array, which have initials elements.
- Parameters
-
array_ – buffer where the elements will be stored.
size_ – Number of elements already presents in the buffer.
Functions
-
void *bp_array_get(bp_array_t *array, usize idx)
Get an element from the array, based on its position.
- Parameters
-
array – Reference to bp_array.
idx – Element index.
- Returns
A reference to the desired element.
- Returns
NULL if the index is out of range or the ‘array’ argument is NULL.
-
int bp_array_push(bp_array_t *array, void *el)
Push an element at the end of array.
- Parameters
-
array – Reference to bp_array.
el – Reference to the element to be pushed.
- Returns
0 on success.
- Returns
-ENODEV if the ‘array’ or the ‘el’ argument is NULL.
- Returns
-ENOMEM if the array is full.
-
int bp_array_del(bp_array_t *array, usize idx)
Delete an array element, based on its position.
- Parameters
-
array – Reference to bp_array.
idx – Element idx.
- Returns
0 on success.
- Returns
-ENODEV if the ‘array’ argument is NULL.
- Returns
-EFAULT if the index ‘idx’ is out of range.
-
usize bp_array_find_idx(bp_array_t *array, void *param, bool (*cmp)(void *el, void *param))
Find the index of an element, based at some parameter related to the element. This parameter could be the element itself, or some field of its type. The match will be done based on cmp function pointer. If the cmp function pointer argument is null, then the elements will be compared with the parameter byte by byte.
- Parameters
-
array – Reference to bp_array.
param – Reference to the parameter used to compare elements.
cmp – Function to compare an element with the parameter passed at argument param.
- Returns
The index of found element.
- Returns
BP_ARRAY_INVALID_INDEX if the element wasn’t found or if the ‘array’ or the ‘param’ argument is NULL.
-
void *bp_array_find(bp_array_t *array, void *param, bool (*cmp)(void *el, void *param))
Find the element in the array, based at some parameter related to the element. This parameter could be the element itself, or some field of its type. The match will be done based on cmp function pointer. If the cmp function point argument is null, then the elements will be compared with the parameter byte by byte.
- Parameters
-
array – Reference to bp_array.
param – Reference to the parameter used to compare elements.
cmp – Function to compare an element with the parameter passed at argument param.
- Returns
A reference to the found element.
- Returns
NULL if the element wasn’t found or if the ‘array’ or the ‘param’ argument is NULL.
-
int bp_array_clear(bp_array_t *array)
Drop all elements in the array.
Warning
After this function the array size is zero, but the elements stay in the buffer.
- Parameters
array – Reference to bp_array.
- Returns
0 on success.
- Returns
-ENODEV if the ‘array’ argument is NULL.
-
usize bp_array_size(bp_array_t *array)
Get the array size.
- Parameters
array – Reference to bp_array.
- Returns
The size of array.
- Returns
0 if the ‘array’ argument is NULL.
-
bp_iter_t bp_array_iter(bp_array_t *array)
Get a iterator to walk through the bp_array.
Warning
This function doesn’t check if the array argument is null. So if this argument is null, a crash will occur. That check must be done outside the function.
- Parameters
array – Reference to bp_array.
- Returns
A new iterator instance for the bp_array.
-
struct bp_array_t
- #include <bp_array.h>
Struct with metadata about an external buffer.
Note
This struct need an external buffer to work properly.