Provides macros to work with dynamic arrays.
More...
#include "attributes.h"
Go to the source code of this file.
|
| #define | ARRAY_SENTINEL 0x15bada55 |
| #define | array_create(basic_type) |
| | Creates a new dynamic array of ‘basic_type’.
|
| #define | array_create_size(basic_type, capacity) |
| | Creates a new dynamic array of ‘basic_type’ with an initial capacity.
|
| #define | array_resize(ptr_array, new_size) |
| | Resizes the array to accomodate new_size elements.
|
| #define | array_grow(ptr_array) |
| | Increases the number of elements by one and returns the last element.
|
| #define | array_push_back(ptr_array, element) |
| | Adds a new element at the end of the array.
|
| #define | array_erase(ptr_array, first, last) |
| | Erases elements in interval [first, last).
|
| #define | array_shrink(ptr_array) |
| | Shrinks memory to fit only ‘size’ elements.
|
| #define | array_free(ptr_array) |
| | Frees memory allocated and sets array to NULL.
|
| #define | array_reserved(array) |
| | Returns number of elements reserved.
|
| #define | array_begin(array) |
| | Returns a pointer to the beginning of the reserved memory space.
|
| #define | array_end(array) |
| | Returns a pointer to the end of the reserved memory space.
|
| #define | array_front(ptr_array) |
| | Returns the first element in the array.
|
| #define | array_back(ptr_array) |
| | Returns the last element in the array.
|
| #define | array_copy(basic_type, ptr_array) |
| | Returns a shallow copy of the input array.
|
Provides macros to work with dynamic arrays.
- Note
- Except were noted, macros do not have side effects from expansions.
Usage example:
static my_type *my_array = NULL;
while (need_fill)
need_fill = fill_array_member( &
array_grow( &my_array ) );
do_stuff( &my_array[i] );
my_array = NULL;
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
#define array_shrink(ptr_array)
Shrinks memory to fit only ‘size’ elements.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Definition in file array.h.
◆ array_back
| #define array_back |
( |
| ptr_array | ) |
|
Value:
#define array_end(array)
Returns a pointer to the end of the reserved memory space.
Returns the last element in the array.
- Parameters
-
| ptr_array | Array being manipulated. |
- Returns
- The last element in the array.
Definition at line 228 of file array.h.
◆ array_begin
| #define array_begin |
( |
| array | ) |
|
Value:
Returns a pointer to the beginning of the reserved memory space.
- Parameters
-
| array | Array being manipulated. |
- Returns
- Beginning of memory space.
Definition at line 206 of file array.h.
◆ array_copy
| #define array_copy |
( |
| basic_type, |
|
|
| ptr_array ) |
Value: ( (basic_type *)( _array_copy_helper( sizeof( basic_type ), \
(void *)( ptr_array ) ) ) )
Returns a shallow copy of the input array.
Definition at line 230 of file array.h.
◆ array_create
| #define array_create |
( |
| basic_type | ) |
|
Value: ( (basic_type *)( _array_create_helper( sizeof( basic_type ), 1 ) ) )
Creates a new dynamic array of ‘basic_type’.
- Parameters
-
| basic_type | Type of the array to create. |
Definition at line 93 of file array.h.
◆ array_create_size
| #define array_create_size |
( |
| basic_type, |
|
|
| capacity ) |
Value: ( (basic_type *)( _array_create_helper( sizeof( basic_type ), capacity ) ) )
Creates a new dynamic array of ‘basic_type’ with an initial capacity.
- Parameters
-
| basic_type | Type of the array to create. |
| capacity | Initial size. |
Definition at line 102 of file array.h.
◆ array_end
| #define array_end |
( |
| array | ) |
|
Value:
Returns a pointer to the end of the reserved memory space.
- Warning
- macro, may evaluate argument twice.
- Parameters
-
| array | Array being manipulated. |
- Returns
- End of memory space.
Definition at line 214 of file array.h.
◆ array_erase
| #define array_erase |
( |
| ptr_array, |
|
|
| first, |
|
|
| last ) |
Value: ( _array_erase_helper( (void **)( ptr_array ), \
sizeof( ( ptr_array )[0][0] ), (void *)( first ), \
(void *)( last ) ) )
Erases elements in interval [first, last).
- Note
- Invalidates all iterators.
- Parameters
-
| ptr_array | Array being manipulated. |
| first | First iterator to erase. |
| last | Last iterator in erase section but is not erased. |
Definition at line 148 of file array.h.
◆ array_free
| #define array_free |
( |
| ptr_array | ) |
|
Value:_array_free_helper( (void *)( ptr_array ) )
Frees memory allocated and sets array to NULL.
- Note
- Invalidates all iterators.
- Parameters
-
| ptr_array | Array being manipulated. |
Definition at line 170 of file array.h.
◆ array_front
| #define array_front |
( |
| ptr_array | ) |
|
Value:
#define array_begin(array)
Returns a pointer to the beginning of the reserved memory space.
Returns the first element in the array.
- Parameters
-
| ptr_array | Array being manipulated. |
- Returns
- The first element in the array.
Definition at line 221 of file array.h.
◆ array_grow
| #define array_grow |
( |
| ptr_array | ) |
|
Value: ( *(__typeof__( ( ptr_array )[0] ))_array_grow_helper( \
(void **)( ptr_array ), \
sizeof( ( ptr_array )[0][0] ) ) )
Increases the number of elements by one and returns the last element.
- Note
- Invalidates all iterators.
Definition at line 122 of file array.h.
◆ array_push_back
| #define array_push_back |
( |
| ptr_array, |
|
|
| element ) |
Value: do \
array_grow( ptr_array ) = element; \
while ( 0 )
Adds a new element at the end of the array.
- Note
- Invalidates all iterators.
- Parameters
-
| ptr_array | Array being manipulated. |
| element | Element being pushed to the back. |
Definition at line 134 of file array.h.
◆ array_reserved
| #define array_reserved |
( |
| array | ) |
|
Value:
static _private_container * _array_private_container(void *a)
Gets the container of an array.
Returns number of elements reserved.
- Parameters
-
| array | Array being manipulated. |
- Returns
- The size of the array (memory usage).
Definition at line 199 of file array.h.
◆ array_resize
| #define array_resize |
( |
| ptr_array, |
|
|
| new_size ) |
Value: ( _array_resize_helper( (void **)( ptr_array ), \
sizeof( ( ptr_array )[0][0] ), new_size ) )
Resizes the array to accomodate new_size elements.
- Note
- Invalidates all iterators.
- Parameters
-
| ptr_array | Array being manipulated. |
| new_size | New size to grow to (in number of elements). |
Definition at line 113 of file array.h.
◆ ARRAY_SENTINEL
| #define ARRAY_SENTINEL 0x15bada55 |
Badass sentinel.
Definition at line 46 of file array.h.
◆ array_shrink
| #define array_shrink |
( |
| ptr_array | ) |
|
Value: ( _array_shrink_helper( (void **)( ptr_array ), \
sizeof( ( ptr_array )[0][0] ) ) )
Shrinks memory to fit only ‘size’ elements.
- Note
- Invalidates all iterators.
- Parameters
-
| ptr_array | Array being manipulated. |
Definition at line 160 of file array.h.
◆ _array_copy_helper()
| void * _array_copy_helper |
( |
size_t | e_size, |
|
|
void * | a ) |
◆ _array_create_helper()
| void * _array_create_helper |
( |
size_t | e_size, |
|
|
size_t | initial_size ) |
◆ _array_erase_helper()
| void _array_erase_helper |
( |
void ** | a, |
|
|
size_t | e_size, |
|
|
void * | first, |
|
|
void * | last ) |
◆ _array_free_helper()
| void _array_free_helper |
( |
void * | a | ) |
|
◆ _array_grow_helper()
| void * _array_grow_helper |
( |
void ** | a, |
|
|
size_t | e_size ) |
◆ _array_private_container()
Gets the container of an array.
- Parameters
-
| a | Array to get container of. |
- Returns
- The container of the array a.
Definition at line 74 of file array.h.
◆ _array_resize_helper()
| void _array_resize_helper |
( |
void ** | a, |
|
|
size_t | e_size, |
|
|
size_t | new_size ) |
◆ _array_shrink_helper()
| void _array_shrink_helper |
( |
void ** | a, |
|
|
size_t | e_size ) |
◆ array_size()
| ALWAYS_INLINE int array_size |
( |
const void * | array | ) |
|
|
inlinestatic |
Returns number of elements in the array.
- Warning
- macro, may evaluate argument twice.
- Parameters
-
| array | Array being manipulated. |
- Returns
- The size of the array (number of elements).
Definition at line 179 of file array.h.