PostgreSQL Source Code  git master
bitmapset.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * bitmapset.c
4  * PostgreSQL generic bitmap set package
5  *
6  * A bitmap set can represent any set of nonnegative integers, although
7  * it is mainly intended for sets where the maximum value is not large,
8  * say at most a few hundred. By convention, we always represent a set with
9  * the minimum possible number of words, i.e, there are never any trailing
10  * zero words. Enforcing this requires that an empty set is represented as
11  * NULL. Because an empty Bitmapset is represented as NULL, a non-NULL
12  * Bitmapset always has at least 1 Bitmapword. We can exploit this fact to
13  * speed up various loops over the Bitmapset's words array by using "do while"
14  * loops instead of "for" loops. This means the code does not waste time
15  * checking the loop condition before the first iteration. For Bitmapsets
16  * containing only a single word (likely the majority of them) this halves the
17  * number of loop condition checks.
18  *
19  *
20  * Copyright (c) 2003-2023, PostgreSQL Global Development Group
21  *
22  * IDENTIFICATION
23  * src/backend/nodes/bitmapset.c
24  *
25  *-------------------------------------------------------------------------
26  */
27 #include "postgres.h"
28 
29 #include "common/hashfn.h"
30 #include "nodes/bitmapset.h"
31 #include "nodes/pg_list.h"
32 #include "port/pg_bitutils.h"
33 
34 
35 #define WORDNUM(x) ((x) / BITS_PER_BITMAPWORD)
36 #define BITNUM(x) ((x) % BITS_PER_BITMAPWORD)
37 
38 #define BITMAPSET_SIZE(nwords) \
39  (offsetof(Bitmapset, words) + (nwords) * sizeof(bitmapword))
40 
41 /*----------
42  * This is a well-known cute trick for isolating the rightmost one-bit
43  * in a word. It assumes two's complement arithmetic. Consider any
44  * nonzero value, and focus attention on the rightmost one. The value is
45  * then something like
46  * xxxxxx10000
47  * where x's are unspecified bits. The two's complement negative is formed
48  * by inverting all the bits and adding one. Inversion gives
49  * yyyyyy01111
50  * where each y is the inverse of the corresponding x. Incrementing gives
51  * yyyyyy10000
52  * and then ANDing with the original value gives
53  * 00000010000
54  * This works for all cases except original value = zero, where of course
55  * we get zero.
56  *----------
57  */
58 #define RIGHTMOST_ONE(x) ((signedbitmapword) (x) & -((signedbitmapword) (x)))
59 
60 #define HAS_MULTIPLE_ONES(x) ((bitmapword) RIGHTMOST_ONE(x) != (x))
61 
62 /* Select appropriate bit-twiddling functions for bitmap word size */
63 #if BITS_PER_BITMAPWORD == 32
64 #define bmw_leftmost_one_pos(w) pg_leftmost_one_pos32(w)
65 #define bmw_rightmost_one_pos(w) pg_rightmost_one_pos32(w)
66 #define bmw_popcount(w) pg_popcount32(w)
67 #elif BITS_PER_BITMAPWORD == 64
68 #define bmw_leftmost_one_pos(w) pg_leftmost_one_pos64(w)
69 #define bmw_rightmost_one_pos(w) pg_rightmost_one_pos64(w)
70 #define bmw_popcount(w) pg_popcount64(w)
71 #else
72 #error "invalid BITS_PER_BITMAPWORD"
73 #endif
74 
75 
76 /*
77  * bms_copy - make a palloc'd copy of a bitmapset
78  */
79 Bitmapset *
81 {
82  Bitmapset *result;
83  size_t size;
84 
85  if (a == NULL)
86  return NULL;
87  size = BITMAPSET_SIZE(a->nwords);
88  result = (Bitmapset *) palloc(size);
89  memcpy(result, a, size);
90  return result;
91 }
92 
93 /*
94  * bms_equal - are two bitmapsets equal? or both NULL?
95  */
96 bool
97 bms_equal(const Bitmapset *a, const Bitmapset *b)
98 {
99  int i;
100 
101  Assert(a == NULL || a->words[a->nwords - 1] != 0);
102  Assert(b == NULL || b->words[b->nwords - 1] != 0);
103 
104  /* Handle cases where either input is NULL */
105  if (a == NULL)
106  {
107  if (b == NULL)
108  return true;
109  return false;
110  }
111  else if (b == NULL)
112  return false;
113 
114  /* can't be equal if the word counts don't match */
115  if (a->nwords != b->nwords)
116  return false;
117 
118  /* check each word matches */
119  i = 0;
120  do
121  {
122  if (a->words[i] != b->words[i])
123  return false;
124  } while (++i < a->nwords);
125 
126  return true;
127 }
128 
129 /*
130  * bms_compare - qsort-style comparator for bitmapsets
131  *
132  * This guarantees to report values as equal iff bms_equal would say they are
133  * equal. Otherwise, the highest-numbered bit that is set in one value but
134  * not the other determines the result. (This rule means that, for example,
135  * {6} is greater than {5}, which seems plausible.)
136  */
137 int
139 {
140  int i;
141 
142  Assert(a == NULL || a->words[a->nwords - 1] != 0);
143  Assert(b == NULL || b->words[b->nwords - 1] != 0);
144 
145  /* Handle cases where either input is NULL */
146  if (a == NULL)
147  return (b == NULL) ? 0 : -1;
148  else if (b == NULL)
149  return +1;
150 
151  /* the set with the most words must be greater */
152  if (a->nwords != b->nwords)
153  return (a->nwords > b->nwords) ? +1 : -1;
154 
155  i = a->nwords - 1;
156  do
157  {
158  bitmapword aw = a->words[i];
159  bitmapword bw = b->words[i];
160 
161  if (aw != bw)
162  return (aw > bw) ? +1 : -1;
163  } while (--i >= 0);
164  return 0;
165 }
166 
167 /*
168  * bms_make_singleton - build a bitmapset containing a single member
169  */
170 Bitmapset *
172 {
173  Bitmapset *result;
174  int wordnum,
175  bitnum;
176 
177  if (x < 0)
178  elog(ERROR, "negative bitmapset member not allowed");
179  wordnum = WORDNUM(x);
180  bitnum = BITNUM(x);
181  result = (Bitmapset *) palloc0(BITMAPSET_SIZE(wordnum + 1));
182  result->type = T_Bitmapset;
183  result->nwords = wordnum + 1;
184  result->words[wordnum] = ((bitmapword) 1 << bitnum);
185  return result;
186 }
187 
188 /*
189  * bms_free - free a bitmapset
190  *
191  * Same as pfree except for allowing NULL input
192  */
193 void
195 {
196  if (a)
197  pfree(a);
198 }
199 
200 
201 /*
202  * These operations all make a freshly palloc'd result,
203  * leaving their inputs untouched
204  */
205 
206 
207 /*
208  * bms_union - set union
209  */
210 Bitmapset *
211 bms_union(const Bitmapset *a, const Bitmapset *b)
212 {
213  Bitmapset *result;
214  const Bitmapset *other;
215  int otherlen;
216  int i;
217 
218  /* Handle cases where either input is NULL */
219  if (a == NULL)
220  return bms_copy(b);
221  if (b == NULL)
222  return bms_copy(a);
223  /* Identify shorter and longer input; copy the longer one */
224  if (a->nwords <= b->nwords)
225  {
226  result = bms_copy(b);
227  other = a;
228  }
229  else
230  {
231  result = bms_copy(a);
232  other = b;
233  }
234  /* And union the shorter input into the result */
235  otherlen = other->nwords;
236  i = 0;
237  do
238  {
239  result->words[i] |= other->words[i];
240  } while (++i < otherlen);
241  return result;
242 }
243 
244 /*
245  * bms_intersect - set intersection
246  */
247 Bitmapset *
249 {
250  Bitmapset *result;
251  const Bitmapset *other;
252  int lastnonzero;
253  int resultlen;
254  int i;
255 
256  /* Handle cases where either input is NULL */
257  if (a == NULL || b == NULL)
258  return NULL;
259  /* Identify shorter and longer input; copy the shorter one */
260  if (a->nwords <= b->nwords)
261  {
262  result = bms_copy(a);
263  other = b;
264  }
265  else
266  {
267  result = bms_copy(b);
268  other = a;
269  }
270  /* And intersect the longer input with the result */
271  resultlen = result->nwords;
272  lastnonzero = -1;
273  i = 0;
274  do
275  {
276  result->words[i] &= other->words[i];
277 
278  if (result->words[i] != 0)
279  lastnonzero = i;
280  } while (++i < resultlen);
281  /* If we computed an empty result, we must return NULL */
282  if (lastnonzero == -1)
283  {
284  pfree(result);
285  return NULL;
286  }
287 
288  /* get rid of trailing zero words */
289  result->nwords = lastnonzero + 1;
290  return result;
291 }
292 
293 /*
294  * bms_difference - set difference (ie, A without members of B)
295  */
296 Bitmapset *
298 {
299  Bitmapset *result;
300  int i;
301 
302  Assert(a == NULL || a->words[a->nwords - 1] != 0);
303  Assert(b == NULL || b->words[b->nwords - 1] != 0);
304 
305  /* Handle cases where either input is NULL */
306  if (a == NULL)
307  return NULL;
308  if (b == NULL)
309  return bms_copy(a);
310 
311  /*
312  * In Postgres' usage, an empty result is a very common case, so it's
313  * worth optimizing for that by testing bms_nonempty_difference(). This
314  * saves us a palloc/pfree cycle compared to checking after-the-fact.
315  */
316  if (!bms_nonempty_difference(a, b))
317  return NULL;
318 
319  /* Copy the left input */
320  result = bms_copy(a);
321 
322  /* And remove b's bits from result */
323  if (result->nwords > b->nwords)
324  {
325  /*
326  * We'll never need to remove trailing zero words when 'a' has more
327  * words than 'b' as the additional words must be non-zero.
328  */
329  i = 0;
330  do
331  {
332  result->words[i] &= ~b->words[i];
333  } while (++i < b->nwords);
334  }
335  else
336  {
337  int lastnonzero = -1;
338 
339  /* we may need to remove trailing zero words from the result. */
340  i = 0;
341  do
342  {
343  result->words[i] &= ~b->words[i];
344 
345  /* remember the last non-zero word */
346  if (result->words[i] != 0)
347  lastnonzero = i;
348  } while (++i < result->nwords);
349 
350  /* trim off trailing zero words */
351  result->nwords = lastnonzero + 1;
352  }
353  Assert(result->nwords != 0);
354 
355  /* Need not check for empty result, since we handled that case above */
356  return result;
357 }
358 
359 /*
360  * bms_is_subset - is A a subset of B?
361  */
362 bool
364 {
365  int i;
366 
367  Assert(a == NULL || a->words[a->nwords - 1] != 0);
368  Assert(b == NULL || b->words[b->nwords - 1] != 0);
369 
370  /* Handle cases where either input is NULL */
371  if (a == NULL)
372  return true; /* empty set is a subset of anything */
373  if (b == NULL)
374  return false;
375 
376  /* 'a' can't be a subset of 'b' if it contains more words */
377  if (a->nwords > b->nwords)
378  return false;
379 
380  /* Check all 'a' members are set in 'b' */
381  i = 0;
382  do
383  {
384  if ((a->words[i] & ~b->words[i]) != 0)
385  return false;
386  } while (++i < a->nwords);
387  return true;
388 }
389 
390 /*
391  * bms_subset_compare - compare A and B for equality/subset relationships
392  *
393  * This is more efficient than testing bms_is_subset in both directions.
394  */
397 {
398  BMS_Comparison result;
399  int shortlen;
400  int i;
401 
402  Assert(a == NULL || a->words[a->nwords - 1] != 0);
403  Assert(b == NULL || b->words[b->nwords - 1] != 0);
404 
405  /* Handle cases where either input is NULL */
406  if (a == NULL)
407  {
408  if (b == NULL)
409  return BMS_EQUAL;
410  return BMS_SUBSET1;
411  }
412  if (b == NULL)
413  return BMS_SUBSET2;
414  /* Check common words */
415  result = BMS_EQUAL; /* status so far */
416  shortlen = Min(a->nwords, b->nwords);
417  i = 0;
418  do
419  {
420  bitmapword aword = a->words[i];
421  bitmapword bword = b->words[i];
422 
423  if ((aword & ~bword) != 0)
424  {
425  /* a is not a subset of b */
426  if (result == BMS_SUBSET1)
427  return BMS_DIFFERENT;
428  result = BMS_SUBSET2;
429  }
430  if ((bword & ~aword) != 0)
431  {
432  /* b is not a subset of a */
433  if (result == BMS_SUBSET2)
434  return BMS_DIFFERENT;
435  result = BMS_SUBSET1;
436  }
437  } while (++i < shortlen);
438  /* Check extra words */
439  if (a->nwords > b->nwords)
440  {
441  /* if a has more words then a is not a subset of b */
442  if (result == BMS_SUBSET1)
443  return BMS_DIFFERENT;
444  return BMS_SUBSET2;
445  }
446  else if (a->nwords < b->nwords)
447  {
448  /* if b has more words then b is not a subset of a */
449  if (result == BMS_SUBSET2)
450  return BMS_DIFFERENT;
451  return BMS_SUBSET1;
452  }
453  return result;
454 }
455 
456 /*
457  * bms_is_member - is X a member of A?
458  */
459 bool
461 {
462  int wordnum,
463  bitnum;
464 
465  /* XXX better to just return false for x<0 ? */
466  if (x < 0)
467  elog(ERROR, "negative bitmapset member not allowed");
468  if (a == NULL)
469  return false;
470  wordnum = WORDNUM(x);
471  bitnum = BITNUM(x);
472  if (wordnum >= a->nwords)
473  return false;
474  if ((a->words[wordnum] & ((bitmapword) 1 << bitnum)) != 0)
475  return true;
476  return false;
477 }
478 
479 /*
480  * bms_member_index
481  * determine 0-based index of member x in the bitmap
482  *
483  * Returns (-1) when x is not a member.
484  */
485 int
487 {
488  int i;
489  int bitnum;
490  int wordnum;
491  int result = 0;
492  bitmapword mask;
493 
494  /* return -1 if not a member of the bitmap */
495  if (!bms_is_member(x, a))
496  return -1;
497 
498  wordnum = WORDNUM(x);
499  bitnum = BITNUM(x);
500 
501  /* count bits in preceding words */
502  for (i = 0; i < wordnum; i++)
503  {
504  bitmapword w = a->words[i];
505 
506  /* No need to count the bits in a zero word */
507  if (w != 0)
508  result += bmw_popcount(w);
509  }
510 
511  /*
512  * Now add bits of the last word, but only those before the item. We can
513  * do that by applying a mask and then using popcount again. To get
514  * 0-based index, we want to count only preceding bits, not the item
515  * itself, so we subtract 1.
516  */
517  mask = ((bitmapword) 1 << bitnum) - 1;
518  result += bmw_popcount(a->words[wordnum] & mask);
519 
520  return result;
521 }
522 
523 /*
524  * bms_overlap - do sets overlap (ie, have a nonempty intersection)?
525  */
526 bool
528 {
529  int shortlen;
530  int i;
531 
532  /* Handle cases where either input is NULL */
533  if (a == NULL || b == NULL)
534  return false;
535  /* Check words in common */
536  shortlen = Min(a->nwords, b->nwords);
537  i = 0;
538  do
539  {
540  if ((a->words[i] & b->words[i]) != 0)
541  return true;
542  } while (++i < shortlen);
543  return false;
544 }
545 
546 /*
547  * bms_overlap_list - does a set overlap an integer list?
548  */
549 bool
551 {
552  ListCell *lc;
553  int wordnum,
554  bitnum;
555 
556  if (a == NULL || b == NIL)
557  return false;
558 
559  foreach(lc, b)
560  {
561  int x = lfirst_int(lc);
562 
563  if (x < 0)
564  elog(ERROR, "negative bitmapset member not allowed");
565  wordnum = WORDNUM(x);
566  bitnum = BITNUM(x);
567  if (wordnum < a->nwords)
568  if ((a->words[wordnum] & ((bitmapword) 1 << bitnum)) != 0)
569  return true;
570  }
571 
572  return false;
573 }
574 
575 /*
576  * bms_nonempty_difference - do sets have a nonempty difference?
577  *
578  * i.e., are any members set in 'a' that are not also set in 'b'.
579  */
580 bool
582 {
583  int i;
584 
585  Assert(a == NULL || a->words[a->nwords - 1] != 0);
586  Assert(b == NULL || b->words[b->nwords - 1] != 0);
587 
588  /* Handle cases where either input is NULL */
589  if (a == NULL)
590  return false;
591  if (b == NULL)
592  return true;
593  /* if 'a' has more words then it must contain additional members */
594  if (a->nwords > b->nwords)
595  return true;
596  /* Check all 'a' members are set in 'b' */
597  i = 0;
598  do
599  {
600  if ((a->words[i] & ~b->words[i]) != 0)
601  return true;
602  } while (++i < a->nwords);
603  return false;
604 }
605 
606 /*
607  * bms_singleton_member - return the sole integer member of set
608  *
609  * Raises error if |a| is not 1.
610  */
611 int
613 {
614  int result = -1;
615  int nwords;
616  int wordnum;
617 
618  if (a == NULL)
619  elog(ERROR, "bitmapset is empty");
620  nwords = a->nwords;
621  wordnum = 0;
622  do
623  {
624  bitmapword w = a->words[wordnum];
625 
626  if (w != 0)
627  {
628  if (result >= 0 || HAS_MULTIPLE_ONES(w))
629  elog(ERROR, "bitmapset has multiple members");
630  result = wordnum * BITS_PER_BITMAPWORD;
631  result += bmw_rightmost_one_pos(w);
632  }
633  } while (++wordnum < nwords);
634 
635  /* we don't expect non-NULL sets to be empty */
636  Assert(result >= 0);
637  return result;
638 }
639 
640 /*
641  * bms_get_singleton_member
642  *
643  * Test whether the given set is a singleton.
644  * If so, set *member to the value of its sole member, and return true.
645  * If not, return false, without changing *member.
646  *
647  * This is more convenient and faster than calling bms_membership() and then
648  * bms_singleton_member(), if we don't care about distinguishing empty sets
649  * from multiple-member sets.
650  */
651 bool
653 {
654  int result = -1;
655  int nwords;
656  int wordnum;
657 
658  if (a == NULL)
659  return false;
660  nwords = a->nwords;
661  wordnum = 0;
662  do
663  {
664  bitmapword w = a->words[wordnum];
665 
666  if (w != 0)
667  {
668  if (result >= 0 || HAS_MULTIPLE_ONES(w))
669  return false;
670  result = wordnum * BITS_PER_BITMAPWORD;
671  result += bmw_rightmost_one_pos(w);
672  }
673  } while (++wordnum < nwords);
674 
675  /* we don't expect non-NULL sets to be empty */
676  Assert(result >= 0);
677  *member = result;
678  return true;
679 }
680 
681 /*
682  * bms_num_members - count members of set
683  */
684 int
686 {
687  int result = 0;
688  int nwords;
689  int wordnum;
690 
691  if (a == NULL)
692  return 0;
693  nwords = a->nwords;
694  wordnum = 0;
695  do
696  {
697  bitmapword w = a->words[wordnum];
698 
699  /* No need to count the bits in a zero word */
700  if (w != 0)
701  result += bmw_popcount(w);
702  } while (++wordnum < nwords);
703  return result;
704 }
705 
706 /*
707  * bms_membership - does a set have zero, one, or multiple members?
708  *
709  * This is faster than making an exact count with bms_num_members().
710  */
713 {
714  BMS_Membership result = BMS_EMPTY_SET;
715  int nwords;
716  int wordnum;
717 
718  if (a == NULL)
719  return BMS_EMPTY_SET;
720  nwords = a->nwords;
721  wordnum = 0;
722  do
723  {
724  bitmapword w = a->words[wordnum];
725 
726  if (w != 0)
727  {
728  if (result != BMS_EMPTY_SET || HAS_MULTIPLE_ONES(w))
729  return BMS_MULTIPLE;
730  result = BMS_SINGLETON;
731  }
732  } while (++wordnum < nwords);
733  return result;
734 }
735 
736 
737 /*
738  * These operations all "recycle" their non-const inputs, ie, either
739  * return the modified input or pfree it if it can't hold the result.
740  *
741  * These should generally be used in the style
742  *
743  * foo = bms_add_member(foo, x);
744  */
745 
746 
747 /*
748  * bms_add_member - add a specified member to set
749  *
750  * Input set is modified or recycled!
751  */
752 Bitmapset *
754 {
755  int wordnum,
756  bitnum;
757 
758  if (x < 0)
759  elog(ERROR, "negative bitmapset member not allowed");
760  if (a == NULL)
761  return bms_make_singleton(x);
762  wordnum = WORDNUM(x);
763  bitnum = BITNUM(x);
764 
765  /* enlarge the set if necessary */
766  if (wordnum >= a->nwords)
767  {
768  int oldnwords = a->nwords;
769  int i;
770 
771  a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(wordnum + 1));
772  a->nwords = wordnum + 1;
773  /* zero out the enlarged portion */
774  i = oldnwords;
775  do
776  {
777  a->words[i] = 0;
778  } while (++i < a->nwords);
779  }
780 
781  a->words[wordnum] |= ((bitmapword) 1 << bitnum);
782  return a;
783 }
784 
785 /*
786  * bms_del_member - remove a specified member from set
787  *
788  * No error if x is not currently a member of set
789  *
790  * Input set is modified in-place!
791  */
792 Bitmapset *
794 {
795  int wordnum,
796  bitnum;
797 
798  if (x < 0)
799  elog(ERROR, "negative bitmapset member not allowed");
800  if (a == NULL)
801  return NULL;
802  wordnum = WORDNUM(x);
803  bitnum = BITNUM(x);
804 
805  /* member can't exist. Return 'a' unmodified */
806  if (unlikely(wordnum >= a->nwords))
807  return a;
808 
809  a->words[wordnum] &= ~((bitmapword) 1 << bitnum);
810 
811  /* when last word becomes empty, trim off all trailing empty words */
812  if (a->words[wordnum] == 0 && wordnum == a->nwords - 1)
813  {
814  /* find the last non-empty word and make that the new final word */
815  for (int i = wordnum - 1; i >= 0; i--)
816  {
817  if (a->words[i] != 0)
818  {
819  a->nwords = i + 1;
820  return a;
821  }
822  }
823 
824  /* the set is now empty */
825  pfree(a);
826  return NULL;
827  }
828  return a;
829 }
830 
831 /*
832  * bms_add_members - like bms_union, but left input is recycled
833  */
834 Bitmapset *
836 {
837  Bitmapset *result;
838  const Bitmapset *other;
839  int otherlen;
840  int i;
841 
842  /* Handle cases where either input is NULL */
843  if (a == NULL)
844  return bms_copy(b);
845  if (b == NULL)
846  return a;
847  /* Identify shorter and longer input; copy the longer one if needed */
848  if (a->nwords < b->nwords)
849  {
850  result = bms_copy(b);
851  other = a;
852  }
853  else
854  {
855  result = a;
856  other = b;
857  }
858  /* And union the shorter input into the result */
859  otherlen = other->nwords;
860  i = 0;
861  do
862  {
863  result->words[i] |= other->words[i];
864  } while (++i < otherlen);
865  if (result != a)
866  pfree(a);
867  return result;
868 }
869 
870 /*
871  * bms_add_range
872  * Add members in the range of 'lower' to 'upper' to the set.
873  *
874  * Note this could also be done by calling bms_add_member in a loop, however,
875  * using this function will be faster when the range is large as we work at
876  * the bitmapword level rather than at bit level.
877  */
878 Bitmapset *
880 {
881  int lwordnum,
882  lbitnum,
883  uwordnum,
884  ushiftbits,
885  wordnum;
886 
887  /* do nothing if nothing is called for, without further checking */
888  if (upper < lower)
889  return a;
890 
891  if (lower < 0)
892  elog(ERROR, "negative bitmapset member not allowed");
893  uwordnum = WORDNUM(upper);
894 
895  if (a == NULL)
896  {
897  a = (Bitmapset *) palloc0(BITMAPSET_SIZE(uwordnum + 1));
898  a->type = T_Bitmapset;
899  a->nwords = uwordnum + 1;
900  }
901  else if (uwordnum >= a->nwords)
902  {
903  int oldnwords = a->nwords;
904  int i;
905 
906  /* ensure we have enough words to store the upper bit */
907  a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(uwordnum + 1));
908  a->nwords = uwordnum + 1;
909  /* zero out the enlarged portion */
910  i = oldnwords;
911  do
912  {
913  a->words[i] = 0;
914  } while (++i < a->nwords);
915  }
916 
917  wordnum = lwordnum = WORDNUM(lower);
918 
919  lbitnum = BITNUM(lower);
920  ushiftbits = BITS_PER_BITMAPWORD - (BITNUM(upper) + 1);
921 
922  /*
923  * Special case when lwordnum is the same as uwordnum we must perform the
924  * upper and lower masking on the word.
925  */
926  if (lwordnum == uwordnum)
927  {
928  a->words[lwordnum] |= ~(bitmapword) (((bitmapword) 1 << lbitnum) - 1)
929  & (~(bitmapword) 0) >> ushiftbits;
930  }
931  else
932  {
933  /* turn on lbitnum and all bits left of it */
934  a->words[wordnum++] |= ~(bitmapword) (((bitmapword) 1 << lbitnum) - 1);
935 
936  /* turn on all bits for any intermediate words */
937  while (wordnum < uwordnum)
938  a->words[wordnum++] = ~(bitmapword) 0;
939 
940  /* turn on upper's bit and all bits right of it. */
941  a->words[uwordnum] |= (~(bitmapword) 0) >> ushiftbits;
942  }
943 
944  return a;
945 }
946 
947 /*
948  * bms_int_members - like bms_intersect, but left input is recycled
949  */
950 Bitmapset *
952 {
953  int lastnonzero;
954  int shortlen;
955  int i;
956 
957  /* Handle cases where either input is NULL */
958  if (a == NULL)
959  return NULL;
960  if (b == NULL)
961  {
962  pfree(a);
963  return NULL;
964  }
965  /* Intersect b into a; we need never copy */
966  shortlen = Min(a->nwords, b->nwords);
967  lastnonzero = -1;
968  i = 0;
969  do
970  {
971  a->words[i] &= b->words[i];
972 
973  if (a->words[i] != 0)
974  lastnonzero = i;
975  } while (++i < shortlen);
976 
977  /* If we computed an empty result, we must return NULL */
978  if (lastnonzero == -1)
979  {
980  pfree(a);
981  return NULL;
982  }
983 
984  /* get rid of trailing zero words */
985  a->nwords = lastnonzero + 1;
986  return a;
987 }
988 
989 /*
990  * bms_del_members - like bms_difference, but left input is recycled
991  */
992 Bitmapset *
994 {
995  int i;
996 
997  Assert(a == NULL || a->words[a->nwords - 1] != 0);
998  Assert(b == NULL || b->words[b->nwords - 1] != 0);
999 
1000  /* Handle cases where either input is NULL */
1001  if (a == NULL)
1002  return NULL;
1003  if (b == NULL)
1004  return a;
1005  /* Remove b's bits from a; we need never copy */
1006  if (a->nwords > b->nwords)
1007  {
1008  /*
1009  * We'll never need to remove trailing zero words when 'a' has more
1010  * words than 'b'.
1011  */
1012  i = 0;
1013  do
1014  {
1015  a->words[i] &= ~b->words[i];
1016  } while (++i < b->nwords);
1017  }
1018  else
1019  {
1020  int lastnonzero = -1;
1021 
1022  /* we may need to remove trailing zero words from the result. */
1023  i = 0;
1024  do
1025  {
1026  a->words[i] &= ~b->words[i];
1027 
1028  /* remember the last non-zero word */
1029  if (a->words[i] != 0)
1030  lastnonzero = i;
1031  } while (++i < a->nwords);
1032 
1033  /* check if 'a' has become empty */
1034  if (lastnonzero == -1)
1035  {
1036  pfree(a);
1037  return NULL;
1038  }
1039 
1040  /* trim off any trailing zero words */
1041  a->nwords = lastnonzero + 1;
1042  }
1043 
1044  return a;
1045 }
1046 
1047 /*
1048  * bms_join - like bms_union, but *both* inputs are recycled
1049  */
1050 Bitmapset *
1052 {
1053  Bitmapset *result;
1054  Bitmapset *other;
1055  int otherlen;
1056  int i;
1057 
1058  /* Handle cases where either input is NULL */
1059  if (a == NULL)
1060  return b;
1061  if (b == NULL)
1062  return a;
1063  /* Identify shorter and longer input; use longer one as result */
1064  if (a->nwords < b->nwords)
1065  {
1066  result = b;
1067  other = a;
1068  }
1069  else
1070  {
1071  result = a;
1072  other = b;
1073  }
1074  /* And union the shorter input into the result */
1075  otherlen = other->nwords;
1076  i = 0;
1077  do
1078  {
1079  result->words[i] |= other->words[i];
1080  } while (++i < otherlen);
1081  if (other != result) /* pure paranoia */
1082  pfree(other);
1083  return result;
1084 }
1085 
1086 /*
1087  * bms_next_member - find next member of a set
1088  *
1089  * Returns smallest member greater than "prevbit", or -2 if there is none.
1090  * "prevbit" must NOT be less than -1, or the behavior is unpredictable.
1091  *
1092  * This is intended as support for iterating through the members of a set.
1093  * The typical pattern is
1094  *
1095  * x = -1;
1096  * while ((x = bms_next_member(inputset, x)) >= 0)
1097  * process member x;
1098  *
1099  * Notice that when there are no more members, we return -2, not -1 as you
1100  * might expect. The rationale for that is to allow distinguishing the
1101  * loop-not-started state (x == -1) from the loop-completed state (x == -2).
1102  * It makes no difference in simple loop usage, but complex iteration logic
1103  * might need such an ability.
1104  */
1105 int
1106 bms_next_member(const Bitmapset *a, int prevbit)
1107 {
1108  int nwords;
1109  int wordnum;
1110  bitmapword mask;
1111 
1112  if (a == NULL)
1113  return -2;
1114  nwords = a->nwords;
1115  prevbit++;
1116  mask = (~(bitmapword) 0) << BITNUM(prevbit);
1117  for (wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
1118  {
1119  bitmapword w = a->words[wordnum];
1120 
1121  /* ignore bits before prevbit */
1122  w &= mask;
1123 
1124  if (w != 0)
1125  {
1126  int result;
1127 
1128  result = wordnum * BITS_PER_BITMAPWORD;
1129  result += bmw_rightmost_one_pos(w);
1130  return result;
1131  }
1132 
1133  /* in subsequent words, consider all bits */
1134  mask = (~(bitmapword) 0);
1135  }
1136  return -2;
1137 }
1138 
1139 /*
1140  * bms_prev_member - find prev member of a set
1141  *
1142  * Returns largest member less than "prevbit", or -2 if there is none.
1143  * "prevbit" must NOT be more than one above the highest possible bit that can
1144  * be set at the Bitmapset at its current size.
1145  *
1146  * To ease finding the highest set bit for the initial loop, the special
1147  * prevbit value of -1 can be passed to have the function find the highest
1148  * valued member in the set.
1149  *
1150  * This is intended as support for iterating through the members of a set in
1151  * reverse. The typical pattern is
1152  *
1153  * x = -1;
1154  * while ((x = bms_prev_member(inputset, x)) >= 0)
1155  * process member x;
1156  *
1157  * Notice that when there are no more members, we return -2, not -1 as you
1158  * might expect. The rationale for that is to allow distinguishing the
1159  * loop-not-started state (x == -1) from the loop-completed state (x == -2).
1160  * It makes no difference in simple loop usage, but complex iteration logic
1161  * might need such an ability.
1162  */
1163 
1164 int
1165 bms_prev_member(const Bitmapset *a, int prevbit)
1166 {
1167  int wordnum;
1168  int ushiftbits;
1169  bitmapword mask;
1170 
1171  /*
1172  * If set is NULL or if there are no more bits to the right then we've
1173  * nothing to do.
1174  */
1175  if (a == NULL || prevbit == 0)
1176  return -2;
1177 
1178  /* transform -1 to the highest possible bit we could have set */
1179  if (prevbit == -1)
1180  prevbit = a->nwords * BITS_PER_BITMAPWORD - 1;
1181  else
1182  prevbit--;
1183 
1184  ushiftbits = BITS_PER_BITMAPWORD - (BITNUM(prevbit) + 1);
1185  mask = (~(bitmapword) 0) >> ushiftbits;
1186  for (wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
1187  {
1188  bitmapword w = a->words[wordnum];
1189 
1190  /* mask out bits left of prevbit */
1191  w &= mask;
1192 
1193  if (w != 0)
1194  {
1195  int result;
1196 
1197  result = wordnum * BITS_PER_BITMAPWORD;
1198  result += bmw_leftmost_one_pos(w);
1199  return result;
1200  }
1201 
1202  /* in subsequent words, consider all bits */
1203  mask = (~(bitmapword) 0);
1204  }
1205  return -2;
1206 }
1207 
1208 /*
1209  * bms_hash_value - compute a hash key for a Bitmapset
1210  */
1211 uint32
1213 {
1214  if (a == NULL)
1215  return 0; /* All empty sets hash to 0 */
1216  return DatumGetUInt32(hash_any((const unsigned char *) a->words,
1217  a->nwords * sizeof(bitmapword)));
1218 }
1219 
1220 /*
1221  * bitmap_hash - hash function for keys that are (pointers to) Bitmapsets
1222  *
1223  * Note: don't forget to specify bitmap_match as the match function!
1224  */
1225 uint32
1226 bitmap_hash(const void *key, Size keysize)
1227 {
1228  Assert(keysize == sizeof(Bitmapset *));
1229  return bms_hash_value(*((const Bitmapset *const *) key));
1230 }
1231 
1232 /*
1233  * bitmap_match - match function to use with bitmap_hash
1234  */
1235 int
1236 bitmap_match(const void *key1, const void *key2, Size keysize)
1237 {
1238  Assert(keysize == sizeof(Bitmapset *));
1239  return !bms_equal(*((const Bitmapset *const *) key1),
1240  *((const Bitmapset *const *) key2));
1241 }
#define BITMAPSET_SIZE(nwords)
Definition: bitmapset.c:38
#define bmw_rightmost_one_pos(w)
Definition: bitmapset.c:65
int bms_prev_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1165
uint32 bitmap_hash(const void *key, Size keysize)
Definition: bitmapset.c:1226
#define WORDNUM(x)
Definition: bitmapset.c:35
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:1051
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:97
BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:396
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1106
uint32 bms_hash_value(const Bitmapset *a)
Definition: bitmapset.c:1212
#define bmw_leftmost_one_pos(w)
Definition: bitmapset.c:64
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:363
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:612
void bms_free(Bitmapset *a)
Definition: bitmapset.c:194
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:685
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:460
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:171
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:753
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:211
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:297
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:248
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:835
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:993
#define BITNUM(x)
Definition: bitmapset.c:36
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:951
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:793
#define HAS_MULTIPLE_ONES(x)
Definition: bitmapset.c:60
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: bitmapset.c:1236
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:712
int bms_member_index(Bitmapset *a, int x)
Definition: bitmapset.c:486
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:527
int bms_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:138
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:80
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:652
Bitmapset * bms_add_range(Bitmapset *a, int lower, int upper)
Definition: bitmapset.c:879
#define bmw_popcount(w)
Definition: bitmapset.c:66
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:581
bool bms_overlap_list(const Bitmapset *a, const List *b)
Definition: bitmapset.c:550
BMS_Comparison
Definition: bitmapset.h:61
@ BMS_DIFFERENT
Definition: bitmapset.h:65
@ BMS_SUBSET1
Definition: bitmapset.h:63
@ BMS_EQUAL
Definition: bitmapset.h:62
@ BMS_SUBSET2
Definition: bitmapset.h:64
BMS_Membership
Definition: bitmapset.h:70
@ BMS_SINGLETON
Definition: bitmapset.h:72
@ BMS_EMPTY_SET
Definition: bitmapset.h:71
@ BMS_MULTIPLE
Definition: bitmapset.h:73
uint32 bitmapword
Definition: bitmapset.h:44
#define BITS_PER_BITMAPWORD
Definition: bitmapset.h:43
unsigned int uint32
Definition: c.h:495
#define Min(x, y)
Definition: c.h:993
#define unlikely(x)
Definition: c.h:300
size_t Size
Definition: c.h:594
#define ERROR
Definition: elog.h:39
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
int b
Definition: isn.c:70
int x
Definition: isn.c:71
int a
Definition: isn.c:69
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1456
void * palloc0(Size size)
Definition: mcxt.c:1257
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1476
void * palloc(Size size)
Definition: mcxt.c:1226
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:49
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:80
#define NIL
Definition: pg_list.h:68
#define lfirst_int(lc)
Definition: pg_list.h:173
static uint32 DatumGetUInt32(Datum X)
Definition: postgres.h:222
int nwords
Definition: bitmapset.h:54
bitmapword words[FLEXIBLE_ARRAY_MEMBER]
Definition: bitmapset.h:55
Definition: pg_list.h:54