KrisLibrary  1.0.0
Interval.h
1 #ifndef MATH_INTERVAL_H
2 #define MATH_INTERVAL_H
3 
4 #include "math.h"
5 #include "infnan.h"
6 
7 namespace Math {
8 
9 struct Interval;
10 struct OpenInterval;
11 struct ClosedInterval;
12 
16 struct Interval
17 {
18  inline Interval();
19  inline Interval(Real a, Real b);
20  inline void set(Real a, Real b);
21  inline void setFull();
22  inline void setEmpty();
23  inline bool isFull() const;
24 
25  Real a,b;
26 };
27 
31 struct OpenInterval : public Interval
32 {
33  inline OpenInterval();
34  inline OpenInterval(Real a, Real b);
35  inline void setIntersection(const OpenInterval& i, const OpenInterval& j);
36  inline void setUnion(const OpenInterval& i, const OpenInterval& j);
37  inline void expand(Real x);
38  inline bool isEmpty() const;
39  inline bool contains(Real x) const;
40  inline bool contains(const OpenInterval&) const;
41  inline bool contains(const ClosedInterval&) const;
42  inline bool intersects(const OpenInterval&) const;
43  inline bool intersects(const ClosedInterval&) const;
44 };
45 
49 struct ClosedInterval : public Interval
50 {
51  inline ClosedInterval();
52  inline ClosedInterval(Real a, Real b);
53  inline void setIntersection(const ClosedInterval& i, const ClosedInterval& j);
54  inline void setUnion(const ClosedInterval& i, const ClosedInterval& j);
55  inline void expand(Real x);
56  inline bool isEmpty() const;
57  inline bool contains(Real x) const;
58  inline bool contains(const Interval&) const;
59  inline bool intersects(const ClosedInterval&) const;
60  inline bool intersects(const OpenInterval&) const;
61 };
62 
63 Interval::Interval()
64 {}
65 
66 Interval::Interval(Real _a, Real _b)
67 :a(_a),b(_b)
68 {}
69 
70 void Interval::set(Real _a, Real _b)
71 {
72  a = _a;
73  b = _b;
74 }
75 
76 void Interval::setFull()
77 {
78  a = -Inf;
79  b = Inf;
80 }
81 
82 void Interval::setEmpty()
83 {
84  a = Inf;
85  b = -Inf;
86 }
87 
88 bool Interval::isFull() const
89 {
90  return (IsInf(a)==-1 && IsInf(b)==1);
91 }
92 
93 
94 
95 OpenInterval::OpenInterval()
96 {}
97 
98 OpenInterval::OpenInterval(Real _a, Real _b)
99 :Interval(_a,_b)
100 {}
101 
102 void OpenInterval::setIntersection(const OpenInterval& i, const OpenInterval& j)
103 {
104  a = Max(i.a,j.a);
105  b = Min(i.b,j.b);
106 }
107 
108 void OpenInterval::setUnion(const OpenInterval& i, const OpenInterval& j)
109 {
110  a = Min(i.a,j.a);
111  b = Min(i.b,j.b);
112 }
113 
114 void OpenInterval::expand(Real x)
115 {
116  if(x < a) a = x;
117  else if (x > b) b = x;
118 }
119 
120 bool OpenInterval::isEmpty() const
121 {
122  return a >= b;
123 }
124 
125 bool OpenInterval::contains(Real x) const
126 {
127  //subtle point -- if a or b is +/- infinity, and x is +/- infinity,
128  //this should return true
129  return (x > a && x < b) || isFull();
130 }
131 
132 bool OpenInterval::contains(const OpenInterval& i) const
133 {
134  //ANSI C requires inf==inf and -inf==-inf to return true
135  return a <= i.a && i.b <= b;
136 }
137 
138 bool OpenInterval::contains(const ClosedInterval& i) const
139 {
140  if(IsInf(a) == -1) {
141  if(IsInf(b)) return true;
142  return (i.b < b);
143  }
144  else if(IsInf(b)) return (a < i.a);
145  else return (a < i.a && i.b < b);
146 }
147 
148 bool OpenInterval::intersects(const OpenInterval& i) const
149 {
150  if(IsInf(a) == -1) {
151  if(IsInf(b) == 1) return !i.isEmpty();
152  return (i.a < b);
153  }
154  else if(IsInf(b)) return (a < i.b);
155  else return (a < i.b && i.a < b);
156 }
157 
158 
159 
160 ClosedInterval::ClosedInterval()
161 {}
162 
163 ClosedInterval::ClosedInterval(Real _a, Real _b)
164 :Interval(_a,_b)
165 {}
166 
167 void ClosedInterval::setIntersection(const ClosedInterval& i, const ClosedInterval& j)
168 {
169  a = Max(i.a,j.a);
170  b = Min(i.b,j.b);
171 }
172 
173 void ClosedInterval::setUnion(const ClosedInterval& i, const ClosedInterval& j)
174 {
175  a = Min(i.a,j.a);
176  b = Min(i.b,j.b);
177 }
178 
179 void ClosedInterval::expand(Real x)
180 {
181  if(x < a) a = x;
182  else if (x > b) b = x;
183 }
184 
185 bool ClosedInterval::isEmpty() const
186 {
187  return a > b;
188 }
189 
190 bool ClosedInterval::contains(Real x) const
191 {
192  return x >= a && x <= b;
193 }
194 
195 bool ClosedInterval::contains(const Interval& i) const
196 {
197  return a <= i.a && i.b <= b;
198 }
199 
200 bool ClosedInterval::intersects(const ClosedInterval& i) const
201 {
202  return a <= i.b && i.a <= b;
203 }
204 
205 bool ClosedInterval::intersects(const OpenInterval& i) const
206 {
207  if(IsInf(a) == -1) {
208  if(IsInf(b) == 1) return !i.isEmpty();
209  return (i.a < b);
210  }
211  else if(IsInf(b) == 1) return (a < i.b);
212  else return (a < i.b && i.a < b);
213 }
214 
215 }
216 
217 #endif
Common math typedefs, constants, functions.
int IsInf(double x)
Returns +1 if x is +inf, -1 if x is -inf, and 0 otherwise.
Definition: infnan.cpp:40
Cross-platform infinity and not-a-number routines.
An open interval (a,b)
Definition: Interval.h:31
A generic interval {a,b}, where { and } can be either (,[ or ),].
Definition: Interval.h:16
A closed interval [a,b].
Definition: Interval.h:49
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12