Arrays — ViennaRNA Package

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

    Define an array.

        #include <ViennaRNA/datastructures/array.h>

  vrna_array_make(Type, Name)

    Make an array `Name` of type `Type`.

        #include <ViennaRNA/datastructures/array.h>

  VRNA_ARRAY_GROW_FORMULA(n)

    The default growth formula for array.

        #include <ViennaRNA/datastructures/array.h>

  VRNA_ARRAY_HEADER(input)

    Retrieve a pointer to the header of an array `input`.

        #include <ViennaRNA/datastructures/array.h>

  vrna_array_size(input)

    Get the number of elements of an array `input`.

        #include <ViennaRNA/datastructures/array.h>

  vrna_array_capacity(input)

    Get the size of an array `input`, i.e. its actual capacity.

        #include <ViennaRNA/datastructures/array.h>

  vrna_array_set_capacity(a, capacity)

    Explicitely set the capacity of an array `a`.

        #include <ViennaRNA/datastructures/array.h>

  vrna_array_init_size(a, init_size)

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

        #include <ViennaRNA/datastructures/array.h>

  vrna_array_init(a)

    Initialize an array `a`.

        #include <ViennaRNA/datastructures/array.h>

  vrna_array_free(a)

    Release memory of an array `a`.

        #include <ViennaRNA/datastructures/array.h>

  vrna_array_append(a, item)

    Safely append an item to an array `a`.

        #include <ViennaRNA/datastructures/array.h>

  vrna_array_grow(a, min_capacity)

    Grow an array `a` to provide a minimum capacity `min_capacity`.

        #include <ViennaRNA/datastructures/array.h>

  Typedefs

  typedef struct vrna_array_header_s vrna_array_header_t

    The header of an array.

        #include <ViennaRNA/datastructures/array.h>

  Functions

  void *vrna__array_set_capacity(void *array, size_t capacity, size_t element_size)

    Explicitely set the capacity of an array.

        #include <ViennaRNA/datastructures/array.h>

    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.

    Public Members

    size_t num

      The number of elements in an array.

          #include <ViennaRNA/datastructures/array.h>

    size_t size

      The actual capacity of an array.

          #include <ViennaRNA/datastructures/array.h>