LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
util::UniqueRangeSet< T > Class Template Reference

std::set of util::Range, which does not allow any overlap in contained element. std::set<Range> w/ modified insert/emplace function. Original std::set does not allow
modification of element. I assume what we're interested in is "find if the range already \n exists, and merge if it exists". The insert function does that by recursively looking up
overlapping elements w.r.t. input argument of insert function.
More...

#include "Range.h"

Inheritance diagram for util::UniqueRangeSet< T >:

Public Member Functions

 UniqueRangeSet ()
 default ctor More...
 
 ~UniqueRangeSet ()
 default dtor More...
 
void Merge (const UniqueRangeSet< T > &in)
 Merge two UniqueRangeSet<T> More...
 
const T & Start () const
 Very first "start" of all contained range. More...
 
const T & End () const
 Very last "end" of all contained range. More...
 
UniqueRangeSet< T > Exclusive (const T start, const T end) const
 
size_t emplace (const T &start, const T &end)
 Modified emplace that merges overlapping range. Return = # merged range. More...
 
size_t insert (const Range< T > &a)
 Modified insert that merges overlapping range. Return = # merged range. More...
 

Public Attributes

keys
 STL member. More...
 

Detailed Description

template<class T>
class util::UniqueRangeSet< T >

std::set of util::Range, which does not allow any overlap in contained element. std::set<Range> w/ modified insert/emplace function. Original std::set does not allow
modification of element. I assume what we're interested in is "find if the range already \n exists, and merge if it exists". The insert function does that by recursively looking up
overlapping elements w.r.t. input argument of insert function.

One important function worth noting is util::UniqueRangeSet::Exclusive which takes two
input arguments, "start" and "end", and returns util::UniqueRangeSet of all exclusive
regions between "start" and "end". By definition, merging this return with the original
instance will result in 1 huge util::Range.

Definition at line 24 of file Range.h.

Constructor & Destructor Documentation

template<class T >
util::UniqueRangeSet< T >::UniqueRangeSet ( )
inline

default ctor

Definition at line 39 of file UniqueRangeSet.h.

39 {}
template<class T >
util::UniqueRangeSet< T >::~UniqueRangeSet ( )
inline

default dtor

Definition at line 41 of file UniqueRangeSet.h.

41 {}

Member Function Documentation

template<class T >
size_t util::UniqueRangeSet< T >::emplace ( const T &  start,
const T &  end 
)
inline

Modified emplace that merges overlapping range. Return = # merged range.

Definition at line 95 of file UniqueRangeSet.h.

References util::end(), util::UniqueRangeSet< T >::insert(), and util::Range< T >::Merge().

Referenced by util::UniqueRangeSet< T >::insert(), and util::UniqueRangeSet< T >::Merge().

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  }
size_t insert(const Range< T > &a)
Modified insert that merges overlapping range. Return = # merged range.
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
size_t emplace(const T &start, const T &end)
Modified emplace that merges overlapping range. Return = # merged range.
template<class T >
const T& util::UniqueRangeSet< T >::End ( ) const
inline

Very last "end" of all contained range.

Definition at line 58 of file UniqueRangeSet.h.

References util::size().

59  {
60  if (!(this->size())) throw std::runtime_error("Nothing in the set!");
61  return (*(this->rbegin()))._window.second;
62  }
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
template<class T >
UniqueRangeSet<T> util::UniqueRangeSet< T >::Exclusive ( const T  start,
const T  end 
) const
inline

It takes two input arguments, "start" and "end", and returns util::UniqueRangeSet
of all exclusive regions between "start" and "end". By definition, merging this
return with the original instance will result in 1 huge util::Range.

Definition at line 69 of file UniqueRangeSet.h.

References util::begin(), and util::end().

70  {
71  UniqueRangeSet<T> res;
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  }
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
template<class T >
size_t util::UniqueRangeSet< T >::insert ( const Range< T > &  a)
inline

Modified insert that merges overlapping range. Return = # merged range.

Definition at line 115 of file UniqueRangeSet.h.

References util::Range< T >::_window, and util::UniqueRangeSet< T >::emplace().

Referenced by util::UniqueRangeSet< T >::emplace().

115 { return emplace(a._window.first, a._window.second); }
size_t emplace(const T &start, const T &end)
Modified emplace that merges overlapping range. Return = # merged range.
template<class T >
void util::UniqueRangeSet< T >::Merge ( const UniqueRangeSet< T > &  in)
inline

Merge two UniqueRangeSet<T>

Definition at line 44 of file UniqueRangeSet.h.

References util::UniqueRangeSet< T >::emplace(), and r.

45  {
46  for (auto const& r : in)
47  emplace(r._window.first, r._window.second);
48  }
TRandom r
Definition: spectrum.C:23
size_t emplace(const T &start, const T &end)
Modified emplace that merges overlapping range. Return = # merged range.
ifstream in
Definition: comparison.C:7
template<class T >
const T& util::UniqueRangeSet< T >::Start ( ) const
inline

Very first "start" of all contained range.

Definition at line 51 of file UniqueRangeSet.h.

References util::begin(), and util::size().

52  {
53  if (!(this->size())) throw std::runtime_error("Nothing in the set!");
54  return (*(this->begin()))._window.first;
55  }
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69

Member Data Documentation

K std::set< K >::keys
inherited

STL member.


The documentation for this class was generated from the following files: