Linear Array data structure. More...
Go to the source code of this file.
Macros | |
#define | ARRAY_HEADROOM 25 |
Additional number of elements to reserve, to prevent frequent reallocations. | |
#define | ARRAY_HEAD(name, type) |
Define a named struct for arrays of elements of a certain type. | |
#define | ARRAY_HEAD_INITIALIZER { 0, 0, NULL } |
Static initializer for arrays. | |
#define | ARRAY_INIT(head) memset((head), 0, sizeof(*(head))) |
Initialize an array. | |
#define | ARRAY_EMPTY(head) ((head)->size == 0) |
Check if an array is empty. | |
#define | ARRAY_SIZE(head) ((head)->size) |
The number of elements stored. | |
#define | ARRAY_CAPACITY(head) ((head)->capacity) |
The number of elements the array can store without reallocation. | |
#define | ARRAY_GET(head, idx) ((head)->size > (idx) ? &(head)->entries[(idx)] : NULL) |
Return the element at index. | |
#define | ARRAY_SET(head, idx, elem) |
Set an element in the array. | |
#define | ARRAY_FIRST(head) ARRAY_GET((head), 0) |
Convenience method to get the first element. | |
#define | ARRAY_LAST(head) |
Convenience method to get the last element. | |
#define | ARRAY_ADD(head, elem) |
Add an element at the end of the array. | |
#define | ARRAY_SHRINK(head, num) ((head)->size -= MIN((num), (head)->size)) |
Mark a number of slots at the end of the array as unused. | |
#define | ARRAY_ELEM_SIZE(head) (sizeof(*(head)->entries)) |
Number of bytes occupied by an element of this array. | |
#define | ARRAY_RESERVE(head, num) |
Reserve memory for the array. | |
#define | ARRAY_FREE(head) (FREE(&(head)->entries), (head)->size = (head)->capacity = 0) |
Release all memory. | |
#define | ARRAY_FOREACH(elem, head) ARRAY_FOREACH_FROM_TO((elem), (head), 0, (head)->size) |
Iterate over all elements of the array. | |
#define | ARRAY_FOREACH_FROM(elem, head, from) ARRAY_FOREACH_FROM_TO((elem), (head), (from), (head)->size) |
Iterate from an index to the end. | |
#define | ARRAY_FOREACH_TO(elem, head, to) ARRAY_FOREACH_FROM_TO((elem), (head), 0, (to)) |
Iterate from the beginning to an index. | |
#define | ARRAY_FOREACH_FROM_TO(elem, head, from, to) |
Iterate between two indexes. | |
#define | ARRAY_IDX(head, elem) (elem - (head)->entries) |
Return the index of an element of the array. | |
#define | ARRAY_REMOVE(head, elem) |
Remove an entry from the array, shifting down the subsequent entries. | |
#define | ARRAY_SORT(head, fn, sdata) |
Sort an array. | |
#define | ARRAY_SET_NORESERVE(head, idx, elem) |
#define | __coverity_escape__(x) 0 |
#define | ARRAY_ADD_NORESERVE(head, elem) |
Linear Array data structure.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Definition in file array.h.
#define ARRAY_HEADROOM 25 |
#define ARRAY_HEAD | ( | name, | |
type | |||
) |
#define ARRAY_HEAD_INITIALIZER { 0, 0, NULL } |
#define ARRAY_INIT | ( | head | ) | memset((head), 0, sizeof(*(head))) |
Initialize an array.
head | Pointer to a struct defined using ARRAY_HEAD() |
#define ARRAY_EMPTY | ( | head | ) | ((head)->size == 0) |
Check if an array is empty.
head | Pointer to a struct defined using ARRAY_HEAD() |
true | The array is empty |
false | The array is not empty |
#define ARRAY_SIZE | ( | head | ) | ((head)->size) |
The number of elements stored.
head | Pointer to a struct defined using ARRAY_HEAD() |
num | Number of elements stored |
#define ARRAY_CAPACITY | ( | head | ) | ((head)->capacity) |
The number of elements the array can store without reallocation.
head | Pointer to a struct defined using ARRAY_HEAD() |
num | The capacity of the array |
#define ARRAY_GET | ( | head, | |
idx | |||
) | ((head)->size > (idx) ? &(head)->entries[(idx)] : NULL) |
Return the element at index.
head | Pointer to a struct defined using ARRAY_HEAD() |
idx | Index, between 0 and ARRAY_SIZE()-1 |
ptr | Pointer to the element at the given index |
NULL | Index was out of bounds |
#define ARRAY_SET | ( | head, | |
idx, | |||
elem | |||
) |
Set an element in the array.
head | Pointer to a struct defined using ARRAY_HEAD() |
idx | Index, between 0 and ARRAY_SIZE()-1 |
elem | Element to copy |
true | Element was inserted |
false | Element was not inserted, array was full |
#define ARRAY_FIRST | ( | head | ) | ARRAY_GET((head), 0) |
Convenience method to get the first element.
head | Pointer to a struct defined using ARRAY_HEAD() |
ptr | Pointer to the first element |
NULL | Array is empty |
#define ARRAY_LAST | ( | head | ) |
Convenience method to get the last element.
head | Pointer to a struct defined using ARRAY_HEAD() |
ptr | Pointer to the last element |
NULL | Array is empty |
#define ARRAY_ADD | ( | head, | |
elem | |||
) |
Add an element at the end of the array.
head | Pointer to a struct defined using ARRAY_HEAD() |
elem | Element to copy |
true | Element was added |
false | Element was not added, array was full |
#define ARRAY_SHRINK | ( | head, | |
num | |||
) | ((head)->size -= MIN((num), (head)->size)) |
Mark a number of slots at the end of the array as unused.
head | Pointer to a struct defined using ARRAY_HEAD() |
num | Number of slots to mark as unused |
num | New size of the array |
#define ARRAY_ELEM_SIZE | ( | head | ) | (sizeof(*(head)->entries)) |
Number of bytes occupied by an element of this array.
head | Pointer to a struct defined using ARRAY_HEAD() |
num | Number of bytes per element |
#define ARRAY_RESERVE | ( | head, | |
num | |||
) |
Reserve memory for the array.
head | Pointer to a struct defined using ARRAY_HEAD() |
num | Number of elements to make room for |
num | New capacity of the array |
#define ARRAY_FREE | ( | head | ) | (FREE(&(head)->entries), (head)->size = (head)->capacity = 0) |
Release all memory.
head | Pointer to a struct defined using ARRAY_HEAD() |
0 | Always |
#define ARRAY_FOREACH | ( | elem, | |
head | |||
) | ARRAY_FOREACH_FROM_TO((elem), (head), 0, (head)->size) |
Iterate over all elements of the array.
elem | Variable to be used as pointer to the element at each iteration |
head | Pointer to a struct defined using ARRAY_HEAD() |
#define ARRAY_FOREACH_FROM | ( | elem, | |
head, | |||
from | |||
) | ARRAY_FOREACH_FROM_TO((elem), (head), (from), (head)->size) |
Iterate from an index to the end.
elem | Variable to be used as pointer to the element at each iteration |
head | Pointer to a struct defined using ARRAY_HEAD() |
from | Starting index (inclusive) |
#define ARRAY_FOREACH_TO | ( | elem, | |
head, | |||
to | |||
) | ARRAY_FOREACH_FROM_TO((elem), (head), 0, (to)) |
Iterate from the beginning to an index.
elem | Variable to be used as pointer to the element at each iteration |
head | Pointer to a struct defined using ARRAY_HEAD() |
to | Terminating index (exclusive) |
#define ARRAY_FOREACH_FROM_TO | ( | elem, | |
head, | |||
from, | |||
to | |||
) |
Iterate between two indexes.
elem | Variable to be used as pointer to the element at each iteration |
head | Pointer to a struct defined using ARRAY_HEAD() |
from | Starting index (inclusive) |
to | Terminating index (exclusive) |
#define ARRAY_IDX | ( | head, | |
elem | |||
) | (elem - (head)->entries) |
Return the index of an element of the array.
head | Pointer to a struct defined using ARRAY_HEAD() |
elem | Pointer to an element of the array |
num | The index of element in the array |
#define ARRAY_REMOVE | ( | head, | |
elem | |||
) |
Remove an entry from the array, shifting down the subsequent entries.
head | Pointer to a struct defined using ARRAY_HEAD() |
elem | Pointer to the element of the array to remove |
#define ARRAY_SORT | ( | head, | |
fn, | |||
sdata | |||
) |
Sort an array.
head | Pointer to a struct defined using ARRAY_HEAD() |
fn | Sort function, see sort_t |
sdata | Opaque argument to pass to sort function |
#define ARRAY_SET_NORESERVE | ( | head, | |
idx, | |||
elem | |||
) |
#define ARRAY_ADD_NORESERVE | ( | head, | |
elem | |||
) |