LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
UniqueRangeSet.h
Go to the documentation of this file.
1 
14 #ifndef UNIQUERANGESET_H
15 #define UNIQUERANGESET_H
16 
17 #include <set>
18 #include <iostream>
19 #include <exception>
20 #include "Range.h"
21 
22 namespace util {
23 
37  template <class T>
38  class UniqueRangeSet : public std::set<util::Range<T> > {
39  public:
44 
46  void Merge(const UniqueRangeSet<T>& in)
47  { for(auto const& r : in) emplace(r._window.first,r._window.second); }
48 
50  const T& Start() const
51  {
52  if(!(this->size())) throw std::runtime_error("Nothing in the set!");
53  return (*(this->begin()))._window.first;
54  }
55 
57  const T& End() const
58  {
59  if(!(this->size())) throw std::runtime_error("Nothing in the set!");
60  return (*(this->rbegin()))._window.second;
61  }
62 
68  UniqueRangeSet<T> Exclusive(const T start, const T end) const
69  {
71 
72  auto start_iter = std::lower_bound(this->begin(),this->end(),start);
73  auto end_iter = std::lower_bound(this->begin(),this->end(),end);
74 
75  // Anything to add to the head?
76  if(start < (*start_iter)._window.first) res.emplace(start,(*start_iter)._window.first);
77 
78  auto iter = start_iter;
79  T tmp_end=end;
80  while(iter != this->end()) {
81  if(iter != start_iter)
82  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)
90  res.emplace(tmp_end,end);
91 
92  return res;
93  }
94 
96  size_t emplace(const T& start,const T& end) {
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)
116  {return emplace(a._window.first,a._window.second);}
117 
118  };
119 }
120 
121 #endif
122  // end of doxygen group
size_t insert(const Range< T > &a)
Modified insert that merges overlapping range. Return = # merged range.
void Merge(const Range &a)
Merge two util::Range into 1.
Definition: Range.h:86
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:17
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.
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.
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
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:93
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.
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)