-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathrangeset.c
413 lines (342 loc) · 10.3 KB
/
rangeset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* ------------------------------------------------------------------------
*
* rangeset.c
* IndexRange functions
*
* Copyright (c) 2015-2020, Postgres Professional
*
* ------------------------------------------------------------------------
*/
#include "compat/pg_compat.h"
#include "rangeset.h"
static IndexRange irange_handle_cover_internal(IndexRange ir_covering,
IndexRange ir_inner,
List **new_iranges);
static IndexRange irange_union_internal(IndexRange first,
IndexRange second,
List **new_iranges);
/* Make union of two conjuncted ranges */
IndexRange
irange_union_simple(IndexRange a, IndexRange b)
{
/* Ranges should be connected somehow */
Assert(iranges_intersect(a, b) || iranges_adjoin(a, b));
return make_irange(Min(irange_lower(a), irange_lower(b)),
Max(irange_upper(a), irange_upper(b)),
is_irange_lossy(a) && is_irange_lossy(b));
}
/* Get intersection of two conjuncted ranges */
IndexRange
irange_intersection_simple(IndexRange a, IndexRange b)
{
/* Ranges should be connected somehow */
Assert(iranges_intersect(a, b) || iranges_adjoin(a, b));
return make_irange(Max(irange_lower(a), irange_lower(b)),
Min(irange_upper(a), irange_upper(b)),
is_irange_lossy(a) || is_irange_lossy(b));
}
/* Split covering IndexRange into several IndexRanges if needed */
static IndexRange
irange_handle_cover_internal(IndexRange ir_covering,
IndexRange ir_inner,
List **new_iranges)
{
/* Equal lossiness should've been taken into cosideration earlier */
Assert(is_irange_lossy(ir_covering) != is_irange_lossy(ir_inner));
/* range 'ir_inner' is lossy */
if (is_irange_lossy(ir_covering) == false)
return ir_covering;
/* range 'ir_covering' is lossy, 'ir_inner' is lossless! */
else
{
IndexRange ret; /* IndexRange to be returned */
/* 'left_range_upper' should not be less than 'left_range_lower' */
uint32 left_range_lower = irange_lower(ir_covering),
left_range_upper = Max(irb_pred(irange_lower(ir_inner)),
left_range_lower);
/* 'right_range_lower' should not be greater than 'right_range_upper' */
uint32 right_range_upper = irange_upper(ir_covering),
right_range_lower = Min(irb_succ(irange_upper(ir_inner)),
right_range_upper);
/* We have to split the covering lossy IndexRange */
Assert(is_irange_lossy(ir_covering) == true);
/* 'ir_inner' should not cover leftmost IndexRange */
if (irange_lower(ir_inner) > left_range_upper)
{
IndexRange left_range;
/* Leftmost IndexRange is lossy */
left_range = make_irange(left_range_lower,
left_range_upper,
IR_LOSSY);
/* Append leftmost IndexRange ('left_range') to 'new_iranges' */
*new_iranges = lappend_irange(*new_iranges, left_range);
}
/* 'ir_inner' should not cover rightmost IndexRange */
if (right_range_lower > irange_upper(ir_inner))
{
IndexRange right_range;
/* Rightmost IndexRange is also lossy */
right_range = make_irange(right_range_lower,
right_range_upper,
IR_LOSSY);
/* 'right_range' is indeed rightmost IndexRange */
ret = right_range;
/* Append medial IndexRange ('ir_inner') to 'new_iranges' */
*new_iranges = lappend_irange(*new_iranges, ir_inner);
}
/* Else return 'ir_inner' as rightmost IndexRange */
else ret = ir_inner;
/* Return rightmost IndexRange (right_range | ir_inner) */
return ret;
}
}
/* Calculate union of two IndexRanges, return rightmost IndexRange */
static IndexRange
irange_union_internal(IndexRange first,
IndexRange second,
List **new_iranges)
{
/* Assert that both IndexRanges are valid */
Assert(is_irange_valid(first));
Assert(is_irange_valid(second));
/* Swap 'first' and 'second' if order is incorrect */
if (irange_lower(first) > irange_lower(second))
{
IndexRange temp;
temp = first;
first = second;
second = temp;
}
/* IndexRanges intersect */
if (iranges_intersect(first, second))
{
/* Calculate the union of 'first' and 'second' */
IndexRange ir_union = irange_union_simple(first, second);
/* if lossiness is the same, unite them and skip */
if (is_irange_lossy(first) == is_irange_lossy(second))
return ir_union;
/* range 'first' covers 'second' */
if (irange_eq_bounds(ir_union, first))
{
/* Return rightmost IndexRange, save others to 'new_iranges' */
return irange_handle_cover_internal(first, second, new_iranges);
}
/* range 'second' covers 'first' */
else if (irange_eq_bounds(ir_union, second))
{
/* Retun rightmost IndexRange, save others to 'new_iranges' */
return irange_handle_cover_internal(second, first, new_iranges);
}
/* No obvious leader, lossiness differs */
else
{
/* range 'second' is lossy */
if (is_irange_lossy(first) == false)
{
IndexRange ret;
/* Set new current IndexRange */
ret = make_irange(irb_succ(irange_upper(first)),
irange_upper(second),
is_irange_lossy(second));
/* Append lower part to 'new_iranges' */
*new_iranges = lappend_irange(*new_iranges, first);
/* Return a part of 'second' */
return ret;
}
/* range 'first' is lossy */
else
{
IndexRange new_irange;
new_irange = make_irange(irange_lower(first),
irb_pred(irange_lower(second)),
is_irange_lossy(first));
/* Append lower part to 'new_iranges' */
*new_iranges = lappend_irange(*new_iranges, new_irange);
/* Return 'second' */
return second;
}
}
}
/* IndexRanges do not intersect */
else
{
/* Try to unite these IndexRanges if it's possible */
if (irange_cmp_lossiness(first, second) == IR_EQ_LOSSINESS &&
iranges_adjoin(first, second))
{
/* Return united IndexRange */
return irange_union_simple(first, second);
}
/* IndexRanges are not adjoint */
else
{
/* add 'first' to 'new_iranges' */
*new_iranges = lappend_irange(*new_iranges, first);
/* Return 'second' */
return second;
}
}
}
/* Make union of two index rage lists */
List *
irange_list_union(List *a, List *b)
{
ListCell *ca, /* iterator of A */
*cb; /* iterator of B */
List *result = NIL; /* list of IndexRanges */
IndexRange cur = InvalidIndexRange; /* current irange */
/* Initialize iterators */
ca = list_head(a);
cb = list_head(b);
/* Loop until we have no cells */
while (ca || cb)
{
IndexRange next = InvalidIndexRange;
/* Fetch next irange with lesser lower bound */
if (ca && cb)
{
if (irange_lower(lfirst_irange(ca)) <= irange_lower(lfirst_irange(cb)))
{
next = lfirst_irange(ca);
ca = lnext_compat(a, ca); /* move to next cell */
}
else
{
next = lfirst_irange(cb);
cb = lnext_compat(b, cb); /* move to next cell */
}
}
/* Fetch next irange from A */
else if (ca)
{
next = lfirst_irange(ca);
ca = lnext_compat(a, ca); /* move to next cell */
}
/* Fetch next irange from B */
else if (cb)
{
next = lfirst_irange(cb);
cb = lnext_compat(b, cb); /* move to next cell */
}
/* Put this irange to 'cur' if don't have it yet */
if (!is_irange_valid(cur))
{
cur = next;
continue; /* skip this iteration */
}
/* Unite 'cur' and 'next' in an appropriate way */
cur = irange_union_internal(cur, next, &result);
}
/* Put current value into result list if any */
if (is_irange_valid(cur))
result = lappend_irange(result, cur);
return result;
}
/* Find intersection of two range lists */
List *
irange_list_intersection(List *a, List *b)
{
ListCell *ca, /* iterator of A */
*cb; /* iterator of B */
List *result = NIL; /* list of IndexRanges */
/* Initialize iterators */
ca = list_head(a);
cb = list_head(b);
/* Loop until we have no cells */
while (ca && cb)
{
IndexRange ra = lfirst_irange(ca),
rb = lfirst_irange(cb);
/* Assert that both IndexRanges are valid */
Assert(is_irange_valid(ra));
Assert(is_irange_valid(rb));
/* Only care about intersecting ranges */
if (iranges_intersect(ra, rb))
{
IndexRange ir_intersection;
bool glued_to_last = false;
/*
* Get intersection and try to "glue" it to
* last irange, put it separately otherwise.
*/
ir_intersection = irange_intersection_simple(ra, rb);
if (result != NIL)
{
IndexRange last = llast_irange(result);
/* Test if we can glue 'last' and 'ir_intersection' */
if (irange_cmp_lossiness(last, ir_intersection) == IR_EQ_LOSSINESS &&
iranges_adjoin(last, ir_intersection))
{
IndexRange ir_union = irange_union_simple(last, ir_intersection);
/* We allocate a new IndexRange for safety */
llast(result) = alloc_irange(ir_union);
/* Successfully glued them */
glued_to_last = true;
}
}
/* Append IndexRange if we couldn't glue it */
if (!glued_to_last)
result = lappend_irange(result, ir_intersection);
}
/*
* Fetch next iranges. We use upper bound of current irange to
* determine which lists to fetch, since lower bound of next
* irange is greater (or equal) to upper bound of current.
*/
if (irange_upper(ra) <= irange_upper(rb))
ca = lnext_compat(a, ca);
if (irange_upper(ra) >= irange_upper(rb))
cb = lnext_compat(b, cb);
}
return result;
}
/* Set lossiness of rangeset */
List *
irange_list_set_lossiness(List *ranges, bool lossy)
{
List *result = NIL;
ListCell *lc;
if (ranges == NIL)
return NIL;
foreach (lc, ranges)
{
IndexRange ir = lfirst_irange(lc);
result = lappend_irange(result, make_irange(irange_lower(ir),
irange_upper(ir),
lossy));
}
/* Unite adjacent and overlapping IndexRanges */
return irange_list_union(result, NIL);
}
/* Get total number of elements in range list */
int
irange_list_length(List *rangeset)
{
ListCell *lc;
uint32 result = 0;
foreach (lc, rangeset)
{
IndexRange irange = lfirst_irange(lc);
uint32 diff = irange_upper(irange) - irange_lower(irange);
Assert(irange_upper(irange) >= irange_lower(irange));
result += diff + 1;
}
return (int) result;
}
/* Find particular index in range list */
bool
irange_list_find(List *rangeset, int index, bool *lossy)
{
ListCell *lc;
foreach (lc, rangeset)
{
IndexRange irange = lfirst_irange(lc);
if (index >= irange_lower(irange) && index <= irange_upper(irange))
{
if (lossy)
*lossy = is_irange_lossy(irange);
return true;
}
}
return false;
}