LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
UniqueRangeSet.h
Go to the documentation of this file.
1 
14 #ifndef UNIQUERANGESET_H
15 #define UNIQUERANGESET_H
16 
17 #include "Range.h"
18 #include <set>
19 
20 namespace util {
21 
35  template <class T>
36  class UniqueRangeSet : public std::set<util::Range<T>> {
37  public:
42 
44  void Merge(const UniqueRangeSet<T>& in)
45  {
46  for (auto const& r : in)
47  emplace(r._window.first, r._window.second);
48  }
49 
51  const T& Start() const
52  {
53  if (!(this->size())) throw std::runtime_error("Nothing in the set!");
54  return (*(this->begin()))._window.first;
55  }
56 
58  const T& End() const
59  {
60  if (!(this->size())) throw std::runtime_error("Nothing in the set!");
61  return (*(this->rbegin()))._window.second;
62  }
63 
69  UniqueRangeSet<T> Exclusive(const T start, const T end) const
70  {
72 
73  auto start_iter = std::lower_bound(this->begin(), this->end(), start);
74  auto end_iter = std::lower_bound(this->begin(), this->end(), end);
75 
76  // Anything to add to the head?
77  if (start < (*start_iter)._window.first) res.emplace(start, (*start_iter)._window.first);
78 
79  auto iter = start_iter;
80  T tmp_end = end;
81  while (iter != this->end()) {
82  if (iter != start_iter) res.emplace(tmp_end, (*iter)._window.first);
83  tmp_end = (*iter)._window.second;
84  if (iter == end_iter) break;
85  ++iter;
86  }
87 
88  // Anything to add to the tail?
89  if (tmp_end < end) res.emplace(tmp_end, end);
90 
91  return res;
92  }
93 
95  size_t emplace(const T& start, const T& end)
96  {
97 
98  auto res = std::set<util::Range<T>>::emplace(start, end);
99  if (res.second) return 0;
100 
101  auto& iter = res.first;
102  auto tmp_a = Range<T>(start, end);
103  size_t ctr = 0;
104  while (iter != this->end()) {
105  tmp_a.Merge((*iter));
106  this->erase(iter);
107  iter = this->find(tmp_a);
108  ++ctr;
109  }
110  this->insert(tmp_a);
111  return ctr;
112  }
113 
115  size_t insert(const Range<T>& a) { return emplace(a._window.first, a._window.second); }
116  };
117 }
118 
119 #endif
120  // end of doxygen group
size_t insert(const Range< T > &a)
Modified insert that merges overlapping range. Return = # merged range.
TRandom r
Definition: spectrum.C:23
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
UniqueRangeSet()
default ctor
void Merge(const UniqueRangeSet< T > &in)
Merge two UniqueRangeSet<T>
~UniqueRangeSet()
default dtor
const T & Start() const
Very first "start" of all contained range.
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
Class def header for a class Range.
size_t emplace(const T &start, const T &end)
Modified emplace that merges overlapping range. Return = # merged range.
UniqueRangeSet< T > Exclusive(const T start, const T end) const
std::pair< T, T > _window
Protected to avoid user&#39;s illegal modification on first/second (sorry users!)
Definition: Range.h:91
ifstream in
Definition: comparison.C:7
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
Very last "end" of all contained range.
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69