-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhgl_da.h
220 lines (197 loc) · 10.7 KB
/
hgl_da.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
/**
* LICENSE:
*
* MIT License
*
* Copyright (c) 2023 Henrik A. Glass
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* MIT License
*
*
* ABOUT:
*
* hgl_da.h implements a general purpose minimal macro-only dynamic array.
*
*
* USAGE:
*
* Just create a struct of the correct form by using the HglDynamicArray(T) macro:
*
* typedef HglDynamicArray(float) Floats;
* Floats fs = {0};
* hgl_da_append(&fs, 123.456f);
*
* or:
*
* HglDynamicArray(float) fs = {0};
* hgl_da_append(&fs, 123.456f);
*
*
* AUTHOR: Henrik A. Glass
*
*/
#ifndef HGL_DA_H
#define HGL_DA_H
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdint.h>
#if !defined(HGL_ALLOC) && \
!defined(HGL_REALLOC) && \
!defined(HGL_FREE)
#include <stdlib.h>
#define HGL_ALLOC malloc
#define HGL_REALLOC realloc
#define HGL_FREE free
#endif
#define HglDynamicArray(T) \
struct { \
T *arr; \
size_t length; \
size_t capacity; \
}
#ifndef HGL_DA_INITIAL_CAPACITY
#define HGL_DA_INITIAL_CAPACITY 16
#endif
#ifndef HGL_DA_GROWTH_RATE
#define HGL_DA_GROWTH_RATE 1.5
#endif
#define hgl_da_push(da, item) \
do { \
if ((da)->arr == NULL) { \
(da)->length = 0; \
(da)->capacity = HGL_DA_INITIAL_CAPACITY; \
(da)->arr = HGL_ALLOC((da)->capacity * sizeof(*(da)->arr)); \
} \
if ((da)->capacity < ((da)->length + 1)) { \
(da)->capacity *= HGL_DA_GROWTH_RATE; \
(da)->arr = HGL_REALLOC((da)->arr, (da)->capacity * sizeof(*(da)->arr)); \
} \
assert(((da)->arr != NULL) && "[hgl] Error: (re)alloc failed"); \
(da)->arr[(da)->length++] = (item); \
} while (0)
#define hgl_da_pop(da) ((da)->arr[--(da)->length])
#define hgl_da_at(da, i) \
int res__ = (i) % (int)((da)->length); \
(da)->arr[(res__ >= 0) ? res__ : res__ + (int)((da)->length)]
#define hgl_da_extend(da, items, n) \
do { \
if ((da)->arr == NULL) { \
(da)->length = 0; \
(da)->capacity = max(HGL_DA_INITIAL_CAPACITY, \
(int)hgl_da_next_pow2_((n))); \
(da)->arr = HGL_ALLOC((da)->capacity * sizeof(*(da)->arr)); \
} \
if ((da)->capacity < ((da)->length + (n))) { \
while ((da)->capacity < ((da)->length + (n))) { \
(da)->capacity *= HGL_DA_GROWTH_RATE; \
} \
(da)->arr = HGL_REALLOC((da)->arr, (da)->capacity * sizeof(*(da)->arr)); \
} \
assert(((da)->arr != NULL) && "[hgl] Error: (re)alloc failed"); \
memcpy(&(da)->arr[(da)->length], (items), (n)*sizeof(*(da)->arr)); \
(da)->length += (n); \
} while (0)
#define hgl_da_reserve(da, new_capacity) \
do { \
if ((da)->arr == NULL) { \
(da)->length = 0; \
(da)->capacity = max(HGL_DA_INITIAL_CAPACITY, \
(int) hgl_da_next_pow2_(new_capacity)); \
(da)->arr = HGL_ALLOC((da)->capacity * sizeof(*(da)->arr)); \
} \
if ((da)->capacity >= (new_capacity)) break; \
(da)->capacity = hgl_da_next_pow2_((int)new_capacity); \
(da)->arr = HGL_REALLOC((da)->arr, (da)->capacity * sizeof(*(da)->arr)); \
} while (0)
#define hgl_da_reserve_exact(da, new_capacity) \
do { \
if ((da)->arr == NULL) { \
(da)->length = 0; \
(da)->capacity = max(HGL_DA_INITIAL_CAPACITY, (int)new_capacity); \
(da)->arr = HGL_ALLOC((da)->capacity * sizeof(*(da)->arr)); \
} \
if ((da)->capacity >= (new_capacity)) break; \
(da)->capacity = (new_capacity); \
(da)->arr = HGL_REALLOC((da)->arr, (da)->capacity * sizeof(*(da)->arr)); \
} while (0)
#define hgl_da_free(da) \
do { \
HGL_FREE((da)->arr); \
} while (0)
#define hgl_da_insert(da, index, item) \
do { \
assert((ssize_t)(index) >= 0 && (index) < (da)->length); \
if ((da)->capacity < ((da)->length + 1)) { \
(da)->capacity *= HGL_DA_GROWTH_RATE; \
(da)->arr = HGL_REALLOC((da)->arr, (da)->capacity * sizeof(*(da)->arr)); \
} \
memmove(&((da)->arr[(index) + 1]), \
&((da)->arr[(index)]), \
((da)->length - (index))* sizeof(*(da)->arr)); \
(da)->length++; \
(da)->arr[(index)] = item; \
} while (0)
#ifndef HGL_ISO_C
#define hgl_da_get(da, i) \
({ \
int res__ = (i) % (int)((da)->length); \
(da)->arr[(res__ >= 0) ? res__ : res__ + (int)((da)->length)]; \
})
#define hgl_da_remove(da, index) \
({ \
assert((ssize_t)(index) >= 0 && (index) < (da)->length); \
__typeof__(*(da)->arr) item = (da)->arr[(index)]; \
memmove(&((da)->arr[(index)]), \
&((da)->arr[(index) + 1]), \
((da)->length - (index))* sizeof(*(da)->arr)); \
(da)->length--; \
item; \
})
#define hgl_da_remove_backswap(da, index) \
({ \
assert((ssize_t)(index) >= 0 && (index) < (da)->length); \
__typeof__(*(da)->arr) item = (da)->arr[(index)]; \
(da)->arr[(index)] = (da)->arr[--(da)->length]; \
item; \
})
//#define hgl_da_remove_backswap(da, index)
// ({
// assert((ssize_t)(index) >= 0 && (index) < (da)->length);
// __typeof__(*(da)->arr) item = (da)->arr[(index)];
// __typeof__(*(da)->arr) item_at_end = (da)->arr[(da)->length - 1];
// (da)->arr[(index)] = item_at_end;
// (da)->length--;
// item;
// })
#endif /* HGL_ISO_C */
/* helper for hgl_da_extend */
static inline uint32_t hgl_da_next_pow2_(uint32_t v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
#endif /* HGL_DA_H */