LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Range.h
Go to the documentation of this file.
1 
13 #ifndef RANGE_H
14 #define RANGE_H
15 
16 #include <algorithm>
17 #include <functional>
18 #include <stdexcept>
19 #include <utility>
20 
21 namespace util {
22 
23  template <class T>
25 
34  template <class T>
35  class Range {
36  // Make it a friend so UniqueRangeSet can access protected guys
37  friend class UniqueRangeSet<T>;
38 
39  private:
41  Range() {}
42 
43  public:
45  Range(const T& start, const T& end) : _window(start, end)
46  {
47  if (start > end) throw std::runtime_error("Inserted invalid range: end before start.");
48  }
49 
51  ~Range() {}
52 
54  const T& Start() const { return _window.first; }
56  const T& End() const { return _window.second; }
58  void Set(const T& s, const T& e)
59  {
60  if (s >= e) throw std::runtime_error("Inserted invalid range: end before start.");
61  _window.first = s;
62  _window.second = e;
63  }
64 
65  //
66  // Ordering w/ another Range
67  //
68  inline bool operator<(const Range& rhs) const { return (_window.second < rhs.Start()); }
69  inline bool operator>(const Range& rhs) const { return (_window.first > rhs.End()); }
70  inline bool operator==(const Range& rhs) const
71  {
72  return (_window.first == rhs.Start() && _window.second == rhs.End());
73  }
74  inline bool operator!=(const Range& rhs) const { return !((*this) == rhs); }
75 
76  //
77  // Ordering w/ T
78  //
79  inline bool operator<(const T& rhs) const { return (_window.second < rhs); }
80  inline bool operator>(const T& rhs) const { return (_window.first > rhs); }
81 
83  void Merge(const Range& a)
84  {
85  _window.first = std::min(_window.first, a.Start());
86  _window.second = std::max(_window.second, a.End());
87  }
88 
89  protected:
91  std::pair<T, T> _window;
92  };
93 }
94 
95 namespace std {
96  // Implement pointer comparison in case it's useful
97  template <class T>
102  class less<util::Range<T>*> {
103  public:
104  bool operator()(const util::Range<T>* lhs, const util::Range<T>* rhs)
105  {
106  return (*lhs) < (*rhs);
107  }
108  };
109 }
110 
111 #endif
112  // end of doxygen group
void Merge(const Range &a)
Merge two util::Range into 1.
Definition: Range.h:83
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:26
STL namespace.
Range()
Default ctor is hidden.
Definition: Range.h:41
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
bool operator!=(const Range &rhs) const
Definition: Range.h:74
bool operator()(const util::Range< T > *lhs, const util::Range< T > *rhs)
Definition: Range.h:104
Range(const T &start, const T &end)
Enforced ctor. start must be less than end.
Definition: Range.h:45
std::pair< T, T > _window
Protected to avoid user&#39;s illegal modification on first/second (sorry users!)
Definition: Range.h:91
bool operator<(const Range &rhs) const
Definition: Range.h:68
const T & Start() const
"start" accessor
Definition: Range.h:54
bool operator<(const T &rhs) const
Definition: Range.h:79
bool operator==(const Range &rhs) const
Definition: Range.h:70
bool operator>(const T &rhs) const
Definition: Range.h:80
std::set of util::Range, which does not allow any overlap in contained element. std::set<Range> w/ mo...
Definition: Range.h:24
represents a "Range" w/ notion of ordering. A range is defined by a pair of "start" and "end" values...
Definition: Range.h:35
const T & End() const
"end" accessor
Definition: Range.h:56
bool operator>(const Range &rhs) const
Definition: Range.h:69
~Range()
Default dtor.
Definition: Range.h:51
Float_t e
Definition: plot.C:35
void Set(const T &s, const T &e)
Setter.
Definition: Range.h:58