/* Get intersection of two ranges */ IndexRange irange_intersect(IndexRange a, IndexRange b) { return make_irange(Max(irange_lower(a), irange_lower(b)), Min(irange_upper(a), irange_upper(b)), irange_is_lossy(a) || irange_is_lossy(b)); }
/* Make union of two ranges. They should have the same lossiness. */ IndexRange irange_union(IndexRange a, IndexRange b) { Assert(irange_is_lossy(a) == irange_is_lossy(b)); return make_irange(Min(irange_lower(a), irange_lower(b)), Max(irange_upper(a), irange_upper(b)), irange_is_lossy(a)); }
/* Basic behavior tests */ static void test_irange_basic(void **state) { IndexRange irange; List *irange_list; /* test irb_pred() */ assert_int_equal(99, irb_pred(100)); assert_int_equal(0, irb_pred(1)); assert_int_equal(0, irb_pred(0)); /* test irb_succ() */ assert_int_equal(100, irb_succ(99)); assert_int_equal(IRANGE_BOUNDARY_MASK, irb_succ(IRANGE_BOUNDARY_MASK)); assert_int_equal(IRANGE_BOUNDARY_MASK, irb_succ(IRANGE_BOUNDARY_MASK + 1)); /* test convenience macros */ irange = make_irange(0, IRANGE_BOUNDARY_MASK, IR_LOSSY); assert_int_equal(irange_lower(irange), 0); assert_int_equal(irange_upper(irange), IRANGE_BOUNDARY_MASK); assert_true(is_irange_lossy(irange)); assert_true(is_irange_valid(irange)); /* test allocation */ irange_list = NIL; irange_list = lappend_irange(irange_list, irange); assert_memory_equal(&irange, &linitial_irange(irange_list), sizeof(IndexRange)); assert_memory_equal(&irange, &llast_irange(irange_list), sizeof(IndexRange)); }
/* Print range list in debug purposes */ static char * print_irange(List *l) { ListCell *c; StringInfoData str; initStringInfo(&str); foreach (c, l) { IndexRange ir = lfirst_irange(c); appendStringInfo(&str, "[%d,%d]%c ", irange_lower(ir), irange_upper(ir), irange_is_lossy(ir) ? 'l' : 'e'); }
/* Check if two ranges are conjuncted */ bool irange_conjuncted(IndexRange a, IndexRange b) { return (irange_lower(a) - 1 <= irange_upper(b)) && (irange_lower(b) - 1 <= irange_upper(a)); }
/* Check if two ranges are intersecting */ bool irange_intersects(IndexRange a, IndexRange b) { return (irange_lower(a) <= irange_upper(b)) && (irange_lower(b) <= irange_upper(a)); }