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#include "test_util.h"
159#include "sparsebit.h"
160#include <limits.h>
161#include <assert.h>
162
163#define DUMP_LINE_MAX 100
164
165typedef uint32_t mask_t;
166#define MASK_BITS (sizeof(mask_t) * CHAR_BIT)
167
168struct node {
169 struct node *parent;
170 struct node *left;
171 struct node *right;
172 sparsebit_idx_t idx;
173 sparsebit_num_t num_after;
174 mask_t mask;
175};
176
177struct sparsebit {
178
179
180
181
182
183 struct node *root;
184
185
186
187
188
189
190
191 sparsebit_num_t num_set;
192};
193
194
195
196
197static sparsebit_num_t node_num_set(struct node *nodep)
198{
199 return nodep->num_after + __builtin_popcount(nodep->mask);
200}
201
202
203
204
205static struct node *node_first(struct sparsebit *s)
206{
207 struct node *nodep;
208
209 for (nodep = s->root; nodep && nodep->left; nodep = nodep->left)
210 ;
211
212 return nodep;
213}
214
215
216
217
218
219static struct node *node_next(struct sparsebit *s, struct node *np)
220{
221 struct node *nodep = np;
222
223
224
225
226
227 if (nodep->right) {
228 for (nodep = nodep->right; nodep->left; nodep = nodep->left)
229 ;
230 return nodep;
231 }
232
233
234
235
236
237 while (nodep->parent && nodep == nodep->parent->right)
238 nodep = nodep->parent;
239
240 return nodep->parent;
241}
242
243
244
245
246
247static struct node *node_prev(struct sparsebit *s, struct node *np)
248{
249 struct node *nodep = np;
250
251
252
253
254
255 if (nodep->left) {
256 for (nodep = nodep->left; nodep->right; nodep = nodep->right)
257 ;
258 return (struct node *) nodep;
259 }
260
261
262
263
264
265 while (nodep->parent && nodep == nodep->parent->left)
266 nodep = nodep->parent;
267
268 return (struct node *) nodep->parent;
269}
270
271
272
273
274
275
276static struct node *node_copy_subtree(struct node *subtree)
277{
278 struct node *root;
279
280
281 root = calloc(1, sizeof(*root));
282 if (!root) {
283 perror("calloc");
284 abort();
285 }
286
287 root->idx = subtree->idx;
288 root->mask = subtree->mask;
289 root->num_after = subtree->num_after;
290
291
292 if (subtree->left) {
293 root->left = node_copy_subtree(subtree->left);
294 root->left->parent = root;
295 }
296
297 if (subtree->right) {
298 root->right = node_copy_subtree(subtree->right);
299 root->right->parent = root;
300 }
301
302 return root;
303}
304
305
306
307
308
309
310static struct node *node_find(struct sparsebit *s, sparsebit_idx_t idx)
311{
312 struct node *nodep;
313
314
315 for (nodep = s->root; nodep;
316 nodep = nodep->idx > idx ? nodep->left : nodep->right) {
317 if (idx >= nodep->idx &&
318 idx <= nodep->idx + MASK_BITS + nodep->num_after - 1)
319 break;
320 }
321
322 return nodep;
323}
324
325
326
327
328
329
330
331
332
333static struct node *node_add(struct sparsebit *s, sparsebit_idx_t idx)
334{
335 struct node *nodep, *parentp, *prev;
336
337
338 nodep = calloc(1, sizeof(*nodep));
339 if (!nodep) {
340 perror("calloc");
341 abort();
342 }
343
344 nodep->idx = idx & -MASK_BITS;
345
346
347 if (!s->root) {
348 s->root = nodep;
349 return nodep;
350 }
351
352
353
354
355
356 parentp = s->root;
357 while (true) {
358 if (idx < parentp->idx) {
359 if (!parentp->left) {
360 parentp->left = nodep;
361 nodep->parent = parentp;
362 break;
363 }
364 parentp = parentp->left;
365 } else {
366 assert(idx > parentp->idx + MASK_BITS + parentp->num_after - 1);
367 if (!parentp->right) {
368 parentp->right = nodep;
369 nodep->parent = parentp;
370 break;
371 }
372 parentp = parentp->right;
373 }
374 }
375
376
377
378
379
380
381 prev = node_prev(s, nodep);
382 while (prev && prev->idx + MASK_BITS + prev->num_after - 1 >= nodep->idx) {
383 unsigned int n1 = (prev->idx + MASK_BITS + prev->num_after - 1)
384 - nodep->idx;
385 assert(prev->num_after > 0);
386 assert(n1 < MASK_BITS);
387 assert(!(nodep->mask & (1 << n1)));
388 nodep->mask |= (1 << n1);
389 prev->num_after--;
390 }
391
392 return nodep;
393}
394
395
396bool sparsebit_all_set(struct sparsebit *s)
397{
398
399
400
401
402
403 return s->root && s->num_set == 0;
404}
405
406
407
408
409static void node_rm(struct sparsebit *s, struct node *nodep)
410{
411 struct node *tmp;
412 sparsebit_num_t num_set;
413
414 num_set = node_num_set(nodep);
415 assert(s->num_set >= num_set || sparsebit_all_set(s));
416 s->num_set -= node_num_set(nodep);
417
418
419 if (nodep->left && nodep->right) {
420
421
422
423
424 for (tmp = nodep->right; tmp->left; tmp = tmp->left)
425 ;
426 tmp->left = nodep->left;
427 nodep->left = NULL;
428 tmp->left->parent = tmp;
429 }
430
431
432 if (nodep->left) {
433 if (!nodep->parent) {
434 s->root = nodep->left;
435 nodep->left->parent = NULL;
436 } else {
437 nodep->left->parent = nodep->parent;
438 if (nodep == nodep->parent->left)
439 nodep->parent->left = nodep->left;
440 else {
441 assert(nodep == nodep->parent->right);
442 nodep->parent->right = nodep->left;
443 }
444 }
445
446 nodep->parent = nodep->left = nodep->right = NULL;
447 free(nodep);
448
449 return;
450 }
451
452
453
454 if (nodep->right) {
455 if (!nodep->parent) {
456 s->root = nodep->right;
457 nodep->right->parent = NULL;
458 } else {
459 nodep->right->parent = nodep->parent;
460 if (nodep == nodep->parent->left)
461 nodep->parent->left = nodep->right;
462 else {
463 assert(nodep == nodep->parent->right);
464 nodep->parent->right = nodep->right;
465 }
466 }
467
468 nodep->parent = nodep->left = nodep->right = NULL;
469 free(nodep);
470
471 return;
472 }
473
474
475 if (!nodep->parent) {
476 s->root = NULL;
477 } else {
478 if (nodep->parent->left == nodep)
479 nodep->parent->left = NULL;
480 else {
481 assert(nodep == nodep->parent->right);
482 nodep->parent->right = NULL;
483 }
484 }
485
486 nodep->parent = nodep->left = nodep->right = NULL;
487 free(nodep);
488
489 return;
490}
491
492
493
494
495
496
497
498static struct node *node_split(struct sparsebit *s, sparsebit_idx_t idx)
499{
500 struct node *nodep1, *nodep2;
501 sparsebit_idx_t offset;
502 sparsebit_num_t orig_num_after;
503
504 assert(!(idx % MASK_BITS));
505
506
507
508
509
510 nodep1 = node_find(s, idx);
511 if (!nodep1)
512 return node_add(s, idx);
513
514
515
516
517
518 if (nodep1->idx == idx)
519 return nodep1;
520
521
522
523
524
525
526
527
528
529
530 offset = idx - (nodep1->idx + MASK_BITS);
531 orig_num_after = nodep1->num_after;
532
533
534
535
536
537 nodep1->num_after = offset;
538 nodep2 = node_add(s, idx);
539
540
541 nodep2->num_after = orig_num_after - offset;
542 if (nodep2->num_after >= MASK_BITS) {
543 nodep2->mask = ~(mask_t) 0;
544 nodep2->num_after -= MASK_BITS;
545 } else {
546 nodep2->mask = (1 << nodep2->num_after) - 1;
547 nodep2->num_after = 0;
548 }
549
550 return nodep2;
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
599static void node_reduce(struct sparsebit *s, struct node *nodep)
600{
601 bool reduction_performed;
602
603 do {
604 reduction_performed = false;
605 struct node *prev, *next, *tmp;
606
607
608
609
610 if (nodep->mask == 0 && nodep->num_after == 0) {
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632 tmp = node_next(s, nodep);
633 if (!tmp)
634 tmp = node_prev(s, nodep);
635
636 node_rm(s, nodep);
637 nodep = NULL;
638
639 nodep = tmp;
640 reduction_performed = true;
641 continue;
642 }
643
644
645
646
647
648 if (nodep->mask == 0) {
649 assert(nodep->num_after != 0);
650 assert(nodep->idx + MASK_BITS > nodep->idx);
651
652 nodep->idx += MASK_BITS;
653
654 if (nodep->num_after >= MASK_BITS) {
655 nodep->mask = ~0;
656 nodep->num_after -= MASK_BITS;
657 } else {
658 nodep->mask = (1u << nodep->num_after) - 1;
659 nodep->num_after = 0;
660 }
661
662 reduction_performed = true;
663 continue;
664 }
665
666
667
668
669
670 prev = node_prev(s, nodep);
671 if (prev) {
672 sparsebit_idx_t prev_highest_bit;
673
674
675 if (prev->mask == 0 && prev->num_after == 0) {
676 node_rm(s, prev);
677
678 reduction_performed = true;
679 continue;
680 }
681
682
683
684
685
686 if (nodep->mask + 1 == 0 &&
687 prev->idx + MASK_BITS == nodep->idx) {
688 prev->num_after += MASK_BITS + nodep->num_after;
689 nodep->mask = 0;
690 nodep->num_after = 0;
691
692 reduction_performed = true;
693 continue;
694 }
695
696
697
698
699
700
701 prev_highest_bit = prev->idx + MASK_BITS - 1 + prev->num_after;
702 if (prev_highest_bit + 1 == nodep->idx &&
703 (nodep->mask | (nodep->mask >> 1)) == nodep->mask) {
704
705
706
707
708
709
710
711 unsigned int num_contiguous
712 = __builtin_popcount(nodep->mask);
713 assert((num_contiguous > 0) &&
714 ((1ULL << num_contiguous) - 1) == nodep->mask);
715
716 prev->num_after += num_contiguous;
717 nodep->mask = 0;
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732 if (num_contiguous == MASK_BITS) {
733 prev->num_after += nodep->num_after;
734 nodep->num_after = 0;
735 }
736
737 reduction_performed = true;
738 continue;
739 }
740 }
741
742
743
744
745
746 next = node_next(s, nodep);
747 if (next) {
748
749 if (next->mask == 0 && next->num_after == 0) {
750 node_rm(s, next);
751 reduction_performed = true;
752 continue;
753 }
754
755
756
757
758
759 if (next->idx == nodep->idx + MASK_BITS + nodep->num_after &&
760 next->mask == ~(mask_t) 0) {
761 nodep->num_after += MASK_BITS;
762 next->mask = 0;
763 nodep->num_after += next->num_after;
764 next->num_after = 0;
765
766 node_rm(s, next);
767 next = NULL;
768
769 reduction_performed = true;
770 continue;
771 }
772 }
773 } while (nodep && reduction_performed);
774}
775
776
777
778
779bool sparsebit_is_set(struct sparsebit *s, sparsebit_idx_t idx)
780{
781 struct node *nodep;
782
783
784 for (nodep = s->root; nodep;
785 nodep = nodep->idx > idx ? nodep->left : nodep->right)
786 if (idx >= nodep->idx &&
787 idx <= nodep->idx + MASK_BITS + nodep->num_after - 1)
788 goto have_node;
789
790 return false;
791
792have_node:
793
794 if (nodep->num_after && idx >= nodep->idx + MASK_BITS)
795 return true;
796
797
798 assert(idx >= nodep->idx && idx - nodep->idx < MASK_BITS);
799 return !!(nodep->mask & (1 << (idx - nodep->idx)));
800}
801
802
803
804
805static void bit_set(struct sparsebit *s, sparsebit_idx_t idx)
806{
807 struct node *nodep;
808
809
810 if (sparsebit_is_set(s, idx))
811 return;
812
813
814
815
816
817
818 nodep = node_split(s, idx & -MASK_BITS);
819
820
821 assert(idx >= nodep->idx && idx <= nodep->idx + MASK_BITS - 1);
822 assert(!(nodep->mask & (1 << (idx - nodep->idx))));
823 nodep->mask |= 1 << (idx - nodep->idx);
824 s->num_set++;
825
826 node_reduce(s, nodep);
827}
828
829
830
831
832static void bit_clear(struct sparsebit *s, sparsebit_idx_t idx)
833{
834 struct node *nodep;
835
836
837 if (!sparsebit_is_set(s, idx))
838 return;
839
840
841 nodep = node_find(s, idx);
842 if (!nodep)
843 return;
844
845
846
847
848
849 if (idx >= nodep->idx + MASK_BITS)
850 nodep = node_split(s, idx & -MASK_BITS);
851
852
853
854
855
856 assert(idx >= nodep->idx && idx <= nodep->idx + MASK_BITS - 1);
857 assert(nodep->mask & (1 << (idx - nodep->idx)));
858 nodep->mask &= ~(1 << (idx - nodep->idx));
859 assert(s->num_set > 0 || sparsebit_all_set(s));
860 s->num_set--;
861
862 node_reduce(s, nodep);
863}
864
865
866
867
868
869
870
871
872static void dump_nodes(FILE *stream, struct node *nodep,
873 unsigned int indent)
874{
875 char *node_type;
876
877
878 if (!nodep->parent)
879 node_type = "root";
880 else if (nodep == nodep->parent->left)
881 node_type = "left";
882 else {
883 assert(nodep == nodep->parent->right);
884 node_type = "right";
885 }
886 fprintf(stream, "%*s---- %s nodep: %p\n", indent, "", node_type, nodep);
887 fprintf(stream, "%*s parent: %p left: %p right: %p\n", indent, "",
888 nodep->parent, nodep->left, nodep->right);
889 fprintf(stream, "%*s idx: 0x%lx mask: 0x%x num_after: 0x%lx\n",
890 indent, "", nodep->idx, nodep->mask, nodep->num_after);
891
892
893 if (nodep->left)
894 dump_nodes(stream, nodep->left, indent + 2);
895
896
897 if (nodep->right)
898 dump_nodes(stream, nodep->right, indent + 2);
899}
900
901static inline sparsebit_idx_t node_first_set(struct node *nodep, int start)
902{
903 mask_t leading = (mask_t)1 << start;
904 int n1 = __builtin_ctz(nodep->mask & -leading);
905
906 return nodep->idx + n1;
907}
908
909static inline sparsebit_idx_t node_first_clear(struct node *nodep, int start)
910{
911 mask_t leading = (mask_t)1 << start;
912 int n1 = __builtin_ctz(~nodep->mask & -leading);
913
914 return nodep->idx + n1;
915}
916
917
918
919
920
921
922
923
924
925static void sparsebit_dump_internal(FILE *stream, struct sparsebit *s,
926 unsigned int indent)
927{
928
929 fprintf(stream, "%*sroot: %p\n", indent, "", s->root);
930 fprintf(stream, "%*snum_set: 0x%lx\n", indent, "", s->num_set);
931
932 if (s->root)
933 dump_nodes(stream, s->root, indent);
934}
935
936
937
938
939struct sparsebit *sparsebit_alloc(void)
940{
941 struct sparsebit *s;
942
943
944 s = calloc(1, sizeof(*s));
945 if (!s) {
946 perror("calloc");
947 abort();
948 }
949
950 return s;
951}
952
953
954
955
956void sparsebit_free(struct sparsebit **sbitp)
957{
958 struct sparsebit *s = *sbitp;
959
960 if (!s)
961 return;
962
963 sparsebit_clear_all(s);
964 free(s);
965 *sbitp = NULL;
966}
967
968
969
970
971
972
973void sparsebit_copy(struct sparsebit *d, struct sparsebit *s)
974{
975
976 sparsebit_clear_all(d);
977
978 if (s->root) {
979 d->root = node_copy_subtree(s->root);
980 d->num_set = s->num_set;
981 }
982}
983
984
985bool sparsebit_is_set_num(struct sparsebit *s,
986 sparsebit_idx_t idx, sparsebit_num_t num)
987{
988 sparsebit_idx_t next_cleared;
989
990 assert(num > 0);
991 assert(idx + num - 1 >= idx);
992
993
994 if (!sparsebit_is_set(s, idx))
995 return false;
996
997
998 next_cleared = sparsebit_next_clear(s, idx);
999
1000
1001
1002
1003
1004
1005 return next_cleared == 0 || next_cleared - idx >= num;
1006}
1007
1008
1009bool sparsebit_is_clear(struct sparsebit *s,
1010 sparsebit_idx_t idx)
1011{
1012 return !sparsebit_is_set(s, idx);
1013}
1014
1015
1016bool sparsebit_is_clear_num(struct sparsebit *s,
1017 sparsebit_idx_t idx, sparsebit_num_t num)
1018{
1019 sparsebit_idx_t next_set;
1020
1021 assert(num > 0);
1022 assert(idx + num - 1 >= idx);
1023
1024
1025 if (!sparsebit_is_clear(s, idx))
1026 return false;
1027
1028
1029 next_set = sparsebit_next_set(s, idx);
1030
1031
1032
1033
1034
1035
1036 return next_set == 0 || next_set - idx >= num;
1037}
1038
1039
1040
1041
1042
1043
1044
1045sparsebit_num_t sparsebit_num_set(struct sparsebit *s)
1046{
1047 return s->num_set;
1048}
1049
1050
1051bool sparsebit_any_set(struct sparsebit *s)
1052{
1053
1054
1055
1056
1057 if (!s->root)
1058 return false;
1059
1060
1061
1062
1063
1064
1065 assert(s->root->mask != 0);
1066 assert(s->num_set > 0 ||
1067 (s->root->num_after == ((sparsebit_num_t) 0) - MASK_BITS &&
1068 s->root->mask == ~(mask_t) 0));
1069
1070 return true;
1071}
1072
1073
1074bool sparsebit_all_clear(struct sparsebit *s)
1075{
1076 return !sparsebit_any_set(s);
1077}
1078
1079
1080bool sparsebit_any_clear(struct sparsebit *s)
1081{
1082 return !sparsebit_all_set(s);
1083}
1084
1085
1086
1087sparsebit_idx_t sparsebit_first_set(struct sparsebit *s)
1088{
1089 struct node *nodep;
1090
1091
1092 assert(sparsebit_any_set(s));
1093
1094 nodep = node_first(s);
1095 return node_first_set(nodep, 0);
1096}
1097
1098
1099
1100
1101sparsebit_idx_t sparsebit_first_clear(struct sparsebit *s)
1102{
1103 struct node *nodep1, *nodep2;
1104
1105
1106 assert(sparsebit_any_clear(s));
1107
1108
1109 nodep1 = node_first(s);
1110 if (!nodep1 || nodep1->idx > 0)
1111 return 0;
1112
1113
1114 if (nodep1->mask != ~(mask_t) 0)
1115 return node_first_clear(nodep1, 0);
1116
1117
1118
1119
1120
1121
1122 nodep2 = node_next(s, nodep1);
1123 if (!nodep2) {
1124
1125
1126
1127
1128 assert(nodep1->mask == ~(mask_t) 0);
1129 assert(nodep1->idx + MASK_BITS + nodep1->num_after != (sparsebit_idx_t) 0);
1130 return nodep1->idx + MASK_BITS + nodep1->num_after;
1131 }
1132
1133
1134
1135
1136
1137
1138
1139 if (nodep1->idx + MASK_BITS + nodep1->num_after != nodep2->idx)
1140 return nodep1->idx + MASK_BITS + nodep1->num_after;
1141
1142
1143
1144
1145
1146
1147
1148
1149 return node_first_clear(nodep2, 0);
1150}
1151
1152
1153
1154
1155sparsebit_idx_t sparsebit_next_set(struct sparsebit *s,
1156 sparsebit_idx_t prev)
1157{
1158 sparsebit_idx_t lowest_possible = prev + 1;
1159 sparsebit_idx_t start;
1160 struct node *nodep;
1161
1162
1163 if (lowest_possible == 0)
1164 return 0;
1165
1166
1167
1168
1169
1170 struct node *candidate = NULL;
1171
1172
1173 bool contains = false;
1174
1175
1176
1177
1178
1179
1180 for (nodep = s->root; nodep;) {
1181 if ((nodep->idx + MASK_BITS + nodep->num_after - 1)
1182 >= lowest_possible) {
1183 candidate = nodep;
1184 if (candidate->idx <= lowest_possible) {
1185 contains = true;
1186 break;
1187 }
1188 nodep = nodep->left;
1189 } else {
1190 nodep = nodep->right;
1191 }
1192 }
1193 if (!candidate)
1194 return 0;
1195
1196 assert(candidate->mask != 0);
1197
1198
1199 if (!contains) {
1200
1201
1202
1203
1204
1205 assert(candidate->idx > lowest_possible);
1206
1207 return node_first_set(candidate, 0);
1208 }
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218 start = lowest_possible - candidate->idx;
1219
1220 if (start < MASK_BITS && candidate->mask >= (1 << start))
1221 return node_first_set(candidate, start);
1222
1223 if (candidate->num_after) {
1224 sparsebit_idx_t first_num_after_idx = candidate->idx + MASK_BITS;
1225
1226 return lowest_possible < first_num_after_idx
1227 ? first_num_after_idx : lowest_possible;
1228 }
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238 candidate = node_next(s, candidate);
1239 if (!candidate)
1240 return 0;
1241
1242 return node_first_set(candidate, 0);
1243}
1244
1245
1246
1247
1248sparsebit_idx_t sparsebit_next_clear(struct sparsebit *s,
1249 sparsebit_idx_t prev)
1250{
1251 sparsebit_idx_t lowest_possible = prev + 1;
1252 sparsebit_idx_t idx;
1253 struct node *nodep1, *nodep2;
1254
1255
1256 if (lowest_possible == 0)
1257 return 0;
1258
1259
1260
1261
1262
1263 nodep1 = node_find(s, lowest_possible);
1264 if (!nodep1)
1265 return lowest_possible;
1266
1267
1268 for (idx = lowest_possible - nodep1->idx; idx < MASK_BITS; idx++)
1269 if (!(nodep1->mask & (1 << idx)))
1270 return nodep1->idx + idx;
1271
1272
1273
1274
1275
1276
1277 nodep2 = node_next(s, nodep1);
1278 if (!nodep2)
1279 return nodep1->idx + MASK_BITS + nodep1->num_after;
1280
1281
1282
1283
1284
1285
1286
1287 if (nodep1->idx + MASK_BITS + nodep1->num_after != nodep2->idx)
1288 return nodep1->idx + MASK_BITS + nodep1->num_after;
1289
1290
1291
1292
1293
1294
1295
1296
1297 return node_first_clear(nodep2, 0);
1298}
1299
1300
1301
1302
1303
1304sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *s,
1305 sparsebit_idx_t start, sparsebit_num_t num)
1306{
1307 sparsebit_idx_t idx;
1308
1309 assert(num >= 1);
1310
1311 for (idx = sparsebit_next_set(s, start);
1312 idx != 0 && idx + num - 1 >= idx;
1313 idx = sparsebit_next_set(s, idx)) {
1314 assert(sparsebit_is_set(s, idx));
1315
1316
1317
1318
1319
1320 if (sparsebit_is_set_num(s, idx, num))
1321 return idx;
1322
1323
1324
1325
1326
1327 idx = sparsebit_next_clear(s, idx);
1328 if (idx == 0)
1329 return 0;
1330 }
1331
1332 return 0;
1333}
1334
1335
1336
1337
1338
1339sparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *s,
1340 sparsebit_idx_t start, sparsebit_num_t num)
1341{
1342 sparsebit_idx_t idx;
1343
1344 assert(num >= 1);
1345
1346 for (idx = sparsebit_next_clear(s, start);
1347 idx != 0 && idx + num - 1 >= idx;
1348 idx = sparsebit_next_clear(s, idx)) {
1349 assert(sparsebit_is_clear(s, idx));
1350
1351
1352
1353
1354
1355 if (sparsebit_is_clear_num(s, idx, num))
1356 return idx;
1357
1358
1359
1360
1361
1362 idx = sparsebit_next_set(s, idx);
1363 if (idx == 0)
1364 return 0;
1365 }
1366
1367 return 0;
1368}
1369
1370
1371void sparsebit_set_num(struct sparsebit *s,
1372 sparsebit_idx_t start, sparsebit_num_t num)
1373{
1374 struct node *nodep, *next;
1375 unsigned int n1;
1376 sparsebit_idx_t idx;
1377 sparsebit_num_t n;
1378 sparsebit_idx_t middle_start, middle_end;
1379
1380 assert(num > 0);
1381 assert(start + num - 1 >= start);
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403 for (idx = start, n = num; n > 0 && idx % MASK_BITS != 0; idx++, n--)
1404 bit_set(s, idx);
1405
1406
1407 middle_start = idx;
1408 middle_end = middle_start + (n & -MASK_BITS) - 1;
1409 if (n >= MASK_BITS) {
1410 nodep = node_split(s, middle_start);
1411
1412
1413
1414
1415
1416
1417 if (middle_end + 1 > middle_end)
1418 (void) node_split(s, middle_end + 1);
1419
1420
1421 for (next = node_next(s, nodep);
1422 next && (next->idx < middle_end);
1423 next = node_next(s, nodep)) {
1424 assert(next->idx + MASK_BITS + next->num_after - 1 <= middle_end);
1425 node_rm(s, next);
1426 next = NULL;
1427 }
1428
1429
1430 for (n1 = 0; n1 < MASK_BITS; n1++) {
1431 if (!(nodep->mask & (1 << n1))) {
1432 nodep->mask |= 1 << n1;
1433 s->num_set++;
1434 }
1435 }
1436
1437 s->num_set -= nodep->num_after;
1438 nodep->num_after = middle_end - middle_start + 1 - MASK_BITS;
1439 s->num_set += nodep->num_after;
1440
1441 node_reduce(s, nodep);
1442 }
1443 idx = middle_end + 1;
1444 n -= middle_end - middle_start + 1;
1445
1446
1447 assert(n < MASK_BITS);
1448 for (; n > 0; idx++, n--)
1449 bit_set(s, idx);
1450}
1451
1452
1453void sparsebit_clear_num(struct sparsebit *s,
1454 sparsebit_idx_t start, sparsebit_num_t num)
1455{
1456 struct node *nodep, *next;
1457 unsigned int n1;
1458 sparsebit_idx_t idx;
1459 sparsebit_num_t n;
1460 sparsebit_idx_t middle_start, middle_end;
1461
1462 assert(num > 0);
1463 assert(start + num - 1 >= start);
1464
1465
1466 for (idx = start, n = num; n > 0 && idx % MASK_BITS != 0; idx++, n--)
1467 bit_clear(s, idx);
1468
1469
1470 middle_start = idx;
1471 middle_end = middle_start + (n & -MASK_BITS) - 1;
1472 if (n >= MASK_BITS) {
1473 nodep = node_split(s, middle_start);
1474
1475
1476
1477
1478
1479
1480 if (middle_end + 1 > middle_end)
1481 (void) node_split(s, middle_end + 1);
1482
1483
1484 for (next = node_next(s, nodep);
1485 next && (next->idx < middle_end);
1486 next = node_next(s, nodep)) {
1487 assert(next->idx + MASK_BITS + next->num_after - 1 <= middle_end);
1488 node_rm(s, next);
1489 next = NULL;
1490 }
1491
1492
1493 for (n1 = 0; n1 < MASK_BITS; n1++) {
1494 if (nodep->mask & (1 << n1)) {
1495 nodep->mask &= ~(1 << n1);
1496 s->num_set--;
1497 }
1498 }
1499
1500
1501 s->num_set -= nodep->num_after;
1502 nodep->num_after = 0;
1503
1504
1505
1506
1507
1508
1509 node_reduce(s, nodep);
1510 nodep = NULL;
1511 }
1512 idx = middle_end + 1;
1513 n -= middle_end - middle_start + 1;
1514
1515
1516 assert(n < MASK_BITS);
1517 for (; n > 0; idx++, n--)
1518 bit_clear(s, idx);
1519}
1520
1521
1522void sparsebit_set(struct sparsebit *s, sparsebit_idx_t idx)
1523{
1524 sparsebit_set_num(s, idx, 1);
1525}
1526
1527
1528void sparsebit_clear(struct sparsebit *s, sparsebit_idx_t idx)
1529{
1530 sparsebit_clear_num(s, idx, 1);
1531}
1532
1533
1534void sparsebit_set_all(struct sparsebit *s)
1535{
1536 sparsebit_set(s, 0);
1537 sparsebit_set_num(s, 1, ~(sparsebit_idx_t) 0);
1538 assert(sparsebit_all_set(s));
1539}
1540
1541
1542void sparsebit_clear_all(struct sparsebit *s)
1543{
1544 sparsebit_clear(s, 0);
1545 sparsebit_clear_num(s, 1, ~(sparsebit_idx_t) 0);
1546 assert(!sparsebit_any_set(s));
1547}
1548
1549static size_t display_range(FILE *stream, sparsebit_idx_t low,
1550 sparsebit_idx_t high, bool prepend_comma_space)
1551{
1552 char *fmt_str;
1553 size_t sz;
1554
1555
1556 if (low == high)
1557 fmt_str = prepend_comma_space ? ", 0x%lx" : "0x%lx";
1558 else
1559 fmt_str = prepend_comma_space ? ", 0x%lx:0x%lx" : "0x%lx:0x%lx";
1560
1561
1562
1563
1564
1565 if (!stream)
1566 sz = snprintf(NULL, 0, fmt_str, low, high);
1567 else
1568 sz = fprintf(stream, fmt_str, low, high);
1569
1570 return sz;
1571}
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587void sparsebit_dump(FILE *stream, struct sparsebit *s,
1588 unsigned int indent)
1589{
1590 size_t current_line_len = 0;
1591 size_t sz;
1592 struct node *nodep;
1593
1594 if (!sparsebit_any_set(s))
1595 return;
1596
1597
1598 fprintf(stream, "%*s", indent, "");
1599
1600
1601 for (nodep = node_first(s); nodep; nodep = node_next(s, nodep)) {
1602 unsigned int n1;
1603 sparsebit_idx_t low, high;
1604
1605
1606 for (n1 = 0; n1 < MASK_BITS; n1++) {
1607 if (nodep->mask & (1 << n1)) {
1608 low = high = nodep->idx + n1;
1609
1610 for (; n1 < MASK_BITS; n1++) {
1611 if (nodep->mask & (1 << n1))
1612 high = nodep->idx + n1;
1613 else
1614 break;
1615 }
1616
1617 if ((n1 == MASK_BITS) && nodep->num_after)
1618 high += nodep->num_after;
1619
1620
1621
1622
1623
1624 sz = display_range(NULL, low, high,
1625 current_line_len != 0);
1626
1627
1628
1629
1630
1631
1632 if (current_line_len + sz > DUMP_LINE_MAX) {
1633 fputs("\n", stream);
1634 fprintf(stream, "%*s", indent, "");
1635 current_line_len = 0;
1636 }
1637
1638
1639 sz = display_range(stream, low, high,
1640 current_line_len != 0);
1641 current_line_len += sz;
1642 }
1643 }
1644
1645
1646
1647
1648
1649
1650 if (!(nodep->mask & (1 << (MASK_BITS - 1))) && nodep->num_after) {
1651 low = nodep->idx + MASK_BITS;
1652 high = nodep->idx + MASK_BITS + nodep->num_after - 1;
1653
1654
1655
1656
1657
1658 sz = display_range(NULL, low, high,
1659 current_line_len != 0);
1660
1661
1662
1663
1664
1665
1666 if (current_line_len + sz > DUMP_LINE_MAX) {
1667 fputs("\n", stream);
1668 fprintf(stream, "%*s", indent, "");
1669 current_line_len = 0;
1670 }
1671
1672
1673 sz = display_range(stream, low, high,
1674 current_line_len != 0);
1675 current_line_len += sz;
1676 }
1677 }
1678 fputs("\n", stream);
1679}
1680
1681
1682
1683
1684
1685void sparsebit_validate_internal(struct sparsebit *s)
1686{
1687 bool error_detected = false;
1688 struct node *nodep, *prev = NULL;
1689 sparsebit_num_t total_bits_set = 0;
1690 unsigned int n1;
1691
1692
1693 for (nodep = node_first(s); nodep;
1694 prev = nodep, nodep = node_next(s, nodep)) {
1695
1696
1697
1698
1699
1700 for (n1 = 0; n1 < MASK_BITS; n1++)
1701 if (nodep->mask & (1 << n1))
1702 total_bits_set++;
1703
1704 total_bits_set += nodep->num_after;
1705
1706
1707
1708
1709
1710
1711
1712
1713 if (nodep->mask == 0) {
1714 fprintf(stderr, "Node mask of zero, "
1715 "nodep: %p nodep->mask: 0x%x",
1716 nodep, nodep->mask);
1717 error_detected = true;
1718 break;
1719 }
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732 if (nodep->num_after
1733 > (~(sparsebit_num_t) 0) - MASK_BITS + 1) {
1734 fprintf(stderr, "num_after too large, "
1735 "nodep: %p nodep->num_after: 0x%lx",
1736 nodep, nodep->num_after);
1737 error_detected = true;
1738 break;
1739 }
1740
1741
1742 if (nodep->idx % MASK_BITS) {
1743 fprintf(stderr, "Node index not divisible by "
1744 "mask size,\n"
1745 " nodep: %p nodep->idx: 0x%lx "
1746 "MASK_BITS: %lu\n",
1747 nodep, nodep->idx, MASK_BITS);
1748 error_detected = true;
1749 break;
1750 }
1751
1752
1753
1754
1755
1756 if ((nodep->idx + MASK_BITS + nodep->num_after - 1) < nodep->idx) {
1757 fprintf(stderr, "Bits described by node wrap "
1758 "beyond highest supported index,\n"
1759 " nodep: %p nodep->idx: 0x%lx\n"
1760 " MASK_BITS: %lu nodep->num_after: 0x%lx",
1761 nodep, nodep->idx, MASK_BITS, nodep->num_after);
1762 error_detected = true;
1763 break;
1764 }
1765
1766
1767 if (nodep->left) {
1768 if (nodep->left->parent != nodep) {
1769 fprintf(stderr, "Left child parent pointer "
1770 "doesn't point to this node,\n"
1771 " nodep: %p nodep->left: %p "
1772 "nodep->left->parent: %p",
1773 nodep, nodep->left,
1774 nodep->left->parent);
1775 error_detected = true;
1776 break;
1777 }
1778 }
1779
1780 if (nodep->right) {
1781 if (nodep->right->parent != nodep) {
1782 fprintf(stderr, "Right child parent pointer "
1783 "doesn't point to this node,\n"
1784 " nodep: %p nodep->right: %p "
1785 "nodep->right->parent: %p",
1786 nodep, nodep->right,
1787 nodep->right->parent);
1788 error_detected = true;
1789 break;
1790 }
1791 }
1792
1793 if (!nodep->parent) {
1794 if (s->root != nodep) {
1795 fprintf(stderr, "Unexpected root node, "
1796 "s->root: %p nodep: %p",
1797 s->root, nodep);
1798 error_detected = true;
1799 break;
1800 }
1801 }
1802
1803 if (prev) {
1804
1805
1806
1807
1808 if (prev->idx >= nodep->idx) {
1809 fprintf(stderr, "Previous node index "
1810 ">= current node index,\n"
1811 " prev: %p prev->idx: 0x%lx\n"
1812 " nodep: %p nodep->idx: 0x%lx",
1813 prev, prev->idx, nodep, nodep->idx);
1814 error_detected = true;
1815 break;
1816 }
1817
1818
1819
1820
1821
1822 if ((prev->idx + MASK_BITS + prev->num_after - 1)
1823 >= nodep->idx) {
1824 fprintf(stderr, "Previous node bit range "
1825 "overlap with current node bit range,\n"
1826 " prev: %p prev->idx: 0x%lx "
1827 "prev->num_after: 0x%lx\n"
1828 " nodep: %p nodep->idx: 0x%lx "
1829 "nodep->num_after: 0x%lx\n"
1830 " MASK_BITS: %lu",
1831 prev, prev->idx, prev->num_after,
1832 nodep, nodep->idx, nodep->num_after,
1833 MASK_BITS);
1834 error_detected = true;
1835 break;
1836 }
1837
1838
1839
1840
1841
1842
1843 if (nodep->mask == ~(mask_t) 0 &&
1844 prev->idx + MASK_BITS + prev->num_after == nodep->idx) {
1845 fprintf(stderr, "Current node has mask with "
1846 "all bits set and is adjacent to the "
1847 "previous node,\n"
1848 " prev: %p prev->idx: 0x%lx "
1849 "prev->num_after: 0x%lx\n"
1850 " nodep: %p nodep->idx: 0x%lx "
1851 "nodep->num_after: 0x%lx\n"
1852 " MASK_BITS: %lu",
1853 prev, prev->idx, prev->num_after,
1854 nodep, nodep->idx, nodep->num_after,
1855 MASK_BITS);
1856
1857 error_detected = true;
1858 break;
1859 }
1860 }
1861 }
1862
1863 if (!error_detected) {
1864
1865
1866
1867
1868 if (s->num_set != total_bits_set) {
1869 fprintf(stderr, "Number of bits set missmatch,\n"
1870 " s->num_set: 0x%lx total_bits_set: 0x%lx",
1871 s->num_set, total_bits_set);
1872
1873 error_detected = true;
1874 }
1875 }
1876
1877 if (error_detected) {
1878 fputs(" dump_internal:\n", stderr);
1879 sparsebit_dump_internal(stderr, s, 4);
1880 abort();
1881 }
1882}
1883
1884
1885#ifdef FUZZ
1886
1887
1888
1889
1890
1891
1892#include <stdlib.h>
1893#include <assert.h>
1894
1895struct range {
1896 sparsebit_idx_t first, last;
1897 bool set;
1898};
1899
1900struct sparsebit *s;
1901struct range ranges[1000];
1902int num_ranges;
1903
1904static bool get_value(sparsebit_idx_t idx)
1905{
1906 int i;
1907
1908 for (i = num_ranges; --i >= 0; )
1909 if (ranges[i].first <= idx && idx <= ranges[i].last)
1910 return ranges[i].set;
1911
1912 return false;
1913}
1914
1915static void operate(int code, sparsebit_idx_t first, sparsebit_idx_t last)
1916{
1917 sparsebit_num_t num;
1918 sparsebit_idx_t next;
1919
1920 if (first < last) {
1921 num = last - first + 1;
1922 } else {
1923 num = first - last + 1;
1924 first = last;
1925 last = first + num - 1;
1926 }
1927
1928 switch (code) {
1929 case 0:
1930 sparsebit_set(s, first);
1931 assert(sparsebit_is_set(s, first));
1932 assert(!sparsebit_is_clear(s, first));
1933 assert(sparsebit_any_set(s));
1934 assert(!sparsebit_all_clear(s));
1935 if (get_value(first))
1936 return;
1937 if (num_ranges == 1000)
1938 exit(0);
1939 ranges[num_ranges++] = (struct range)
1940 { .first = first, .last = first, .set = true };
1941 break;
1942 case 1:
1943 sparsebit_clear(s, first);
1944 assert(!sparsebit_is_set(s, first));
1945 assert(sparsebit_is_clear(s, first));
1946 assert(sparsebit_any_clear(s));
1947 assert(!sparsebit_all_set(s));
1948 if (!get_value(first))
1949 return;
1950 if (num_ranges == 1000)
1951 exit(0);
1952 ranges[num_ranges++] = (struct range)
1953 { .first = first, .last = first, .set = false };
1954 break;
1955 case 2:
1956 assert(sparsebit_is_set(s, first) == get_value(first));
1957 assert(sparsebit_is_clear(s, first) == !get_value(first));
1958 break;
1959 case 3:
1960 if (sparsebit_any_set(s))
1961 assert(get_value(sparsebit_first_set(s)));
1962 if (sparsebit_any_clear(s))
1963 assert(!get_value(sparsebit_first_clear(s)));
1964 sparsebit_set_all(s);
1965 assert(!sparsebit_any_clear(s));
1966 assert(sparsebit_all_set(s));
1967 num_ranges = 0;
1968 ranges[num_ranges++] = (struct range)
1969 { .first = 0, .last = ~(sparsebit_idx_t)0, .set = true };
1970 break;
1971 case 4:
1972 if (sparsebit_any_set(s))
1973 assert(get_value(sparsebit_first_set(s)));
1974 if (sparsebit_any_clear(s))
1975 assert(!get_value(sparsebit_first_clear(s)));
1976 sparsebit_clear_all(s);
1977 assert(!sparsebit_any_set(s));
1978 assert(sparsebit_all_clear(s));
1979 num_ranges = 0;
1980 break;
1981 case 5:
1982 next = sparsebit_next_set(s, first);
1983 assert(next == 0 || next > first);
1984 assert(next == 0 || get_value(next));
1985 break;
1986 case 6:
1987 next = sparsebit_next_clear(s, first);
1988 assert(next == 0 || next > first);
1989 assert(next == 0 || !get_value(next));
1990 break;
1991 case 7:
1992 next = sparsebit_next_clear(s, first);
1993 if (sparsebit_is_set_num(s, first, num)) {
1994 assert(next == 0 || next > last);
1995 if (first)
1996 next = sparsebit_next_set(s, first - 1);
1997 else if (sparsebit_any_set(s))
1998 next = sparsebit_first_set(s);
1999 else
2000 return;
2001 assert(next == first);
2002 } else {
2003 assert(sparsebit_is_clear(s, first) || next <= last);
2004 }
2005 break;
2006 case 8:
2007 next = sparsebit_next_set(s, first);
2008 if (sparsebit_is_clear_num(s, first, num)) {
2009 assert(next == 0 || next > last);
2010 if (first)
2011 next = sparsebit_next_clear(s, first - 1);
2012 else if (sparsebit_any_clear(s))
2013 next = sparsebit_first_clear(s);
2014 else
2015 return;
2016 assert(next == first);
2017 } else {
2018 assert(sparsebit_is_set(s, first) || next <= last);
2019 }
2020 break;
2021 case 9:
2022 sparsebit_set_num(s, first, num);
2023 assert(sparsebit_is_set_num(s, first, num));
2024 assert(!sparsebit_is_clear_num(s, first, num));
2025 assert(sparsebit_any_set(s));
2026 assert(!sparsebit_all_clear(s));
2027 if (num_ranges == 1000)
2028 exit(0);
2029 ranges[num_ranges++] = (struct range)
2030 { .first = first, .last = last, .set = true };
2031 break;
2032 case 10:
2033 sparsebit_clear_num(s, first, num);
2034 assert(!sparsebit_is_set_num(s, first, num));
2035 assert(sparsebit_is_clear_num(s, first, num));
2036 assert(sparsebit_any_clear(s));
2037 assert(!sparsebit_all_set(s));
2038 if (num_ranges == 1000)
2039 exit(0);
2040 ranges[num_ranges++] = (struct range)
2041 { .first = first, .last = last, .set = false };
2042 break;
2043 case 11:
2044 sparsebit_validate_internal(s);
2045 break;
2046 default:
2047 break;
2048 }
2049}
2050
2051unsigned char get8(void)
2052{
2053 int ch;
2054
2055 ch = getchar();
2056 if (ch == EOF)
2057 exit(0);
2058 return ch;
2059}
2060
2061uint64_t get64(void)
2062{
2063 uint64_t x;
2064
2065 x = get8();
2066 x = (x << 8) | get8();
2067 x = (x << 8) | get8();
2068 x = (x << 8) | get8();
2069 x = (x << 8) | get8();
2070 x = (x << 8) | get8();
2071 x = (x << 8) | get8();
2072 return (x << 8) | get8();
2073}
2074
2075int main(void)
2076{
2077 s = sparsebit_alloc();
2078 for (;;) {
2079 uint8_t op = get8() & 0xf;
2080 uint64_t first = get64();
2081 uint64_t last = get64();
2082
2083 operate(op, first, last);
2084 }
2085}
2086#endif
2087