Belle II Software  release-08-01-10
Range.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/GetIterator.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 Range : public std::pair<AIterator, AIterator> {
26 
27  private:
29  using Super = std::pair<AIterator, AIterator>;
30 
31  public:
33  using Iterator = AIterator;
34 
36  using iterator = Iterator;
37 
39  using Reference = typename std::iterator_traits<AIterator>::reference;
40 
42  using value_type = typename std::iterator_traits<AIterator>::value_type;
43 
44  public:
46  Range() = default;
47 
49  template<class AOtherIterator>
50  explicit Range(const std::pair<AOtherIterator, AOtherIterator>& itPair)
51  : Super(AIterator(itPair.first), AIterator(itPair.second))
52  {}
53 
55  template<class Ts, class ItT = GetIterator<Ts>>
56  explicit Range(const Ts& ts)
57  : Super(AIterator(std::begin(ts)), AIterator(std::end(ts)))
58  {}
59 
61  using Super::Super;
62 
64  Iterator begin() const
65  { return this->first; }
66 
68  Iterator end() const
69  { return this->second; }
70 
72  bool empty() const
73  { return begin() == end(); }
74 
76  std::size_t size() const
77  { return std::distance(begin(), end()); }
78 
80  Reference front() const
81  { return *(begin()); }
82 
84  Reference back() const
85  { return *(end() - 1); }
86 
88  Reference operator[](std::size_t i) const
89  { return *(begin() + i); }
90 
92  Reference at(std::size_t i) const
93  {
94  if (not(i < size())) {
95  throw std::out_of_range("Range : Requested index " + std::to_string(i) + " is out of bounds.");
96  }
97  return operator[](i);
98  }
99 
101  bool count(Reference t)
102  { return std::count(this->begin(), this->end(), t); }
103 
104  };
105 
107  template<class AIterator>
108  Range<AIterator> asRange(std::pair<AIterator, AIterator> const& x)
109  {
110  return Range<AIterator>(x);
111  }
112 
114  template<class AIterator>
115  Range<AIterator> asRange(AIterator const& itBegin, AIterator const& itEnd)
116  {
117  return Range<AIterator>(std::make_pair(itBegin, itEnd));
118  }
119  }
121 }
Represents a range of arithmetic types.
Definition: Range.h:29
A pair of iterators usable with the range base for loop.
Definition: Range.h:25
Iterator iterator
Iterator definition for stl.
Definition: Range.h:36
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
Range(const std::pair< AOtherIterator, AOtherIterator > &itPair)
Constructor to adapt a pair as returned by e.g. std::equal_range.
Definition: Range.h:50
Range()=default
Default constructor for ROOT.
Iterator end() const
End of the range for range based for.
Definition: Range.h:68
bool empty() const
Checks if the begin equals the end iterator, hence if the range is empty.
Definition: Range.h:72
Reference back() const
Returns the derefenced iterator before end()
Definition: Range.h:84
Reference operator[](std::size_t i) const
Returns the object at index i.
Definition: Range.h:88
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
Reference front() const
Returns the derefenced iterator at begin()
Definition: Range.h:80
AIterator Iterator
Iterator type of the range.
Definition: Range.h:33
std::size_t size() const
Returns the total number of objects in this range.
Definition: Range.h:76
Reference at(std::size_t i) const
Returns the object at index i.
Definition: Range.h:92
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:42
Range(const Ts &ts)
Constructor from another range.
Definition: Range.h:56
Abstract base class for different kinds of events.