Age Owner Branch data TLA Line data Source code
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 : : * Callers must ensure that the set returned by functions in this file which
20 : : * adjust the members of an existing set is assigned to all pointers pointing
21 : : * to that existing set. No guarantees are made that we'll ever modify the
22 : : * existing set in-place and return it.
23 : : *
24 : : * To help find bugs caused by callers failing to record the return value of
25 : : * the function which manipulates an existing set, we support building with
26 : : * REALLOCATE_BITMAPSETS. This results in the set being reallocated each time
27 : : * the set is altered and the existing being pfreed. This is useful as if any
28 : : * references still exist to the old set, we're more likely to notice as
29 : : * any users of the old set will be accessing pfree'd memory. This option is
30 : : * only intended to be used for debugging.
31 : : *
32 : : * Copyright (c) 2003-2025, PostgreSQL Global Development Group
33 : : *
34 : : * IDENTIFICATION
35 : : * src/backend/nodes/bitmapset.c
36 : : *
37 : : *-------------------------------------------------------------------------
38 : : */
39 : : #include "postgres.h"
40 : :
41 : : #include "common/hashfn.h"
42 : : #include "nodes/bitmapset.h"
43 : : #include "nodes/pg_list.h"
44 : : #include "port/pg_bitutils.h"
45 : :
46 : :
47 : : #define WORDNUM(x) ((x) / BITS_PER_BITMAPWORD)
48 : : #define BITNUM(x) ((x) % BITS_PER_BITMAPWORD)
49 : :
50 : : #define BITMAPSET_SIZE(nwords) \
51 : : (offsetof(Bitmapset, words) + (nwords) * sizeof(bitmapword))
52 : :
53 : : /*----------
54 : : * This is a well-known cute trick for isolating the rightmost one-bit
55 : : * in a word. It assumes two's complement arithmetic. Consider any
56 : : * nonzero value, and focus attention on the rightmost one. The value is
57 : : * then something like
58 : : * xxxxxx10000
59 : : * where x's are unspecified bits. The two's complement negative is formed
60 : : * by inverting all the bits and adding one. Inversion gives
61 : : * yyyyyy01111
62 : : * where each y is the inverse of the corresponding x. Incrementing gives
63 : : * yyyyyy10000
64 : : * and then ANDing with the original value gives
65 : : * 00000010000
66 : : * This works for all cases except original value = zero, where of course
67 : : * we get zero.
68 : : *----------
69 : : */
70 : : #define RIGHTMOST_ONE(x) ((signedbitmapword) (x) & -((signedbitmapword) (x)))
71 : :
72 : : #define HAS_MULTIPLE_ONES(x) ((bitmapword) RIGHTMOST_ONE(x) != (x))
73 : :
74 : : #ifdef USE_ASSERT_CHECKING
75 : : /*
76 : : * bms_is_valid_set - for cassert builds to check for valid sets
77 : : */
78 : : static bool
699 drowley@postgresql.o 79 :CBC 171364875 : bms_is_valid_set(const Bitmapset *a)
80 : : {
81 : : /* NULL is the correct representation of an empty set */
82 [ + + ]: 171364875 : if (a == NULL)
83 : 69063558 : return true;
84 : :
85 : : /* check the node tag is set correctly. pfree'd pointer, maybe? */
86 [ - + ]: 102301317 : if (!IsA(a, Bitmapset))
699 drowley@postgresql.o 87 :UBC 0 : return false;
88 : :
89 : : /* trailing zero words are not allowed */
699 drowley@postgresql.o 90 [ - + ]:CBC 102301317 : if (a->words[a->nwords - 1] == 0)
699 drowley@postgresql.o 91 :UBC 0 : return false;
92 : :
699 drowley@postgresql.o 93 :CBC 102301317 : return true;
94 : : }
95 : : #endif
96 : :
97 : : #ifdef REALLOCATE_BITMAPSETS
98 : : /*
99 : : * bms_copy_and_free
100 : : * Only required in REALLOCATE_BITMAPSETS builds. Provide a simple way
101 : : * to return a freshly allocated set and pfree the original.
102 : : *
103 : : * Note: callers which accept multiple sets must be careful when calling this
104 : : * function to clone one parameter as other parameters may point to the same
105 : : * set. A good option is to call this just before returning the resulting
106 : : * set.
107 : : */
108 : : static Bitmapset *
109 : : bms_copy_and_free(Bitmapset *a)
110 : : {
111 : : Bitmapset *c = bms_copy(a);
112 : :
113 : : bms_free(a);
114 : : return c;
115 : : }
116 : : #endif
117 : :
118 : : /*
119 : : * bms_copy - make a palloc'd copy of a bitmapset
120 : : */
121 : : Bitmapset *
8166 bruce@momjian.us 122 : 19316186 : bms_copy(const Bitmapset *a)
123 : : {
124 : : Bitmapset *result;
125 : : size_t size;
126 : :
699 drowley@postgresql.o 127 [ - + ]: 19316186 : Assert(bms_is_valid_set(a));
128 : :
8347 tgl@sss.pgh.pa.us 129 [ + + ]: 19316186 : if (a == NULL)
130 : 10532207 : return NULL;
131 : :
132 : 8783979 : size = BITMAPSET_SIZE(a->nwords);
133 : 8783979 : result = (Bitmapset *) palloc(size);
134 : 8783979 : memcpy(result, a, size);
135 : 8783979 : return result;
136 : : }
137 : :
138 : : /*
139 : : * bms_equal - are two bitmapsets equal? or both NULL?
140 : : */
141 : : bool
8166 bruce@momjian.us 142 : 6484602 : bms_equal(const Bitmapset *a, const Bitmapset *b)
143 : : {
144 : : int i;
145 : :
699 drowley@postgresql.o 146 [ - + ]: 6484602 : Assert(bms_is_valid_set(a));
147 [ - + ]: 6484602 : Assert(bms_is_valid_set(b));
148 : :
149 : : /* Handle cases where either input is NULL */
8347 tgl@sss.pgh.pa.us 150 [ + + ]: 6484602 : if (a == NULL)
151 : : {
152 [ + + ]: 3740228 : if (b == NULL)
153 : 3705161 : return true;
1020 154 : 35067 : return false;
155 : : }
8347 156 [ + + ]: 2744374 : else if (b == NULL)
1020 157 : 11127 : return false;
158 : :
159 : : /* can't be equal if the word counts don't match */
896 drowley@postgresql.o 160 [ + + ]: 2733247 : if (a->nwords != b->nwords)
896 drowley@postgresql.o 161 :GBC 2 : return false;
162 : :
163 : : /* check each word matches */
896 drowley@postgresql.o 164 :CBC 2733245 : i = 0;
165 : : do
166 : : {
167 [ + + ]: 2733245 : if (a->words[i] != b->words[i])
8347 tgl@sss.pgh.pa.us 168 : 1540208 : return false;
896 drowley@postgresql.o 169 [ - + ]: 1193037 : } while (++i < a->nwords);
170 : :
8347 tgl@sss.pgh.pa.us 171 : 1193037 : return true;
172 : : }
173 : :
174 : : /*
175 : : * bms_compare - qsort-style comparator for bitmapsets
176 : : *
177 : : * This guarantees to report values as equal iff bms_equal would say they are
178 : : * equal. Otherwise, the highest-numbered bit that is set in one value but
179 : : * not the other determines the result. (This rule means that, for example,
180 : : * {6} is greater than {5}, which seems plausible.)
181 : : */
182 : : int
2898 183 : 13070 : bms_compare(const Bitmapset *a, const Bitmapset *b)
184 : : {
185 : : int i;
186 : :
699 drowley@postgresql.o 187 [ - + ]: 13070 : Assert(bms_is_valid_set(a));
188 [ - + ]: 13070 : Assert(bms_is_valid_set(b));
189 : :
190 : : /* Handle cases where either input is NULL */
2898 tgl@sss.pgh.pa.us 191 [ + + ]: 13070 : if (a == NULL)
1020 tgl@sss.pgh.pa.us 192 [ + + ]:GBC 4 : return (b == NULL) ? 0 : -1;
2898 tgl@sss.pgh.pa.us 193 [ + + ]:CBC 13066 : else if (b == NULL)
1020 tgl@sss.pgh.pa.us 194 :GBC 2 : return +1;
195 : :
196 : : /* the set with the most words must be greater */
896 drowley@postgresql.o 197 [ + + ]:CBC 13064 : if (a->nwords != b->nwords)
896 drowley@postgresql.o 198 [ - + ]:GBC 1 : return (a->nwords > b->nwords) ? +1 : -1;
199 : :
896 drowley@postgresql.o 200 :CBC 13063 : i = a->nwords - 1;
201 : : do
202 : : {
2898 tgl@sss.pgh.pa.us 203 : 13063 : bitmapword aw = a->words[i];
204 : 13063 : bitmapword bw = b->words[i];
205 : :
206 [ + + ]: 13063 : if (aw != bw)
207 [ + + ]: 13062 : return (aw > bw) ? +1 : -1;
896 drowley@postgresql.o 208 [ - + ]:GBC 1 : } while (--i >= 0);
2898 tgl@sss.pgh.pa.us 209 : 1 : return 0;
210 : : }
211 : :
212 : : /*
213 : : * bms_make_singleton - build a bitmapset containing a single member
214 : : */
215 : : Bitmapset *
8347 tgl@sss.pgh.pa.us 216 :CBC 8273164 : bms_make_singleton(int x)
217 : : {
218 : : Bitmapset *result;
219 : : int wordnum,
220 : : bitnum;
221 : :
222 [ + + ]: 8273164 : if (x < 0)
8183 tgl@sss.pgh.pa.us 223 [ + - ]:GBC 1 : elog(ERROR, "negative bitmapset member not allowed");
8347 tgl@sss.pgh.pa.us 224 :CBC 8273163 : wordnum = WORDNUM(x);
225 : 8273163 : bitnum = BITNUM(x);
226 : 8273163 : result = (Bitmapset *) palloc0(BITMAPSET_SIZE(wordnum + 1));
1129 227 : 8273163 : result->type = T_Bitmapset;
8347 228 : 8273163 : result->nwords = wordnum + 1;
229 : 8273163 : result->words[wordnum] = ((bitmapword) 1 << bitnum);
230 : 8273163 : return result;
231 : : }
232 : :
233 : : /*
234 : : * bms_free - free a bitmapset
235 : : *
236 : : * Same as pfree except for allowing NULL input
237 : : */
238 : : void
8166 bruce@momjian.us 239 : 13978810 : bms_free(Bitmapset *a)
240 : : {
8347 tgl@sss.pgh.pa.us 241 [ + + ]: 13978810 : if (a)
242 : 5313260 : pfree(a);
243 : 13978810 : }
244 : :
245 : :
246 : : /*
247 : : * bms_union - create and return a new set containing all members from both
248 : : * input sets. Both inputs are left unmodified.
249 : : */
250 : : Bitmapset *
8166 bruce@momjian.us 251 : 4380683 : bms_union(const Bitmapset *a, const Bitmapset *b)
252 : : {
253 : : Bitmapset *result;
254 : : const Bitmapset *other;
255 : : int otherlen;
256 : : int i;
257 : :
699 drowley@postgresql.o 258 [ - + ]: 4380683 : Assert(bms_is_valid_set(a));
259 [ - + ]: 4380683 : Assert(bms_is_valid_set(b));
260 : :
261 : : /* Handle cases where either input is NULL */
8347 tgl@sss.pgh.pa.us 262 [ + + ]: 4380683 : if (a == NULL)
263 : 2796135 : return bms_copy(b);
264 [ + + ]: 1584548 : if (b == NULL)
265 : 684697 : return bms_copy(a);
266 : : /* Identify shorter and longer input; copy the longer one */
267 [ + + ]: 899851 : if (a->nwords <= b->nwords)
268 : : {
269 : 899850 : result = bms_copy(b);
270 : 899850 : other = a;
271 : : }
272 : : else
273 : : {
8347 tgl@sss.pgh.pa.us 274 :GBC 1 : result = bms_copy(a);
275 : 1 : other = b;
276 : : }
277 : : /* And union the shorter input into the result */
8347 tgl@sss.pgh.pa.us 278 :CBC 899851 : otherlen = other->nwords;
896 drowley@postgresql.o 279 : 899851 : i = 0;
280 : : do
281 : : {
8347 tgl@sss.pgh.pa.us 282 : 901130 : result->words[i] |= other->words[i];
896 drowley@postgresql.o 283 [ + + ]: 901130 : } while (++i < otherlen);
8347 tgl@sss.pgh.pa.us 284 : 899851 : return result;
285 : : }
286 : :
287 : : /*
288 : : * bms_intersect - create and return a new set containing members which both
289 : : * input sets have in common. Both inputs are left unmodified.
290 : : */
291 : : Bitmapset *
8166 bruce@momjian.us 292 : 2245193 : bms_intersect(const Bitmapset *a, const Bitmapset *b)
293 : : {
294 : : Bitmapset *result;
295 : : const Bitmapset *other;
296 : : int lastnonzero;
297 : : int resultlen;
298 : : int i;
299 : :
699 drowley@postgresql.o 300 [ - + ]: 2245193 : Assert(bms_is_valid_set(a));
301 [ - + ]: 2245193 : Assert(bms_is_valid_set(b));
302 : :
303 : : /* Handle cases where either input is NULL */
8347 tgl@sss.pgh.pa.us 304 [ + + + + ]: 2245193 : if (a == NULL || b == NULL)
305 : 1236189 : return NULL;
306 : :
307 : : /* Identify shorter and longer input; copy the shorter one */
308 [ + + ]: 1009004 : if (a->nwords <= b->nwords)
309 : : {
310 : 1009003 : result = bms_copy(a);
311 : 1009003 : other = b;
312 : : }
313 : : else
314 : : {
8347 tgl@sss.pgh.pa.us 315 :GBC 1 : result = bms_copy(b);
316 : 1 : other = a;
317 : : }
318 : : /* And intersect the longer input with the result */
8347 tgl@sss.pgh.pa.us 319 :CBC 1009004 : resultlen = result->nwords;
896 drowley@postgresql.o 320 : 1009004 : lastnonzero = -1;
321 : 1009004 : i = 0;
322 : : do
323 : : {
8347 tgl@sss.pgh.pa.us 324 : 1010283 : result->words[i] &= other->words[i];
325 : :
896 drowley@postgresql.o 326 [ + + ]: 1010283 : if (result->words[i] != 0)
327 : 996472 : lastnonzero = i;
328 [ + + ]: 1010283 : } while (++i < resultlen);
329 : : /* If we computed an empty result, we must return NULL */
330 [ + + ]: 1009004 : if (lastnonzero == -1)
331 : : {
1020 tgl@sss.pgh.pa.us 332 : 12672 : pfree(result);
333 : 12672 : return NULL;
334 : : }
335 : :
336 : : /* get rid of trailing zero words */
896 drowley@postgresql.o 337 : 996332 : result->nwords = lastnonzero + 1;
8347 tgl@sss.pgh.pa.us 338 : 996332 : return result;
339 : : }
340 : :
341 : : /*
342 : : * bms_difference - create and return a new set containing all the members of
343 : : * 'a' without the members of 'b'.
344 : : */
345 : : Bitmapset *
8166 bruce@momjian.us 346 : 2715191 : bms_difference(const Bitmapset *a, const Bitmapset *b)
347 : : {
348 : : Bitmapset *result;
349 : : int i;
350 : :
699 drowley@postgresql.o 351 [ - + ]: 2715191 : Assert(bms_is_valid_set(a));
352 [ - + ]: 2715191 : Assert(bms_is_valid_set(b));
353 : :
354 : : /* Handle cases where either input is NULL */
8347 tgl@sss.pgh.pa.us 355 [ + + ]: 2715191 : if (a == NULL)
356 : 1436289 : return NULL;
357 [ + + ]: 1278902 : if (b == NULL)
358 : 613579 : return bms_copy(a);
359 : :
360 : : /*
361 : : * In Postgres' usage, an empty result is a very common case, so it's
362 : : * worth optimizing for that by testing bms_nonempty_difference(). This
363 : : * saves us a palloc/pfree cycle compared to checking after-the-fact.
364 : : */
1020 365 [ + + ]: 665323 : if (!bms_nonempty_difference(a, b))
366 : 492634 : return NULL;
367 : :
368 : : /* Copy the left input */
8347 369 : 172689 : result = bms_copy(a);
370 : :
371 : : /* And remove b's bits from result */
896 drowley@postgresql.o 372 [ + + ]: 172689 : if (result->nwords > b->nwords)
373 : : {
374 : : /*
375 : : * We'll never need to remove trailing zero words when 'a' has more
376 : : * words than 'b' as the additional words must be non-zero.
377 : : */
896 drowley@postgresql.o 378 :GBC 2 : i = 0;
379 : : do
380 : : {
381 : 2 : result->words[i] &= ~b->words[i];
382 [ - + ]: 2 : } while (++i < b->nwords);
383 : : }
384 : : else
385 : : {
896 drowley@postgresql.o 386 :CBC 172687 : int lastnonzero = -1;
387 : :
388 : : /* we may need to remove trailing zero words from the result. */
389 : 172687 : i = 0;
390 : : do
391 : : {
392 : 172688 : result->words[i] &= ~b->words[i];
393 : :
394 : : /* remember the last non-zero word */
395 [ + + ]: 172688 : if (result->words[i] != 0)
396 : 172687 : lastnonzero = i;
397 [ + + ]: 172688 : } while (++i < result->nwords);
398 : :
399 : : /* trim off trailing zero words */
400 : 172687 : result->nwords = lastnonzero + 1;
401 : : }
402 [ - + ]: 172689 : Assert(result->nwords != 0);
403 : :
404 : : /* Need not check for empty result, since we handled that case above */
8347 tgl@sss.pgh.pa.us 405 : 172689 : return result;
406 : : }
407 : :
408 : : /*
409 : : * bms_is_subset - is A a subset of B?
410 : : */
411 : : bool
8166 bruce@momjian.us 412 : 14214821 : bms_is_subset(const Bitmapset *a, const Bitmapset *b)
413 : : {
414 : : int i;
415 : :
699 drowley@postgresql.o 416 [ - + ]: 14214821 : Assert(bms_is_valid_set(a));
417 [ - + ]: 14214821 : Assert(bms_is_valid_set(b));
418 : :
419 : : /* Handle cases where either input is NULL */
8347 tgl@sss.pgh.pa.us 420 [ + + ]: 14214821 : if (a == NULL)
421 : 3175227 : return true; /* empty set is a subset of anything */
422 [ + + ]: 11039594 : if (b == NULL)
1020 423 : 212869 : return false;
424 : :
425 : : /* 'a' can't be a subset of 'b' if it contains more words */
896 drowley@postgresql.o 426 [ + + ]: 10826725 : if (a->nwords > b->nwords)
896 drowley@postgresql.o 427 :GBC 2 : return false;
428 : :
429 : : /* Check all 'a' members are set in 'b' */
896 drowley@postgresql.o 430 :CBC 10826723 : i = 0;
431 : : do
432 : : {
8170 bruce@momjian.us 433 [ + + ]: 10826723 : if ((a->words[i] & ~b->words[i]) != 0)
8347 tgl@sss.pgh.pa.us 434 : 3724761 : return false;
896 drowley@postgresql.o 435 [ - + ]: 7101962 : } while (++i < a->nwords);
8347 tgl@sss.pgh.pa.us 436 : 7101962 : return true;
437 : : }
438 : :
439 : : /*
440 : : * bms_subset_compare - compare A and B for equality/subset relationships
441 : : *
442 : : * This is more efficient than testing bms_is_subset in both directions.
443 : : */
444 : : BMS_Comparison
5072 445 : 1391980 : bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
446 : : {
447 : : BMS_Comparison result;
448 : : int shortlen;
449 : : int i;
450 : :
699 drowley@postgresql.o 451 [ - + ]: 1391980 : Assert(bms_is_valid_set(a));
452 [ - + ]: 1391980 : Assert(bms_is_valid_set(b));
453 : :
454 : : /* Handle cases where either input is NULL */
5072 tgl@sss.pgh.pa.us 455 [ + + ]: 1391980 : if (a == NULL)
456 : : {
457 [ + + ]: 1178833 : if (b == NULL)
458 : 1144041 : return BMS_EQUAL;
1020 459 : 34792 : return BMS_SUBSET1;
460 : : }
5072 461 [ + + ]: 213147 : if (b == NULL)
1020 462 : 102060 : return BMS_SUBSET2;
463 : :
464 : : /* Check common words */
5072 465 : 111087 : result = BMS_EQUAL; /* status so far */
466 : 111087 : shortlen = Min(a->nwords, b->nwords);
896 drowley@postgresql.o 467 : 111087 : i = 0;
468 : : do
469 : : {
4937 bruce@momjian.us 470 : 111090 : bitmapword aword = a->words[i];
471 : 111090 : bitmapword bword = b->words[i];
472 : :
5072 tgl@sss.pgh.pa.us 473 [ + + ]: 111090 : if ((aword & ~bword) != 0)
474 : : {
475 : : /* a is not a subset of b */
476 [ + + ]: 29877 : if (result == BMS_SUBSET1)
5072 tgl@sss.pgh.pa.us 477 :GBC 2 : return BMS_DIFFERENT;
5072 tgl@sss.pgh.pa.us 478 :CBC 29875 : result = BMS_SUBSET2;
479 : : }
480 [ + + ]: 111088 : if ((bword & ~aword) != 0)
481 : : {
482 : : /* b is not a subset of a */
483 [ + + ]: 30117 : if (result == BMS_SUBSET2)
484 : 27886 : return BMS_DIFFERENT;
485 : 2231 : result = BMS_SUBSET1;
486 : : }
896 drowley@postgresql.o 487 [ + + ]: 83202 : } while (++i < shortlen);
488 : : /* Check extra words */
5072 tgl@sss.pgh.pa.us 489 [ + + ]: 83199 : if (a->nwords > b->nwords)
490 : : {
491 : : /* if a has more words then a is not a subset of b */
896 drowley@postgresql.o 492 [ + + ]:GBC 2 : if (result == BMS_SUBSET1)
493 : 1 : return BMS_DIFFERENT;
494 : 1 : return BMS_SUBSET2;
495 : : }
5072 tgl@sss.pgh.pa.us 496 [ + + ]:CBC 83197 : else if (a->nwords < b->nwords)
497 : : {
498 : : /* if b has more words then b is not a subset of a */
896 drowley@postgresql.o 499 [ + + ]:GBC 4 : if (result == BMS_SUBSET2)
500 : 2 : return BMS_DIFFERENT;
501 : 2 : return BMS_SUBSET1;
502 : : }
5072 tgl@sss.pgh.pa.us 503 :CBC 83193 : return result;
504 : : }
505 : :
506 : : /*
507 : : * bms_is_member - is X a member of A?
508 : : */
509 : : bool
8166 bruce@momjian.us 510 : 8801482 : bms_is_member(int x, const Bitmapset *a)
511 : : {
512 : : int wordnum,
513 : : bitnum;
514 : :
699 drowley@postgresql.o 515 [ - + ]: 8801482 : Assert(bms_is_valid_set(a));
516 : :
517 : : /* XXX better to just return false for x<0 ? */
8347 tgl@sss.pgh.pa.us 518 [ + + ]: 8801482 : if (x < 0)
8183 tgl@sss.pgh.pa.us 519 [ + - ]:GBC 1 : elog(ERROR, "negative bitmapset member not allowed");
8347 tgl@sss.pgh.pa.us 520 [ + + ]:CBC 8801481 : if (a == NULL)
521 : 4933935 : return false;
522 : :
523 : 3867546 : wordnum = WORDNUM(x);
524 : 3867546 : bitnum = BITNUM(x);
525 [ + + ]: 3867546 : if (wordnum >= a->nwords)
526 : 485 : return false;
527 [ + + ]: 3867061 : if ((a->words[wordnum] & ((bitmapword) 1 << bitnum)) != 0)
528 : 2686215 : return true;
529 : 1180846 : return false;
530 : : }
531 : :
532 : : /*
533 : : * bms_member_index
534 : : * determine 0-based index of member x in the bitmap
535 : : *
536 : : * Returns (-1) when x is not a member.
537 : : */
538 : : int
2456 tomas.vondra@postgre 539 : 2659 : bms_member_index(Bitmapset *a, int x)
540 : : {
541 : : int bitnum;
542 : : int wordnum;
543 : 2659 : int result = 0;
544 : : bitmapword mask;
545 : :
699 drowley@postgresql.o 546 [ - + ]: 2659 : Assert(bms_is_valid_set(a));
547 : :
548 : : /* return -1 if not a member of the bitmap */
2456 tomas.vondra@postgre 549 [ + + ]: 2659 : if (!bms_is_member(x, a))
2456 tomas.vondra@postgre 550 :GBC 2 : return -1;
551 : :
2456 tomas.vondra@postgre 552 :CBC 2657 : wordnum = WORDNUM(x);
553 : 2657 : bitnum = BITNUM(x);
554 : :
555 : : /* count bits in preceding words */
21 peter@eisentraut.org 556 [ + + ]:GNC 2664 : for (int i = 0; i < wordnum; i++)
557 : : {
2456 tomas.vondra@postgre 558 :GBC 7 : bitmapword w = a->words[i];
559 : :
560 : : /* No need to count the bits in a zero word */
561 [ + + ]: 7 : if (w != 0)
562 : 3 : result += bmw_popcount(w);
563 : : }
564 : :
565 : : /*
566 : : * Now add bits of the last word, but only those before the item. We can
567 : : * do that by applying a mask and then using popcount again. To get
568 : : * 0-based index, we want to count only preceding bits, not the item
569 : : * itself, so we subtract 1.
570 : : */
2456 tomas.vondra@postgre 571 :CBC 2657 : mask = ((bitmapword) 1 << bitnum) - 1;
572 : 2657 : result += bmw_popcount(a->words[wordnum] & mask);
573 : :
574 : 2657 : return result;
575 : : }
576 : :
577 : : /*
578 : : * bms_overlap - do sets overlap (ie, have a nonempty intersection)?
579 : : */
580 : : bool
8166 bruce@momjian.us 581 : 13940580 : bms_overlap(const Bitmapset *a, const Bitmapset *b)
582 : : {
583 : : int shortlen;
584 : : int i;
585 : :
699 drowley@postgresql.o 586 [ - + ]: 13940580 : Assert(bms_is_valid_set(a));
587 [ - + ]: 13940580 : Assert(bms_is_valid_set(b));
588 : :
589 : : /* Handle cases where either input is NULL */
8347 tgl@sss.pgh.pa.us 590 [ + + + + ]: 13940580 : if (a == NULL || b == NULL)
591 : 8581964 : return false;
592 : : /* Check words in common */
593 : 5358616 : shortlen = Min(a->nwords, b->nwords);
896 drowley@postgresql.o 594 : 5358616 : i = 0;
595 : : do
596 : : {
8347 tgl@sss.pgh.pa.us 597 [ + + ]: 5358616 : if ((a->words[i] & b->words[i]) != 0)
598 : 3213074 : return true;
896 drowley@postgresql.o 599 [ - + ]: 2145542 : } while (++i < shortlen);
8347 tgl@sss.pgh.pa.us 600 : 2145542 : return false;
601 : : }
602 : :
603 : : /*
604 : : * bms_overlap_list - does a set overlap an integer list?
605 : : */
606 : : bool
3186 rhodiumtoad@postgres 607 : 805 : bms_overlap_list(const Bitmapset *a, const List *b)
608 : : {
609 : : ListCell *lc;
610 : : int wordnum,
611 : : bitnum;
612 : :
699 drowley@postgresql.o 613 [ - + ]: 805 : Assert(bms_is_valid_set(a));
614 : :
3186 rhodiumtoad@postgres 615 [ + + + + ]: 805 : if (a == NULL || b == NIL)
616 : 729 : return false;
617 : :
618 [ + - + + : 142 : foreach(lc, b)
+ + ]
619 : : {
620 : 111 : int x = lfirst_int(lc);
621 : :
622 [ + + ]: 111 : if (x < 0)
3186 rhodiumtoad@postgres 623 [ + - ]:GBC 2 : elog(ERROR, "negative bitmapset member not allowed");
3186 rhodiumtoad@postgres 624 :CBC 109 : wordnum = WORDNUM(x);
625 : 109 : bitnum = BITNUM(x);
626 [ + - ]: 109 : if (wordnum < a->nwords)
627 [ + + ]: 109 : if ((a->words[wordnum] & ((bitmapword) 1 << bitnum)) != 0)
628 : 43 : return true;
629 : : }
630 : :
631 : 31 : return false;
632 : : }
633 : :
634 : : /*
635 : : * bms_nonempty_difference - do sets have a nonempty difference?
636 : : *
637 : : * i.e., are any members set in 'a' that are not also set in 'b'.
638 : : */
639 : : bool
8166 bruce@momjian.us 640 : 1996587 : bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
641 : : {
642 : : int i;
643 : :
699 drowley@postgresql.o 644 [ - + ]: 1996587 : Assert(bms_is_valid_set(a));
645 [ - + ]: 1996587 : Assert(bms_is_valid_set(b));
646 : :
647 : : /* Handle cases where either input is NULL */
8206 tgl@sss.pgh.pa.us 648 [ + + ]: 1996587 : if (a == NULL)
649 : 2620 : return false;
650 [ + + ]: 1993967 : if (b == NULL)
1020 tgl@sss.pgh.pa.us 651 :GBC 1 : return true;
652 : : /* if 'a' has more words then it must contain additional members */
896 drowley@postgresql.o 653 [ + + ]:CBC 1993966 : if (a->nwords > b->nwords)
896 drowley@postgresql.o 654 :GBC 3 : return true;
655 : : /* Check all 'a' members are set in 'b' */
896 drowley@postgresql.o 656 :CBC 1993963 : i = 0;
657 : : do
658 : : {
8170 bruce@momjian.us 659 [ + + ]: 1993963 : if ((a->words[i] & ~b->words[i]) != 0)
8206 tgl@sss.pgh.pa.us 660 : 898400 : return true;
896 drowley@postgresql.o 661 [ - + ]: 1095563 : } while (++i < a->nwords);
8206 tgl@sss.pgh.pa.us 662 : 1095563 : return false;
663 : : }
664 : :
665 : : /*
666 : : * bms_singleton_member - return the sole integer member of set
667 : : *
668 : : * Raises error if |a| is not 1.
669 : : */
670 : : int
8166 bruce@momjian.us 671 : 10817 : bms_singleton_member(const Bitmapset *a)
672 : : {
8170 673 : 10817 : int result = -1;
674 : : int nwords;
675 : : int wordnum;
676 : :
699 drowley@postgresql.o 677 [ - + ]: 10817 : Assert(bms_is_valid_set(a));
678 : :
8347 tgl@sss.pgh.pa.us 679 [ + + ]: 10817 : if (a == NULL)
8183 tgl@sss.pgh.pa.us 680 [ + - ]:GBC 1 : elog(ERROR, "bitmapset is empty");
681 : :
8347 tgl@sss.pgh.pa.us 682 :CBC 10816 : nwords = a->nwords;
896 drowley@postgresql.o 683 : 10816 : wordnum = 0;
684 : : do
685 : : {
8347 tgl@sss.pgh.pa.us 686 : 10816 : bitmapword w = a->words[wordnum];
687 : :
688 [ + - ]: 10816 : if (w != 0)
689 : : {
690 [ + - + + ]: 10816 : if (result >= 0 || HAS_MULTIPLE_ONES(w))
8183 tgl@sss.pgh.pa.us 691 [ + - ]:GBC 1 : elog(ERROR, "bitmapset has multiple members");
8347 tgl@sss.pgh.pa.us 692 :CBC 10815 : result = wordnum * BITS_PER_BITMAPWORD;
2496 693 : 10815 : result += bmw_rightmost_one_pos(w);
694 : : }
896 drowley@postgresql.o 695 [ - + ]: 10815 : } while (++wordnum < nwords);
696 : :
697 : : /* we don't expect non-NULL sets to be empty */
698 [ - + ]: 10815 : Assert(result >= 0);
8347 tgl@sss.pgh.pa.us 699 : 10815 : return result;
700 : : }
701 : :
702 : : /*
703 : : * bms_get_singleton_member
704 : : *
705 : : * Test whether the given set is a singleton.
706 : : * If so, set *member to the value of its sole member, and return true.
707 : : * If not, return false, without changing *member.
708 : : *
709 : : * This is more convenient and faster than calling bms_membership() and then
710 : : * bms_singleton_member(), if we don't care about distinguishing empty sets
711 : : * from multiple-member sets.
712 : : */
713 : : bool
4036 714 : 1318459 : bms_get_singleton_member(const Bitmapset *a, int *member)
715 : : {
716 : 1318459 : int result = -1;
717 : : int nwords;
718 : : int wordnum;
719 : :
699 drowley@postgresql.o 720 [ - + ]: 1318459 : Assert(bms_is_valid_set(a));
721 : :
4036 tgl@sss.pgh.pa.us 722 [ + + ]: 1318459 : if (a == NULL)
4036 tgl@sss.pgh.pa.us 723 :GBC 2 : return false;
724 : :
4036 tgl@sss.pgh.pa.us 725 :CBC 1318457 : nwords = a->nwords;
896 drowley@postgresql.o 726 : 1318457 : wordnum = 0;
727 : : do
728 : : {
4036 tgl@sss.pgh.pa.us 729 : 1318463 : bitmapword w = a->words[wordnum];
730 : :
731 [ + + ]: 1318463 : if (w != 0)
732 : : {
733 [ + - + + ]: 1318457 : if (result >= 0 || HAS_MULTIPLE_ONES(w))
734 : 235782 : return false;
735 : 1082675 : result = wordnum * BITS_PER_BITMAPWORD;
2496 736 : 1082675 : result += bmw_rightmost_one_pos(w);
737 : : }
896 drowley@postgresql.o 738 [ + + ]: 1082681 : } while (++wordnum < nwords);
739 : :
740 : : /* we don't expect non-NULL sets to be empty */
741 [ - + ]: 1082675 : Assert(result >= 0);
4036 tgl@sss.pgh.pa.us 742 : 1082675 : *member = result;
743 : 1082675 : return true;
744 : : }
745 : :
746 : : /*
747 : : * bms_num_members - count members of set
748 : : */
749 : : int
8166 bruce@momjian.us 750 : 1389798 : bms_num_members(const Bitmapset *a)
751 : : {
8170 752 : 1389798 : int result = 0;
753 : : int nwords;
754 : : int wordnum;
755 : :
699 drowley@postgresql.o 756 [ - + ]: 1389798 : Assert(bms_is_valid_set(a));
757 : :
8347 tgl@sss.pgh.pa.us 758 [ + + ]: 1389798 : if (a == NULL)
759 : 214195 : return 0;
760 : :
761 : 1175603 : nwords = a->nwords;
896 drowley@postgresql.o 762 : 1175603 : wordnum = 0;
763 : : do
764 : : {
8347 tgl@sss.pgh.pa.us 765 : 1175604 : bitmapword w = a->words[wordnum];
766 : :
767 : : /* No need to count the bits in a zero word */
2496 768 [ + - ]: 1175604 : if (w != 0)
769 : 1175604 : result += bmw_popcount(w);
896 drowley@postgresql.o 770 [ + + ]: 1175604 : } while (++wordnum < nwords);
8347 tgl@sss.pgh.pa.us 771 : 1175603 : return result;
772 : : }
773 : :
774 : : /*
775 : : * bms_membership - does a set have zero, one, or multiple members?
776 : : *
777 : : * This is faster than making an exact count with bms_num_members().
778 : : */
779 : : BMS_Membership
8166 bruce@momjian.us 780 : 889466 : bms_membership(const Bitmapset *a)
781 : : {
8347 tgl@sss.pgh.pa.us 782 : 889466 : BMS_Membership result = BMS_EMPTY_SET;
783 : : int nwords;
784 : : int wordnum;
785 : :
699 drowley@postgresql.o 786 [ - + ]: 889466 : Assert(bms_is_valid_set(a));
787 : :
8347 tgl@sss.pgh.pa.us 788 [ + + ]: 889466 : if (a == NULL)
789 : 243 : return BMS_EMPTY_SET;
790 : :
791 : 889223 : nwords = a->nwords;
896 drowley@postgresql.o 792 : 889223 : wordnum = 0;
793 : : do
794 : : {
8347 tgl@sss.pgh.pa.us 795 : 889223 : bitmapword w = a->words[wordnum];
796 : :
797 [ + - ]: 889223 : if (w != 0)
798 : : {
799 [ + - + + ]: 889223 : if (result != BMS_EMPTY_SET || HAS_MULTIPLE_ONES(w))
800 : 241729 : return BMS_MULTIPLE;
801 : 647494 : result = BMS_SINGLETON;
802 : : }
896 drowley@postgresql.o 803 [ - + ]: 647494 : } while (++wordnum < nwords);
8347 tgl@sss.pgh.pa.us 804 : 647494 : return result;
805 : : }
806 : :
807 : :
808 : : /*
809 : : * bms_add_member - add a specified member to set
810 : : *
811 : : * 'a' is recycled when possible.
812 : : */
813 : : Bitmapset *
8166 bruce@momjian.us 814 : 11672782 : bms_add_member(Bitmapset *a, int x)
815 : : {
816 : : int wordnum,
817 : : bitnum;
818 : :
699 drowley@postgresql.o 819 [ - + ]: 11672782 : Assert(bms_is_valid_set(a));
820 : :
8347 tgl@sss.pgh.pa.us 821 [ + + ]: 11672782 : if (x < 0)
8183 tgl@sss.pgh.pa.us 822 [ + - ]:GBC 2 : elog(ERROR, "negative bitmapset member not allowed");
8347 tgl@sss.pgh.pa.us 823 [ + + ]:CBC 11672780 : if (a == NULL)
824 : 6111118 : return bms_make_singleton(x);
825 : :
826 : 5561662 : wordnum = WORDNUM(x);
827 : 5561662 : bitnum = BITNUM(x);
828 : :
829 : : /* enlarge the set if necessary */
830 [ + + ]: 5561662 : if (wordnum >= a->nwords)
831 : : {
4460 heikki.linnakangas@i 832 : 531 : int oldnwords = a->nwords;
833 : : int i;
834 : :
835 : 531 : a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(wordnum + 1));
836 : 531 : a->nwords = wordnum + 1;
837 : : /* zero out the enlarged portion */
896 drowley@postgresql.o 838 : 531 : i = oldnwords;
839 : : do
840 : : {
4460 heikki.linnakangas@i 841 : 75219 : a->words[i] = 0;
896 drowley@postgresql.o 842 [ + + ]: 75219 : } while (++i < a->nwords);
843 : : }
844 : :
699 845 : 5561662 : a->words[wordnum] |= ((bitmapword) 1 << bitnum);
846 : :
847 : : #ifdef REALLOCATE_BITMAPSETS
848 : :
849 : : /*
850 : : * There's no guarantee that the repalloc returned a new pointer, so copy
851 : : * and free unconditionally here.
852 : : */
853 : : a = bms_copy_and_free(a);
854 : : #endif
855 : :
8347 tgl@sss.pgh.pa.us 856 : 5561662 : return a;
857 : : }
858 : :
859 : : /*
860 : : * bms_del_member - remove a specified member from set
861 : : *
862 : : * No error if x is not currently a member of set
863 : : *
864 : : * 'a' is recycled when possible.
865 : : */
866 : : Bitmapset *
8166 bruce@momjian.us 867 : 916257 : bms_del_member(Bitmapset *a, int x)
868 : : {
869 : : int wordnum,
870 : : bitnum;
871 : :
699 drowley@postgresql.o 872 [ - + ]: 916257 : Assert(bms_is_valid_set(a));
873 : :
8347 tgl@sss.pgh.pa.us 874 [ + + ]: 916257 : if (x < 0)
8183 tgl@sss.pgh.pa.us 875 [ + - ]:GBC 1 : elog(ERROR, "negative bitmapset member not allowed");
8347 tgl@sss.pgh.pa.us 876 [ + + ]:CBC 916256 : if (a == NULL)
877 : 322174 : return NULL;
878 : :
879 : 594082 : wordnum = WORDNUM(x);
880 : 594082 : bitnum = BITNUM(x);
881 : :
882 : : #ifdef REALLOCATE_BITMAPSETS
883 : : a = bms_copy_and_free(a);
884 : : #endif
885 : :
886 : : /* member can't exist. Return 'a' unmodified */
896 drowley@postgresql.o 887 [ - + ]: 594082 : if (unlikely(wordnum >= a->nwords))
896 drowley@postgresql.o 888 :UBC 0 : return a;
889 : :
896 drowley@postgresql.o 890 :CBC 594082 : a->words[wordnum] &= ~((bitmapword) 1 << bitnum);
891 : :
892 : : /* when last word becomes empty, trim off all trailing empty words */
893 [ + + + + ]: 594082 : if (a->words[wordnum] == 0 && wordnum == a->nwords - 1)
894 : : {
895 : : /* find the last non-empty word and make that the new final word */
896 [ + + ]: 443646 : for (int i = wordnum - 1; i >= 0; i--)
897 : : {
896 drowley@postgresql.o 898 [ + + ]:GBC 153858 : if (a->words[i] != 0)
899 : : {
900 : 349 : a->nwords = i + 1;
901 : 349 : return a;
902 : : }
903 : : }
904 : :
905 : : /* the set is now empty */
1020 tgl@sss.pgh.pa.us 906 :CBC 289788 : pfree(a);
907 : 289788 : return NULL;
908 : : }
8347 909 : 303945 : return a;
910 : : }
911 : :
912 : : /*
913 : : * bms_add_members - like bms_union, but left input is recycled when possible
914 : : */
915 : : Bitmapset *
8166 bruce@momjian.us 916 : 7733006 : bms_add_members(Bitmapset *a, const Bitmapset *b)
917 : : {
918 : : Bitmapset *result;
919 : : const Bitmapset *other;
920 : : int otherlen;
921 : : int i;
922 : :
699 drowley@postgresql.o 923 [ - + ]: 7733006 : Assert(bms_is_valid_set(a));
924 [ - + ]: 7733006 : Assert(bms_is_valid_set(b));
925 : :
926 : : /* Handle cases where either input is NULL */
8347 tgl@sss.pgh.pa.us 927 [ + + ]: 7733006 : if (a == NULL)
928 : 4046032 : return bms_copy(b);
929 [ + + ]: 3686974 : if (b == NULL)
930 : : {
931 : : #ifdef REALLOCATE_BITMAPSETS
932 : : a = bms_copy_and_free(a);
933 : : #endif
934 : :
935 : 2381386 : return a;
936 : : }
937 : : /* Identify shorter and longer input; copy the longer one if needed */
938 [ + + ]: 1305588 : if (a->nwords < b->nwords)
939 : : {
8347 tgl@sss.pgh.pa.us 940 :GBC 1 : result = bms_copy(b);
941 : 1 : other = a;
942 : : }
943 : : else
944 : : {
8347 tgl@sss.pgh.pa.us 945 :CBC 1305587 : result = a;
946 : 1305587 : other = b;
947 : : }
948 : : /* And union the shorter input into the result */
949 : 1305588 : otherlen = other->nwords;
896 drowley@postgresql.o 950 : 1305588 : i = 0;
951 : : do
952 : : {
8347 tgl@sss.pgh.pa.us 953 : 1305588 : result->words[i] |= other->words[i];
896 drowley@postgresql.o 954 [ - + ]: 1305588 : } while (++i < otherlen);
8347 tgl@sss.pgh.pa.us 955 [ + + ]: 1305588 : if (result != a)
8347 tgl@sss.pgh.pa.us 956 :GBC 1 : pfree(a);
957 : : #ifdef REALLOCATE_BITMAPSETS
958 : : else
959 : : result = bms_copy_and_free(result);
960 : : #endif
961 : :
8347 tgl@sss.pgh.pa.us 962 :CBC 1305588 : return result;
963 : : }
964 : :
965 : : /*
966 : : * bms_replace_members
967 : : * Remove all existing members from 'a' and repopulate the set with members
968 : : * from 'b', recycling 'a', when possible.
969 : : */
970 : : Bitmapset *
697 drowley@postgresql.o 971 : 6747 : bms_replace_members(Bitmapset *a, const Bitmapset *b)
972 : : {
973 : : int i;
974 : :
975 [ - + ]: 6747 : Assert(bms_is_valid_set(a));
976 [ - + ]: 6747 : Assert(bms_is_valid_set(b));
977 : :
978 [ + + ]: 6747 : if (a == NULL)
697 drowley@postgresql.o 979 :GBC 1 : return bms_copy(b);
697 drowley@postgresql.o 980 [ + + ]:CBC 6746 : if (b == NULL)
981 : : {
697 drowley@postgresql.o 982 :GBC 1 : pfree(a);
983 : 1 : return NULL;
984 : : }
985 : :
697 drowley@postgresql.o 986 [ + + ]:CBC 6745 : if (a->nwords < b->nwords)
697 drowley@postgresql.o 987 :GBC 1 : a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(b->nwords));
988 : :
697 drowley@postgresql.o 989 :CBC 6745 : i = 0;
990 : : do
991 : : {
992 : 6754 : a->words[i] = b->words[i];
993 [ + + ]: 6754 : } while (++i < b->nwords);
994 : :
995 : 6745 : a->nwords = b->nwords;
996 : :
997 : : #ifdef REALLOCATE_BITMAPSETS
998 : :
999 : : /*
1000 : : * There's no guarantee that the repalloc returned a new pointer, so copy
1001 : : * and free unconditionally here.
1002 : : */
1003 : : a = bms_copy_and_free(a);
1004 : : #endif
1005 : :
1006 : 6745 : return a;
1007 : : }
1008 : :
1009 : : /*
1010 : : * bms_add_range
1011 : : * Add members in the range of 'lower' to 'upper' to the set.
1012 : : *
1013 : : * Note this could also be done by calling bms_add_member in a loop, however,
1014 : : * using this function will be faster when the range is large as we work at
1015 : : * the bitmapword level rather than at bit level.
1016 : : */
1017 : : Bitmapset *
2939 rhaas@postgresql.org 1018 : 33086 : bms_add_range(Bitmapset *a, int lower, int upper)
1019 : : {
1020 : : int lwordnum,
1021 : : lbitnum,
1022 : : uwordnum,
1023 : : ushiftbits,
1024 : : wordnum;
1025 : :
699 drowley@postgresql.o 1026 [ - + ]: 33086 : Assert(bms_is_valid_set(a));
1027 : :
1028 : : /* do nothing if nothing is called for, without further checking */
2696 alvherre@alvh.no-ip. 1029 [ + + ]: 33086 : if (upper < lower)
1030 : : {
1031 : : #ifdef REALLOCATE_BITMAPSETS
1032 : : a = bms_copy_and_free(a);
1033 : : #endif
1034 : :
1035 : 15 : return a;
1036 : : }
1037 : :
tgl@sss.pgh.pa.us 1038 [ + + ]: 33071 : if (lower < 0)
2939 rhaas@postgresql.org 1039 [ + - ]:GBC 1 : elog(ERROR, "negative bitmapset member not allowed");
2939 rhaas@postgresql.org 1040 :CBC 33070 : uwordnum = WORDNUM(upper);
1041 : :
1042 [ + + ]: 33070 : if (a == NULL)
1043 : : {
1044 : 23047 : a = (Bitmapset *) palloc0(BITMAPSET_SIZE(uwordnum + 1));
1129 tgl@sss.pgh.pa.us 1045 : 23047 : a->type = T_Bitmapset;
2939 rhaas@postgresql.org 1046 : 23047 : a->nwords = uwordnum + 1;
1047 : : }
1048 [ + + ]: 10023 : else if (uwordnum >= a->nwords)
1049 : : {
2939 rhaas@postgresql.org 1050 :GBC 2 : int oldnwords = a->nwords;
1051 : : int i;
1052 : :
1053 : : /* ensure we have enough words to store the upper bit */
1054 : 2 : a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(uwordnum + 1));
1055 : 2 : a->nwords = uwordnum + 1;
1056 : : /* zero out the enlarged portion */
896 drowley@postgresql.o 1057 : 2 : i = oldnwords;
1058 : : do
1059 : : {
2939 rhaas@postgresql.org 1060 : 4 : a->words[i] = 0;
896 drowley@postgresql.o 1061 [ + + ]: 4 : } while (++i < a->nwords);
1062 : : }
1063 : :
2939 rhaas@postgresql.org 1064 :CBC 33070 : wordnum = lwordnum = WORDNUM(lower);
1065 : :
1066 : 33070 : lbitnum = BITNUM(lower);
1067 : 33070 : ushiftbits = BITS_PER_BITMAPWORD - (BITNUM(upper) + 1);
1068 : :
1069 : : /*
1070 : : * Special case when lwordnum is the same as uwordnum we must perform the
1071 : : * upper and lower masking on the word.
1072 : : */
1073 [ + + ]: 33070 : if (lwordnum == uwordnum)
1074 : : {
1075 : 32098 : a->words[lwordnum] |= ~(bitmapword) (((bitmapword) 1 << lbitnum) - 1)
2898 tgl@sss.pgh.pa.us 1076 : 32098 : & (~(bitmapword) 0) >> ushiftbits;
1077 : : }
1078 : : else
1079 : : {
1080 : : /* turn on lbitnum and all bits left of it */
2939 rhaas@postgresql.org 1081 :GBC 972 : a->words[wordnum++] |= ~(bitmapword) (((bitmapword) 1 << lbitnum) - 1);
1082 : :
1083 : : /* turn on all bits for any intermediate words */
1084 [ + + ]: 990 : while (wordnum < uwordnum)
1085 : 18 : a->words[wordnum++] = ~(bitmapword) 0;
1086 : :
1087 : : /* turn on upper's bit and all bits right of it. */
1088 : 972 : a->words[uwordnum] |= (~(bitmapword) 0) >> ushiftbits;
1089 : : }
1090 : :
1091 : : #ifdef REALLOCATE_BITMAPSETS
1092 : :
1093 : : /*
1094 : : * There's no guarantee that the repalloc returned a new pointer, so copy
1095 : : * and free unconditionally here.
1096 : : */
1097 : : a = bms_copy_and_free(a);
1098 : : #endif
1099 : :
2939 rhaas@postgresql.org 1100 :CBC 33070 : return a;
1101 : : }
1102 : :
1103 : : /*
1104 : : * bms_int_members - like bms_intersect, but left input is recycled when
1105 : : * possible
1106 : : */
1107 : : Bitmapset *
8166 bruce@momjian.us 1108 : 362641 : bms_int_members(Bitmapset *a, const Bitmapset *b)
1109 : : {
1110 : : int lastnonzero;
1111 : : int shortlen;
1112 : : int i;
1113 : :
699 drowley@postgresql.o 1114 [ - + ]: 362641 : Assert(bms_is_valid_set(a));
1115 [ - + ]: 362641 : Assert(bms_is_valid_set(b));
1116 : :
1117 : : /* Handle cases where either input is NULL */
8347 tgl@sss.pgh.pa.us 1118 [ + + ]: 362641 : if (a == NULL)
1119 : 13910 : return NULL;
1120 [ + + ]: 348731 : if (b == NULL)
1121 : : {
1122 : 2637 : pfree(a);
1123 : 2637 : return NULL;
1124 : : }
1125 : :
1126 : : /* Intersect b into a; we need never copy */
1127 : 346094 : shortlen = Min(a->nwords, b->nwords);
896 drowley@postgresql.o 1128 : 346094 : lastnonzero = -1;
1129 : 346094 : i = 0;
1130 : : do
1131 : : {
8347 tgl@sss.pgh.pa.us 1132 : 346095 : a->words[i] &= b->words[i];
1133 : :
896 drowley@postgresql.o 1134 [ + + ]: 346095 : if (a->words[i] != 0)
1135 : 286444 : lastnonzero = i;
1136 [ + + ]: 346095 : } while (++i < shortlen);
1137 : :
1138 : : /* If we computed an empty result, we must return NULL */
1139 [ + + ]: 346094 : if (lastnonzero == -1)
1140 : : {
1020 tgl@sss.pgh.pa.us 1141 : 59651 : pfree(a);
1142 : 59651 : return NULL;
1143 : : }
1144 : :
1145 : : /* get rid of trailing zero words */
896 drowley@postgresql.o 1146 : 286443 : a->nwords = lastnonzero + 1;
1147 : :
1148 : : #ifdef REALLOCATE_BITMAPSETS
1149 : : a = bms_copy_and_free(a);
1150 : : #endif
1151 : :
8347 tgl@sss.pgh.pa.us 1152 : 286443 : return a;
1153 : : }
1154 : :
1155 : : /*
1156 : : * bms_del_members - delete members in 'a' that are set in 'b'. 'a' is
1157 : : * recycled when possible.
1158 : : */
1159 : : Bitmapset *
8166 bruce@momjian.us 1160 : 1166463 : bms_del_members(Bitmapset *a, const Bitmapset *b)
1161 : : {
1162 : : int i;
1163 : :
699 drowley@postgresql.o 1164 [ - + ]: 1166463 : Assert(bms_is_valid_set(a));
1165 [ - + ]: 1166463 : Assert(bms_is_valid_set(b));
1166 : :
1167 : : /* Handle cases where either input is NULL */
8347 tgl@sss.pgh.pa.us 1168 [ + + ]: 1166463 : if (a == NULL)
1169 : 499141 : return NULL;
1170 [ + + ]: 667322 : if (b == NULL)
1171 : : {
1172 : : #ifdef REALLOCATE_BITMAPSETS
1173 : : a = bms_copy_and_free(a);
1174 : : #endif
1175 : :
699 drowley@postgresql.o 1176 : 106179 : return a;
1177 : : }
1178 : :
1179 : : /* Remove b's bits from a; we need never copy */
896 1180 [ + + ]: 561143 : if (a->nwords > b->nwords)
1181 : : {
1182 : : /*
1183 : : * We'll never need to remove trailing zero words when 'a' has more
1184 : : * words than 'b'.
1185 : : */
896 drowley@postgresql.o 1186 :GBC 1 : i = 0;
1187 : : do
1188 : : {
1189 : 1 : a->words[i] &= ~b->words[i];
1190 [ - + ]: 1 : } while (++i < b->nwords);
1191 : : }
1192 : : else
1193 : : {
896 drowley@postgresql.o 1194 :CBC 561142 : int lastnonzero = -1;
1195 : :
1196 : : /* we may need to remove trailing zero words from the result. */
1197 : 561142 : i = 0;
1198 : : do
1199 : : {
1200 : 561150 : a->words[i] &= ~b->words[i];
1201 : :
1202 : : /* remember the last non-zero word */
1203 [ + + ]: 561150 : if (a->words[i] != 0)
1204 : 120881 : lastnonzero = i;
1205 [ + + ]: 561150 : } while (++i < a->nwords);
1206 : :
1207 : : /* check if 'a' has become empty */
1208 [ + + ]: 561142 : if (lastnonzero == -1)
1209 : : {
1210 : 440263 : pfree(a);
1211 : 440263 : return NULL;
1212 : : }
1213 : :
1214 : : /* trim off any trailing zero words */
1215 : 120879 : a->nwords = lastnonzero + 1;
1216 : : }
1217 : :
1218 : : #ifdef REALLOCATE_BITMAPSETS
1219 : : a = bms_copy_and_free(a);
1220 : : #endif
1221 : :
8347 tgl@sss.pgh.pa.us 1222 : 120880 : return a;
1223 : : }
1224 : :
1225 : : /*
1226 : : * bms_join - like bms_union, but *either* input *may* be recycled
1227 : : */
1228 : : Bitmapset *
8166 bruce@momjian.us 1229 : 926589 : bms_join(Bitmapset *a, Bitmapset *b)
1230 : : {
1231 : : Bitmapset *result;
1232 : : Bitmapset *other;
1233 : : int otherlen;
1234 : : int i;
1235 : :
699 drowley@postgresql.o 1236 [ - + ]: 926589 : Assert(bms_is_valid_set(a));
1237 [ - + ]: 926589 : Assert(bms_is_valid_set(b));
1238 : :
1239 : : /* Handle cases where either input is NULL */
8347 tgl@sss.pgh.pa.us 1240 [ + + ]: 926589 : if (a == NULL)
1241 : : {
1242 : : #ifdef REALLOCATE_BITMAPSETS
1243 : : b = bms_copy_and_free(b);
1244 : : #endif
1245 : :
1246 : 373843 : return b;
1247 : : }
1248 [ + + ]: 552746 : if (b == NULL)
1249 : : {
1250 : : #ifdef REALLOCATE_BITMAPSETS
1251 : : a = bms_copy_and_free(a);
1252 : : #endif
1253 : :
699 drowley@postgresql.o 1254 : 97124 : return a;
1255 : : }
1256 : :
1257 : : /* Identify shorter and longer input; use longer one as result */
8347 tgl@sss.pgh.pa.us 1258 [ + + ]: 455622 : if (a->nwords < b->nwords)
1259 : : {
8347 tgl@sss.pgh.pa.us 1260 :GBC 2 : result = b;
1261 : 2 : other = a;
1262 : : }
1263 : : else
1264 : : {
8347 tgl@sss.pgh.pa.us 1265 :CBC 455620 : result = a;
1266 : 455620 : other = b;
1267 : : }
1268 : : /* And union the shorter input into the result */
1269 : 455622 : otherlen = other->nwords;
896 drowley@postgresql.o 1270 : 455622 : i = 0;
1271 : : do
1272 : : {
8347 tgl@sss.pgh.pa.us 1273 : 455622 : result->words[i] |= other->words[i];
896 drowley@postgresql.o 1274 [ - + ]: 455622 : } while (++i < otherlen);
8347 tgl@sss.pgh.pa.us 1275 [ + - ]: 455622 : if (other != result) /* pure paranoia */
1276 : 455622 : pfree(other);
1277 : :
1278 : : #ifdef REALLOCATE_BITMAPSETS
1279 : : result = bms_copy_and_free(result);
1280 : : #endif
1281 : :
1282 : 455622 : return result;
1283 : : }
1284 : :
1285 : : /*
1286 : : * bms_next_member - find next member of a set
1287 : : *
1288 : : * Returns smallest member greater than "prevbit", or -2 if there is none.
1289 : : * "prevbit" must NOT be less than -1, or the behavior is unpredictable.
1290 : : *
1291 : : * This is intended as support for iterating through the members of a set.
1292 : : * The typical pattern is
1293 : : *
1294 : : * x = -1;
1295 : : * while ((x = bms_next_member(inputset, x)) >= 0)
1296 : : * process member x;
1297 : : *
1298 : : * Notice that when there are no more members, we return -2, not -1 as you
1299 : : * might expect. The rationale for that is to allow distinguishing the
1300 : : * loop-not-started state (x == -1) from the loop-completed state (x == -2).
1301 : : * It makes no difference in simple loop usage, but complex iteration logic
1302 : : * might need such an ability.
1303 : : */
1304 : : int
4036 1305 : 11853194 : bms_next_member(const Bitmapset *a, int prevbit)
1306 : : {
1307 : : int nwords;
1308 : : bitmapword mask;
1309 : :
699 drowley@postgresql.o 1310 [ - + ]: 11853194 : Assert(bms_is_valid_set(a));
1311 : :
4036 tgl@sss.pgh.pa.us 1312 [ + + ]: 11853194 : if (a == NULL)
1313 : 2798270 : return -2;
1314 : 9054924 : nwords = a->nwords;
1315 : 9054924 : prevbit++;
1316 : 9054924 : mask = (~(bitmapword) 0) << BITNUM(prevbit);
21 peter@eisentraut.org 1317 [ + + ]:GNC 11926535 : for (int wordnum = WORDNUM(prevbit); wordnum < nwords; wordnum++)
1318 : : {
4036 tgl@sss.pgh.pa.us 1319 :CBC 9056274 : bitmapword w = a->words[wordnum];
1320 : :
1321 : : /* ignore bits before prevbit */
1322 : 9056274 : w &= mask;
1323 : :
1324 [ + + ]: 9056274 : if (w != 0)
1325 : : {
1326 : : int result;
1327 : :
1328 : 6184663 : result = wordnum * BITS_PER_BITMAPWORD;
2496 1329 : 6184663 : result += bmw_rightmost_one_pos(w);
4036 1330 : 6184663 : return result;
1331 : : }
1332 : :
1333 : : /* in subsequent words, consider all bits */
1334 : 2871611 : mask = (~(bitmapword) 0);
1335 : : }
1336 : 2870261 : return -2;
1337 : : }
1338 : :
1339 : : /*
1340 : : * bms_prev_member - find prev member of a set
1341 : : *
1342 : : * Returns largest member less than "prevbit", or -2 if there is none.
1343 : : * "prevbit" must NOT be more than one above the highest possible bit that can
1344 : : * be set in the Bitmapset at its current size.
1345 : : *
1346 : : * To ease finding the highest set bit for the initial loop, the special
1347 : : * prevbit value of -1 can be passed to have the function find the highest
1348 : : * valued member in the set.
1349 : : *
1350 : : * This is intended as support for iterating through the members of a set in
1351 : : * reverse. The typical pattern is
1352 : : *
1353 : : * x = -1;
1354 : : * while ((x = bms_prev_member(inputset, x)) >= 0)
1355 : : * process member x;
1356 : : *
1357 : : * Notice that when there are no more members, we return -2, not -1 as you
1358 : : * might expect. The rationale for that is to allow distinguishing the
1359 : : * loop-not-started state (x == -1) from the loop-completed state (x == -2).
1360 : : * It makes no difference in simple loop usage, but complex iteration logic
1361 : : * might need such an ability.
1362 : : */
1363 : :
1364 : : int
2810 alvherre@alvh.no-ip. 1365 : 15 : bms_prev_member(const Bitmapset *a, int prevbit)
1366 : : {
1367 : : int ushiftbits;
1368 : : bitmapword mask;
1369 : :
699 drowley@postgresql.o 1370 [ - + ]: 15 : Assert(bms_is_valid_set(a));
1371 : :
1372 : : /*
1373 : : * If set is NULL or if there are no more bits to the right then we've
1374 : : * nothing to do.
1375 : : */
2810 alvherre@alvh.no-ip. 1376 [ + + - + ]: 15 : if (a == NULL || prevbit == 0)
2810 alvherre@alvh.no-ip. 1377 :GBC 2 : return -2;
1378 : :
1379 : : /* Validate callers didn't give us something out of range */
123 drowley@postgresql.o 1380 [ - + ]:GNC 13 : Assert(prevbit <= a->nwords * BITS_PER_BITMAPWORD);
1381 [ - + ]: 13 : Assert(prevbit >= -1);
1382 : :
1383 : : /* transform -1 to the highest possible bit we could have set */
2810 alvherre@alvh.no-ip. 1384 [ + + ]:CBC 13 : if (prevbit == -1)
2810 alvherre@alvh.no-ip. 1385 :GBC 1 : prevbit = a->nwords * BITS_PER_BITMAPWORD - 1;
1386 : : else
2810 alvherre@alvh.no-ip. 1387 :CBC 12 : prevbit--;
1388 : :
1389 : 13 : ushiftbits = BITS_PER_BITMAPWORD - (BITNUM(prevbit) + 1);
1390 : 13 : mask = (~(bitmapword) 0) >> ushiftbits;
21 peter@eisentraut.org 1391 [ + + ]:GNC 17 : for (int wordnum = WORDNUM(prevbit); wordnum >= 0; wordnum--)
1392 : : {
2810 alvherre@alvh.no-ip. 1393 :CBC 13 : bitmapword w = a->words[wordnum];
1394 : :
1395 : : /* mask out bits left of prevbit */
1396 : 13 : w &= mask;
1397 : :
1398 [ + + ]: 13 : if (w != 0)
1399 : : {
1400 : : int result;
1401 : :
1402 : 9 : result = wordnum * BITS_PER_BITMAPWORD;
2496 tgl@sss.pgh.pa.us 1403 : 9 : result += bmw_leftmost_one_pos(w);
2810 alvherre@alvh.no-ip. 1404 : 9 : return result;
1405 : : }
1406 : :
1407 : : /* in subsequent words, consider all bits */
1408 : 4 : mask = (~(bitmapword) 0);
1409 : : }
1410 : 4 : return -2;
1411 : : }
1412 : :
1413 : : /*
1414 : : * bms_hash_value - compute a hash key for a Bitmapset
1415 : : */
1416 : : uint32
7496 tgl@sss.pgh.pa.us 1417 : 3563 : bms_hash_value(const Bitmapset *a)
1418 : : {
699 drowley@postgresql.o 1419 [ - + ]: 3563 : Assert(bms_is_valid_set(a));
1420 : :
6773 tgl@sss.pgh.pa.us 1421 [ + + ]: 3563 : if (a == NULL)
7496 tgl@sss.pgh.pa.us 1422 :GBC 4 : return 0; /* All empty sets hash to 0 */
6773 tgl@sss.pgh.pa.us 1423 :CBC 3559 : return DatumGetUInt32(hash_any((const unsigned char *) a->words,
896 drowley@postgresql.o 1424 : 3559 : a->nwords * sizeof(bitmapword)));
1425 : : }
1426 : :
1427 : : /*
1428 : : * bitmap_hash - hash function for keys that are (pointers to) Bitmapsets
1429 : : *
1430 : : * Note: don't forget to specify bitmap_match as the match function!
1431 : : */
1432 : : uint32
2122 rhaas@postgresql.org 1433 : 3556 : bitmap_hash(const void *key, Size keysize)
1434 : : {
1435 [ - + ]: 3556 : Assert(keysize == sizeof(Bitmapset *));
1436 : 3556 : return bms_hash_value(*((const Bitmapset *const *) key));
1437 : : }
1438 : :
1439 : : /*
1440 : : * bitmap_match - match function to use with bitmap_hash
1441 : : */
1442 : : int
1443 : 2091 : bitmap_match(const void *key1, const void *key2, Size keysize)
1444 : : {
1445 [ - + ]: 2091 : Assert(keysize == sizeof(Bitmapset *));
1446 : 2091 : return !bms_equal(*((const Bitmapset *const *) key1),
1447 : : *((const Bitmapset *const *) key2));
1448 : : }
|