Range.h
Go to the documentation of this file.
1 /**
2  * \file Range.h
3  *
4  * \ingroup RangeTool
5  *
6  * \brief Class def header for a class Range
7  *
8  * @author kazuhiro
9  */
10 
11 /** \addtogroup RangeTool
12  @{*/
13 #ifndef RANGE_H
14 #define RANGE_H
15 
16 #include <algorithm>
17 #include <functional>
18 #include <utility>
19 
20 namespace util {
21 
22  template <class T>
24 
25  /**
26  \class Range
27  @brief represents a "Range" w/ notion of ordering.
28  A range is defined by a pair of "start" and "end" values. This is stored in std::pair \n
29  attribute util::Range::_window. This attribute is protected so that the start/end cannot \n
30  be changed w/o a check that start is always less than end. Note the specialization \n
31  requires a template class T to have less operator implemented. \n
32  */
33  template <class T>
34  class Range {
35  // Make it a friend so UniqueRangeSet can access protected guys
36  friend class UniqueRangeSet<T>;
37 
38  private:
39  /// Default ctor is hidden
40  Range(){}
41 
42  public:
43  /// Enforced ctor. start must be less than end.
44  Range(const T& start,
45  const T& end)
46  : _window(start,end)
47  { if(start>end) throw std::runtime_error("Inserted invalid range: end before start."); }
48 
49  /// Default dtor
50  ~Range(){}
51 
52  /// "start" accessor
53  const T& Start() const { return _window.first; }
54  /// "end" accessor
55  const T& End() const { return _window.second; }
56  /// Setter
57  void Set(const T& s, const T& e)
58  {
59  if(s>=e) throw std::runtime_error("Inserted invalid range: end before start.");
60  _window.first = s;
61  _window.second = e;
62  }
63 
64  //
65  // Ordering w/ another Range
66  //
67  inline bool operator< (const Range& rhs) const
68  {return ( _window.second < rhs.Start() ); }
69  inline bool operator> (const Range& rhs) const
70  {return ( _window.first > rhs.End() ); }
71  inline bool operator==(const Range& rhs) const
72  {return ( _window.first == rhs.Start() && _window.second == rhs.End() ); }
73  inline bool operator!=(const Range& rhs) const
74  {return !( (*this) == rhs ); }
75 
76  //
77  // Ordering w/ T
78  //
79  inline bool operator< (const T& rhs) const
80  {return (_window.second < rhs); }
81  inline bool operator> (const T& rhs) const
82  {return (_window.first > rhs); }
83 
84  /// Merge two util::Range into 1
85  void Merge(const Range& a) {
86  _window.first = std::min( _window.first, a.Start() );
87  _window.second = std::max( _window.second, a.End() );
88  }
89 
90  protected:
91  /// Protected to avoid user's illegal modification on first/second (sorry users!)
92  std::pair<T,T> _window;
93 
94  };
95 }
96 
97 namespace std {
98  // Implement pointer comparison in case it's useful
99  template <class T>
100  /**
101  \class less
102  Implementation of std::less for util::Range pointers
103  */
104  class less<util::Range<T>*>
105  {
106  public:
107  bool operator()( const util::Range<T>* lhs, const util::Range<T>* rhs )
108  { return (*lhs) < (*rhs); }
109  };
110 }
111 
112 #endif
113 /** @} */ // end of doxygen group
void Merge(const Range &a)
Merge two util::Range into 1.
Definition: Range.h:85
Namespace for general, non-LArSoft-specific utilities.
STL namespace.
Range()
Default ctor is hidden.
Definition: Range.h:40
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:73
const double e
bool operator()(const util::Range< T > *lhs, const util::Range< T > *rhs)
Definition: Range.h:107
const double a
Range(const T &start, const T &end)
Enforced ctor. start must be less than end.
Definition: Range.h:44
static int max(int a, int b)
std::pair< T, T > _window
Protected to avoid user&#39;s illegal modification on first/second (sorry users!)
Definition: Range.h:92
bool operator<(const Range &rhs) const
Definition: Range.h:67
const T & Start() const
"start" accessor
Definition: Range.h:53
bool operator==(const Range &rhs) const
Definition: Range.h:71
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
std::set of util::Range, which does not allow any overlap in contained element. std::set<Range> w/ mo...
Definition: Range.h:23
represents a "Range" w/ notion of ordering. A range is defined by a pair of "start" and "end" values...
Definition: Range.h:34
const T & End() const
"end" accessor
Definition: Range.h:55
bool operator>(const Range &rhs) const
Definition: Range.h:69
~Range()
Default dtor.
Definition: Range.h:50
void Set(const T &s, const T &e)
Setter.
Definition: Range.h:57
static QCString * s
Definition: config.cpp:1042