forked from postgres/postgres
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbitmapset.c
1447 lines (1242 loc) · 30.2 KB
/
bitmapset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
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
/*-------------------------------------------------------------------------
*
* bitmapset.c
* PostgreSQL generic bitmap set package
*
* A bitmap set can represent any set of nonnegative integers, although
* it is mainly intended for sets where the maximum value is not large,
* say at most a few hundred. By convention, we always represent a set with
* the minimum possible number of words, i.e, there are never any trailing
* zero words. Enforcing this requires that an empty set is represented as
* NULL. Because an empty Bitmapset is represented as NULL, a non-NULL
* Bitmapset always has at least 1 Bitmapword. We can exploit this fact to
* speed up various loops over the Bitmapset's words array by using "do while"
* loops instead of "for" loops. This means the code does not waste time
* checking the loop condition before the first iteration. For Bitmapsets
* containing only a single word (likely the majority of them) this halves the
* number of loop condition checks.
*
* Callers must ensure that the set returned by functions in this file which
* adjust the members of an existing set is assigned to all pointers pointing
* to that existing set. No guarantees are made that we'll ever modify the
* existing set in-place and return it.
*
* To help find bugs caused by callers failing to record the return value of
* the function which manipulates an existing set, we support building with
* REALLOCATE_BITMAPSETS. This results in the set being reallocated each time
* the set is altered and the existing being pfreed. This is useful as if any
* references still exist to the old set, we're more likely to notice as
* any users of the old set will be accessing pfree'd memory. This option is
* only intended to be used for debugging.
*
* Copyright (c) 2003-2024, PostgreSQL Global Development Group
*
* IDENTIFICATION
* src/backend/nodes/bitmapset.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "common/hashfn.h"
#include "nodes/bitmapset.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#define WORDNUM(x) ((x) / BITS_PER_BITMAPWORD)
#define BITNUM(x) ((x) % BITS_PER_BITMAPWORD)
#define BITMAPSET_SIZE(nwords) \
(offsetof(Bitmapset, words) + (nwords) * sizeof(bitmapword))
/*----------
* This is a well-known cute trick for isolating the rightmost one-bit
* in a word. It assumes two's complement arithmetic. Consider any
* nonzero value, and focus attention on the rightmost one. The value is
* then something like
* xxxxxx10000
* where x's are unspecified bits. The two's complement negative is formed
* by inverting all the bits and adding one. Inversion gives
* yyyyyy01111
* where each y is the inverse of the corresponding x. Incrementing gives
* yyyyyy10000
* and then ANDing with the original value gives
* 00000010000
* This works for all cases except original value = zero, where of course
* we get zero.
*----------
*/
#define RIGHTMOST_ONE(x) ((signedbitmapword) (x) & -((signedbitmapword) (x)))
#define HAS_MULTIPLE_ONES(x) ((bitmapword) RIGHTMOST_ONE(x) != (x))
#ifdef USE_ASSERT_CHECKING
/*
* bms_is_valid_set - for cassert builds to check for valid sets
*/
static bool
bms_is_valid_set(const Bitmapset *a)
{
/* NULL is the correct representation of an empty set */
if (a == NULL)
return true;
/* check the node tag is set correctly. pfree'd pointer, maybe? */
if (!IsA(a, Bitmapset))
return false;
/* trailing zero words are not allowed */
if (a->words[a->nwords - 1] == 0)
return false;
return true;
}
#endif
#ifdef REALLOCATE_BITMAPSETS
/*
* bms_copy_and_free
* Only required in REALLOCATE_BITMAPSETS builds. Provide a simple way
* to return a freshly allocated set and pfree the original.
*
* Note: callers which accept multiple sets must be careful when calling this
* function to clone one parameter as other parameters may point to the same
* set. A good option is to call this just before returning the resulting
* set.
*/
static Bitmapset *
bms_copy_and_free(Bitmapset *a)
{
Bitmapset *c = bms_copy(a);
bms_free(a);
return c;
}
#endif
/*
* bms_copy - make a palloc'd copy of a bitmapset
*/
Bitmapset *
bms_copy(const Bitmapset *a)
{
Bitmapset *result;
size_t size;
Assert(bms_is_valid_set(a));
if (a == NULL)
return NULL;
size = BITMAPSET_SIZE(a->nwords);
result = (Bitmapset *) palloc(size);
memcpy(result, a, size);
return result;
}
/*
* bms_equal - are two bitmapsets equal? or both NULL?
*/
bool
bms_equal(const Bitmapset *a, const Bitmapset *b)
{
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
/* Handle cases where either input is NULL */
if (a == NULL)
{
if (b == NULL)
return true;
return false;
}
else if (b == NULL)
return false;
/* can't be equal if the word counts don't match */
if (a->nwords != b->nwords)
return false;
/* check each word matches */
i = 0;
do
{
if (a->words[i] != b->words[i])
return false;
} while (++i < a->nwords);
return true;
}
/*
* bms_compare - qsort-style comparator for bitmapsets
*
* This guarantees to report values as equal iff bms_equal would say they are
* equal. Otherwise, the highest-numbered bit that is set in one value but
* not the other determines the result. (This rule means that, for example,
* {6} is greater than {5}, which seems plausible.)
*/
int
bms_compare(const Bitmapset *a, const Bitmapset *b)
{
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
/* Handle cases where either input is NULL */
if (a == NULL)
return (b == NULL) ? 0 : -1;
else if (b == NULL)
return +1;
/* the set with the most words must be greater */
if (a->nwords != b->nwords)
return (a->nwords > b->nwords) ? +1 : -1;
i = a->nwords - 1;
do
{
bitmapword aw = a->words[i];
bitmapword bw = b->words[i];
if (aw != bw)
return (aw > bw) ? +1 : -1;
} while (--i >= 0);
return 0;
}
/*
* bms_make_singleton - build a bitmapset containing a single member
*/
Bitmapset *
bms_make_singleton(int x)
{
Bitmapset *result;
int wordnum,
bitnum;
if (x < 0)
elog(ERROR, "negative bitmapset member not allowed");
wordnum = WORDNUM(x);
bitnum = BITNUM(x);
result = (Bitmapset *) palloc0(BITMAPSET_SIZE(wordnum + 1));
result->type = T_Bitmapset;
result->nwords = wordnum + 1;
result->words[wordnum] = ((bitmapword) 1 << bitnum);
return result;
}
/*
* bms_free - free a bitmapset
*
* Same as pfree except for allowing NULL input
*/
void
bms_free(Bitmapset *a)
{
if (a)
pfree(a);
}
/*
* bms_union - create and return a new set containing all members from both
* input sets. Both inputs are left unmodified.
*/
Bitmapset *
bms_union(const Bitmapset *a, const Bitmapset *b)
{
Bitmapset *result;
const Bitmapset *other;
int otherlen;
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
/* Handle cases where either input is NULL */
if (a == NULL)
return bms_copy(b);
if (b == NULL)
return bms_copy(a);
/* Identify shorter and longer input; copy the longer one */
if (a->nwords <= b->nwords)
{
result = bms_copy(b);
other = a;
}
else
{
result = bms_copy(a);
other = b;
}
/* And union the shorter input into the result */
otherlen = other->nwords;
i = 0;
do
{
result->words[i] |= other->words[i];
} while (++i < otherlen);
return result;
}
/*
* bms_intersect - create and return a new set containing members which both
* input sets have in common. Both inputs are left unmodified.
*/
Bitmapset *
bms_intersect(const Bitmapset *a, const Bitmapset *b)
{
Bitmapset *result;
const Bitmapset *other;
int lastnonzero;
int resultlen;
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
/* Handle cases where either input is NULL */
if (a == NULL || b == NULL)
return NULL;
/* Identify shorter and longer input; copy the shorter one */
if (a->nwords <= b->nwords)
{
result = bms_copy(a);
other = b;
}
else
{
result = bms_copy(b);
other = a;
}
/* And intersect the longer input with the result */
resultlen = result->nwords;
lastnonzero = -1;
i = 0;
do
{
result->words[i] &= other->words[i];
if (result->words[i] != 0)
lastnonzero = i;
} while (++i < resultlen);
/* If we computed an empty result, we must return NULL */
if (lastnonzero == -1)
{
pfree(result);
return NULL;
}
/* get rid of trailing zero words */
result->nwords = lastnonzero + 1;
return result;
}
/*
* bms_difference - create and return a new set containing all the members of
* 'a' without the members of 'b'.
*/
Bitmapset *
bms_difference(const Bitmapset *a, const Bitmapset *b)
{
Bitmapset *result;
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
/* Handle cases where either input is NULL */
if (a == NULL)
return NULL;
if (b == NULL)
return bms_copy(a);
/*
* In Postgres' usage, an empty result is a very common case, so it's
* worth optimizing for that by testing bms_nonempty_difference(). This
* saves us a palloc/pfree cycle compared to checking after-the-fact.
*/
if (!bms_nonempty_difference(a, b))
return NULL;
/* Copy the left input */
result = bms_copy(a);
/* And remove b's bits from result */
if (result->nwords > b->nwords)
{
/*
* We'll never need to remove trailing zero words when 'a' has more
* words than 'b' as the additional words must be non-zero.
*/
i = 0;
do
{
result->words[i] &= ~b->words[i];
} while (++i < b->nwords);
}
else
{
int lastnonzero = -1;
/* we may need to remove trailing zero words from the result. */
i = 0;
do
{
result->words[i] &= ~b->words[i];
/* remember the last non-zero word */
if (result->words[i] != 0)
lastnonzero = i;
} while (++i < result->nwords);
/* trim off trailing zero words */
result->nwords = lastnonzero + 1;
}
Assert(result->nwords != 0);
/* Need not check for empty result, since we handled that case above */
return result;
}
/*
* bms_is_subset - is A a subset of B?
*/
bool
bms_is_subset(const Bitmapset *a, const Bitmapset *b)
{
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
/* Handle cases where either input is NULL */
if (a == NULL)
return true; /* empty set is a subset of anything */
if (b == NULL)
return false;
/* 'a' can't be a subset of 'b' if it contains more words */
if (a->nwords > b->nwords)
return false;
/* Check all 'a' members are set in 'b' */
i = 0;
do
{
if ((a->words[i] & ~b->words[i]) != 0)
return false;
} while (++i < a->nwords);
return true;
}
/*
* bms_subset_compare - compare A and B for equality/subset relationships
*
* This is more efficient than testing bms_is_subset in both directions.
*/
BMS_Comparison
bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
{
BMS_Comparison result;
int shortlen;
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
/* Handle cases where either input is NULL */
if (a == NULL)
{
if (b == NULL)
return BMS_EQUAL;
return BMS_SUBSET1;
}
if (b == NULL)
return BMS_SUBSET2;
/* Check common words */
result = BMS_EQUAL; /* status so far */
shortlen = Min(a->nwords, b->nwords);
i = 0;
do
{
bitmapword aword = a->words[i];
bitmapword bword = b->words[i];
if ((aword & ~bword) != 0)
{
/* a is not a subset of b */
if (result == BMS_SUBSET1)
return BMS_DIFFERENT;
result = BMS_SUBSET2;
}
if ((bword & ~aword) != 0)
{
/* b is not a subset of a */
if (result == BMS_SUBSET2)
return BMS_DIFFERENT;
result = BMS_SUBSET1;
}
} while (++i < shortlen);
/* Check extra words */
if (a->nwords > b->nwords)
{
/* if a has more words then a is not a subset of b */
if (result == BMS_SUBSET1)
return BMS_DIFFERENT;
return BMS_SUBSET2;
}
else if (a->nwords < b->nwords)
{
/* if b has more words then b is not a subset of a */
if (result == BMS_SUBSET2)
return BMS_DIFFERENT;
return BMS_SUBSET1;
}
return result;
}
/*
* bms_is_member - is X a member of A?
*/
bool
bms_is_member(int x, const Bitmapset *a)
{
int wordnum,
bitnum;
Assert(bms_is_valid_set(a));
/* XXX better to just return false for x<0 ? */
if (x < 0)
elog(ERROR, "negative bitmapset member not allowed");
if (a == NULL)
return false;
wordnum = WORDNUM(x);
bitnum = BITNUM(x);
if (wordnum >= a->nwords)
return false;
if ((a->words[wordnum] & ((bitmapword) 1 << bitnum)) != 0)
return true;
return false;
}
/*
* bms_member_index
* determine 0-based index of member x in the bitmap
*
* Returns (-1) when x is not a member.
*/
int
bms_member_index(Bitmapset *a, int x)
{
int i;
int bitnum;
int wordnum;
int result = 0;
bitmapword mask;
Assert(bms_is_valid_set(a));
/* return -1 if not a member of the bitmap */
if (!bms_is_member(x, a))
return -1;
wordnum = WORDNUM(x);
bitnum = BITNUM(x);
/* count bits in preceding words */
for (i = 0; i < wordnum; i++)
{
bitmapword w = a->words[i];
/* No need to count the bits in a zero word */
if (w != 0)
result += bmw_popcount(w);
}
/*
* Now add bits of the last word, but only those before the item. We can
* do that by applying a mask and then using popcount again. To get
* 0-based index, we want to count only preceding bits, not the item
* itself, so we subtract 1.
*/
mask = ((bitmapword) 1 << bitnum) - 1;
result += bmw_popcount(a->words[wordnum] & mask);
return result;
}
/*
* bms_overlap - do sets overlap (ie, have a nonempty intersection)?
*/
bool
bms_overlap(const Bitmapset *a, const Bitmapset *b)
{
int shortlen;
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
/* Handle cases where either input is NULL */
if (a == NULL || b == NULL)
return false;
/* Check words in common */
shortlen = Min(a->nwords, b->nwords);
i = 0;
do
{
if ((a->words[i] & b->words[i]) != 0)
return true;
} while (++i < shortlen);
return false;
}
/*
* bms_overlap_list - does a set overlap an integer list?
*/
bool
bms_overlap_list(const Bitmapset *a, const List *b)
{
ListCell *lc;
int wordnum,
bitnum;
Assert(bms_is_valid_set(a));
if (a == NULL || b == NIL)
return false;
foreach(lc, b)
{
int x = lfirst_int(lc);
if (x < 0)
elog(ERROR, "negative bitmapset member not allowed");
wordnum = WORDNUM(x);
bitnum = BITNUM(x);
if (wordnum < a->nwords)
if ((a->words[wordnum] & ((bitmapword) 1 << bitnum)) != 0)
return true;
}
return false;
}
/*
* bms_nonempty_difference - do sets have a nonempty difference?
*
* i.e., are any members set in 'a' that are not also set in 'b'.
*/
bool
bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
{
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
/* Handle cases where either input is NULL */
if (a == NULL)
return false;
if (b == NULL)
return true;
/* if 'a' has more words then it must contain additional members */
if (a->nwords > b->nwords)
return true;
/* Check all 'a' members are set in 'b' */
i = 0;
do
{
if ((a->words[i] & ~b->words[i]) != 0)
return true;
} while (++i < a->nwords);
return false;
}
/*
* bms_singleton_member - return the sole integer member of set
*
* Raises error if |a| is not 1.
*/
int
bms_singleton_member(const Bitmapset *a)
{
int result = -1;
int nwords;
int wordnum;
Assert(bms_is_valid_set(a));
if (a == NULL)
elog(ERROR, "bitmapset is empty");
nwords = a->nwords;
wordnum = 0;
do
{
bitmapword w = a->words[wordnum];
if (w != 0)
{
if (result >= 0 || HAS_MULTIPLE_ONES(w))
elog(ERROR, "bitmapset has multiple members");
result = wordnum * BITS_PER_BITMAPWORD;
result += bmw_rightmost_one_pos(w);
}
} while (++wordnum < nwords);
/* we don't expect non-NULL sets to be empty */
Assert(result >= 0);
return result;
}
/*
* bms_get_singleton_member
*
* Test whether the given set is a singleton.
* If so, set *member to the value of its sole member, and return true.
* If not, return false, without changing *member.
*
* This is more convenient and faster than calling bms_membership() and then
* bms_singleton_member(), if we don't care about distinguishing empty sets
* from multiple-member sets.
*/
bool
bms_get_singleton_member(const Bitmapset *a, int *member)
{
int result = -1;
int nwords;
int wordnum;
Assert(bms_is_valid_set(a));
if (a == NULL)
return false;
nwords = a->nwords;
wordnum = 0;
do
{
bitmapword w = a->words[wordnum];
if (w != 0)
{
if (result >= 0 || HAS_MULTIPLE_ONES(w))
return false;
result = wordnum * BITS_PER_BITMAPWORD;
result += bmw_rightmost_one_pos(w);
}
} while (++wordnum < nwords);
/* we don't expect non-NULL sets to be empty */
Assert(result >= 0);
*member = result;
return true;
}
/*
* bms_num_members - count members of set
*/
int
bms_num_members(const Bitmapset *a)
{
int result = 0;
int nwords;
int wordnum;
Assert(bms_is_valid_set(a));
if (a == NULL)
return 0;
nwords = a->nwords;
wordnum = 0;
do
{
bitmapword w = a->words[wordnum];
/* No need to count the bits in a zero word */
if (w != 0)
result += bmw_popcount(w);
} while (++wordnum < nwords);
return result;
}
/*
* bms_membership - does a set have zero, one, or multiple members?
*
* This is faster than making an exact count with bms_num_members().
*/
BMS_Membership
bms_membership(const Bitmapset *a)
{
BMS_Membership result = BMS_EMPTY_SET;
int nwords;
int wordnum;
Assert(bms_is_valid_set(a));
if (a == NULL)
return BMS_EMPTY_SET;
nwords = a->nwords;
wordnum = 0;
do
{
bitmapword w = a->words[wordnum];
if (w != 0)
{
if (result != BMS_EMPTY_SET || HAS_MULTIPLE_ONES(w))
return BMS_MULTIPLE;
result = BMS_SINGLETON;
}
} while (++wordnum < nwords);
return result;
}
/*
* bms_add_member - add a specified member to set
*
* 'a' is recycled when possible.
*/
Bitmapset *
bms_add_member(Bitmapset *a, int x)
{
int wordnum,
bitnum;
Assert(bms_is_valid_set(a));
if (x < 0)
elog(ERROR, "negative bitmapset member not allowed");
if (a == NULL)
return bms_make_singleton(x);
wordnum = WORDNUM(x);
bitnum = BITNUM(x);
/* enlarge the set if necessary */
if (wordnum >= a->nwords)
{
int oldnwords = a->nwords;
int i;
a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(wordnum + 1));
a->nwords = wordnum + 1;
/* zero out the enlarged portion */
i = oldnwords;
do
{
a->words[i] = 0;
} while (++i < a->nwords);
}
a->words[wordnum] |= ((bitmapword) 1 << bitnum);
#ifdef REALLOCATE_BITMAPSETS
/*
* There's no guarantee that the repalloc returned a new pointer, so copy
* and free unconditionally here.
*/
a = bms_copy_and_free(a);
#endif
return a;
}
/*
* bms_del_member - remove a specified member from set
*
* No error if x is not currently a member of set
*
* 'a' is recycled when possible.
*/
Bitmapset *
bms_del_member(Bitmapset *a, int x)
{
int wordnum,
bitnum;
Assert(bms_is_valid_set(a));
if (x < 0)
elog(ERROR, "negative bitmapset member not allowed");
if (a == NULL)
return NULL;
wordnum = WORDNUM(x);
bitnum = BITNUM(x);
#ifdef REALLOCATE_BITMAPSETS
a = bms_copy_and_free(a);
#endif
/* member can't exist. Return 'a' unmodified */
if (unlikely(wordnum >= a->nwords))
return a;
a->words[wordnum] &= ~((bitmapword) 1 << bitnum);
/* when last word becomes empty, trim off all trailing empty words */
if (a->words[wordnum] == 0 && wordnum == a->nwords - 1)
{
/* find the last non-empty word and make that the new final word */
for (int i = wordnum - 1; i >= 0; i--)
{
if (a->words[i] != 0)
{
a->nwords = i + 1;
return a;
}
}
/* the set is now empty */
pfree(a);
return NULL;
}
return a;
}
/*
* bms_add_members - like bms_union, but left input is recycled when possible
*/
Bitmapset *
bms_add_members(Bitmapset *a, const Bitmapset *b)
{
Bitmapset *result;
const Bitmapset *other;
int otherlen;
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
/* Handle cases where either input is NULL */
if (a == NULL)
return bms_copy(b);
if (b == NULL)
{
#ifdef REALLOCATE_BITMAPSETS
a = bms_copy_and_free(a);
#endif
return a;
}
/* Identify shorter and longer input; copy the longer one if needed */
if (a->nwords < b->nwords)
{
result = bms_copy(b);
other = a;
}
else
{
result = a;
other = b;
}
/* And union the shorter input into the result */
otherlen = other->nwords;
i = 0;
do
{
result->words[i] |= other->words[i];
} while (++i < otherlen);
if (result != a)
pfree(a);
#ifdef REALLOCATE_BITMAPSETS
else
result = bms_copy_and_free(result);
#endif
return result;
}
/*
* bms_replace_members
* Remove all existing members from 'a' and repopulate the set with members
* from 'b', recycling 'a', when possible.
*/
Bitmapset *
bms_replace_members(Bitmapset *a, const Bitmapset *b)
{
int i;
Assert(bms_is_valid_set(a));
Assert(bms_is_valid_set(b));
if (a == NULL)
return bms_copy(b);
if (b == NULL)
{
pfree(a);
return NULL;
}
if (a->nwords < b->nwords)
a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(b->nwords));
i = 0;
do
{
a->words[i] = b->words[i];
} while (++i < b->nwords);
a->nwords = b->nwords;
#ifdef REALLOCATE_BITMAPSETS
/*