Belle II Software  release-05-02-19
Range.h
1 /**************************************************************************
2 * BASF2 (Belle Analysis Framework 2) *
3 * Copyright(C) 2015 - Belle II Collaboration *
4 * *
5 * Author: The Belle II Collaboration *
6 * Contributors: Oliver Frost *
7 * *
8 * This software is provided "as is" without any warranty. *
9 **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/utilities/GetIterator.h>
13 
14 #include <algorithm>
15 #include <iterator>
16 #include <utility>
17 
18 namespace Belle2 {
23  namespace TrackFindingCDC {
24 
26  template<class AIterator>
27  class Range : public std::pair<AIterator, AIterator> {
28 
29  private:
31  using Super = std::pair<AIterator, AIterator>;
32 
33  public:
35  using Iterator = AIterator;
36 
38  using iterator = Iterator;
39 
41  using Reference = typename std::iterator_traits<AIterator>::reference;
42 
44  using value_type = typename std::iterator_traits<AIterator>::value_type;
45 
46  public:
48  Range() = default;
49 
51  template<class AOtherIterator>
52  explicit Range(const std::pair<AOtherIterator, AOtherIterator>& itPair)
53  : Super(AIterator(itPair.first), AIterator(itPair.second))
54  {}
55 
57  template<class Ts, class ItT = GetIterator<Ts>>
58  explicit Range(const Ts& ts)
59  : Super(AIterator(std::begin(ts)), AIterator(std::end(ts)))
60  {}
61 
63  using Super::Super;
64 
66  Iterator begin() const
67  { return this->first; }
68 
70  Iterator end() const
71  { return this->second; }
72 
74  bool empty() const
75  { return begin() == end(); }
76 
78  std::size_t size() const
79  { return std::distance(begin(), end()); }
80 
82  Reference front() const
83  { return *(begin()); }
84 
86  Reference back() const
87  { return *(end() - 1); }
88 
90  Reference operator[](std::size_t i) const
91  { return *(begin() + i); }
92 
94  Reference at(std::size_t i) const
95  {
96  if (not(i < size())) {
97  throw std::out_of_range("Range : Requested index " + std::to_string(i) + " is out of bounds.");
98  }
99  return operator[](i);
100  }
101 
103  bool count(Reference t)
104  { return std::count(this->begin(), this->end(), t); }
105 
106  };
107 
109  template<class AIterator>
110  Range<AIterator> asRange(std::pair<AIterator, AIterator> const& x)
111  {
112  return Range<AIterator>(x);
113  }
114 
116  template<class AIterator>
117  Range<AIterator> asRange(AIterator const& itBegin, AIterator const& itEnd)
118  {
119  return Range<AIterator>(std::make_pair(itBegin, itEnd));
120  }
121  }
123 }
Belle2::TrackFindingCDC::Range::operator[]
Reference operator[](std::size_t i) const
Returns the object at index i.
Definition: Range.h:98
Belle2::TrackFindingCDC::Range::back
Reference back() const
Returns the derefenced iterator before end()
Definition: Range.h:94
Belle2::TrackFindingCDC::Range::Range
Range()=default
Default constructor for ROOT.
Belle2::TrackFindingCDC::Range::size
std::size_t size() const
Returns the total number of objects in this range.
Definition: Range.h:86
Belle2::TrackFindingCDC::Range::begin
Iterator begin() const
Begin of the range for range based for.
Definition: Range.h:74
Belle2::TrackFindingCDC::Range::Reference
typename std::iterator_traits< AIterator >::reference Reference
The type the iterator references.
Definition: Range.h:49
Belle2::TrackFindingCDC::Range::end
Iterator end() const
End of the range for range based for.
Definition: Range.h:78
Belle2::Iterator
map< unsigned, const TOPSampleTimes * >::const_iterator Iterator
Iteratior for m_map.
Definition: TOPCalTimebase.cc:25
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::Range::count
bool count(Reference t)
Counts the number of equivalent items in the range.
Definition: Range.h:111
Belle2::TrackFindingCDC::Range::Iterator
AIterator Iterator
Iterator type of the range.
Definition: Range.h:43
Belle2::TrackFindingCDC::Range
A pair of iterators usable with the range base for loop.
Definition: Range.h:35
Belle2::TrackFindingCDC::Range::iterator
Iterator iterator
Iterator definition for stl.
Definition: Range.h:46
Belle2::TrackFindingCDC::Range::at
Reference at(std::size_t i) const
Returns the object at index i.
Definition: Range.h:102
Belle2::TrackFindingCDC::Range::Super
std::pair< AIterator, AIterator > Super
Type of the base class.
Definition: Range.h:39
Belle2::TrackFindingCDC::Range::empty
bool empty() const
Checks if the begin equals the end iterator, hence if the range is empty.
Definition: Range.h:82
Belle2::TrackFindingCDC::Range::front
Reference front() const
Returns the derefenced iterator at begin()
Definition: Range.h:90
Belle2::TrackFindingCDC::Range::value_type
typename std::iterator_traits< AIterator >::value_type value_type
The type behind the iterator (make it possible to use the range as a "list")
Definition: Range.h:52