Skip to content

Commit 549f211

Browse files
committed
Merge branch 'master_fixed_rangeset' into rel_1_2_beta
2 parents b803886 + fcccced commit 549f211

File tree

4 files changed

+252
-49
lines changed

4 files changed

+252
-49
lines changed

src/rangeset.c

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,6 @@ irange_eq_bounds(IndexRange a, IndexRange b)
4444
(irange_upper(a) == irange_upper(b));
4545
}
4646

47-
/* Comapre lossiness factor of two ranges */
48-
ir_cmp_lossiness
49-
irange_cmp_lossiness(IndexRange a, IndexRange b)
50-
{
51-
if (is_irange_lossy(a) == is_irange_lossy(b))
52-
return IR_EQ_LOSSINESS;
53-
54-
if (is_irange_lossy(a))
55-
return IR_A_LOSSY;
56-
57-
if (is_irange_lossy(b))
58-
return IR_B_LOSSY;
59-
60-
return IR_EQ_LOSSINESS;
61-
}
62-
6347

6448
/* Make union of two conjuncted ranges */
6549
IndexRange
@@ -161,6 +145,10 @@ irange_union_internal(IndexRange first,
161145
IndexRange second,
162146
List **new_iranges)
163147
{
148+
/* Assert that both IndexRanges are valid */
149+
Assert(is_irange_valid(first));
150+
Assert(is_irange_valid(second));
151+
164152
/* Swap 'first' and 'second' if order is incorrect */
165153
if (irange_lower(first) > irange_lower(second))
166154
{
@@ -332,39 +320,48 @@ irange_list_intersection(List *a, List *b)
332320
IndexRange ra = lfirst_irange(ca),
333321
rb = lfirst_irange(cb);
334322

323+
/* Assert that both IndexRanges are valid */
324+
Assert(is_irange_valid(ra));
325+
Assert(is_irange_valid(rb));
326+
335327
/* Only care about intersecting ranges */
336328
if (iranges_intersect(ra, rb))
337329
{
338-
IndexRange intersect, last;
330+
IndexRange ir_intersection;
331+
bool glued_to_last = false;
339332

340333
/*
341334
* Get intersection and try to "glue" it to
342-
* previous range, put it separately otherwise.
335+
* last irange, put it separately otherwise.
343336
*/
344-
intersect = irange_intersection_simple(ra, rb);
337+
ir_intersection = irange_intersection_simple(ra, rb);
345338
if (result != NIL)
346339
{
347-
last = llast_irange(result);
348-
if (iranges_adjoin(last, intersect) &&
349-
is_irange_lossy(last) == is_irange_lossy(intersect))
350-
{
351-
llast(result) = alloc_irange(irange_union_simple(last, intersect));
352-
}
353-
else
340+
IndexRange last = llast_irange(result);
341+
342+
/* Test if we can glue 'last' and 'ir_intersection' */
343+
if (irange_cmp_lossiness(last, ir_intersection) == IR_EQ_LOSSINESS &&
344+
iranges_adjoin(last, ir_intersection))
354345
{
355-
result = lappend_irange(result, intersect);
346+
IndexRange ir_union = irange_union_simple(last, ir_intersection);
347+
348+
/* We allocate a new IndexRange for safety */
349+
llast(result) = alloc_irange(ir_union);
350+
351+
/* Successfully glued them */
352+
glued_to_last = true;
356353
}
357354
}
358-
else
359-
{
360-
result = lappend_irange(result, intersect);
361-
}
355+
356+
/* Append IndexRange if we couldn't glue it */
357+
if (!glued_to_last)
358+
result = lappend_irange(result, ir_intersection);
362359
}
363360

364361
/*
365-
* Fetch next ranges. We use upper bound of current range to determine
366-
* which lists to fetch, since lower bound of next range is greater (or
367-
* equal) to upper bound of current.
362+
* Fetch next iranges. We use upper bound of current irange to
363+
* determine which lists to fetch, since lower bound of next
364+
* irange is greater (or equal) to upper bound of current.
368365
*/
369366
if (irange_upper(ra) <= irange_upper(rb))
370367
ca = lnext(ca);

src/rangeset.h

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ typedef struct {
4444
#define irange_upper(irange) ( (uint32) (irange.upper & IRANGE_BONDARY_MASK) )
4545

4646

47+
#define lfirst_irange(lc) ( *(IndexRange *) lfirst(lc) )
48+
#define lappend_irange(list, irange) ( lappend((list), alloc_irange(irange)) )
49+
#define lcons_irange(irange, list) ( lcons(alloc_irange(irange), (list)) )
50+
#define list_make1_irange(irange) ( lcons_irange(irange, NIL) )
51+
#define llast_irange(list) ( lfirst_irange(list_tail(list)) )
52+
#define linitial_irange(list) ( lfirst_irange(list_head(list)) )
53+
54+
4755
inline static IndexRange
4856
make_irange(uint32 lower, uint32 upper, bool lossy)
4957
{
@@ -82,25 +90,17 @@ irb_pred(uint32 boundary)
8290
return 0;
8391
}
8492

85-
/* Return predecessor or IRANGE_BONDARY_MASK */
93+
/* Return successor or IRANGE_BONDARY_MASK */
8694
inline static uint32
8795
irb_succ(uint32 boundary)
8896
{
8997
if (boundary >= IRANGE_BONDARY_MASK)
90-
return boundary;
98+
return IRANGE_BONDARY_MASK;
9199

92100
return boundary + 1;
93101
}
94102

95103

96-
#define lfirst_irange(lc) ( *(IndexRange *) lfirst(lc) )
97-
#define lappend_irange(list, irange) ( lappend((list), alloc_irange(irange)) )
98-
#define lcons_irange(irange, list) ( lcons(alloc_irange(irange), (list)) )
99-
#define list_make1_irange(irange) ( lcons(alloc_irange(irange), NIL) )
100-
#define llast_irange(list) ( lfirst_irange(list_tail(list)) )
101-
#define linitial_irange(list) ( lfirst_irange(list_head(list)) )
102-
103-
104104
/* Result of function irange_cmp_lossiness() */
105105
typedef enum
106106
{
@@ -109,12 +109,27 @@ typedef enum
109109
IR_B_LOSSY /* IndexRange 'b' is lossy ('a' is not) */
110110
} ir_cmp_lossiness;
111111

112+
/* Comapre lossiness factor of two IndexRanges */
113+
inline static ir_cmp_lossiness
114+
irange_cmp_lossiness(IndexRange a, IndexRange b)
115+
{
116+
if (is_irange_lossy(a) == is_irange_lossy(b))
117+
return IR_EQ_LOSSINESS;
118+
119+
if (is_irange_lossy(a))
120+
return IR_A_LOSSY;
121+
122+
if (is_irange_lossy(b))
123+
return IR_B_LOSSY;
124+
125+
return IR_EQ_LOSSINESS;
126+
}
127+
112128

113129
/* Various traits */
114130
bool iranges_intersect(IndexRange a, IndexRange b);
115131
bool iranges_adjoin(IndexRange a, IndexRange b);
116132
bool irange_eq_bounds(IndexRange a, IndexRange b);
117-
ir_cmp_lossiness irange_cmp_lossiness(IndexRange a, IndexRange b);
118133

119134
/* Basic operations on IndexRanges */
120135
IndexRange irange_union_simple(IndexRange a, IndexRange b);

tests/cmocka/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ build_extension:
2626

2727
clean:
2828
rm -f $(OBJ) $(TEST_BIN)
29+
30+
check: all
31+
./$(TEST_BIN)

0 commit comments

Comments
 (0)