HOPS
HOPS class reference
MHO_Interval.hh
Go to the documentation of this file.
1 #ifndef MHO_Interval_HH__
2 #define MHO_Interval_HH__
3 
4 #include <algorithm>
5 #include <utility>
6 
7 #include "MHO_MathUtilities.hh"
8 
9 namespace hops
10 {
11 
23 template< typename XIntegerType = std::size_t > class MHO_Interval
24 {
25  public:
27 
28  MHO_Interval(XIntegerType lower_bound, XIntegerType upper_bound): fValid(false)
29  {
30  SetIntervalImpl(lower_bound, upper_bound);
31  };
32 
34 
35  virtual ~MHO_Interval(){};
36 
43  void SetBounds(XIntegerType lower_bound, XIntegerType upper_bound) { SetIntervalImpl(lower_bound, upper_bound); }
44 
51  void SetBounds(const std::pair< XIntegerType, XIntegerType >& lower_upper)
52  {
53  SetIntervalImpl(lower_upper.first, lower_upper.second);
54  }
55 
61  std::pair< XIntegerType, XIntegerType > GetInterval() const
62  {
63  return std::pair< XIntegerType, XIntegerType >(fLowerBound, fUpperBound);
64  }
65 
71  void SetLowerBound(XIntegerType low) { SetIntervalImpl(low, fUpperBound); }
72 
78  void SetUpperBound(XIntegerType up) { SetIntervalImpl(fLowerBound, up); }
79 
85  XIntegerType GetLowerBound() const { return fLowerBound; }
86 
92  XIntegerType GetUpperBound() const { return fUpperBound; }
93 
99  XIntegerType GetLength() const { return fUpperBound - fLowerBound; }
100 
101  //test if this object itersects with an other interval
108  bool Intersects(const MHO_Interval& other) const
109  {
110  XIntegerType result[2];
111  int numIntersections;
112  numIntersections = MHO_MathUtilities::FindIntersection< XIntegerType >(
113  fLowerBound, fUpperBound, other.GetLowerBound(), other.GetUpperBound(), result);
114  if(numIntersections != 0)
115  {
116  return true;
117  }
118  return false;
119  }
120 
121  //test if the closed interval itersects with a point
128  bool Intersects(const XIntegerType& idx) const
129  {
130  if(fLowerBound <= idx && idx <= fUpperBound)
131  {
132  return true;
133  }
134  return false;
135  }
136 
137  //returns the union of the two intervals
144  MHO_Interval Union(const MHO_Interval& other) const
145  {
146  MHO_Interval interval;
147  XIntegerType result[2];
148  int numIntersections;
149  numIntersections = MHO_MathUtilities::FindIntersection< XIntegerType >(
150  fLowerBound, fUpperBound, other.GetLowerBound(), other.GetUpperBound(), result);
151  if(numIntersections != 0)
152  {
153  //the two intervals do intersect, so the union of the
154  //new interval is just the min/max of the following
155  XIntegerType low = std::min(fLowerBound, other.GetLowerBound());
156  XIntegerType up = std::max(fUpperBound, other.GetUpperBound());
157  interval.SetInterval(low, up);
158  }
159  //they don't intersect, so just return an empty interval
160  return interval;
161  }
162 
170  {
171  MHO_Interval interval;
172  XIntegerType result[2];
173  int numIntersections;
174  numIntersections = MHO_MathUtilities::FindIntersection< XIntegerType >(
175  fLowerBound, fUpperBound, other.GetLowerBound(), other.GetUpperBound(), result);
176  if(numIntersections != 0)
177  {
178  interval.SetInterval(result[0], result[1]);
179  }
180  return interval;
181  }
182 
184  {
185  if(this != &rhs)
186  {
187  fValid = rhs.fValid;
188  fLowerBound = rhs.fLowerBound;
189  fUpperBound = rhs.fUpperBound;
190  }
191  return *this;
192  }
193 
194  protected:
201  void SetIntervalImpl(XIntegerType low, XIntegerType up)
202  {
203  if(low < up)
204  {
205  fLowerBound = low;
206  fUpperBound = up;
207  fValid = true;
208  }
209  else if(up < low)
210  {
211  fLowerBound = up;
212  fUpperBound = low;
213  fValid = true;
214  }
215  else if(low == up)
216  {
217  fLowerBound = low;
218  fUpperBound = up;
219  fValid = true;
220  }
221  }
222 
223  bool fValid; //boolean to indicate if any of the upper/lower bounds have been set
224  XIntegerType fLowerBound;
225  XIntegerType fUpperBound;
226 };
227 
228 } // namespace hops
229 
230 #endif
Class MHO_Interval.
Definition: MHO_Interval.hh:24
bool Intersects(const XIntegerType &idx) const
Checks if closed interval intersects with another interval.
Definition: MHO_Interval.hh:128
void SetBounds(XIntegerType lower_bound, XIntegerType upper_bound)
Setter for bounds.
Definition: MHO_Interval.hh:43
MHO_Interval(const MHO_Interval &copy)
Definition: MHO_Interval.hh:33
XIntegerType fUpperBound
Definition: MHO_Interval.hh:225
MHO_Interval Intersection(const MHO_Interval &other) const
Checks if this interval intersects with another and returns true if it does.
Definition: MHO_Interval.hh:169
bool fValid
Definition: MHO_Interval.hh:223
void SetLowerBound(XIntegerType low)
Setter for lower bound.
Definition: MHO_Interval.hh:71
MHO_Interval(XIntegerType lower_bound, XIntegerType upper_bound)
Definition: MHO_Interval.hh:28
MHO_Interval Union(const MHO_Interval &other) const
Calculates and returns the union interval of this interval and another.
Definition: MHO_Interval.hh:144
XIntegerType GetUpperBound() const
Getter for upper bound.
Definition: MHO_Interval.hh:92
std::pair< XIntegerType, XIntegerType > GetInterval() const
Getter for interval bounds.
Definition: MHO_Interval.hh:61
XIntegerType GetLowerBound() const
Getter for lower bound.
Definition: MHO_Interval.hh:85
MHO_Interval & operator=(const MHO_Interval &rhs)
Definition: MHO_Interval.hh:183
virtual ~MHO_Interval()
Definition: MHO_Interval.hh:35
MHO_Interval()
Definition: MHO_Interval.hh:26
void SetUpperBound(XIntegerType up)
Setter for upper bound.
Definition: MHO_Interval.hh:78
XIntegerType GetLength() const
Getter for length.
Definition: MHO_Interval.hh:99
XIntegerType fLowerBound
Definition: MHO_Interval.hh:224
void SetBounds(const std::pair< XIntegerType, XIntegerType > &lower_upper)
Setter for bounds.
Definition: MHO_Interval.hh:51
void SetIntervalImpl(XIntegerType low, XIntegerType up)
Setter for interval impl.
Definition: MHO_Interval.hh:201
bool Intersects(const MHO_Interval &other) const
Checks if this interval intersects with another.
Definition: MHO_Interval.hh:108
#define min(a, b)
Definition: max555.c:9
#define max(a, b)
Definition: max555.c:10
Definition: MHO_ChannelLabeler.hh:17