RNAlib-2.6.3
 
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1#ifndef VIENNA_RNA_PACKAGE_ARRAY_H
2#define VIENNA_RNA_PACKAGE_ARRAY_H
3
4#include <stddef.h>
5
6
7#if !defined(VRNA_NO_INLINE)
8 #if defined(_MSC_VER)
9 #define VRNA_NO_INLINE __declspec(noinline)
10 #else
11 #define VRNA_NO_INLINE __attribute__ ((noinline))
12 #endif
13#endif
14
91typedef struct vrna_array_header_s {
92 size_t num;
93 size_t size;
95
99#define vrna_array(Type) Type *
100
104#define vrna_array_make(Type, Name) Type * Name; vrna_array_init(Name)
105
106
107#ifndef VRNA_ARRAY_GROW_FORMULA
111#define VRNA_ARRAY_GROW_FORMULA(n) (1.4 * (n) + 8)
112#endif
113
117#define VRNA_ARRAY_HEADER(input) ((vrna_array_header_t *)(input) - 1)
121#define vrna_array_size(input) (VRNA_ARRAY_HEADER(input)->num)
125#define vrna_array_capacity(input) (VRNA_ARRAY_HEADER(input)->size)
126
130#define vrna_array_set_capacity(a, capacity) do { \
131 if (a) { \
132 void **a_ptr = (void **)&(a); \
133 *a_ptr = vrna__array_set_capacity((a), (capacity), sizeof(*(a))); \
134 } \
135} while (0)
136
137
143VRNA_NO_INLINE void *
145 size_t capacity,
146 size_t element_size);
147
152#define vrna_array_init_size(a, init_size) do { \
153 void **a_ptr = (void **)&(a); \
154 size_t size = sizeof(*(a)) * (init_size) + sizeof(vrna_array_header_t); \
155 vrna_array_header_t *h = (void *)vrna_alloc(size); \
156 h->num = 0; \
157 h->size = init_size; \
158 *a_ptr = (void *)(h + 1); \
159} while (0)
160
164#define vrna_array_init(a) vrna_array_init_size(a, VRNA_ARRAY_GROW_FORMULA(0));
165
166
170#define vrna_array_free(a) do { \
171 vrna_array_header_t *h = VRNA_ARRAY_HEADER(a); \
172 free(h); \
173} while (0)
174
175
179#define vrna_array_append(a, item) do { \
180 if (vrna_array_capacity(a) < vrna_array_size(a) + 1) \
181 vrna_array_grow(a, 0); \
182 (a)[vrna_array_size(a)++] = (item); \
183} while (0)
184
185
189#define vrna_array_grow(a, min_capacity) do { \
190 size_t new_capacity = VRNA_ARRAY_GROW_FORMULA(vrna_array_capacity(a)); \
191 if (new_capacity < (min_capacity)) \
192 new_capacity = (min_capacity); \
193 vrna_array_set_capacity(a, new_capacity); \
194} while (0)
195
201#endif
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
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 vrna_array_header_t
The header of an array.
The header of an array.
Definition array.h:91