Commit 4b101bc7 authored by Brice Videau's avatar Brice Videau

Added union operation for intervals.

parent f74a8a11
......@@ -23,6 +23,11 @@ ccs_interval_intersect(ccs_interval_t *interval1,
ccs_interval_t *interval2,
ccs_interval_t *interval_res);
extern ccs_result_t
ccs_interval_union(ccs_interval_t *interval1,
ccs_interval_t *interval2,
ccs_interval_t *interval_res);
extern ccs_result_t
ccs_interval_equal(ccs_interval_t *interval1,
ccs_interval_t *interval2,
......
......@@ -88,6 +88,34 @@ ccs_interval_intersect(ccs_interval_t *interval1,
return CCS_SUCCESS;
}
ccs_result_t
ccs_interval_union(ccs_interval_t *interval1,
ccs_interval_t *interval2,
ccs_interval_t *interval_res) {
if (!interval1 || !interval2 || !interval_res)
return CCS_INVALID_VALUE;
if (interval1->type != interval2->type)
return CCS_INVALID_TYPE;
if (interval1->type == CCS_NUM_FLOAT) {
interval_res->type = CCS_NUM_FLOAT;
MERGE_MIN(interval1->lower.f, interval1->lower_included,
interval2->lower.f, interval2->lower_included,
interval_res->lower.f, interval_res->lower_included);
MERGE_MAX(interval1->upper.f, interval1->upper_included,
interval2->upper.f, interval2->upper_included,
interval_res->upper.f, interval_res->upper_included);
} else {
interval_res->type = CCS_NUM_INTEGER;
MERGE_MIN(interval1->lower.i, interval1->lower_included,
interval2->lower.i, interval2->lower_included,
interval_res->lower.i, interval_res->lower_included);
MERGE_MAX(interval1->upper.i, interval1->upper_included,
interval2->upper.i, interval2->upper_included,
interval_res->upper.i, interval_res->upper_included);
}
return CCS_SUCCESS;
}
ccs_result_t
ccs_interval_equal(ccs_interval_t *interval1,
ccs_interval_t *interval2,
......
......@@ -139,6 +139,62 @@ void test_intersect_int() {
}
void test_union_float() {
ccs_interval_t interval1, interval2, u;
ccs_bool_t empty;
ccs_result_t err;
interval1.type = CCS_NUM_FLOAT;
interval1.lower.f = -3.0;
interval1.upper.f = 5.0;
interval1.lower_included = CCS_TRUE;
interval1.upper_included = CCS_FALSE;
interval2.type = CCS_NUM_FLOAT;
interval2.lower.f = 2.0;
interval2.upper.f = 7.0;
interval2.lower_included = CCS_TRUE;
interval2.upper_included = CCS_TRUE;
err = ccs_interval_union(&interval1, &interval2, &u);
assert( err == CCS_SUCCESS );
assert( !empty );
assert( u.type == CCS_NUM_FLOAT );
assert( u.lower.f == -3.0 );
assert( u.lower_included == CCS_TRUE );
assert( u.upper.f == 7.0 );
assert( u.upper_included == CCS_TRUE );
}
void test_union_int() {
ccs_interval_t interval1, interval2, u;
ccs_bool_t empty;
ccs_result_t err;
interval1.type = CCS_NUM_INTEGER;
interval1.lower.i = -3;
interval1.upper.i = 5;
interval1.lower_included = CCS_TRUE;
interval1.upper_included = CCS_FALSE;
interval2.type = CCS_NUM_INTEGER;
interval2.lower.i = 2;
interval2.upper.i = 7;
interval2.lower_included = CCS_TRUE;
interval2.upper_included = CCS_TRUE;
err = ccs_interval_union(&interval1, &interval2, &u);
assert( err == CCS_SUCCESS );
assert( !empty );
assert( u.type == CCS_NUM_INTEGER );
assert( u.lower.i == -3 );
assert( u.lower_included == CCS_TRUE);
assert( u.upper.i == 7 );
assert( u.upper_included == CCS_TRUE);
}
void test_equal_float() {
ccs_interval_t interval1, interval2;
ccs_bool_t equal;
......@@ -249,6 +305,8 @@ int main(int argc, char *argv[]) {
test_empty_int();
test_intersect_float();
test_intersect_int();
test_union_float();
test_union_int();
test_equal_float();
test_equal_int();
test_interval_include_float();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment