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 #ifndef UCX_ARRAY_LIST_H
38 #define UCX_ARRAY_LIST_H
39
40 #include "list.h"
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46
47
48
49
50 extern const unsigned cx_array_swap_sbo_size;
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 #define CX_ARRAY_DECLARE_SIZED(type, name, size_type) \
79 type * name; \
80 size_type name##_size; \
81 size_type name##_capacity
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 #define CX_ARRAY_DECLARE(type, name)
CX_ARRAY_DECLARE_SIZED(type, name,
size_t)
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 #define cx_array_initialize(array, capacity) \
133 array##_capacity = capacity; \
134 array##_size =
0; \
135 array = malloc(
sizeof(array[
0]) * capacity)
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 #define cx_array_initialize_a(allocator, array, capacity) \
160 array##_capacity = capacity; \
161 array##_size =
0; \
162 array = cxMalloc(allocator,
sizeof(array[
0]) * capacity)
163
164
165
166
167
168
169 struct cx_array_reallocator_s {
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 cx_attr_nodiscard
186 cx_attr_nonnull_arg(
4)
187 cx_attr_allocsize(
2,
3)
188 void *(*realloc)(
189 void *array,
190 size_t capacity,
191 size_t elem_size,
192 struct cx_array_reallocator_s *alloc
193 );
194
195
196
197
198 void *ptr1;
199
200
201
202 void *ptr2;
203
204
205
206 size_t int1;
207
208
209
210 size_t int2;
211 };
212
213
214
215
216 typedef struct cx_array_reallocator_s CxArrayReallocator;
217
218
219
220
221 extern CxArrayReallocator *cx_array_default_reallocator;
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241 CxArrayReallocator cx_array_reallocator(
242 const struct cx_allocator_s *allocator,
243 const void *stackmem
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 cx_attr_nonnull_arg(
1,
2,
3)
277 int cx_array_reserve(
278 void **array,
279 void *size,
280 void *capacity,
281 unsigned width,
282 size_t elem_size,
283 size_t elem_count,
284 CxArrayReallocator *reallocator
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 cx_attr_nonnull_arg(
1,
2,
3,
6)
320 int cx_array_copy(
321 void **target,
322 void *size,
323 void *capacity,
324 unsigned width,
325 size_t index,
326 const void *src,
327 size_t elem_size,
328 size_t elem_count,
329 CxArrayReallocator *reallocator
330 );
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346 #define cx_array_simple_copy_a(reallocator, array, index, src, count) \
347 cx_array_copy((
void**)&(array), &(array##_size), &(array##_capacity), \
348 sizeof(array##_size), index, src,
sizeof((array)[
0]), count, \
349 reallocator)
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364 #define cx_array_simple_copy(array, index, src, count) \
365 cx_array_simple_copy_a(
NULL, array, index, src, count)
366
367
368
369
370
371
372
373
374
375
376
377
378
379 #define cx_array_simple_reserve_a(reallocator, array, count) \
380 cx_array_reserve((
void**)&(array), &(array##_size), &(array##_capacity), \
381 sizeof(array##_size),
sizeof((array)[
0]), count, \
382 reallocator)
383
384
385
386
387
388
389
390
391
392
393
394
395 #define cx_array_simple_reserve(array, count) \
396 cx_array_simple_reserve_a(
NULL, array, count)
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 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \
422 cx_array_copy((
void**)(target), size, capacity,
sizeof(*(size)), \
423 *(size), elem, elem_size,
1, reallocator)
424
425
426
427
428
429
430
431
432
433
434
435
436
437 #define cx_array_simple_add_a(reallocator, array, elem) \
438 cx_array_simple_copy_a(reallocator, array, array##_size, &(elem),
1)
439
440
441
442
443
444
445
446
447
448
449
450
451 #define cx_array_simple_add(array, elem) \
452 cx_array_simple_add_a(cx_array_default_reallocator, array, elem)
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 cx_attr_nonnull_arg(
1,
2,
3,
5)
478 int cx_array_insert_sorted(
479 void **target,
480 size_t *size,
481 size_t *capacity,
482 cx_compare_func cmp_func,
483 const void *src,
484 size_t elem_size,
485 size_t elem_count,
486 CxArrayReallocator *reallocator
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 #define cx_array_add_sorted(target, size, capacity, elem_size, elem, cmp_func, reallocator) \
513 cx_array_insert_sorted((
void**)(target), size, capacity, cmp_func, elem, elem_size,
1, reallocator)
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528 #define cx_array_simple_add_sorted_a(reallocator, array, elem, cmp_func) \
529 cx_array_add_sorted(&array, &(array##_size), &(array##_capacity), \
530 sizeof((array)[
0]), &(elem), cmp_func, reallocator)
531
532
533
534
535
536
537
538
539
540
541
542
543
544 #define cx_array_simple_add_sorted(array, elem, cmp_func) \
545 cx_array_simple_add_sorted_a(
NULL, array, elem, cmp_func)
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561 #define cx_array_simple_insert_sorted_a(reallocator, array, src, n, cmp_func) \
562 cx_array_insert_sorted((
void**)(&array), &(array##_size), &(array##_capacity), \
563 cmp_func, src,
sizeof((array)[
0]), n, reallocator)
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578 #define cx_array_simple_insert_sorted(array, src, n, cmp_func) \
579 cx_array_simple_insert_sorted_a(
NULL, array, src, n, cmp_func)
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603 cx_attr_nonnull
604 size_t cx_array_binary_search_inf(
605 const void *arr,
606 size_t size,
607 size_t elem_size,
608 const void *elem,
609 cx_compare_func cmp_func
610 );
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628 cx_attr_nonnull
629 size_t cx_array_binary_search(
630 const void *arr,
631 size_t size,
632 size_t elem_size,
633 const void *elem,
634 cx_compare_func cmp_func
635 );
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659 cx_attr_nonnull
660 size_t cx_array_binary_search_sup(
661 const void *arr,
662 size_t size,
663 size_t elem_size,
664 const void *elem,
665 cx_compare_func cmp_func
666 );
667
668
669
670
671
672
673
674
675
676 cx_attr_nonnull
677 void cx_array_swap(
678 void *arr,
679 size_t elem_size,
680 size_t idx1,
681 size_t idx2
682 );
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700 cx_attr_nodiscard
701 cx_attr_malloc
702 cx_attr_dealloc(cxListFree,
1)
703 CxList *cxArrayListCreate(
704 const CxAllocator *allocator,
705 cx_compare_func comparator,
706 size_t elem_size,
707 size_t initial_capacity
708 );
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725 #define cxArrayListCreateSimple(elem_size, initial_capacity) \
726 cxArrayListCreate(
NULL,
NULL, elem_size, initial_capacity)
727
728 #ifdef __cplusplus
729 }
730 #endif
731
732 #endif
733