Interface for an abstract implementation of an array data structure. More...
Interface for an abstract implementation of an array data structure.
Arrays of a particular Type
are defined and initialized using the following code:
or equivalently:
Dynamic arrays can be used like regular pointers, i.e. elements are simply addressed using the [] operator, e.g.:
Using the vrna_array_append() macro, items can be safely appended and the array will grow accordingly if required:
Finally, memory occupied by an array must be released using the vrna_array_free() macro:
Use the vrna_array_size() macro to get the number of items stored in an array, e.g. for looping over its elements:
Under the hood, arrays are preceded by a header that actually stores the number of items they contain and the capacity of elements they are able to store. The general ideas for this implementation are taken from Ginger Bill's C Helper Library (public domain).
Files | |
file | array.h |
A macro-based dynamic array implementation. | |
Data Structures | |
struct | vrna_array_header_s |
The header of an array. More... | |
Macros | |
#define | vrna_array(Type) Type * |
Define an array. | |
#define | vrna_array_make(Type, Name) Type * Name; vrna_array_init(Name) |
Make an array Name of type Type . | |
#define | VRNA_ARRAY_GROW_FORMULA(n) (1.4 * (n) + 8) |
The default growth formula for array. | |
#define | VRNA_ARRAY_HEADER(input) ((vrna_array_header_t *)(input) - 1) |
Retrieve a pointer to the header of an array input . | |
#define | vrna_array_size(input) (VRNA_ARRAY_HEADER(input)->num) |
Get the number of elements of an array input . | |
#define | vrna_array_capacity(input) (VRNA_ARRAY_HEADER(input)->size) |
Get the size of an array input , i.e. its actual capacity. | |
#define | vrna_array_set_capacity(a, capacity) |
Explicitely set the capacity of an array a . | |
#define | vrna_array_init_size(a, init_size) |
Initialize an array a with a particular pre-allocated size init_size . | |
#define | vrna_array_init(a) vrna_array_init_size(a, VRNA_ARRAY_GROW_FORMULA(0)); |
Initialize an array a . | |
#define | vrna_array_free(a) |
Release memory of an array a . | |
#define | vrna_array_append(a, item) |
Safely append an item to an array a . | |
#define | vrna_array_grow(a, min_capacity) |
Grow an array a to provide a minimum capacity min_capacity . | |
Typedefs | |
typedef struct vrna_array_header_s | vrna_array_header_t |
The header of an array. | |
Functions | |
VRNA_NO_INLINE void * | vrna__array_set_capacity (void *array, size_t capacity, size_t element_size) |
Explicitely set the capacity of an array. | |
struct vrna_array_header_s |
#define vrna_array_init_size | ( | a, | |
init_size | |||
) |
#include <ViennaRNA/datastructures/array.h>
Initialize an array a
with a particular pre-allocated size init_size
.
VRNA_NO_INLINE void * vrna__array_set_capacity | ( | void * | array, |
size_t | capacity, | ||
size_t | element_size | ||
) |
#include <ViennaRNA/datastructures/array.h>
Explicitely set the capacity of an array.