-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cvector_short.h
399 lines (325 loc) · 9.75 KB
/
cvector_short.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
/*
CVector 4.2.0 MIT Licensed vector (dynamic array) library in strict C89
http://www.robertwinkler.com/projects/cvector.html
http://www.robertwinkler.com/projects/cvector/
Besides the docs and all the Doxygen comments, see cvector_tests.c for
examples of how to use it or look at any of these other projects for
more practical examples:
https://github.com/rswinkle/C_Interpreter
https://github.com/rswinkle/CPIM2
https://github.com/rswinkle/spelling_game
https://github.com/rswinkle/c_bigint
http://portablegl.com/
The MIT License (MIT)
Copyright (c) 2011-2024 Robert Winkler
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.
*/
#ifndef CVECTOR_short_H
#define CVECTOR_short_H
#ifndef CVEC_SIZE_T
#include <stdlib.h>
#define CVEC_SIZE_T size_t
#endif
#ifndef CVEC_SZ
#define CVEC_SZ
typedef CVEC_SIZE_T cvec_sz;
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** Data structure for short vector. */
typedef struct cvector_short
{
short* a; /**< Array. */
cvec_sz size; /**< Current size (amount you use when manipulating array directly). */
cvec_sz capacity; /**< Allocated size of array; always >= size. */
} cvector_short;
extern cvec_sz CVEC_short_SZ;
int cvec_short(cvector_short* vec, cvec_sz size, cvec_sz capacity);
int cvec_init_short(cvector_short* vec, short* vals, cvec_sz num);
cvector_short* cvec_short_heap(cvec_sz size, cvec_sz capacity);
cvector_short* cvec_init_short_heap(short* vals, cvec_sz num);
int cvec_copyc_short(void* dest, void* src);
int cvec_copy_short(cvector_short* dest, cvector_short* src);
int cvec_push_short(cvector_short* vec, short a);
short cvec_pop_short(cvector_short* vec);
int cvec_extend_short(cvector_short* vec, cvec_sz num);
int cvec_insert_short(cvector_short* vec, cvec_sz i, short a);
int cvec_insert_array_short(cvector_short* vec, cvec_sz i, short* a, cvec_sz num);
short cvec_replace_short(cvector_short* vec, cvec_sz i, short a);
void cvec_erase_short(cvector_short* vec, cvec_sz start, cvec_sz end);
int cvec_reserve_short(cvector_short* vec, cvec_sz size);
#define cvec_shrink_to_fit_short(vec) cvec_set_cap_short((vec), (vec)->size)
int cvec_set_cap_short(cvector_short* vec, cvec_sz size);
void cvec_set_val_sz_short(cvector_short* vec, short val);
void cvec_set_val_cap_short(cvector_short* vec, short val);
short* cvec_back_short(cvector_short* vec);
void cvec_clear_short(cvector_short* vec);
void cvec_free_short_heap(void* vec);
void cvec_free_short(void* vec);
#ifdef __cplusplus
}
#endif
/* CVECTOR_short_H */
#endif
#ifdef CVECTOR_short_IMPLEMENTATION
cvec_sz CVEC_short_SZ = 50;
#define CVEC_short_ALLOCATOR(x) ((x+1) * 2)
#if defined(CVEC_MALLOC) && defined(CVEC_FREE) && defined(CVEC_REALLOC)
/* ok */
#elif !defined(CVEC_MALLOC) && !defined(CVEC_FREE) && !defined(CVEC_REALLOC)
/* ok */
#else
#error "Must define all or none of CVEC_MALLOC, CVEC_FREE, and CVEC_REALLOC."
#endif
#ifndef CVEC_MALLOC
#include <stdlib.h>
#define CVEC_MALLOC(sz) malloc(sz)
#define CVEC_REALLOC(p, sz) realloc(p, sz)
#define CVEC_FREE(p) free(p)
#endif
#ifndef CVEC_MEMMOVE
#include <string.h>
#define CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz)
#endif
#ifndef CVEC_ASSERT
#include <assert.h>
#define CVEC_ASSERT(x) assert(x)
#endif
cvector_short* cvec_short_heap(cvec_sz size, cvec_sz capacity)
{
cvector_short* vec;
if (!(vec = (cvector_short*)CVEC_MALLOC(sizeof(cvector_short)))) {
CVEC_ASSERT(vec != NULL);
return NULL;
}
vec->size = size;
vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_short_SZ;
if (!(vec->a = (short*)CVEC_MALLOC(vec->capacity*sizeof(short)))) {
CVEC_ASSERT(vec->a != NULL);
CVEC_FREE(vec);
return NULL;
}
return vec;
}
cvector_short* cvec_init_short_heap(short* vals, cvec_sz num)
{
cvector_short* vec;
if (!(vec = (cvector_short*)CVEC_MALLOC(sizeof(cvector_short)))) {
CVEC_ASSERT(vec != NULL);
return NULL;
}
vec->capacity = num + CVEC_short_SZ;
vec->size = num;
if (!(vec->a = (short*)CVEC_MALLOC(vec->capacity*sizeof(short)))) {
CVEC_ASSERT(vec->a != NULL);
CVEC_FREE(vec);
return NULL;
}
CVEC_MEMMOVE(vec->a, vals, sizeof(short)*num);
return vec;
}
int cvec_short(cvector_short* vec, cvec_sz size, cvec_sz capacity)
{
vec->size = size;
vec->capacity = (capacity > vec->size || (vec->size && capacity == vec->size)) ? capacity : vec->size + CVEC_short_SZ;
if (!(vec->a = (short*)CVEC_MALLOC(vec->capacity*sizeof(short)))) {
CVEC_ASSERT(vec->a != NULL);
vec->size = vec->capacity = 0;
return 0;
}
return 1;
}
int cvec_init_short(cvector_short* vec, short* vals, cvec_sz num)
{
vec->capacity = num + CVEC_short_SZ;
vec->size = num;
if (!(vec->a = (short*)CVEC_MALLOC(vec->capacity*sizeof(short)))) {
CVEC_ASSERT(vec->a != NULL);
vec->size = vec->capacity = 0;
return 0;
}
CVEC_MEMMOVE(vec->a, vals, sizeof(short)*num);
return 1;
}
int cvec_copyc_short(void* dest, void* src)
{
cvector_short* vec1 = (cvector_short*)dest;
cvector_short* vec2 = (cvector_short*)src;
vec1->a = NULL;
vec1->size = 0;
vec1->capacity = 0;
return cvec_copy_short(vec1, vec2);
}
int cvec_copy_short(cvector_short* dest, cvector_short* src)
{
short* tmp = NULL;
if (!(tmp = (short*)CVEC_REALLOC(dest->a, src->capacity*sizeof(short)))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
dest->a = tmp;
CVEC_MEMMOVE(dest->a, src->a, src->size*sizeof(short));
dest->size = src->size;
dest->capacity = src->capacity;
return 1;
}
int cvec_push_short(cvector_short* vec, short a)
{
short* tmp;
cvec_sz tmp_sz;
if (vec->capacity > vec->size) {
vec->a[vec->size++] = a;
} else {
tmp_sz = CVEC_short_ALLOCATOR(vec->capacity);
if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*tmp_sz))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->a[vec->size++] = a;
vec->capacity = tmp_sz;
}
return 1;
}
short cvec_pop_short(cvector_short* vec)
{
return vec->a[--vec->size];
}
short* cvec_back_short(cvector_short* vec)
{
return &vec->a[vec->size-1];
}
int cvec_extend_short(cvector_short* vec, cvec_sz num)
{
short* tmp;
cvec_sz tmp_sz;
if (vec->capacity < vec->size + num) {
tmp_sz = vec->capacity + num + CVEC_short_SZ;
if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*tmp_sz))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->capacity = tmp_sz;
}
vec->size += num;
return 1;
}
int cvec_insert_short(cvector_short* vec, cvec_sz i, short a)
{
short* tmp;
cvec_sz tmp_sz;
if (vec->capacity > vec->size) {
CVEC_MEMMOVE(&vec->a[i+1], &vec->a[i], (vec->size-i)*sizeof(short));
vec->a[i] = a;
} else {
tmp_sz = CVEC_short_ALLOCATOR(vec->capacity);
if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*tmp_sz))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
CVEC_MEMMOVE(&vec->a[i+1], &vec->a[i], (vec->size-i)*sizeof(short));
vec->a[i] = a;
vec->capacity = tmp_sz;
}
vec->size++;
return 1;
}
int cvec_insert_array_short(cvector_short* vec, cvec_sz i, short* a, cvec_sz num)
{
short* tmp;
cvec_sz tmp_sz;
if (vec->capacity < vec->size + num) {
tmp_sz = vec->capacity + num + CVEC_short_SZ;
if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*tmp_sz))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->capacity = tmp_sz;
}
CVEC_MEMMOVE(&vec->a[i+num], &vec->a[i], (vec->size-i)*sizeof(short));
CVEC_MEMMOVE(&vec->a[i], a, num*sizeof(short));
vec->size += num;
return 1;
}
short cvec_replace_short(cvector_short* vec, cvec_sz i, short a)
{
short tmp = vec->a[i];
vec->a[i] = a;
return tmp;
}
void cvec_erase_short(cvector_short* vec, cvec_sz start, cvec_sz end)
{
cvec_sz d = end - start + 1;
CVEC_MEMMOVE(&vec->a[start], &vec->a[end+1], (vec->size-1-end)*sizeof(short));
vec->size -= d;
}
int cvec_reserve_short(cvector_short* vec, cvec_sz size)
{
short* tmp;
if (vec->capacity < size) {
if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*(size+CVEC_short_SZ)))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->capacity = size + CVEC_short_SZ;
}
return 1;
}
int cvec_set_cap_short(cvector_short* vec, cvec_sz size)
{
short* tmp;
if (size < vec->size) {
vec->size = size;
}
if (!(tmp = (short*)CVEC_REALLOC(vec->a, sizeof(short)*size))) {
CVEC_ASSERT(tmp != NULL);
return 0;
}
vec->a = tmp;
vec->capacity = size;
return 1;
}
void cvec_set_val_sz_short(cvector_short* vec, short val)
{
cvec_sz i;
for (i=0; i<vec->size; i++) {
vec->a[i] = val;
}
}
void cvec_set_val_cap_short(cvector_short* vec, short val)
{
cvec_sz i;
for (i=0; i<vec->capacity; i++) {
vec->a[i] = val;
}
}
void cvec_clear_short(cvector_short* vec) { vec->size = 0; }
void cvec_free_short_heap(void* vec)
{
cvector_short* tmp = (cvector_short*)vec;
if (!tmp) return;
CVEC_FREE(tmp->a);
CVEC_FREE(tmp);
}
void cvec_free_short(void* vec)
{
cvector_short* tmp = (cvector_short*)vec;
CVEC_FREE(tmp->a);
tmp->size = 0;
tmp->capacity = 0;
}
#endif