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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
|
/* Functions to support expandable bitsets.
Copyright (C) 2002-2006, 2009-2013 Free Software Foundation, Inc.
Contributed by Michael Hayes (m.hayes@elec.canterbury.ac.nz).
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "ebitset.h"
#include "obstack.h"
#include <stdlib.h>
#include <string.h>
/* This file implements expandable bitsets. These bitsets can be of
arbitrary length and are more efficient than arrays of bits for
large sparse sets.
Empty elements are represented by a NULL pointer in the table of
element pointers. An alternative is to point to a special zero
element. Similarly, we could represent an all 1's element with
another special ones element pointer.
Bitsets are commonly empty so we need to ensure that this special
case is fast. A zero bitset is indicated when cdata is 0. This is
conservative since cdata may be non zero and the bitset may still
be zero.
The bitset cache can be disabled either by setting cindex to
BITSET_WINDEX_MAX or by setting csize to 0. Here
we use the former approach since cindex needs to be updated whenever
cdata is changed.
*/
/* Number of words to use for each element. */
#define EBITSET_ELT_WORDS 2
/* Number of bits stored in each element. */
#define EBITSET_ELT_BITS \
((unsigned int) (EBITSET_ELT_WORDS * BITSET_WORD_BITS))
/* Ebitset element. We use an array of bits. */
typedef struct ebitset_elt_struct
{
union
{
bitset_word words[EBITSET_ELT_WORDS]; /* Bits that are set. */
struct ebitset_elt_struct *next;
}
u;
}
ebitset_elt;
typedef ebitset_elt *ebitset_elts;
/* Number of elements to initially allocate. */
#ifndef EBITSET_INITIAL_SIZE
#define EBITSET_INITIAL_SIZE 2
#endif
enum ebitset_find_mode
{ EBITSET_FIND, EBITSET_CREATE, EBITSET_SUBST };
static ebitset_elt ebitset_zero_elts[1]; /* Elements of all zero bits. */
/* Obstack to allocate bitset elements from. */
static struct obstack ebitset_obstack;
static bool ebitset_obstack_init = false;
static ebitset_elt *ebitset_free_list; /* Free list of bitset elements. */
#define EBITSET_N_ELTS(N) (((N) + EBITSET_ELT_BITS - 1) / EBITSET_ELT_BITS)
#define EBITSET_ELTS(BSET) ((BSET)->e.elts)
#define EBITSET_SIZE(BSET) EBITSET_N_ELTS (BITSET_NBITS_ (BSET))
#define EBITSET_ASIZE(BSET) ((BSET)->e.size)
#define EBITSET_NEXT(ELT) ((ELT)->u.next)
#define EBITSET_WORDS(ELT) ((ELT)->u.words)
/* Disable bitset cache and mark BSET as being zero. */
#define EBITSET_ZERO_SET(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX, \
(BSET)->b.cdata = 0)
#define EBITSET_CACHE_DISABLE(BSET) ((BSET)->b.cindex = BITSET_WINDEX_MAX)
/* Disable bitset cache and mark BSET as being possibly non-zero. */
#define EBITSET_NONZERO_SET(BSET) \
(EBITSET_CACHE_DISABLE (BSET), (BSET)->b.cdata = (bitset_word *)~0)
/* A conservative estimate of whether the bitset is zero.
This is non-zero only if we know for sure that the bitset is zero. */
#define EBITSET_ZERO_P(BSET) ((BSET)->b.cdata == 0)
/* Enable cache to point to element with table index EINDEX.
The element must exist. */
#define EBITSET_CACHE_SET(BSET, EINDEX) \
((BSET)->b.cindex = (EINDEX) * EBITSET_ELT_WORDS, \
(BSET)->b.cdata = EBITSET_WORDS (EBITSET_ELTS (BSET) [EINDEX]))
#undef min
#undef max
#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))
static bitset_bindex
ebitset_resize (bitset src, bitset_bindex n_bits)
{
bitset_windex oldsize;
bitset_windex newsize;
if (n_bits == BITSET_NBITS_ (src))
return n_bits;
oldsize = EBITSET_SIZE (src);
newsize = EBITSET_N_ELTS (n_bits);
if (oldsize < newsize)
{
bitset_windex size;
/* The bitset needs to grow. If we already have enough memory
allocated, then just zero what we need. */
if (newsize > EBITSET_ASIZE (src))
{
/* We need to allocate more memory. When oldsize is
non-zero this means that we are changing the size, so
grow the bitset 25% larger than requested to reduce
number of reallocations. */
if (oldsize == 0)
size = newsize;
else
size = newsize + newsize / 4;
EBITSET_ELTS (src)
= realloc (EBITSET_ELTS (src), size * sizeof (ebitset_elt *));
EBITSET_ASIZE (src) = size;
}
memset (EBITSET_ELTS (src) + oldsize, 0,
(newsize - oldsize) * sizeof (ebitset_elt *));
}
else
{
/* The bitset needs to shrink. There's no point deallocating
the memory unless it is shrinking by a reasonable amount. */
if ((oldsize - newsize) >= oldsize / 2)
{
EBITSET_ELTS (src)
= realloc (EBITSET_ELTS (src), newsize * sizeof (ebitset_elt *));
EBITSET_ASIZE (src) = newsize;
}
/* Need to prune any excess bits. FIXME. */
}
BITSET_NBITS_ (src) = n_bits;
return n_bits;
}
/* Allocate a ebitset element. The bits are not cleared. */
static inline ebitset_elt *
ebitset_elt_alloc (void)
{
ebitset_elt *elt;
if (ebitset_free_list != 0)
{
elt = ebitset_free_list;
ebitset_free_list = EBITSET_NEXT (elt);
}
else
{
if (!ebitset_obstack_init)
{
ebitset_obstack_init = true;
/* Let particular systems override the size of a chunk. */
#ifndef OBSTACK_CHUNK_SIZE
#define OBSTACK_CHUNK_SIZE 0
#endif
/* Let them override the alloc and free routines too. */
#ifndef OBSTACK_CHUNK_ALLOC
#define OBSTACK_CHUNK_ALLOC xmalloc
#endif
#ifndef OBSTACK_CHUNK_FREE
#define OBSTACK_CHUNK_FREE free
#endif
#if ! defined __GNUC__ || __GNUC__ < 2
#define __alignof__(type) 0
#endif
obstack_specify_allocation (&ebitset_obstack, OBSTACK_CHUNK_SIZE,
__alignof__ (ebitset_elt),
OBSTACK_CHUNK_ALLOC,
OBSTACK_CHUNK_FREE);
}
/* Perhaps we should add a number of new elements to the free
list. */
elt = (ebitset_elt *) obstack_alloc (&ebitset_obstack,
sizeof (ebitset_elt));
}
return elt;
}
/* Allocate a ebitset element. The bits are cleared. */
static inline ebitset_elt *
ebitset_elt_calloc (void)
{
ebitset_elt *elt;
elt = ebitset_elt_alloc ();
memset (EBITSET_WORDS (elt), 0, sizeof (EBITSET_WORDS (elt)));
return elt;
}
static inline void
ebitset_elt_free (ebitset_elt *elt)
{
EBITSET_NEXT (elt) = ebitset_free_list;
ebitset_free_list = elt;
}
/* Remove element with index EINDEX from bitset BSET. */
static inline void
ebitset_elt_remove (bitset bset, bitset_windex eindex)
{
ebitset_elts *elts;
ebitset_elt *elt;
elts = EBITSET_ELTS (bset);
elt = elts[eindex];
elts[eindex] = 0;
ebitset_elt_free (elt);
}
/* Add ELT into elts at index EINDEX of bitset BSET. */
static inline void
ebitset_elt_add (bitset bset, ebitset_elt *elt, bitset_windex eindex)
{
ebitset_elts *elts;
elts = EBITSET_ELTS (bset);
/* Assume that the elts entry not allocated. */
elts[eindex] = elt;
}
/* Are all bits in an element zero? */
static inline bool
ebitset_elt_zero_p (ebitset_elt *elt)
{
int i;
for (i = 0; i < EBITSET_ELT_WORDS; i++)
if (EBITSET_WORDS (elt)[i])
return false;
return true;
}
static ebitset_elt *
ebitset_elt_find (bitset bset, bitset_bindex bindex,
enum ebitset_find_mode mode)
{
ebitset_elt *elt;
bitset_windex size;
bitset_windex eindex;
ebitset_elts *elts;
eindex = bindex / EBITSET_ELT_BITS;
elts = EBITSET_ELTS (bset);
size = EBITSET_SIZE (bset);
if (eindex < size)
{
if ((elt = elts[eindex]))
{
if (EBITSET_WORDS (elt) == bset->b.cdata)
return elt;
EBITSET_CACHE_SET (bset, eindex);
return elt;
}
}
/* The element could not be found. */
switch (mode)
{
default:
abort ();
case EBITSET_FIND:
return 0;
case EBITSET_CREATE:
if (eindex >= size)
ebitset_resize (bset, bindex);
/* Create a new element. */
elt = ebitset_elt_calloc ();
ebitset_elt_add (bset, elt, eindex);
EBITSET_CACHE_SET (bset, eindex);
return elt;
case EBITSET_SUBST:
return &ebitset_zero_elts[0];
}
}
/* Weed out the zero elements from the elts. */
static inline bitset_windex
ebitset_weed (bitset bset)
{
ebitset_elts *elts;
bitset_windex j;
bitset_windex count;
if (EBITSET_ZERO_P (bset))
return 0;
elts = EBITSET_ELTS (bset);
count = 0;
for (j = 0; j < EBITSET_SIZE (bset); j++)
{
ebitset_elt *elt = elts[j];
if (elt)
{
if (ebitset_elt_zero_p (elt))
{
ebitset_elt_remove (bset, j);
count++;
}
}
else
count++;
}
count = j - count;
if (!count)
{
/* All the bits are zero. We could shrink the elts.
For now just mark BSET as known to be zero. */
EBITSET_ZERO_SET (bset);
}
else
EBITSET_NONZERO_SET (bset);
return count;
}
/* Set all bits in the bitset to zero. */
static inline void
ebitset_zero (bitset bset)
{
ebitset_elts *elts;
bitset_windex j;
if (EBITSET_ZERO_P (bset))
return;
elts = EBITSET_ELTS (bset);
for (j = 0; j < EBITSET_SIZE (bset); j++)
{
ebitset_elt *elt = elts[j];
if (elt)
ebitset_elt_remove (bset, j);
}
/* All the bits are zero. We could shrink the elts.
For now just mark BSET as known to be zero. */
EBITSET_ZERO_SET (bset);
}
static inline bool
ebitset_equal_p (bitset dst, bitset src)
{
ebitset_elts *selts;
ebitset_elts *delts;
bitset_windex j;
if (src == dst)
return true;
ebitset_weed (dst);
ebitset_weed (src);
if (EBITSET_SIZE (src) != EBITSET_SIZE (dst))
return false;
selts = EBITSET_ELTS (src);
delts = EBITSET_ELTS (dst);
for (j = 0; j < EBITSET_SIZE (src); j++)
{
unsigned int i;
ebitset_elt *selt = selts[j];
ebitset_elt *delt = delts[j];
if (!selt && !delt)
continue;
if ((selt && !delt) || (!selt && delt))
return false;
for (i = 0; i < EBITSET_ELT_WORDS; i++)
if (EBITSET_WORDS (selt)[i] != EBITSET_WORDS (delt)[i])
return false;
}
return true;
}
/* Copy bits from bitset SRC to bitset DST. */
static inline void
ebitset_copy_ (bitset dst, bitset src)
{
ebitset_elts *selts;
ebitset_elts *delts;
bitset_windex j;
if (src == dst)
return;
ebitset_zero (dst);
if (BITSET_NBITS_ (dst) != BITSET_NBITS_ (src))
ebitset_resize (dst, BITSET_NBITS_ (src));
selts = EBITSET_ELTS (src);
delts = EBITSET_ELTS (dst);
for (j = 0; j < EBITSET_SIZE (src); j++)
{
ebitset_elt *selt = selts[j];
if (selt)
{
ebitset_elt *tmp;
tmp = ebitset_elt_alloc ();
delts[j] = tmp;
memcpy (EBITSET_WORDS (tmp), EBITSET_WORDS (selt),
sizeof (EBITSET_WORDS (selt)));
}
}
EBITSET_NONZERO_SET (dst);
}
/* Copy bits from bitset SRC to bitset DST. Return true if
bitsets different. */
static inline bool
ebitset_copy_cmp (bitset dst, bitset src)
{
if (src == dst)
return false;
if (EBITSET_ZERO_P (dst))
{
ebitset_copy_ (dst, src);
return !EBITSET_ZERO_P (src);
}
if (ebitset_equal_p (dst, src))
return false;
ebitset_copy_ (dst, src);
return true;
}
/* Set bit BITNO in bitset DST. */
static void
ebitset_set (bitset dst, bitset_bindex bitno)
{
bitset_windex windex = bitno / BITSET_WORD_BITS;
ebitset_elt_find (dst, bitno, EBITSET_CREATE);
dst->b.cdata[windex - dst->b.cindex] |=
(bitset_word) 1 << (bitno % BITSET_WORD_BITS);
}
/* Reset bit BITNO in bitset DST. */
static void
ebitset_reset (bitset dst, bitset_bindex bitno)
{
bitset_windex windex = bitno / BITSET_WORD_BITS;
if (!ebitset_elt_find (dst, bitno, EBITSET_FIND))
return;
dst->b.cdata[windex - dst->b.cindex] &=
~((bitset_word) 1 << (bitno % BITSET_WORD_BITS));
/* If all the data is zero, perhaps we should remove it now...
However, there is a good chance that the element will be needed
again soon. */
}
/* Test bit BITNO in bitset SRC. */
static bool
ebitset_test (bitset src, bitset_bindex bitno)
{
bitset_windex windex = bitno / BITSET_WORD_BITS;
return (ebitset_elt_find (src, bitno, EBITSET_FIND)
&& ((src->b.cdata[windex - src->b.cindex]
>> (bitno % BITSET_WORD_BITS))
& 1));
}
static void
ebitset_free (bitset bset)
{
ebitset_zero (bset);
free (EBITSET_ELTS (bset));
}
/* Find list of up to NUM bits set in BSET starting from and including
*NEXT and store in array LIST. Return with actual number of bits
found and with *NEXT indicating where search stopped. */
static bitset_bindex
ebitset_list_reverse (bitset bset, bitset_bindex *list,
bitset_bindex num, bitset_bindex *next)
{
bitset_bindex n_bits;
bitset_bindex bitno;
bitset_bindex rbitno;
unsigned int bcount;
bitset_bindex boffset;
bitset_windex windex;
bitset_windex eindex;
bitset_windex woffset;
bitset_bindex count;
bitset_windex size;
ebitset_elts *elts;
if (EBITSET_ZERO_P (bset))
return 0;
size = EBITSET_SIZE (bset);
n_bits = size * EBITSET_ELT_BITS;
rbitno = *next;
if (rbitno >= n_bits)
return 0;
elts = EBITSET_ELTS (bset);
bitno = n_bits - (rbitno + 1);
windex = bitno / BITSET_WORD_BITS;
eindex = bitno / EBITSET_ELT_BITS;
woffset = windex - eindex * EBITSET_ELT_WORDS;
/* If num is 1, we could speed things up with a binary search
of the word of interest. */
count = 0;
bcount = bitno % BITSET_WORD_BITS;
boffset = windex * BITSET_WORD_BITS;
do
{
ebitset_elt *elt;
bitset_word *srcp;
elt = elts[eindex];
if (elt)
{
srcp = EBITSET_WORDS (elt);
do
{
bitset_word word;
word = srcp[woffset] << (BITSET_WORD_BITS - 1 - bcount);
for (; word; bcount--)
{
if (word & BITSET_MSB)
{
list[count++] = boffset + bcount;
if (count >= num)
{
*next = n_bits - (boffset + bcount);
return count;
}
}
word <<= 1;
}
boffset -= BITSET_WORD_BITS;
bcount = BITSET_WORD_BITS - 1;
}
while (woffset--);
}
woffset = EBITSET_ELT_WORDS - 1;
boffset = eindex * EBITSET_ELT_BITS - BITSET_WORD_BITS;
}
while (eindex--);
*next = n_bits - (boffset + 1);
return count;
}
/* Find list of up to NUM bits set in BSET starting from and including
*NEXT and store in array LIST. Return with actual number of bits
found and with *NEXT indicating where search stopped. */
static bitset_bindex
ebitset_list (bitset bset, bitset_bindex *list,
bitset_bindex num, bitset_bindex *next)
{
bitset_bindex bitno;
bitset_windex windex;
bitset_windex eindex;
bitset_bindex count;
bitset_windex size;
ebitset_elt *elt;
bitset_word word;
ebitset_elts *elts;
if (EBITSET_ZERO_P (bset))
return 0;
bitno = *next;
count = 0;
elts = EBITSET_ELTS (bset);
size = EBITSET_SIZE (bset);
eindex = bitno / EBITSET_ELT_BITS;
if (bitno % EBITSET_ELT_BITS)
{
/* We need to start within an element. This is not very common. */
elt = elts[eindex];
if (elt)
{
bitset_windex woffset;
bitset_word *srcp = EBITSET_WORDS (elt);
windex = bitno / BITSET_WORD_BITS;
woffset = eindex * EBITSET_ELT_WORDS;
for (; (windex - woffset) < EBITSET_ELT_WORDS; windex++)
{
word = srcp[windex - woffset] >> (bitno % BITSET_WORD_BITS);
for (; word; bitno++)
{
if (word & 1)
{
list[count++] = bitno;
if (count >= num)
{
*next = bitno + 1;
return count;
}
}
word >>= 1;
}
bitno = (windex + 1) * BITSET_WORD_BITS;
}
}
/* Skip to next element. */
eindex++;
}
/* If num is 1, we could speed things up with a binary search
of the word of interest. */
for (; eindex < size; eindex++)
{
int i;
bitset_word *srcp;
elt = elts[eindex];
if (!elt)
continue;
srcp = EBITSET_WORDS (elt);
windex = eindex * EBITSET_ELT_WORDS;
if ((count + EBITSET_ELT_BITS) < num)
{
/* The coast is clear, plant boot! */
#if EBITSET_ELT_WORDS == 2
word = srcp[0];
if (word)
{
if (!(word & 0xffff))
{
word >>= 16;
bitno += 16;
}
if (!(word & 0xff))
{
word >>= 8;
bitno += 8;
}
for (; word; bitno++)
{
if (word & 1)
list[count++] = bitno;
word >>= 1;
}
}
windex++;
bitno = windex * BITSET_WORD_BITS;
word = srcp[1];
if (word)
{
if (!(word & 0xffff))
{
word >>= 16;
bitno += 16;
}
for (; word; bitno++)
{
if (word & 1)
list[count++] = bitno;
word >>= 1;
}
}
windex++;
bitno = windex * BITSET_WORD_BITS;
#else
for (i = 0; i < EBITSET_ELT_WORDS; i++, windex++)
{
bitno = windex * BITSET_WORD_BITS;
word = srcp[i];
if (word)
{
if (!(word & 0xffff))
{
word >>= 16;
bitno += 16;
}
if (!(word & 0xff))
{
word >>= 8;
bitno += 8;
}
for (; word; bitno++)
{
if (word & 1)
list[count++] = bitno;
word >>= 1;
}
}
}
#endif
}
else
{
/* Tread more carefully since we need to check
if array overflows. */
for (i = 0; i < EBITSET_ELT_WORDS; i++, windex++)
{
bitno = windex * BITSET_WORD_BITS;
for (word = srcp[i]; word; bitno++)
{
if (word & 1)
{
list[count++] = bitno;
if (count >= num)
{
*next = bitno + 1;
return count;
}
}
word >>= 1;
}
}
}
}
*next = bitno;
return count;
}
/* Ensure that any unused bits within the last element are clear. */
static inline void
ebitset_unused_clear (bitset dst)
{
unsigned int last_bit;
bitset_bindex n_bits;
n_bits = BITSET_NBITS_ (dst);
last_bit = n_bits % EBITSET_ELT_BITS;
if (last_bit)
{
bitset_windex eindex;
ebitset_elts *elts;
ebitset_elt *elt;
elts = EBITSET_ELTS (dst);
eindex = n_bits / EBITSET_ELT_BITS;
elt = elts[eindex];
if (elt)
{
bitset_windex windex;
bitset_windex woffset;
bitset_word *srcp = EBITSET_WORDS (elt);
windex = n_bits / BITSET_WORD_BITS;
woffset = eindex * EBITSET_ELT_WORDS;
srcp[windex - woffset] &= ((bitset_word) 1 << last_bit) - 1;
windex++;
for (; (windex - woffset) < EBITSET_ELT_WORDS; windex++)
srcp[windex - woffset] = 0;
}
}
}
static void
ebitset_ones (bitset dst)
{
bitset_windex j;
ebitset_elt *elt;
for (j = 0; j < EBITSET_SIZE (dst); j++)
{
/* Create new elements if they cannot be found. Perhaps
we should just add pointers to a ones element? */
elt =
ebitset_elt_find (dst, j * EBITSET_ELT_BITS, EBITSET_CREATE);
memset (EBITSET_WORDS (elt), -1, sizeof (EBITSET_WORDS (elt)));
}
EBITSET_NONZERO_SET (dst);
ebitset_unused_clear (dst);
}
static bool
ebitset_empty_p (bitset dst)
{
ebitset_elts *elts;
bitset_windex j;
if (EBITSET_ZERO_P (dst))
return 1;
elts = EBITSET_ELTS (dst);
for (j = 0; j < EBITSET_SIZE (dst); j++)
{
ebitset_elt *elt = elts[j];
if (elt)
{
if (!ebitset_elt_zero_p (elt))
return 0;
/* Do some weeding as we go. */
ebitset_elt_remove (dst, j);
}
}
/* All the bits are zero. We could shrink the elts.
For now just mark DST as known to be zero. */
EBITSET_ZERO_SET (dst);
return 1;
}
static void
ebitset_not (bitset dst, bitset src)
{
unsigned int i;
ebitset_elt *selt;
ebitset_elt *delt;
bitset_windex j;
ebitset_resize (dst, BITSET_NBITS_ (src));
for (j = 0; j < EBITSET_SIZE (src); j++)
{
/* Create new elements for dst if they cannot be found
or substitute zero elements if src elements not found. */
selt =
ebitset_elt_find (dst, j * EBITSET_ELT_BITS, EBITSET_SUBST);
delt =
ebitset_elt_find (dst, j * EBITSET_ELT_BITS, EBITSET_CREATE);
for (i = 0; i < EBITSET_ELT_WORDS; i++)
EBITSET_WORDS (delt)[i] = ~EBITSET_WORDS (selt)[i];
}
EBITSET_NONZERO_SET (dst);
ebitset_unused_clear (dst);
}
/* Is DST == DST | SRC? */
static bool
ebitset_subset_p (bitset dst, bitset src)
{
bitset_windex j;
ebitset_elts *selts;
ebitset_elts *delts;
bitset_windex ssize;
bitset_windex dsize;
selts = EBITSET_ELTS (src);
delts = EBITSET_ELTS (dst);
ssize = EBITSET_SIZE (src);
dsize = EBITSET_SIZE (dst);
for (j = 0; j < ssize; j++)
{
unsigned int i;
ebitset_elt *selt;
ebitset_elt *delt;
selt = j < ssize ? selts[j] : 0;
delt = j < dsize ? delts[j] : 0;
if (!selt && !delt)
continue;
if (!selt)
selt = &ebitset_zero_elts[0];
if (!delt)
delt = &ebitset_zero_elts[0];
for (i = 0; i < EBITSET_ELT_WORDS; i++)
if (EBITSET_WORDS (delt)[i]
!= (EBITSET_WORDS (selt)[i] | EBITSET_WORDS (delt)[i]))
return false;
}
return true;
}
/* Is DST & SRC == 0? */
static bool
ebitset_disjoint_p (bitset dst, bitset src)
{
bitset_windex j;
ebitset_elts *selts;
ebitset_elts *delts;
bitset_windex ssize;
bitset_windex dsize;
selts = EBITSET_ELTS (src);
delts = EBITSET_ELTS (dst);
ssize = EBITSET_SIZE (src);
dsize = EBITSET_SIZE (dst);
for (j = 0; j < ssize; j++)
{
unsigned int i;
ebitset_elt *selt;
ebitset_elt *delt;
selt = j < ssize ? selts[j] : 0;
delt = j < dsize ? delts[j] : 0;
if (!selt || !delt)
continue;
for (i = 0; i < EBITSET_ELT_WORDS; i++)
if ((EBITSET_WORDS (selt)[i] & EBITSET_WORDS (delt)[i]))
return false;
}
return true;
}
static bool
ebitset_op3_cmp (bitset dst, bitset src1, bitset src2, enum bitset_ops op)
{
bitset_windex ssize1;
bitset_windex ssize2;
bitset_windex dsize;
bitset_windex size;
ebitset_elts *selts1;
ebitset_elts *selts2;
ebitset_elts *delts;
bitset_word *srcp1;
bitset_word *srcp2;
bitset_word *dstp;
bool changed = false;
unsigned int i;
bitset_windex j;
ebitset_resize (dst, max (BITSET_NBITS_ (src1), BITSET_NBITS_ (src2)));
ssize1 = EBITSET_SIZE (src1);
ssize2 = EBITSET_SIZE (src2);
dsize = EBITSET_SIZE (dst);
size = ssize1;
if (size < ssize2)
size = ssize2;
selts1 = EBITSET_ELTS (src1);
selts2 = EBITSET_ELTS (src2);
delts = EBITSET_ELTS (dst);
for (j = 0; j < size; j++)
{
ebitset_elt *selt1;
ebitset_elt *selt2;
ebitset_elt *delt;
selt1 = j < ssize1 ? selts1[j] : 0;
selt2 = j < ssize2 ? selts2[j] : 0;
delt = j < dsize ? delts[j] : 0;
if (!selt1 && !selt2)
{
if (delt)
{
changed = true;
ebitset_elt_remove (dst, j);
}
continue;
}
if (!selt1)
selt1 = &ebitset_zero_elts[0];
if (!selt2)
selt2 = &ebitset_zero_elts[0];
if (!delt)
delt = ebitset_elt_calloc ();
else
delts[j] = 0;
srcp1 = EBITSET_WORDS (selt1);
srcp2 = EBITSET_WORDS (selt2);
dstp = EBITSET_WORDS (delt);
switch (op)
{
default:
abort ();
case BITSET_OP_OR:
for (i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
{
bitset_word tmp = *srcp1++ | *srcp2++;
if (*dstp != tmp)
{
changed = true;
*dstp = tmp;
}
}
break;
case BITSET_OP_AND:
for (i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
{
bitset_word tmp = *srcp1++ & *srcp2++;
if (*dstp != tmp)
{
changed = true;
*dstp = tmp;
}
}
break;
case BITSET_OP_XOR:
for (i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
{
bitset_word tmp = *srcp1++ ^ *srcp2++;
if (*dstp != tmp)
{
changed = true;
*dstp = tmp;
}
}
break;
case BITSET_OP_ANDN:
for (i = 0; i < EBITSET_ELT_WORDS; i++, dstp++)
{
bitset_word tmp = *srcp1++ & ~(*srcp2++);
if (*dstp != tmp)
{
changed = true;
*dstp = tmp;
}
}
break;
}
if (!ebitset_elt_zero_p (delt))
{
ebitset_elt_add (dst, delt, j);
}
else
{
ebitset_elt_free (delt);
}
}
/* If we have elements of DST left over, free them all. */
for (; j < dsize; j++)
{
ebitset_elt *delt;
changed = true;
delt = delts[j];
if (delt)
ebitset_elt_remove (dst, j);
}
EBITSET_NONZERO_SET (dst);
return changed;
}
static bool
ebitset_and_cmp (bitset dst, bitset src1, bitset src2)
{
bool changed;
if (EBITSET_ZERO_P (src2))
{
ebitset_weed (dst);
changed = EBITSET_ZERO_P (dst);
ebitset_zero (dst);
return changed;
}
else if (EBITSET_ZERO_P (src1))
{
ebitset_weed (dst);
changed = EBITSET_ZERO_P (dst);
ebitset_zero (dst);
return changed;
}
return ebitset_op3_cmp (dst, src1, src2, BITSET_OP_AND);
}
static void
ebitset_and (bitset dst, bitset src1, bitset src2)
{
ebitset_and_cmp (dst, src1, src2);
}
static bool
ebitset_andn_cmp (bitset dst, bitset src1, bitset src2)
{
bool changed;
if (EBITSET_ZERO_P (src2))
{
return ebitset_copy_cmp (dst, src1);
}
else if (EBITSET_ZERO_P (src1))
{
ebitset_weed (dst);
changed = EBITSET_ZERO_P (dst);
ebitset_zero (dst);
return changed;
}
return ebitset_op3_cmp (dst, src1, src2, BITSET_OP_ANDN);
}
static void
ebitset_andn (bitset dst, bitset src1, bitset src2)
{
ebitset_andn_cmp (dst, src1, src2);
}
static bool
ebitset_or_cmp (bitset dst, bitset src1, bitset src2)
{
if (EBITSET_ZERO_P (src2))
{
return ebitset_copy_cmp (dst, src1);
}
else if (EBITSET_ZERO_P (src1))
{
return ebitset_copy_cmp (dst, src2);
}
return ebitset_op3_cmp (dst, src1, src2, BITSET_OP_OR);
}
static void
ebitset_or (bitset dst, bitset src1, bitset src2)
{
ebitset_or_cmp (dst, src1, src2);
}
static bool
ebitset_xor_cmp (bitset dst, bitset src1, bitset src2)
{
if (EBITSET_ZERO_P (src2))
{
return ebitset_copy_cmp (dst, src1);
}
else if (EBITSET_ZERO_P (src1))
{
return ebitset_copy_cmp (dst, src2);
}
return ebitset_op3_cmp (dst, src1, src2, BITSET_OP_XOR);
}
static void
ebitset_xor (bitset dst, bitset src1, bitset src2)
{
ebitset_xor_cmp (dst, src1, src2);
}
static void
ebitset_copy (bitset dst, bitset src)
{
if (BITSET_COMPATIBLE_ (dst, src))
ebitset_copy_ (dst, src);
else
bitset_copy_ (dst, src);
}
/* Vector of operations for linked-list bitsets. */
struct bitset_vtable ebitset_vtable = {
ebitset_set,
ebitset_reset,
bitset_toggle_,
ebitset_test,
ebitset_resize,
bitset_size_,
bitset_count_,
ebitset_empty_p,
ebitset_ones,
ebitset_zero,
ebitset_copy,
ebitset_disjoint_p,
ebitset_equal_p,
ebitset_not,
ebitset_subset_p,
ebitset_and,
ebitset_and_cmp,
ebitset_andn,
ebitset_andn_cmp,
ebitset_or,
ebitset_or_cmp,
ebitset_xor,
ebitset_xor_cmp,
bitset_and_or_,
bitset_and_or_cmp_,
bitset_andn_or_,
bitset_andn_or_cmp_,
bitset_or_and_,
bitset_or_and_cmp_,
ebitset_list,
ebitset_list_reverse,
ebitset_free,
BITSET_TABLE
};
/* Return size of initial structure. */
size_t
ebitset_bytes (bitset_bindex n_bits ATTRIBUTE_UNUSED)
{
return sizeof (struct ebitset_struct);
}
/* Initialize a bitset. */
bitset
ebitset_init (bitset bset, bitset_bindex n_bits)
{
bset->b.vtable = &ebitset_vtable;
bset->b.csize = EBITSET_ELT_WORDS;
EBITSET_ZERO_SET (bset);
EBITSET_ASIZE (bset) = 0;
EBITSET_ELTS (bset) = 0;
ebitset_resize (bset, n_bits);
return bset;
}
void
ebitset_release_memory (void)
{
ebitset_free_list = 0;
if (ebitset_obstack_init)
{
ebitset_obstack_init = false;
obstack_free (&ebitset_obstack, NULL);
}
}
|