Belle II Software  release-08-01-10
SortedRange.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #pragma once
9 
10 #include <tracking/trackFindingCDC/utilities/Range.h>
11 
12 #include <algorithm>
13 #include <iterator>
14 #include <utility>
15 
16 namespace Belle2 {
21  namespace TrackFindingCDC {
22 
24  template<class AIterator>
25  class SortedRange : public Range<AIterator> {
26 
27  private:
30 
31  public:
33  using Iterator = AIterator;
34 
36  using Reference = typename std::iterator_traits<AIterator>::reference;
37 
38  public:
40  template<class AOtherIterator>
41  explicit SortedRange(const std::pair<AOtherIterator, AOtherIterator>& itPair)
42  : Super(AIterator(itPair.first), AIterator(itPair.second))
43  {}
44 
46  template<class Ts, class ItT = GetIterator<Ts>>
47  explicit SortedRange(const Ts& ts)
48  : Super(AIterator(std::begin(ts)), AIterator(std::end(ts)))
49  {}
50 
52  using Super::Super;
53 
55  template<class T>
57  { return SortedRange<AIterator>(std::equal_range(this->begin(), this->end(), t)); }
58 
60  template<class T>
62  { return SortedRange<AIterator>(std::upper_bound(this->begin(), this->end(), t)); }
63 
65  template<class T>
67  { return SortedRange<AIterator>(std::lower_bound(this->begin(), this->end(), t)); }
68 
70  bool count(Reference t) const
71  {
72  Range<AIterator> relevant_range = this->equal_range(t);
73  return relevant_range.count(t);
74  }
75 
76  };
77  }
79 }
A pair of iterators usable with the range base for loop.
Definition: Range.h:25
Iterator begin() const
Begin of the range for range based for.
Definition: Range.h:64
typename std::iterator_traits< AIterator >::reference Reference
The type the iterator references.
Definition: Range.h:39
Iterator end() const
End of the range for range based for.
Definition: Range.h:68
bool count(Reference t)
Counts the number of equivalent items in the range.
Definition: Range.h:101
std::pair< AIterator, AIterator > Super
Type of the base class.
Definition: Range.h:29
AIterator Iterator
Iterator type of the range.
Definition: Range.h:33
A pair of iterators usable with the range base for loop.
Definition: SortedRange.h:25
SortedRange< AIterator > upper_bound(const T &t) const
Access to a upper bound to mimic the behaviour of a sorted container.
Definition: SortedRange.h:61
SortedRange(const Ts &ts)
Constructor from another range.
Definition: SortedRange.h:47
SortedRange(const std::pair< AOtherIterator, AOtherIterator > &itPair)
Constructor to adapt a pair as returned by e.g. std::equal_range.
Definition: SortedRange.h:41
SortedRange< AIterator > lowers_bound(const T &t) const
Access to a lower bound to mimic the behaviour of a sorted container.
Definition: SortedRange.h:66
bool count(Reference t) const
Counts the number of equivalent items in the range.
Definition: SortedRange.h:70
SortedRange< AIterator > equal_range(const T &t) const
Access to a sub range to mimic the behaviour of a sorted container.
Definition: SortedRange.h:56
Abstract base class for different kinds of events.