CMS 3D CMS Logo

TRange.h
Go to the documentation of this file.
1 #ifndef TRange_H
2 #define TRange_H
3 
6 #include <iostream>
7 #include <utility>
8 #include <algorithm>
9 
10 template <class T>
11 class TRange : public std::pair<T, T> {
12 public:
13  TRange() {}
14 
15  TRange(const T& aMin, const T& aMax) : std::pair<T, T>(aMin, aMax) {}
16 
17  TRange(const std::pair<T, T>& apair) : std::pair<T, T>(apair) {}
18 
20  const T& min() const { return this->first; }
21 
23  const T& max() const { return this->second; }
24 
25  T mean() const { return (this->first + this->second) / 2.; }
26 
28  bool empty() const { return (this->second < this->first); }
29 
31  bool inside(const T& value) const {
32  if (value < this->first || this->second < value)
33  return false;
34  else
35  return true;
36  }
37 
38  /*
40  bool hasIntersection( const TRange<T> & r) const {
41  return rangesIntersect(*this,r);
42  }
43 
45  TRange<T> intersection(
46  const TRange<T> & r) const {
47  return rangeIntersection(*this,r);
48  }
49 */
50 
52  TRange<T> sum(const TRange<T>& r) const {
53  if (this->empty())
54  return r;
55  else if (r.empty())
56  return *this;
57  else
58  return TRange((min() < r.min()) ? min() : r.min(), (max() < r.max()) ? r.max() : max());
59  }
60 
61  void sort() {
62  if (empty())
63  std::swap(this->first, this->second);
64  }
65 };
66 
67 template <class T>
68 std::ostream& operator<<(std::ostream& out, const TRange<T>& r) {
69  return out << "(" << r.min() << "," << r.max() << ")";
70 }
71 #endif
bool inside(const T &value) const
check if object is inside region
Definition: TRange.h:31
const T & max() const
upper edge of range
Definition: TRange.h:23
TRange()
Definition: TRange.h:13
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
Definition: TRange.h:11
U second(std::pair< T, U > const &p)
const T & min() const
lower edge of range
Definition: TRange.h:20
bool empty() const
true for empty region. note that region [x,x] is not empty
Definition: TRange.h:28
T mean() const
Definition: TRange.h:25
void sort()
Definition: TRange.h:61
Definition: value.py:1
TRange< T > sum(const TRange< T > &r) const
sum of overlaping regions. the overlapping should be checked before.
Definition: TRange.h:52
TRange(const std::pair< T, T > &apair)
Definition: TRange.h:17
TRange(const T &aMin, const T &aMax)
Definition: TRange.h:15
long double T