LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
Range.h
Go to the documentation of this file.
1 
13 #ifndef RANGE_H
14 #define RANGE_H
15 
16 #include <vector>
17 #include <iostream>
18 #include <exception>
19 #include <stdexcept>
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,
46  const T& end)
47  : _window(start,end)
48  { if(start>end) throw std::runtime_error("Inserted invalid range: end before start."); }
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
69  {return ( _window.second < rhs.Start() ); }
70  inline bool operator> (const Range& rhs) const
71  {return ( _window.first > rhs.End() ); }
72  inline bool operator==(const Range& rhs) const
73  {return ( _window.first == rhs.Start() && _window.second == rhs.End() ); }
74  inline bool operator!=(const Range& rhs) const
75  {return !( (*this) == rhs ); }
76 
77  //
78  // Ordering w/ T
79  //
80  inline bool operator< (const T& rhs) const
81  {return (_window.second < rhs); }
82  inline bool operator> (const T& rhs) const
83  {return (_window.first > rhs); }
84 
86  void Merge(const Range& a) {
87  _window.first = std::min( _window.first, a.Start() );
88  _window.second = std::max( _window.second, a.End() );
89  }
90 
91  protected:
93  std::pair<T,T> _window;
94 
95  };
96 }
97 
98 namespace std {
99  // Implement pointer comparison in case it's useful
100  template <class T>
105  class less<util::Range<T>*>
106  {
107  public:
108  bool operator()( const util::Range<T>* lhs, const util::Range<T>* rhs )
109  { return (*lhs) < (*rhs); }
110  };
111 }
112 
113 #endif
114  // end of doxygen group
void Merge(const Range &a)
Merge two util::Range into 1.
Definition: Range.h:86
Float_t s
Definition: plot.C:23
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:17
STL namespace.
Range()
Default ctor is hidden.
Definition: Range.h:41
bool operator!=(const Range &rhs) const
Definition: Range.h:74
Int_t max
Definition: plot.C:27
bool operator()(const util::Range< T > *lhs, const util::Range< T > *rhs)
Definition: Range.h:108
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:93
bool operator<(const Range &rhs) const
Definition: Range.h:68
const T & Start() const
"start" accessor
Definition: Range.h:54
bool operator==(const Range &rhs) const
Definition: Range.h:72
Int_t min
Definition: plot.C:26
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
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
bool operator>(const Range &rhs) const
Definition: Range.h:70
~Range()
Default dtor.
Definition: Range.h:51
Float_t e
Definition: plot.C:34
void Set(const T &s, const T &e)
Setter.
Definition: Range.h:58