-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynarrlo.c
458 lines (323 loc) · 9.47 KB
/
dynarrlo.c
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
//
// Created by easy on 14.05.23.
//
#include "dynarrlo.h"
#include <stdbool.h>
#include <string.h>
// Returns the lesser of the two arguments.
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
// Returns the greater of the two arguments.
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
static size_t itemsToBytes(size_t n) {
return n * sizeof(void *);
}
static size_t grownCapacity(size_t n) {
return n + n / 2;
}
static bool growthRequired(const DynarrLO *d) {
return d->length >= d->capacity;
}
static DAL_ERROR setCapacity(DynarrLO *d, size_t capacity) {
capacity = MAX(capacity, DAL_MIN_CAPACITY);
if (capacity == d->capacity)
return DAL_OK;
// Allocate 1 padding element
void **memory = d->realloc(d->array, itemsToBytes(capacity + 1));
if (!memory)
return DAL_ALLOCFAIL;
// Initialise padding element to zero
memory[capacity] = NULL;
d->array = memory;
d->length = MIN(d->length, capacity);
d->capacity = capacity;
return DAL_OK;
}
/**
* Will grow the array if it is necessary to do so. Does nothing on failure.
* @return True iff memory allocation failed.
*/
static bool growArray(DynarrLO *d) {
return growthRequired(d) &&
(d->error = setCapacity(d, grownCapacity(d->capacity)));
}
/**
* Will grow the array if necessary so it can hold at least num elements.
* Does nothing on failure.
* @return True iff memory allocation failed.
*/
static bool growArrayArbitrary(DynarrLO *d, size_t num) {
size_t size = MAX(grownCapacity(d->capacity), num);
return num > d->capacity && (d->error = setCapacity(d, size));
}
DAL_ERROR dal_createDynarrLO(DynarrLO *d,
size_t capacity,
void *(*realloc) (void *, size_t),
void (*free) (void *)) {
if (!d || !realloc || !free)
return DAL_NULLARG;
capacity = MAX(capacity, DAL_MIN_CAPACITY);
// Allocate 1 padding element
void **memory = realloc(NULL, itemsToBytes(capacity + 1));
if (!memory)
return DAL_ALLOCFAIL;
// Initialise padding element to zero
memory[capacity] = NULL;
d->array = memory;
d->length = 0;
d->capacity = capacity;
d->error = DAL_OK;
d->realloc = realloc;
d->free = free;
return DAL_OK;
}
void dal_destroyDynarrLO(DynarrLO *d) {
d->free(d->array);
*d = (DynarrLO) {0};
}
void dal_zeroOut(DynarrLO *d,
size_t iStart,
size_t iEnd) {
d->error = (iStart >= d->capacity) | (iEnd > d->capacity);
iEnd = MIN(iEnd, d->capacity);
iStart = MIN(iStart, iEnd);
memset(d->array + iStart, 0, itemsToBytes(iEnd - iStart));
}
void dal_setCapacity(DynarrLO *d, size_t capacity) {
d->error = setCapacity(d, capacity);
}
void dal_shrinkToFit(DynarrLO *d) {
d->error = setCapacity(d, d->length);
}
void dal_setLength(DynarrLO *d, size_t length) {
d->error = length > d->capacity;
d->length = MIN(length, d->capacity);
}
void dal_write(DynarrLO *d,
size_t index,
void *obj) {
d->error = index >= d->length;
index = MIN(index, d->capacity);
d->array[index] = obj;
d->array[d->capacity] = NULL;
}
void *dal_writeInst(DynarrLO *d,
size_t index,
size_t size) {
d->error = index >= d->length;
if (index >= d->capacity)
return NULL;
void *obj = d->realloc(NULL, size);
if (!obj) {
d->error = DAL_ALLOCFAIL;
return NULL;
}
return d->array[index] = obj;
}
void dal_append(DynarrLO *d, void *obj) {
if (growArray(d))
return;
d->error = DAL_OK;
d->array[d->length++] = obj;
}
void *dal_appendInst(DynarrLO *d, size_t size) {
d->error = DAL_OK;
void *obj = d->realloc(NULL, size);
if (!obj || growArray(d)) {
d->error = DAL_ALLOCFAIL;
d->free(obj);
return NULL;
}
return d->array[d->length++] = obj;
}
void dal_shift(DynarrLO *d,
size_t index,
size_t shift) {
DAL_ERROR error = index >= d->length;
d->error = error;
if (error || growArrayArbitrary(d, d->length + shift))
return;
memmove(d->array + index + shift,
d->array + index,
itemsToBytes(d->length - index));
d->length += shift;
}
void dal_insert(DynarrLO *d,
size_t index,
void *obj) {
DAL_ERROR error = index > d->length;
d->error = error;
if (error || growArray(d))
return;
memmove(d->array + index + 1,
d->array + index,
itemsToBytes(d->length - index));
d->array[index] = obj;
++d->length;
}
void dal_insertMany(DynarrLO *d,
size_t index,
void **objs,
size_t num) {
DAL_ERROR error = index > d->length;
d->error = error;
if (error || growArrayArbitrary(d, d->length + num))
return;
memmove(d->array + index + num,
d->array + index,
itemsToBytes(d->length - index));
memmove(d->array + index,
objs,
itemsToBytes(num));
d->length += num;
}
void *dal_get(DynarrLO *d, size_t index) {
d->error = index >= d->length;
index = MIN(index, d->capacity);
return d->array[index];
}
void *dal_getr(DynarrLO *d, size_t index) {
index += d->length * (index >= d->capacity);
d->error = index >= d->length;
index = MIN(index, d->capacity);
return d->array[index];
}
void *dal_getLast(DynarrLO *d) {
size_t normlen = d->length - !!d->length;
void *obj1 = NULL;
void *obj2 = d->array[normlen];
void *obj = d->length ? obj2 : obj1;
d->error = !d->length;
return obj;
}
void *dal_pop(DynarrLO *d) {
d->error = !d->length;
size_t normlen = d->length - !!d->length;
void *obj1 = NULL;
void *obj2 = d->array[normlen];
void *obj = d->length ? obj2 : obj1;
d->length = normlen;
return obj;
}
void dal_freeItem(DynarrLO *d, size_t index) {
d->error = index >= d->length;
index = MIN(index, d->capacity);
d->free(d->array[index]);
d->array[index] = NULL;
}
void dal_freeItems(DynarrLO *d,
size_t iStart,
size_t iEnd) {
d->error = (iStart >= d->length) | (iEnd > d->length);
iEnd = MIN(iEnd, d->capacity);
for (size_t i = iStart; i < iEnd; ++i) {
d->free(d->array[i]);
d->array[i] = NULL;
}
}
void dal_fremoveLast(DynarrLO *d) {
d->error = !d->length;
size_t normlen = d->length - !!d->length;
d->free(d->array[normlen]);
d->array[normlen] = NULL;
d->length = normlen;
}
void dal_removeLast(DynarrLO *d) {
d->error = !d->length;
d->length -= !!d->length;
}
void dal_removeLastMany(DynarrLO *d, size_t amount) {
d->error = amount > d->length;
amount = MIN(amount, d->length);
d->length -= amount;
}
void dal_remove(DynarrLO *d, size_t index) {
if ((d->error = index >= d->length))
return;
memmove(d->array + index,
d->array + index + 1,
itemsToBytes(d->length - (index + 1)));
--d->length;
}
void dal_removeMany(DynarrLO *d,
size_t iStart,
size_t iEnd) {
d->error = (iStart >= d->length) | (iEnd > d->length);
iEnd = MIN(iEnd, d->length);
iStart = MIN(iStart, iEnd);
memmove(d->array + iStart,
d->array + iEnd,
itemsToBytes(d->length - iEnd));
d->length -= iEnd - iStart;
}
#if DAL_PRIMITIVE_SUPPORT
void dal_pwrite(DynarrLO *d,
size_t index,
size_t val) {
d->error = index >= d->length;
index = MIN(index, d->capacity);
d->arrayp[index] = val;
d->arrayp[d->capacity] = 0;
}
void dal_pappend(DynarrLO *d, size_t val) {
if (growArray(d))
return;
d->arrayp[d->length++] = val;
}
void dal_pinsert(DynarrLO *d,
size_t index,
size_t val) {
DAL_ERROR error = index > d->length;
d->error = error;
if (error || growArray(d))
return;
memmove(d->array + index + 1,
d->array + index,
itemsToBytes(d->length - index));
d->arrayp[index] = val;
++d->length;
}
void dal_pinsertMany(DynarrLO *d,
size_t index,
size_t *vals,
size_t num) {
DAL_ERROR error = index > d->length;
d->error = error;
if (error || growArrayArbitrary(d, d->length + num))
return;
memmove(d->array + index + num,
d->array + index,
itemsToBytes(d->length - index));
memmove(d->array + index,
vals,
itemsToBytes(num));
d->length += num;
}
size_t dal_pget(DynarrLO *d, size_t index) {
d->error = index >= d->length;
index = MIN(index, d->capacity);
return d->arrayp[index];
}
size_t dal_pgetr(DynarrLO *d, size_t index) {
index += d->length * (index >= d->capacity);
d->error = index >= d->length;
index = MIN(index, d->capacity);
return d->arrayp[index];
}
size_t dal_pgetLast(DynarrLO *d) {
size_t normlen = d->length - !!d->length;
size_t val1 = 0;
size_t val2 = d->arrayp[normlen];
size_t val = d->length ? val2 : val1;
d->error = !d->length;
return val;
}
size_t dal_ppop(DynarrLO *d) {
d->error = !d->length;
size_t normlen = d->length - !!d->length;
size_t val1 = 0;
size_t val2 = d->arrayp[normlen];
size_t val = d->length ? val2 : val1;
d->length = normlen;
return val;
}
#endif // DAL_PRIMITIVE_SUPPORT