-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataStructs.h
546 lines (458 loc) · 15.4 KB
/
dataStructs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#ifndef __DATA_STRUCTS__
#define __DATA_STRUCTS__
#include "argStruct.h"
#include "paramStruct.h"
#include "vcfReader.h"
/* FORWARD DECLARATIONS ----------------------------------------------------- */
namespace IO {
int verbose(const int verbose_threshold);
void vprint(const char* format, ...);
void vprint(const int verbose_threshold, const char* format, ...);
void vprint(FILE* fp, const int verbose_threshold, const char* format, ...);
}
typedef struct vcfData vcfData;
typedef struct intArray intArray;
/// ----------------------------------------------------------------------- ///
struct intArray {
/// @var d - array of int data
int* d;
/// @var size - size of d
size_t size;
/// @var len - length of the currently used part of d (
/// @note
/// len = position right after the last element in use in d
/// the next element will be added at position len
/// len-1 = gives the index of the last element in d
/// if intArray size is preallocated, len may be < size-1
/// if len == 0 the data is empty
/// if len == size the data is full
/// len can never be > size
size_t len;
/// @brief add - add an int
/// @param i int to add
/// @return size_t index of the newly added int
size_t add(const int i) {
// note: can never happen since during initialization d is mallocated with size 1 and size is set to 1
// if (0 == this->size) {
// this->size = 1;
// this->d = (int*)malloc(sizeof(int));
// ASSERT(this->d != NULL);
// }
if (this->len == this->size) {
this->size++;
int* tmp = NULL;
tmp = (int*)realloc(this->d, this->size * sizeof(int));
ASSERT(tmp != NULL);
this->d = tmp;
} else if (this->len > this->size) {
NEVER;
}
this->d[this->len] = i;
this->len++;
return (this->len - 1);
}
bool is_full(void) {
return(this->len == this->size);
}
/// @brief find - get index of the given value in the intArray
/// @param val - int value to search for
/// @param val_idx - pointer to the variable to store the index of the given int (if found)
/// @return bool true if the given int is found, false otherwise
bool find(const int val, size_t* val_idx) {
for (size_t i = 0;i < this->len;++i) {
if (val == this->d[i]) {
*val_idx = i;
return(true);
}
}
return(false);
}
/// @brief contains - check if the given value is in the intArray
/// @param val - int value to search for
/// @return bool true if the given int is found, false otherwise
bool contains(const int val) {
for (size_t i = 0;i < this->len;++i) {
if (val == this->d[i]) {
return(true);
}
}
return(false);
}
/// @brief get - get the value at the given index
/// @param idx index of the value to get
/// @return int value at the given index
int get(const size_t idx) {
ASSERT(idx < this->size);
ASSERT(idx <= this->len);
return (this->d[idx]);
}
/// @brief clear - clear the intArray
/// @note does not free memory
void clear(void) {
this->len = 0;
}
};
inline intArray* intArray_alloc(const size_t size) {
ASSERT(size > 0);
intArray* ret = (intArray*)malloc(sizeof(intArray));
ASSERT(ret != NULL);
ret->d = NULL;
ret->d = (int*)malloc(size * sizeof(int));
ASSERT(ret->d != NULL);
ret->size = size;
ret->len = 0;
return(ret);
}
inline intArray* intArray_init(void) {
intArray* ret = (intArray*)malloc(sizeof(intArray));
ASSERT(ret != NULL);
ret->d = NULL;
ret->d = (int*)malloc(sizeof(int));
ASSERT(ret->d != NULL);
ret->size = 1;
ret->len = 0;
return(ret);
}
inline intArray* intArray_init(const int founder_val) {
intArray* ret = (intArray*)malloc(sizeof(intArray));
ASSERT(ret != NULL);
ret->d = NULL;
ret->d = (int*)malloc(sizeof(int));
ASSERT(ret->d != NULL);
ret->d[0] = founder_val;
ret->size = 1;
ret->len = 1;
return(ret);
}
inline void intArray_destroy(intArray* ia) {
FREE(ia->d);
FREE(ia);
}
typedef struct size_tArray size_tArray;
struct size_tArray {
/// @var d - array of
size_t* d;
/// @var size - size of d
size_t size;
/// @var len - position right after the last element in use in d
/// @note
/// len-1 gives the index of the last element in d
/// if size_tArray size is preallocated, len may be < size-1
size_t len;
/// @brief add - add a size_t
/// @param i size_t to add
/// @return size_t index of the newly added size_t
size_t add(const size_t i) {
// if (0 == this->size) {
// this->size = 1;
// this->d = (size_t*)malloc(sizeof(size_t));
// ASSERT(this->d != NULL);
// }
if (this->len == this->size) {
this->size++;
size_t* tmp = NULL;
tmp = (size_t*)realloc(this->d, this->size * sizeof(size_t));
ASSERT(tmp != NULL);
this->d = tmp;
tmp = NULL;
} else if (this->len > this->size) {
NEVER;
}
this->d[this->len] = i;
this->len++;
return (this->len - 1);
}
/// @brief set_val - set the value at the given index
/// @param val
/// @param idx
/// @note use with caution, does not check if the index is already in use, and does not track if any of the previous vals are set/unset
void set_val(const size_t val, const size_t idx) {
ASSERT(idx < this->size);
ASSERT(idx >= this->len);
this->d[idx] = val;
if (len == idx) {
this->len++;
}
return;
}
/// @brief find - get index of the given value in the size_tArray
/// @param val - size_t value to search for
/// @param val_idx - pointer to the variable to store the index of the given size_t (if found)
/// @return bool true if the given size_t is found, false otherwise
bool find(const size_t val, size_t* val_idx) {
for (size_t i = 0;i < this->len;++i) {
if (val == this->d[i]) {
*val_idx = i;
return(true);
}
}
return(false);
}
/// @brief contains - check if the given value is in the size_tArray
/// @param val - size_t value to search for
/// @return bool true if the given size_t is found, false otherwise
bool contains(const size_t val) {
for (size_t i = 0;i < this->len;++i) {
if (val == this->d[i]) {
return(true);
}
}
return(false);
}
/// @brief get - get the value at the given index
/// @param idx index of the value to get
/// @return size_t value at the given index
size_t get(const size_t idx) {
ASSERT(idx < this->size);
ASSERT(idx <= this->len);
return (this->d[idx]);
}
/// @brief clear - clear the size_tArray
/// @note does not free memory
void clear(void) {
this->len = 0;
}
};
inline size_tArray* size_tArray_alloc(const size_t size) {
ASSERT(size > 0);
size_tArray* ret = (size_tArray*)malloc(sizeof(size_tArray));
ASSERT(ret != NULL);
ret->d = NULL;
ret->d = (size_t*)malloc(size * sizeof(size_t));
ASSERT(ret->d != NULL);
ret->size = size;
ret->len = 0;
return(ret);
}
inline size_tArray* size_tArray_init(void) {
size_tArray* ret = (size_tArray*)malloc(sizeof(size_tArray));
ASSERT(ret != NULL);
ret->d = NULL;
ret->d = (size_t*)malloc(sizeof(size_t));
ASSERT(ret->d != NULL);
ret->size = 1;
ret->len = 0;
return(ret);
}
/// @brief size_tArray_init - allocate memory for a size_tArray and initialize it with a founder value
inline size_tArray* size_tArray_init(const size_t founder_val) {
size_tArray* ret = (size_tArray*)malloc(sizeof(size_tArray));
ret->d = (size_t*)malloc(sizeof(size_t));
ASSERT(ret->d != NULL);
ret->size = 1;
ret->d[0] = founder_val;
ret->len = 1;
return(ret);
}
inline void size_tArray_destroy(size_tArray* sa) {
FREE(sa->d);
FREE(sa);
}
typedef struct strArray strArray;
struct strArray {
/// @var d - array of char* data
char** d;
/// @var size - size of d
size_t size;
/// @var len - position right after the last element in use in d
/// @note
/// len-1 gives the index of the last element in d
/// if strArray size is preallocated, len may be < size-1
size_t len;
/// @brief add - add a string (char*)
/// @param str string to add
/// @return size_t index of the newly added string (char*)
size_t add(const char* str) {
ASSERT(str != NULL);
if (this->len == this->size) {
this->size++;
char** tmp = NULL;
DEVASSERT(NULL != this->d);
tmp = (char**)realloc(this->d, this->size * sizeof(char*));
ASSERT(tmp != NULL);
this->d = tmp;
this->d[this->len] = NULL;
} else if (this->len > this->size) {
NEVER;
}
this->d[this->len] = strdup(str);
ASSERT(this->d[this->len] != NULL);
this->len++;
return (this->len - 1);
}
void add_to(const char* val, const size_t idx) {
ASSERT(idx < this->size);
ASSERT(this->size > 0);
ASSERT(NULL != this->d);
ASSERT(NULL == this->d[idx]);
this->d[idx] = strdup(val);
ASSERT(this->d[idx] != NULL);
if (len == idx) {
this->len++;
} else if (len < idx) {
this->len = idx + 1;
}
return;
}
/// @brief find - get index of the given value in the strArray
/// @param val - str (char*) value to search for
/// @param val_idx - pointer to the variable to store the index of the given str (if found)
/// @return bool true if the given str is found, false otherwise
bool find(const char* val, size_t* val_idx) {
ASSERT(val != NULL);
for (size_t i = 0;i < this->len;++i) {
// can be NULL if a specific value is set using add_to which skipped 1 or more indices
if (NULL != this->d[i]) {
if (0 == strcmp(val, this->d[i])) {
*val_idx = i;
return(true);
}
}
}
return(false);
}
/// @brief find_from - get index of the given value in the strArray starting from the given index
/// @param val - str (char*) value to search for
/// @param val_idx - pointer to the variable to store the index of the given str (if found)
/// @param start_idx - index to start the search from
/// @return bool true if the given str is found, false otherwise
bool find_from(const char* val, size_t* val_idx, const size_t start_idx) {
ASSERT(val != NULL);
for (size_t i = start_idx;i < this->len;++i) {
// can be NULL if a specific value is set using add_to which skipped 1 or more indices
if (NULL != this->d[i]) {
if (0 == strcmp(val, this->d[i])) {
*val_idx = i;
return(true);
}
}
}
return(false);
}
/// @brief contains - check if the given value is in the strArray
/// @param val - str (char*) value to search for
/// @return bool true if the given str is found, false otherwise
bool contains(const char* val) {
ASSERT(val != NULL);
DEVASSERT(this->d != NULL);
for (size_t i = 0;i < this->len;++i) {
// can be NULL if a specific value is set using add_to which skipped 1 or more indices
if (NULL != this->d[i]) {
if (0 == strcmp(val, this->d[i])) {
return(true);
}
}
}
return(false);
}
/// @brief get - get the value at the given index
/// @param idx index of the value to get
/// @return char* value at the given index
char* get(const size_t idx) {
ASSERT(idx < this->size);
if (idx < this->len) {
return (this->d[idx]);
}
return(NULL);
}
void clear(void) {
for (size_t i = 0;i < this->len;++i) {
FREE(this->d[i]);
}
this->len = 0;
}
};
inline strArray* strArray_init(void) {
strArray* ret = (strArray*)malloc(sizeof(strArray));
ASSERT(ret != NULL);
ret->d = NULL;
ret->size = 1;
ret->len = 0;
ret->d = (char**)malloc(sizeof(char*));
ASSERT(ret->d != NULL);
ret->d[0] = NULL;
return(ret);
}
inline strArray* strArray_init(const char* founder_val) {
ASSERT(founder_val != NULL);
strArray* ret = (strArray*)malloc(sizeof(strArray));
ASSERT(ret != NULL);
ret->d = NULL;
ret->size = 1;
ret->len = 1;
ret->d = (char**)malloc(sizeof(char*));
ASSERT(ret->d != NULL);
ret->d[0] = strdup(founder_val);
ASSERT(ret->d[0] != NULL);
return(ret);
}
/// @brief strArray_alloc - allocate memory for a strArray
/// @param size size of the strArray to allocate
/// @return strArray* pointer to the allocated strArray
inline strArray* strArray_alloc(const size_t size) {
ASSERT(size > 0);
strArray* ret = (strArray*)malloc(sizeof(strArray));
ASSERT(ret != NULL);
ret->d = NULL;
ret->d = (char**)malloc(size * sizeof(char*));
ASSERT(ret->d != NULL);
for (size_t i = 0;i < size;++i) {
ret->d[i] = NULL;
}
ret->len = 0;
ret->size = size;
return(ret);
}
inline void strArray_destroy(strArray* sa) {
for (size_t i = 0;i < sa->size;++i) {
if (sa->d[i] != NULL) {
FREE(sa->d[i]);
}
}
FREE(sa->d);
FREE(sa);
}
/* -------------------------------------------------------------------------- */
/// @brief read_formula_str - read the formula string into a strArray of formula tokens
/// @param formula formula string
/// @return pointer to newly created strArray
/// @example formula = 'Individual ~ Region/Population/Subpopulation'
/// formulaTokens->len = 4
/// formulaTokens->d[0] = "Region" (level 1, lvlidx 0)
/// formulaTokens->d[1] = "Population" (level 2, lvlidx 1)
/// formulaTokens->d[2] = "Subpopulation" (level 3, lvlidx 2)
/// formulaTokens->d[3] = "Individual" (level 4, lvlidx 3)
strArray* read_formula_str(const char* formula);
inline void trimSpaces(char* str) {
char* ptr = str;
char* end = NULL;
// skip leading spaces and find the start of the non-space chars
while (*ptr && isspace((unsigned char)*ptr)) {
++ptr;
}
if (*ptr == '\0') {
// it is all spaces!?
ERROR("Found empty string.");
}
// find the end of the str and set end to the position of last non-space char
for (char* tmp = ptr; *tmp; ++tmp) {
if (!isspace((unsigned char)*tmp)) {
end = tmp;
}
}
if (ptr != str) {
// we have leading spaces
// shift the non-space chars to the beginning
char* dest = str;
while (ptr <= end) {
*dest++ = *ptr++;
}
*dest = '\0'; // terminate the shifted str
} else {
// no leading spaces
// terminate the str at the last non-space char
*(end + 1) = '\0';
}
}
/// ----------------------------------------------------------------------- ///
#endif // __DATA_STRUCTS__