-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsga.h
82 lines (67 loc) · 2.29 KB
/
gsga.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
#ifndef GSGAH
#define GSGAH
// allocate
#define ALLOC( typ ) (typ *)malloc(sizeof(typ))
#define ALLOCN( typ, n ) (typ *)malloc((n)*sizeof(typ))
#define FREE( x ) free( x )
#define REALLOCN( typ, old, n ) (typ *)realloc((old), (n)*sizeof(typ))
#define COPY( typ, src, dst ) memcpy( dst, src, sizeof(typ))
#define COPYN( typ, src, dst, n ) memcpy( dst, src, (n)*sizeof(typ))
/// generic dynamic array
/// name of array type
#define ARR( base ) gsga_##base
/// define array type
#define ARRDEF( base ) \
typedef struct { \
int count; \
int size; \
base * items; \
} ARR( base )
/// create new array
#define ARRINIT( arr, base, isize ) { \
arr = ALLOC( ARR(base) ); \
arr->count = 0; \
arr->size = isize; \
arr->items = ALLOCN(base, isize); \
}
/// free array
#define ARRFREE( arr ) { \
FREE( arr->items ); \
FREE( arr ); \
}
/// lookup and return a value in array
#define ARRLOOKUP( arr, keyfield, key, valuefield ) \
{ int gsga_i; \
for (gsga_i=0; gsga_i < arr->count; ++gsga_i) { \
if ( arr->items[gsga_i].keyfield == key ) \
return arr->items[gsga_i].valuefield; \
} \
}
/// set array size
#define ARRSIZE( arr, base, asize ) \
{ arr->size = asize; \
arr->items = REALLOCN( base, arr->items, asize ); \
}
/// add new item to array
#define ARRADD( arr, base, item ) { \
if (arr->count >= arr->size) \
ARRSIZE( arr, base, 2*arr->size ); \
arr->items[ arr->count ++ ] = (item); \
}
/// remove item by index
#define ARRREMOVE( arr, base, i ) { \
COPYN( base, arr->items+i+1, \
arr->items+i, arr->count-i-1 ); \
-- arr->count; \
}
/// remove item by field balue
#define ARRREMOVEBY( arr, base, field, val ) { \
int gsga_i; \
for( gsga_i=0; gsga_i < arr->count; ++gsga_i) { \
if ( arr->items[ gsga_i ].field == (val)) { \
ARRREMOVE( arr, base, gsga_i ); \
break; \
} \
} \
}
#endif // GSGAH