RNAlib-2.6.3
 
Loading...
Searching...
No Matches

Interface for an abstract implementation of an array data structure. More...

Detailed Description

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);
#define vrna_array(Type)
Define an array.
Definition array.h:99
#define vrna_array_init(a)
Initialize an array a.
Definition array.h:164

or equivalently:

vrna_array_make(Type, my_array);
#define vrna_array_make(Type, Name)
Make an array Name of type Type.
Definition array.h:104

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);
#define vrna_array_append(a, item)
Safely append an item to an array a.
Definition array.h:179

Finally, memory occupied by an array must be released using the vrna_array_free() macro:

vrna_array_free(my_array);
#define vrna_array_free(a)
Release memory of an array a.
Definition array.h:170

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);
#define vrna_array_size(input)
Get the number of elements of an array input.
Definition array.h:121

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

+ Collaboration diagram for Arrays:

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.
 

Data Structure Documentation

◆ vrna_array_header_s

struct vrna_array_header_s

The header of an array.

Data Fields

size_t num
 The number of elements in an array.
 
size_t size
 The actual capacity of an array.
 

Macro Definition Documentation

◆ vrna_array_init_size

#define vrna_array_init_size (   a,
  init_size 
)

#include <ViennaRNA/datastructures/array.h>

Value:
do { \
void **a_ptr = (void **)&(a); \
size_t size = sizeof(*(a)) * (init_size) + sizeof(vrna_array_header_t); \
vrna_array_header_t *h = (void *)vrna_alloc(size); \
h->num = 0; \
h->size = init_size; \
*a_ptr = (void *)(h + 1); \
} while (0)
size_t num
The number of elements in an array.
Definition array.h:92
size_t size
The actual capacity of an array.
Definition array.h:93
The header of an array.
Definition array.h:91
void * vrna_alloc(unsigned size)
Allocate space safely.

Initialize an array a with a particular pre-allocated size init_size.

Function Documentation

◆ vrna__array_set_capacity()

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.

Note
Do not use this function. Rather resort to the vrna_array_set_capacity macro