-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbgen.h
5235 lines (4911 loc) · 148 KB
/
bgen.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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// https://github.com/tidwall/bgen
//
// Copyright 2024 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//
// Bgen - B-tree collection generator for C
//
// For a complete list of options visit:
// https://github.com/tidwall/bgen#options
// The API namespace.
// This is the prefix for all functions calls, and is also and the name of the
// root node structure.
#ifndef BGEN_NAME
#error BGEN_NAME required
#define BGEN_NAME unnamed_bgen /* unused placeholder */
#endif
// macro concatenate
#define BGEN_CC(a, b) a ## b
#define BGEN_C(a, b) BGEN_CC(a, b)
// API symbols are the calls available to the user.
#define BGEN_API(name) BGEN_C(BGEN_C(BGEN_NAME, _), name)
// Internal symbols are prefixed with an underscore.
// These should not be directly called by the user.
#define BGEN_SYM(name) BGEN_C(BGEN_C(BGEN_C(_, BGEN_NAME), _internal_), name)
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
// The internal item type. This is used as both the value type and the key
// type, and can be pretty much anything.
#ifndef BGEN_TYPE
#error BGEN_TYPE required
#define BGEN_TYPE int /* unused placeholder */
#endif
// The "fanout" is maximum number of child nodes that a branch node may have.
// For example a fanout of 4 is equivalent to a 2-3-4 tree where a branch may
// have 2, 3, or 4 children and branches and leaves may have 1, 2, or 3 items.
// This implementation clamps the fanout to the range of 4 to 4096, and also
// rounds it down to the nearest even number. Such that a value of 9 becomes 8.
#ifndef BGEN_FANOUT
#define BGEN_FANOUTUSED 16
#elif BGEN_FANOUT < 4
#define BGEN_FANOUTUSED 4
#elif BGEN_FANOUT > 4096
#define BGEN_FANOUTUSED 4096
#elif BGEN_FANOUT % 2 == 1
#define BGEN_FANOUTUSED (BGEN_FANOUT-1)
#else
#define BGEN_FANOUTUSED BGEN_FANOUT
#endif
// MAXITEMS and MINITEMS are the minimum and maximum number of items allowed in
// each node, respectively.
#define BGEN_MAXITEMS (BGEN_FANOUTUSED-1)
#define BGEN_MINITEMS (BGEN_MAXITEMS/2)
// Estimated compile time worst case max height for a 64-bit system.
// In other words, this is the maximum possible height of a tree when it's
// fully loaded with SIZE_MAX items.
#if (BGEN_MINITEMS+1) >= 128
#define BGEN_MAXHEIGHT 9 /* pow(128,9+1) >= 18446744073709551615UL */
#elif (BGEN_MINITEMS+1) >= 64
#define BGEN_MAXHEIGHT 10 /* pow(64,10+1) >= 18446744073709551615UL */
#elif (BGEN_MINITEMS+1) >= 32
#define BGEN_MAXHEIGHT 12 /* pow(32,12+1) >= 18446744073709551615UL */
#elif (BGEN_MINITEMS+1) >= 16
#define BGEN_MAXHEIGHT 15 /* pow(16,15+1) >= 18446744073709551615UL */
#elif (BGEN_MINITEMS+1) >= 8
#define BGEN_MAXHEIGHT 21 /* pow(8,21+1) >= 18446744073709551615UL */
#elif (BGEN_MINITEMS+1) >= 4
#define BGEN_MAXHEIGHT 31 /* pow(4,31+1) >= 18446744073709551615UL */
#else
#define BGEN_MAXHEIGHT 63
#endif
#define BGEN_INLINE inline
#ifdef __GNUC__
#define BGEN_NOINLINE __attribute__((noinline))
#else
#define BGEN_NOINLINE
#endif
// Provide a custom allocator using BGEN_MALLOC and BGEN_FREE.
// Such as:
//
// #define BGEN_MALLOC return my_malloc(size);
// #define BGEN_FREE my_free(ptr);
//
// This will ensure that the tree will always use my_malloc/my_free instead of
// the standard malloc/free.
#if !defined(BGEN_MALLOC) || !defined(BGEN_FREE)
#include <stdlib.h>
#ifndef BGEN_MALLOC
#define BGEN_MALLOC return malloc(size);
#endif
#ifndef BGEN_FREE
#define BGEN_FREE free(ptr);
#endif
#endif
#ifndef BGEN_EXTERN
#ifdef BGEN_HEADER
#define BGEN_EXTERN extern
#else
#define BGEN_EXTERN static
#endif
#endif
// Enable Spatial B-tree support
#ifdef BGEN_SPATIAL
#ifndef BGEN_ITEMRECT
#error \
BGEN_ITEMRECT is required when BGEN_SPATIAL is defined. \
Visit https://github.com/tidwall/bgen for more information.
#endif
#else
#ifdef BGEN_ITEMRECT
#error \
BGEN_ITEMRECT must not be defined withou BGEN_SPATIAL. \
Visit https://github.com/tidwall/bgen for more information.
#endif
#endif
// Number of dimensions for Spatial B-tree
#ifndef BGEN_DIMS
#define BGEN_DIMS 2
#elif BGEN_DIMS < 1 || BGEN_DIMS > 4096
#error \
BGEN_DIMS must be between 1 and 4096 \
Visit https://github.com/tidwall/bgen for more information.
#endif
// Rectangle coordinate type for Spatial B-tree
#ifndef BGEN_RTYPE
#define BGEN_RTYPE double
#endif
// A path hint is a search optimization.
// It's most useful when bsearching, and is turned on by default when
// BGEN_BSEARCH is provided.
// This implementation uses one thread local path hint per each btree namespace.
// See https://github.com/tidwall/btree/blob/master/PATH_HINT.md
#if defined(BGEN_BSEARCH) && BGEN_FANOUT < 256
#ifndef BGEN_PATHHINT
#define BGEN_PATHHINT
#endif
#endif
#ifdef BGEN_NOPATHHINT
#undef BGEN_PATHHINT
#endif
// Convenient aliases to common types
#define BGEN_NODE struct BGEN_NAME
#define BGEN_ITEM BGEN_TYPE
#define BGEN_ITER struct BGEN_API(iter)
#define BGEN_SNODE struct BGEN_SYM(snode)
#define BGEN_RECT struct BGEN_SYM(rect)
// The following status codes are private to this file only.
// Users should use the prefixed version such as bt_INSERTED as defined in the
// enum below.
#define BGEN_INSERTED 1 // New item was inserted
#define BGEN_REPLACED 2 // Item replaced an existing item
#define BGEN_DELETED 3 // Item was successfully deleted
#define BGEN_FOUND 4 // Item was successfully accessed
#define BGEN_NOTFOUND 5 // Item was not found
#define BGEN_OUTOFORDER 6 // Item is out of order
#define BGEN_FINISHED 7 // Callback iterator returned all items
#define BGEN_STOPPED 8 // Callback iterator was stopped early
#define BGEN_COPIED 9 // Tree was copied: `clone`, `copy`
#define BGEN_NOMEM 10 // Out of memory
#define BGEN_UNSUPPORTED 11 // Operation not supported
#ifndef BGEN_SOURCE
// Definitions
enum BGEN_API(status) {
BGEN_C(BGEN_NAME, _INSERTED) = BGEN_INSERTED,
BGEN_C(BGEN_NAME, _REPLACED) = BGEN_REPLACED,
BGEN_C(BGEN_NAME, _DELETED) = BGEN_DELETED,
BGEN_C(BGEN_NAME, _FOUND) = BGEN_FOUND,
BGEN_C(BGEN_NAME, _NOTFOUND) = BGEN_NOTFOUND,
BGEN_C(BGEN_NAME, _OUTOFORDER) = BGEN_OUTOFORDER,
BGEN_C(BGEN_NAME, _FINISHED) = BGEN_FINISHED,
BGEN_C(BGEN_NAME, _STOPPED) = BGEN_STOPPED,
BGEN_C(BGEN_NAME, _COPIED) = BGEN_COPIED,
BGEN_C(BGEN_NAME, _NOMEM) = BGEN_NOMEM,
BGEN_C(BGEN_NAME, _UNSUPPORTED) = BGEN_UNSUPPORTED,
};
BGEN_NODE;
BGEN_ITER;
BGEN_EXTERN int BGEN_API(get)(BGEN_NODE **root, BGEN_ITEM key,
BGEN_ITEM *item_out, void *udata);
BGEN_EXTERN int BGEN_API(insert)(BGEN_NODE **root, BGEN_ITEM item,
BGEN_ITEM *item_out, void *udata);
BGEN_EXTERN int BGEN_API(delete)(BGEN_NODE **root, BGEN_ITEM key,
BGEN_ITEM *item_out, void *udata);
BGEN_EXTERN bool BGEN_API(contains)(BGEN_NODE **root, BGEN_ITEM key,
void *udata);
BGEN_EXTERN void BGEN_API(clear)(BGEN_NODE **root, void *udata);
BGEN_EXTERN int BGEN_API(front)(BGEN_NODE **root, BGEN_ITEM *item_out,
void *udata);
BGEN_EXTERN int BGEN_API(back)(BGEN_NODE **root, BGEN_ITEM *item_out,
void *udata);
BGEN_EXTERN int BGEN_API(pop_front)(BGEN_NODE **root, BGEN_ITEM *item_out,
void *udata);
BGEN_EXTERN int BGEN_API(pop_back)(BGEN_NODE **root, BGEN_ITEM *item_out,
void *udata);
BGEN_EXTERN int BGEN_API(push_front)(BGEN_NODE **root, BGEN_ITEM item,
void *udata);
BGEN_EXTERN int BGEN_API(push_back)(BGEN_NODE **root, BGEN_ITEM item,
void *udata);
BGEN_EXTERN int BGEN_API(copy)(BGEN_NODE **root, BGEN_NODE **newroot,
void *udata);
BGEN_EXTERN int BGEN_API(clone)(BGEN_NODE **root, BGEN_NODE **newroot,
void *udata);
BGEN_EXTERN int BGEN_API(compare)(BGEN_ITEM a, BGEN_ITEM b, void *udata);
BGEN_EXTERN bool BGEN_API(less)(BGEN_ITEM a, BGEN_ITEM b, void *udata);
// Optimized for counted B-trees (works with indexes) (rank=index_of,
// select=get_at)
BGEN_EXTERN int BGEN_API(insert_at)(BGEN_NODE **root, size_t index,
BGEN_ITEM item, void *udata);
BGEN_EXTERN int BGEN_API(delete_at)(BGEN_NODE **root, size_t index,
BGEN_ITEM *item_out, void *udata);
BGEN_EXTERN int BGEN_API(replace_at)(BGEN_NODE **root, size_t index,
BGEN_ITEM item, BGEN_ITEM *item_out, void *udata);
BGEN_EXTERN int BGEN_API(get_at)(BGEN_NODE **root, size_t index,
BGEN_ITEM *item_out, void *udata);
BGEN_EXTERN int BGEN_API(index_of)(BGEN_NODE **root, BGEN_ITEM key,
size_t *index, void *udata);
BGEN_EXTERN size_t BGEN_API(count)(BGEN_NODE **root, void *udata);
// Cursor Iterators
BGEN_EXTERN void BGEN_API(iter_init)(BGEN_NODE **root, BGEN_ITER **iter,
void *udata);
BGEN_EXTERN int BGEN_API(iter_status)(BGEN_ITER *iter);
BGEN_EXTERN bool BGEN_API(iter_valid)(BGEN_ITER *iter);
BGEN_EXTERN void BGEN_API(iter_release)(BGEN_ITER *iter);
BGEN_EXTERN void BGEN_API(iter_item)(BGEN_ITER *iter, BGEN_ITEM *item);
BGEN_EXTERN void BGEN_API(iter_next)(BGEN_ITER *iter);
// Curstor iterator seekers
BGEN_EXTERN void BGEN_API(iter_seek)(BGEN_ITER *iter, BGEN_ITEM key);
BGEN_EXTERN void BGEN_API(iter_seek_desc)(BGEN_ITER *iter, BGEN_ITEM key);
BGEN_EXTERN void BGEN_API(iter_scan)(BGEN_ITER *iter);
BGEN_EXTERN void BGEN_API(iter_scan_desc)(BGEN_ITER *iter);
BGEN_EXTERN void BGEN_API(iter_intersects)(BGEN_ITER *iter, BGEN_RTYPE min[],
BGEN_RTYPE max[]);
BGEN_EXTERN void BGEN_API(iter_nearby)(BGEN_ITER *iter, void *target,
BGEN_RTYPE(*dist)(BGEN_RTYPE min[BGEN_DIMS], BGEN_RTYPE max[BGEN_DIMS],
void *target, void *udata));
BGEN_EXTERN void BGEN_API(iter_seek_at)(BGEN_ITER *iter, size_t index);
BGEN_EXTERN void BGEN_API(iter_seek_at_desc)(BGEN_ITER *iter, size_t index);
// Callback iterators
BGEN_EXTERN int BGEN_API(scan)(BGEN_NODE **root, bool(*iter)(BGEN_ITEM item,
void *udata), void *udata);
BGEN_EXTERN int BGEN_API(scan_desc)(BGEN_NODE **root,
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
BGEN_EXTERN int BGEN_API(seek)(BGEN_NODE **root, BGEN_ITEM key,
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
BGEN_EXTERN int BGEN_API(seek_desc)(BGEN_NODE **root, BGEN_ITEM key,
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
BGEN_EXTERN int BGEN_API(intersects)(BGEN_NODE **root,
BGEN_RTYPE min[BGEN_DIMS], BGEN_RTYPE max[BGEN_DIMS],
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
BGEN_EXTERN int BGEN_API(nearby)(BGEN_NODE **root, void *target,
BGEN_RTYPE(*dist)(BGEN_RTYPE min[BGEN_DIMS], BGEN_RTYPE max[BGEN_DIMS],
void *target, void *udata), bool(*iter)(BGEN_ITEM item, void *udata),
void *udata);
BGEN_EXTERN int BGEN_API(seek_at)(BGEN_NODE **root, size_t index,
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
BGEN_EXTERN int BGEN_API(seek_at_desc)(BGEN_NODE **root, size_t index,
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
// General information
BGEN_EXTERN int BGEN_API(feat_maxitems)(void);
BGEN_EXTERN int BGEN_API(feat_minitems)(void);
BGEN_EXTERN int BGEN_API(feat_maxheight)(void);
BGEN_EXTERN int BGEN_API(feat_fanout)(void);
BGEN_EXTERN bool BGEN_API(feat_counted)(void);
BGEN_EXTERN bool BGEN_API(feat_spatial)(void);
BGEN_EXTERN bool BGEN_API(feat_ordered)(void);
BGEN_EXTERN bool BGEN_API(feat_cow)(void);
BGEN_EXTERN bool BGEN_API(feat_atomics)(void);
BGEN_EXTERN bool BGEN_API(feat_bsearch)(void);
BGEN_EXTERN bool BGEN_API(feat_pathhint)(void);
BGEN_EXTERN int BGEN_API(feat_dims)(void);
BGEN_EXTERN size_t BGEN_API(height)(BGEN_NODE **root, void *udata);
BGEN_EXTERN bool BGEN_API(sane)(BGEN_NODE **root, void *udata);
BGEN_EXTERN void BGEN_API(rect)(BGEN_NODE **root, BGEN_RTYPE min[BGEN_DIMS],
BGEN_RTYPE max[BGEN_DIMS], void *udata);
// Read functions that return items which are intended to be mutated.
// These perform copy-on-write on internal nodes and copies the items before
// returning them to the user.
BGEN_EXTERN int BGEN_API(get_mut)(BGEN_NODE **root, BGEN_ITEM key,
BGEN_ITEM *item_out, void *udata);
BGEN_EXTERN int BGEN_API(get_at_mut)(BGEN_NODE **root, size_t index,
BGEN_ITEM *item_out, void *udata);
BGEN_EXTERN int BGEN_API(front_mut)(BGEN_NODE **root, BGEN_ITEM *item_out,
void *udata);
BGEN_EXTERN int BGEN_API(back_mut)(BGEN_NODE **root, BGEN_ITEM *item_out,
void *udata);
BGEN_EXTERN void BGEN_API(iter_init_mut)(BGEN_NODE **root, BGEN_ITER **iter,
void *udata);
BGEN_EXTERN int BGEN_API(scan_mut)(BGEN_NODE **root, bool(*iter)(BGEN_ITEM item,
void *udata), void *udata);
BGEN_EXTERN int BGEN_API(scan_desc_mut)(BGEN_NODE **root,
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
BGEN_EXTERN int BGEN_API(seek_mut)(BGEN_NODE **root, BGEN_ITEM key,
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
BGEN_EXTERN int BGEN_API(seek_desc_mut)(BGEN_NODE **root, BGEN_ITEM key,
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
BGEN_EXTERN int BGEN_API(intersects_mut)(BGEN_NODE **root,
BGEN_RTYPE min[BGEN_DIMS], BGEN_RTYPE max[BGEN_DIMS],
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
BGEN_EXTERN int BGEN_API(nearby_mut)(BGEN_NODE **root, void *target,
BGEN_RTYPE(*dist)(BGEN_RTYPE min[BGEN_DIMS], BGEN_RTYPE max[BGEN_DIMS],
void *target, void *udata), bool(*iter)(BGEN_ITEM item, void *udata),
void *udata);
BGEN_EXTERN int BGEN_API(seek_at_mut)(BGEN_NODE **root, size_t index,
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
BGEN_EXTERN int BGEN_API(seek_at_desc_mut)(BGEN_NODE **root, size_t index,
bool(*iter)(BGEN_ITEM item, void *udata), void *udata);
// Access direct mutable references... use with care.
BGEN_EXTERN int BGEN_API(get_mut_ref)(BGEN_NODE **root, BGEN_ITEM key,
BGEN_ITEM **item, void *udata);
#endif // !BGEN_SOURCE
#ifndef BGEN_HEADER
// IMPLEMENTATION
BGEN_NOINLINE
static void *BGEN_SYM(malloc)(size_t size, void *udata) {
(void)size, (void)udata;
BGEN_MALLOC
}
static void BGEN_SYM(free)(void *ptr, void *udata) {
(void)ptr, (void)udata;
BGEN_FREE
}
#ifdef BGEN_LESS
#ifdef BGEN_COMPARE
#error \
BGEN_COMPARE and BGEN_LESS cannot be both defined
#endif
#ifdef BGEN_KEYED
// Using nested compare for keyed collection type
static bool BGEN_SYM(less)(BGEN_ITEM a2, BGEN_ITEM b2, void *udata) {
BGEN_KEYTYPE a = a2.key, b = b2.key;
(void)a, (void)b, (void)udata;
BGEN_LESS
}
#else
static bool BGEN_SYM(less)(BGEN_ITEM a, BGEN_ITEM b, void *udata) {
(void)a, (void)b, (void)udata;
BGEN_LESS
}
#endif
static int BGEN_SYM(compare)(BGEN_ITEM a, BGEN_ITEM b, void *udata) {
return BGEN_SYM(less)(a, b, udata) ? -1 :
BGEN_SYM(less)(b, a, udata) ? 1 :
0;
}
#elif defined(BGEN_COMPARE)
#ifdef BGEN_KEYED
// Using nested compare for keyed collection type
static int BGEN_SYM(compare)(BGEN_ITEM a2, BGEN_ITEM b2, void *udata) {
BGEN_KEYTYPE a = a2.key, b = b2.key;
(void)a, (void)b, (void)udata;
BGEN_COMPARE
}
#else
static int BGEN_SYM(compare)(BGEN_ITEM a, BGEN_ITEM b, void *udata) {
(void)a, (void)b, (void)udata;
BGEN_COMPARE
}
#endif
static bool BGEN_SYM(less)(BGEN_ITEM a, BGEN_ITEM b, void *udata) {
return BGEN_SYM(compare)(a, b, udata) < 0;
}
#else
static bool BGEN_SYM(less)(BGEN_ITEM a, BGEN_ITEM b, void *udata) {
(void)a, (void)b, (void)udata;
return false;
}
static int BGEN_SYM(compare)(BGEN_ITEM a, BGEN_ITEM b, void *udata) {
(void)a, (void)b, (void)udata;
return -1;
}
#if !defined(BGEN_NOORDER)
#error \
Neither BGEN_COMPARE nor BGEN_LESS were defined. \
Alternatively define BGEN_NOORDER if only the "Counted B-tree" API is desired. \
Visit https://github.com/tidwall/bgen for more information.
#endif
#endif
#if defined(BGEN_NOORDER) && (defined(BGEN_LESS) || defined(BGEN_COMPARE))
#error \
Neither BGEN_COMPARE nor BGEN_LESS are allowed when BGEN_NOORDER is defined. \
Visit https://github.com/tidwall/bgen for more information.
#endif
#ifdef BGEN_MAYBELESSEQUAL
static bool BGEN_SYM(maybelessequal)(BGEN_ITEM a, BGEN_ITEM b, void *udata) {
(void)a, (void)b, (void)udata;
BGEN_MAYBELESSEQUAL
}
#endif
#ifdef BGEN_SPATIAL
BGEN_RECT {
BGEN_RTYPE min[BGEN_DIMS];
BGEN_RTYPE max[BGEN_DIMS];
};
static BGEN_RECT BGEN_SYM(item_rect)(BGEN_ITEM item, void *udata) {
(void)item, (void)udata;
BGEN_RTYPE min[BGEN_DIMS] = { 0 };
BGEN_RTYPE max[BGEN_DIMS] = { 0 };
#ifdef BGEN_ITEMRECT
BGEN_ITEMRECT
#endif
BGEN_RECT rect;
for (int i = 0; i < BGEN_DIMS; i++) {
rect.min[i] = min[i];
}
for (int i = 0; i < BGEN_DIMS; i++) {
rect.max[i] = max[i];
}
return rect;
}
#endif
static bool BGEN_SYM(item_copy)(BGEN_ITEM item, BGEN_ITEM *copy, void *udata) {
(void)item, (void)copy, (void)udata;
#ifdef BGEN_ITEMCOPY
BGEN_ITEMCOPY
#else
*copy = item;
return true;
#endif
}
static void BGEN_SYM(item_free)(BGEN_ITEM item, void *udata) {
(void)item, (void)udata;
#ifdef BGEN_ITEMFREE
BGEN_ITEMFREE
#endif
}
#ifdef BGEN_COW
/*
/// Initialize the reference counter
void rc_init(rc_t *rc);
/// Add one reference.
void rc_retain(rc_t *rc);
/// Remove one reference. Return true if the owned object can be destroyed.
bool rc_release(rc_t *rc);
/// Returns true if there is more that one reference.
int rc_shared(rc_t *rc);
*/
#ifdef BGEN_NOATOMICS
typedef int BGEN_SYM(rc_t);
static void BGEN_SYM(rc_init)(BGEN_SYM(rc_t) *rc) {
*rc = 0;
}
static void BGEN_SYM(rc_retain)(BGEN_SYM(rc_t) *rc) {
*rc++;
}
static bool BGEN_SYM(rc_release)(BGEN_SYM(rc_t) *rc) {
*rc--;
return *rc == 1;
}
static bool BGEN_SYM(rc_shared)(BGEN_SYM(rc_t) *rc) {
return *rc > 1;
}
#else
#include <stdatomic.h>
/*
The relaxed/release/acquire pattern is based on:
http://boost.org/doc/libs/1_87_0/libs/atomic/doc/html/atomic/usage_examples.html
*/
typedef atomic_int BGEN_SYM(rc_t);
static void BGEN_SYM(rc_init)(BGEN_SYM(rc_t) *rc) {
atomic_init(rc, 0);
}
static void BGEN_SYM(rc_retain)(BGEN_SYM(rc_t) *rc) {
atomic_fetch_add_explicit(rc, 1, __ATOMIC_RELAXED);
}
static bool BGEN_SYM(rc_release)(BGEN_SYM(rc_t) *rc) {
if (atomic_fetch_sub_explicit(rc, 1, __ATOMIC_RELEASE) == 1) {
atomic_thread_fence(__ATOMIC_ACQUIRE);
return true;
}
return false;
}
static bool BGEN_SYM(rc_shared)(BGEN_SYM(rc_t) *rc) {
return atomic_load_explicit(rc, __ATOMIC_ACQUIRE) > 1;
}
#endif
#endif
BGEN_NODE {
BGEN_ITEM items[BGEN_MAXITEMS]; // all items in node, ordered
#ifdef BGEN_COW
BGEN_SYM(rc_t) rc; // reference counter
#endif
short len; // number of items in this node
short height; // tree height (one is leaf)
bool isleaf; // node is a leaf
// leaves omit the following fields
BGEN_NODE *children[BGEN_MAXITEMS+1]; // child nodes
#ifdef BGEN_COUNTED
size_t counts[BGEN_MAXITEMS+1]; // counts for child nodes
#endif
#ifdef BGEN_SPATIAL
BGEN_RECT rects[BGEN_MAXITEMS+1];
#endif
};
#ifdef BGEN_ASSERT
#include <assert.h>
#undef BGEN_ASSERT
#define BGEN_ASSERT(cond) assert(cond)
#else
#define BGEN_ASSERT(cond)(void)0
#endif
static int BGEN_SYM(feat_maxitems)(void) {
return BGEN_MAXITEMS;
}
static int BGEN_SYM(feat_minitems)(void) {
return BGEN_MINITEMS;
}
static int BGEN_SYM(feat_maxheight)(void) {
return BGEN_MAXHEIGHT;
}
static int BGEN_SYM(feat_fanout)(void) {
return BGEN_FANOUTUSED;
}
static bool BGEN_SYM(feat_counted)(void) {
#ifdef BGEN_COUNTED
return true;
#else
return false;
#endif
}
static bool BGEN_SYM(feat_spatial)(void) {
#ifdef BGEN_SPATIAL
return true;
#else
return false;
#endif
}
static bool BGEN_SYM(feat_ordered)(void) {
#ifdef BGEN_NOORDER
return false;
#else
return true;
#endif
}
static bool BGEN_SYM(feat_cow)(void) {
#ifdef BGEN_COW
return true;
#else
return false;
#endif
}
static bool BGEN_SYM(feat_bsearch)(void) {
#ifdef BGEN_BSEARCH
return true;
#else
return false;
#endif
}
static bool BGEN_SYM(feat_pathhint)(void) {
#ifdef BGEN_PATHHINT
return true;
#else
return false;
#endif
}
static bool BGEN_SYM(feat_atomics)(void) {
#ifndef BGEN_NOATOMICS
return true;
#else
return false;
#endif
}
static int BGEN_SYM(feat_dims)(void) {
#ifdef BGEN_SPATIAL
return BGEN_DIMS;
#else
return 0;
#endif
}
static BGEN_NODE *BGEN_SYM(alloc_node)(bool isleaf, void *udata) {
void *ptr = isleaf ?
BGEN_SYM(malloc)(offsetof(BGEN_NODE, children), udata) :
BGEN_SYM(malloc)(sizeof(BGEN_NODE), udata);
if (!ptr) {
return 0;
}
BGEN_NODE *node = (BGEN_NODE*)ptr;
#ifdef BGEN_COW
BGEN_SYM(rc_init)(&node->rc);
BGEN_SYM(rc_retain)(&node->rc);
#endif
node->isleaf = isleaf;
node->height = 0;
node->len = 0;
return node;
}
// returns the number of items in a node by counting, recursively
static size_t BGEN_SYM(deepcount)(BGEN_NODE *node) {
size_t count = (size_t)node->len;
if (!node->isleaf) {
for (int i = 0; i < node->len+1; i++) {
count += BGEN_SYM(deepcount)(node->children[i]);
}
}
return count;
}
// returns the height of the node counting the depth, recursively
static int BGEN_SYM(deepheight)(BGEN_NODE *node) {
int height = 0;
while (1) {
height++;
if (node->isleaf) {
return height;
}
node = node->children[0];
}
}
#ifdef BGEN_SPATIAL
static bool BGEN_SYM(rect_intersects)(BGEN_RECT a, BGEN_RECT b) {
int bits = 0;
for (int i = 0; i < BGEN_DIMS; i++) {
bits |= b.min[i] > a.max[i];
bits |= b.max[i] < a.min[i];
}
return bits == 0;
}
static BGEN_RECT BGEN_SYM(rect_join)(BGEN_RECT a, BGEN_RECT b) {
for (int i = 0; i < BGEN_DIMS; i++) {
a.min[i] = a.min[i] < b.min[i] ? a.min[i] : b.min[i];
}
for (int i = 0; i < BGEN_DIMS; i++) {
a.max[i] = a.max[i] > b.max[i] ? a.max[i] : b.max[i];
}
return a;
}
static bool BGEN_SYM(feq)(BGEN_RTYPE a, BGEN_RTYPE b) {
return !(a < b || a > b);
}
static bool BGEN_SYM(recteq)(BGEN_RECT a, BGEN_RECT b) {
for (int i = 0; i < BGEN_DIMS; i++) {
if (!BGEN_SYM(feq)(a.min[i], b.min[i]) ||
!BGEN_SYM(feq)(a.max[i], b.max[i]))
{
return false;
}
}
return true;
}
static bool BGEN_SYM(rect_onedge)(BGEN_RECT rect, BGEN_RECT other) {
for (int i = 0; i < BGEN_DIMS; i++) {
if (BGEN_SYM(feq)(rect.min[i], other.min[i]) ||
BGEN_SYM(feq)(rect.max[i], other.max[i]))
{
return true;
}
}
return false;
}
// Returns a rectangle for child+item at index.
static BGEN_RECT BGEN_SYM(rect_calc)(BGEN_NODE *node, int i, void *udata) {
(void)node;
BGEN_ASSERT(node && !node->isleaf);
#ifndef BGEN_SPATIAL
(void)i, (void)udata;
return (BGEN_RECT){ 0 };
#else
BGEN_NODE *child = node->children[i];
BGEN_RECT rect;
if (!child->isleaf) {
rect = child->rects[0];
for (int j = 1; j <= child->len; j++) {
rect = BGEN_SYM(rect_join)(rect, child->rects[j]);
}
} else {
rect = BGEN_SYM(item_rect)(child->items[0], udata);
for (int j = 1; j < child->len; j++) {
rect = BGEN_SYM(rect_join)(rect,
BGEN_SYM(item_rect)(child->items[j], udata));
}
}
if (i < node->len) {
rect = BGEN_SYM(rect_join)(rect,
BGEN_SYM(item_rect)(node->items[i], udata));
}
return rect;
#endif
}
static BGEN_RECT BGEN_SYM(deeprect)(BGEN_NODE *node, void *udata) {
BGEN_RECT rect = { 0 };
if (node->len <= BGEN_MAXITEMS) {
rect = BGEN_SYM(item_rect)(node->items[0], udata);
for (int i = 0; i < node->len; i++) {
BGEN_RECT irect = BGEN_SYM(item_rect)(node->items[i], udata);
rect = BGEN_SYM(rect_join)(rect, irect);
}
if (!node->isleaf) {
for (int i = 0; i <= node->len; i++) {
rect = BGEN_SYM(rect_join)(rect,
BGEN_SYM(deeprect)(node->children[i], udata));
}
}
}
return rect;
}
#endif
static bool BGEN_SYM(sane0)(BGEN_NODE *node, void *udata, int depth) {
// check the number of items in node.
if (depth == 0) {
// the root is allowed to have one item.
if (node->len < 1 || node->len > BGEN_MAXITEMS) {
return false;
}
} else {
if (node->len < BGEN_MINITEMS || node->len > BGEN_MAXITEMS) {
return false;
}
}
if (node->isleaf && node->height != 1) {
return false;
}
if (!node->isleaf && node->height < 2) {
return false;
}
// Check the height
if (node->height != BGEN_SYM(deepheight)(node)) {
return false;
}
// check the order of items.
#ifndef BGEN_NOORDER
for (int i = 1; i < node->len; i++) {
if (BGEN_SYM(compare)(node->items[i-1], node->items[i], udata) >= 0) {
return false;
}
}
#endif
if (!node->isleaf) {
// continue sanity test down the tree.
#ifndef BGEN_NOORDER
// Check the order of each branch item, comparing to the children to
// the left and right.
for (int i = 0; i < node->len; i++) {
if (node->children[i]->len > 0 &&
node->children[i]->len <= BGEN_MAXITEMS &&
node->children[i+1]->len > 0 &&
node->children[i+1]->len <= BGEN_MAXITEMS)
{
if (BGEN_SYM(compare)(
node->children[i]->items[node->children[i]->len-1],
node->items[i], udata) >= 0 ||
BGEN_SYM(compare)(node->items[i],
node->children[i+1]->items[0], udata) >= 0)
{
return false;
}
}
}
#endif
// check the sanity of child node
for (int i = 0; i <= node->len; i++) {
#ifdef BGEN_COUNTED
size_t count = BGEN_SYM(deepcount)(node->children[i]);
if (count != node->counts[i]) {
return false;
}
#endif
#ifdef BGEN_SPATIAL
BGEN_RECT rect = BGEN_SYM(deeprect)(node->children[i], udata);
if (i < node->len) {
BGEN_RECT irect = BGEN_SYM(item_rect)(node->items[i], udata);
rect = BGEN_SYM(rect_join)(rect, irect);
}
if (!BGEN_SYM(recteq)(node->rects[i], rect)) {
return false;
}
#endif
if (!BGEN_SYM(sane0)(node->children[i], udata, depth+1)) {
return false;
}
}
}
return true;
}
// sanity checker
static bool BGEN_SYM(sane)(BGEN_NODE **root, void *udata) {
bool sane = true;
if (*root) {
return BGEN_SYM(sane0)(*root, udata, 0);
}
return sane;
}
static size_t BGEN_SYM(count0)(BGEN_NODE *node) {
#ifndef BGEN_COUNTED
return BGEN_SYM(deepcount)(node);
#else
size_t count = node->len;
if (!node->isleaf) {
for (int i = 0; i <= node->len; i++) {
count += node->counts[i];
}
}
return count;
#endif
}
// returns the number of items in tree
static size_t BGEN_SYM(count)(BGEN_NODE **root, void *udata) {
(void)udata;
return *root ? BGEN_SYM(count0)(*root) : 0;
}
// returns the number of items in tree
static size_t BGEN_SYM(height)(BGEN_NODE **root, void *udata) {
(void)udata;
return *root ? (size_t)(*root)->height : 0;
}
// Returns the number of items in child node at index.
// This will use the 'count' value if available.
static size_t BGEN_SYM(node_count)(BGEN_NODE *branch, int node_index) {
#ifndef BGEN_COUNTED
return BGEN_SYM(count0)(branch->children[node_index]);
#else
return branch->counts[node_index];
#endif
}
static void BGEN_SYM(node_free)(BGEN_NODE *node, void *udata) {
#ifdef BGEN_COW
if (!BGEN_SYM(rc_release)(&node->rc)) {
return;
}
#endif
if (!node->isleaf) {
for (int i = 0; i < node->len+1; i++) {
BGEN_SYM(node_free)(node->children[i], udata);
}
}
for (int i = 0; i < node->len; i++) {
BGEN_SYM(item_free)(node->items[i], udata);
}
BGEN_SYM(free)(node, udata);
}
/// Free the tree!
static void BGEN_SYM(clear)(BGEN_NODE **root, void *udata) {
if (*root) {
BGEN_SYM(node_free)(*root, udata);
*root = 0;
}
}
#ifdef BGEN_BSEARCH
BGEN_INLINE
static int BGEN_SYM(search_bsearch)(BGEN_ITEM *items, int nitems,
BGEN_ITEM key, void *udata, int *found)
{
// Standard bsearch. Balanced. Relies on branch prediction.
int i = 0;
int n = nitems;
while (i < n) {
int j = (i + n) / 2;
int cmp = BGEN_SYM(compare)(key, items[j], udata);
if (cmp < 0) {
n = j;
} else if (cmp > 0) {
i = j+1;
} else {
*found = 1;
return j;
}
}
*found = 0;
return i;
}
#else
BGEN_INLINE
static int BGEN_SYM(search_linear)(BGEN_ITEM *items, int nitems, BGEN_ITEM key,
void *udata, int *found)
{
int i = 0;
*found = 0;
#ifdef BGEN_MAYBELESSEQUAL
while (nitems-i >= 4) {
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
if (BGEN_SYM(maybelessequal)(key, items[i], udata)){goto compare;}i++;
}
for (; i < nitems; i++) {
if (BGEN_SYM(maybelessequal)(key, items[i], udata)) {
goto compare;
}
}
#endif
#ifdef BGEN_LESS
for (; i < nitems; i++) {
#ifdef BGEN_MAYBELESSEQUAL
compare:
#endif
if (BGEN_SYM(less)(key, items[i], udata)) {
break;
}
if (!BGEN_SYM(less)(items[i], key, udata)) {
*found = 1;
break;
}
}
#else
int cmp;
for (; i < nitems; i++) {
#ifdef BGEN_MAYBELESSEQUAL
compare:
#endif
cmp = BGEN_SYM(compare)(key, items[i], udata);
if (cmp <= 0) {
*found = cmp == 0;
break;
}
}
#endif
return i;
}
#endif