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
/*--------------------------------------------------------------------
* Symbols referenced in this file:
* - bms_copy
* - bms_is_valid_set
* - bms_equal
* - bms_free
* - bms_next_member
* - bms_num_members
*--------------------------------------------------------------------
*/
/*-------------------------------------------------------------------------
*
* 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
*
*-------------------------------------------------------------------------
*/
/*----------
* 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.
*----------
*/
/*
* bms_is_valid_set - for cassert builds to check for valid sets
*/
static bool
/*
* 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 - make a palloc'd copy of a bitmapset
*/
Bitmapset *
/*
* bms_equal - are two bitmapsets equal? or both NULL?
*/
bool
/*
* 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.)
*/
/*
* bms_make_singleton - build a bitmapset containing a single member
*/
/*
* bms_free - free a bitmapset
*
* Same as pfree except for allowing NULL input
*/
void
/*
* bms_union - create and return a new set containing all members from both
* input sets. Both inputs are left unmodified.
*/
/*
* bms_intersect - create and return a new set containing members which both
* input sets have in common. Both inputs are left unmodified.
*/
/*
* bms_difference - create and return a new set containing all the members of
* 'a' without the members of 'b'.
*/
/*
* bms_is_subset - is A a subset of B?
*/
/*
* bms_subset_compare - compare A and B for equality/subset relationships
*
* This is more efficient than testing bms_is_subset in both directions.
*/
/*
* bms_is_member - is X a member of A?
*/
/*
* bms_member_index
* determine 0-based index of member x in the bitmap
*
* Returns (-1) when x is not a member.
*/
/*
* bms_overlap - do sets overlap (ie, have a nonempty intersection)?
*/
/*
* bms_overlap_list - does a set overlap an integer list?
*/
/*
* bms_nonempty_difference - do sets have a nonempty difference?
*
* i.e., are any members set in 'a' that are not also set in 'b'.
*/
/*
* bms_singleton_member - return the sole integer member of set
*
* Raises error if |a| is not 1.
*/
/*
* 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.
*/
/*
* bms_num_members - count members of set
*/
int
/*
* bms_membership - does a set have zero, one, or multiple members?
*
* This is faster than making an exact count with bms_num_members().
*/
/*
* bms_add_member - add a specified member to set
*
* 'a' is recycled when possible.
*/
/*
* 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.
*/
/*
* bms_add_members - like bms_union, but left input is recycled when possible
*/
/*
* bms_replace_members
* Remove all existing members from 'a' and repopulate the set with members
* from 'b', recycling 'a', when possible.
*/
/*
* bms_add_range
* Add members in the range of 'lower' to 'upper' to the set.
*
* Note this could also be done by calling bms_add_member in a loop, however,
* using this function will be faster when the range is large as we work at
* the bitmapword level rather than at bit level.
*/
/*
* bms_int_members - like bms_intersect, but left input is recycled when
* possible
*/
/*
* bms_del_members - delete members in 'a' that are set in 'b'. 'a' is
* recycled when possible.
*/
/*
* bms_join - like bms_union, but *either* input *may* be recycled
*/
/*
* bms_next_member - find next member of a set
*
* Returns smallest member greater than "prevbit", or -2 if there is none.
* "prevbit" must NOT be less than -1, or the behavior is unpredictable.
*
* This is intended as support for iterating through the members of a set.
* The typical pattern is
*
* x = -1;
* while ((x = bms_next_member(inputset, x)) >= 0)
* process member x;
*
* Notice that when there are no more members, we return -2, not -1 as you
* might expect. The rationale for that is to allow distinguishing the
* loop-not-started state (x == -1) from the loop-completed state (x == -2).
* It makes no difference in simple loop usage, but complex iteration logic
* might need such an ability.
*/
int
/*
* bms_prev_member - find prev member of a set
*
* Returns largest member less than "prevbit", or -2 if there is none.
* "prevbit" must NOT be more than one above the highest possible bit that can
* be set at the Bitmapset at its current size.
*
* To ease finding the highest set bit for the initial loop, the special
* prevbit value of -1 can be passed to have the function find the highest
* valued member in the set.
*
* This is intended as support for iterating through the members of a set in
* reverse. The typical pattern is
*
* x = -1;
* while ((x = bms_prev_member(inputset, x)) >= 0)
* process member x;
*
* Notice that when there are no more members, we return -2, not -1 as you
* might expect. The rationale for that is to allow distinguishing the
* loop-not-started state (x == -1) from the loop-completed state (x == -2).
* It makes no difference in simple loop usage, but complex iteration logic
* might need such an ability.
*/
/*
* bms_hash_value - compute a hash key for a Bitmapset
*/
/*
* bitmap_hash - hash function for keys that are (pointers to) Bitmapsets
*
* Note: don't forget to specify bitmap_match as the match function!
*/
/*
* bitmap_match - match function to use with bitmap_hash
*/