naev 0.12.6
array.h File Reference

Provides macros to work with dynamic arrays. More...

#include "attributes.h"
Include dependency graph for array.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  _private_container
 Private container type for the arrays. More...

Macros

#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.

Functions

void * _array_create_helper (size_t e_size, size_t initial_size)
void * _array_grow_helper (void **a, size_t e_size)
void _array_resize_helper (void **a, size_t e_size, size_t new_size)
void _array_erase_helper (void **a, size_t e_size, void *first, void *last)
void _array_shrink_helper (void **a, size_t e_size)
void _array_free_helper (void *a)
void * _array_copy_helper (size_t e_size, void *a)
static _private_container_array_private_container (void *a)
 Gets the container of an array.
static ALWAYS_INLINE int array_size (const void *array)
 Returns number of elements in the array.

Detailed Description

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;
// Create array
my_array = array_create( my_type );
// Fill array
while (need_fill)
need_fill = fill_array_member( &array_grow( &my_array ) );
// Shrink to minimum (if static it's a good idea).
array_shrink( &my_array );
// Do stuff
for (i=0; i<array_size( my_array ); i++)
do_stuff( &my_array[i] );
// Clean up
array_free( my_array );
my_array = NULL;
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:170
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:179
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
Definition array.h:122
#define array_shrink(ptr_array)
Shrinks memory to fit only ‘size’ elements.
Definition array.h:160
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Definition array.h:93

Definition in file array.h.

Macro Definition Documentation

◆ array_back

#define array_back ( ptr_array)
Value:
( *( array_end( ptr_array ) - 1 ) )
#define array_end(array)
Returns a pointer to the end of the reserved memory space.
Definition array.h:214

Returns the last element in the array.

Parameters
ptr_arrayArray being manipulated.
Returns
The last element in the array.

Definition at line 228 of file array.h.

◆ array_begin

#define array_begin ( array)
Value:
( array )

Returns a pointer to the beginning of the reserved memory space.

Parameters
arrayArray 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_typeType 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_typeType of the array to create.
capacityInitial size.

Definition at line 102 of file array.h.

◆ array_end

#define array_end ( array)
Value:
( ( array ) + array_size( array ) )

Returns a pointer to the end of the reserved memory space.

Warning
macro, may evaluate argument twice.
Parameters
arrayArray 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_arrayArray being manipulated.
firstFirst iterator to erase.
lastLast 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_arrayArray being manipulated.

Definition at line 170 of file array.h.

◆ array_front

#define array_front ( ptr_array)
Value:
( *array_begin( ptr_array ) )
#define array_begin(array)
Returns a pointer to the beginning of the reserved memory space.
Definition array.h:206

Returns the first element in the array.

Parameters
ptr_arrayArray 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_arrayArray being manipulated.
elementElement 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.
Definition array.h:74
size_t _reserved
Definition array.h:55

Returns number of elements reserved.

Parameters
arrayArray 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_arrayArray being manipulated.
new_sizeNew 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_arrayArray being manipulated.

Definition at line 160 of file array.h.

Function Documentation

◆ _array_copy_helper()

void * _array_copy_helper ( size_t e_size,
void * a )

Definition at line 109 of file array.c.

◆ _array_create_helper()

void * _array_create_helper ( size_t e_size,
size_t initial_size )

Definition at line 17 of file array.c.

◆ _array_erase_helper()

void _array_erase_helper ( void ** a,
size_t e_size,
void * first,
void * last )

Definition at line 72 of file array.c.

◆ _array_free_helper()

void _array_free_helper ( void * a)

Definition at line 102 of file array.c.

◆ _array_grow_helper()

void * _array_grow_helper ( void ** a,
size_t e_size )

Definition at line 59 of file array.c.

◆ _array_private_container()

_private_container * _array_private_container ( void * a)
inlinestatic

Gets the container of an array.

Parameters
aArray 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 )

Definition at line 52 of file array.c.

◆ _array_shrink_helper()

void _array_shrink_helper ( void ** a,
size_t e_size )

Definition at line 89 of file array.c.

◆ 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
arrayArray being manipulated.
Returns
The size of the array (number of elements).

Definition at line 179 of file array.h.