Arrays
Interface for an abstract implementation of an array data structure.
Arrays of a particular Type
are defined and initialized using the following code:
vrna_array(Type) my_array;
vrna_array_init(my_array);
or equivalently:
vrna_array_make(Type, my_array);
Dynamic arrays can be used like regular pointers, i.e. elements are simply
addressed using the []
operator, e.g.:
my_array[1] = 42;
Using the vrna_array_append
macro, items can be safely appended and the
array will grow accordingly if required:
vrna_array_append(my_array, item);
Finally, memory occupied by an array must be released using the vrna_array_free
macro:
vrna_array_free(my_array);
Use the vrna_array_size
macro to get the number of items stored in an array,
e.g. for looping over its elements:
// define and initialize
vrna_array_make(int, my_array);
// append some items
vrna_array_append(my_array, 42);
vrna_array_append(my_array, 23);
vrna_array_append(my_array, 5);
// loop over items and print
for (size_t i = 0; i < vrna_array_size(my_array); i++)
printf("%d\n", my_array[i]);
// release memory of the array
vrna_array_free(my_array);
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).
Defines
-
vrna_array(Type)
- #include <ViennaRNA/datastructures/array.h>
Define an array.
-
vrna_array_make(Type, Name)
- #include <ViennaRNA/datastructures/array.h>
Make an array
Name
of typeType
.
-
VRNA_ARRAY_GROW_FORMULA(n)
- #include <ViennaRNA/datastructures/array.h>
The default growth formula for array.
-
VRNA_ARRAY_HEADER(input)
- #include <ViennaRNA/datastructures/array.h>
Retrieve a pointer to the header of an array
input
.
-
vrna_array_size(input)
- #include <ViennaRNA/datastructures/array.h>
Get the number of elements of an array
input
.
-
vrna_array_capacity(input)
- #include <ViennaRNA/datastructures/array.h>
Get the size of an array
input
, i.e. its actual capacity.
-
vrna_array_set_capacity(a, capacity)
- #include <ViennaRNA/datastructures/array.h>
Explicitely set the capacity of an array
a
.
-
vrna_array_init_size(a, init_size)
- #include <ViennaRNA/datastructures/array.h>
Initialize an array
a
with a particular pre-allocated sizeinit_size
.
-
vrna_array_init(a)
- #include <ViennaRNA/datastructures/array.h>
Initialize an array
a
.
-
vrna_array_free(a)
- #include <ViennaRNA/datastructures/array.h>
Release memory of an array
a
.
-
vrna_array_append(a, item)
- #include <ViennaRNA/datastructures/array.h>
Safely append an item to an array
a
.
-
vrna_array_grow(a, min_capacity)
- #include <ViennaRNA/datastructures/array.h>
Grow an array
a
to provide a minimum capacitymin_capacity
.
Typedefs
-
typedef struct vrna_array_header_s vrna_array_header_t
- #include <ViennaRNA/datastructures/array.h>
The header of an array.
Functions
-
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.
Note
Do not use this function. Rather resort to the vrna_array_set_capacity macro
-
struct vrna_array_header_s
- #include <ViennaRNA/datastructures/array.h>
The header of an array.
-
vrna_array(Type)