interval.c 3.48 KB
Newer Older
1
2
#include "cconfigspace.h"

3
ccs_result_t
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
ccs_interval_empty(ccs_interval_t *interval, ccs_bool_t *empty_ret) {
	if (!interval)
		return CCS_INVALID_VALUE;
	// Empty ranges
	// [l,u] when l> u; ]l,u] when l>=u
	// [l,u[ when l>=u; ]l,u[ when l>=u
	if (interval->type == CCS_NUM_INTEGER) {
		if ( interval->upper_included && interval->lower_included ?
		    interval->lower.i > interval->upper.i :
		    interval->lower.i >= interval->upper.i ) {
			*empty_ret = CCS_TRUE;
		} else
			*empty_ret = CCS_FALSE;
	} else {
		if ( interval->upper_included && interval->lower_included ?
		    interval->lower.f > interval->upper.f :
		    interval->lower.f >= interval->upper.f ) {
			*empty_ret = CCS_TRUE;
		} else
			*empty_ret = CCS_FALSE;
	}
	return CCS_SUCCESS;
}
#define MERGE_MAX(l1, li1, l2, li2, l, li) { \
	if (l1 > l2) { \
		l = l1; \
		li = li1; \
	} else if (l2 > l1) { \
		l = l2; \
		li = li2; \
	} else { \
		if (li1) { \
			l = l2; \
			li = li2; \
		} else { \
			l = l1; \
			li = li1; \
		} \
	} \
}

#define MERGE_MIN(l1, li1, l2, li2, l, li) { \
	if (l1 < l2) { \
		l = l1; \
		li = li1; \
	} else if (l2 < l1) { \
		l = l2; \
		li = li2; \
	} else { \
		if (li1) { \
			l = l2; \
			li = li2; \
		} else { \
			l = l1; \
			li = li1; \
		} \
	} \
}

63
ccs_result_t
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
ccs_interval_intersect(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_MAX(interval1->lower.f, interval1->lower_included,
		          interval2->lower.f, interval2->lower_included,
		          interval_res->lower.f, interval_res->lower_included);
		MERGE_MIN(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_MAX(interval1->lower.i, interval1->lower_included,
		          interval2->lower.i, interval2->lower_included,
		          interval_res->lower.i, interval_res->lower_included);
		MERGE_MIN(interval1->upper.i, interval1->upper_included,
		          interval2->upper.i, interval2->upper_included,
		          interval_res->upper.i, interval_res->upper_included);
	}
	return CCS_SUCCESS;
}

91
ccs_result_t
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
ccs_interval_equal(ccs_interval_t *interval1,
                   ccs_interval_t *interval2,
                   ccs_bool_t     *equal_res) {
	if (!interval1 || !interval2)
		return CCS_INVALID_VALUE;
	if (interval1->type != interval2->type)
		return CCS_INVALID_TYPE;
	if (interval1->type  == CCS_NUM_FLOAT) {
		*equal_res = (
		    interval1->lower.f == interval2->lower.f &&
		    interval1->upper.f == interval2->upper.f &&
		    interval1->lower_included == interval2->lower_included &&
		    interval1->upper_included == interval2->upper_included );
	} else {
		ccs_int_t l1, u1, l2, u2;
		l1 = interval1->lower_included ?
		    interval1->lower.i : interval1->lower.i + 1;
		u1 = interval1->upper_included ? 
		    interval1->upper.i : interval1->upper.i - 1;
		l2 = interval2->lower_included ?
		    interval2->lower.i : interval2->lower.i + 1;
		u2 = interval2->upper_included ?
		    interval2->upper.i : interval2->upper.i - 1;
		*equal_res = ( l1 == l2 && u1 == u2 );
	}
	return CCS_SUCCESS;
}
119
120
121

extern inline ccs_bool_t
ccs_interval_include(ccs_interval_t *interval, ccs_numeric_t value);