Belle II Software development
ArrayIterator.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
9#pragma once
10
11#include <iterator>
12
13class TObject;
14
15namespace Belle2 {
23 template <class ArrayType, class ValueType> class ObjArrayIterator {
24
25 public:
26
28 using iterator_category = std::forward_iterator_tag;
29
31 using difference_type = std::ptrdiff_t;
32
34 using value_type = ValueType;
35
37 using pointer = ValueType*;
38
40 using reference = ValueType&;
41
43 ObjArrayIterator() = default;
48 ObjArrayIterator(ArrayType& array, size_t index): m_array{array.GetObjectRef() + index} {}
49
57 explicit ObjArrayIterator(ArrayType const* array, bool end = false)
58 {
59 if (array) {
60 m_array = array->GetObjectRef() + (end ? array->GetEntriesFast() : 0);
61 }
62 }
63
71 explicit ObjArrayIterator(ArrayType const* const* array, bool end = false): ObjArrayIterator(array ? * array : nullptr, end) {}
75 ObjArrayIterator<ArrayType, ValueType> operator++(int) { auto old = *this; ++(*this); return old; }
77 bool operator==(const ObjArrayIterator<ArrayType, ValueType>& rhs) const { return m_array == rhs.m_array; }
79 bool operator!=(const ObjArrayIterator<ArrayType, ValueType>& rhs) const { return !(*this == rhs); }
80
82 ValueType& operator*() const { return *static_cast<ValueType*>(*m_array); }
84 ValueType* operator->() const { return &(operator*()); }
85 private:
87 TObject** m_array{nullptr};
88 };
89
102 template <class ArrayType, class ValueType> class ArrayIterator {
103
104 public:
105
107 using iterator_category = std::forward_iterator_tag;
108
110 using difference_type = std::ptrdiff_t;
111
113 using value_type = ValueType;
114
116 using pointer = ValueType*;
117
119 using reference = ValueType&;
120
121 public:
122
125 {
126 }
127
129 explicit ArrayIterator(const ArrayType* array, int index):
130 m_array(array), m_index(index)
131 {
132 }
133
136 {
137 ++m_index;
138 return *this;
139 }
140
143 {
145 ++(*this);
146 return old;
147 }
148
151 {
152 return m_index == rhs.m_index && m_array == rhs.m_array;
153 }
154
157 {
158 return !(*this == rhs);
159 }
160
162 ValueType& operator*() const
163 {
164 return deref_if_needed((*m_array)[m_index]);
165 }
166
168 ValueType* operator->() const
169 {
170 return &(operator*());
171 }
172
173 private:
174
176 static ValueType& deref_if_needed(ValueType& t)
177 {
178 return t;
179 }
180
182 static ValueType& deref_if_needed(ValueType* t)
183 {
184 return *t;
185 }
186
188 const ArrayType* m_array;
189
192
193 };
195}
Generic iterator class for arrays, allowing use of STL algorithms, range-based for etc.
ValueType & operator*() const
Dereference.
ArrayIterator< ArrayType, ValueType > operator++(int)
Postfix increment.
ValueType * operator->() const
Dereference.
ArrayIterator< ArrayType, ValueType > & operator++()
Prefix increment.
ValueType value_type
Value type.
static ValueType & deref_if_needed(ValueType &t)
Dereference if argument is a pointer to ValueType.
bool operator!=(const ArrayIterator< ArrayType, ValueType > &rhs) const
Check inequality.
std::forward_iterator_tag iterator_category
Iterator category.
ArrayIterator(const ArrayType *array, int index)
Constructor.
bool operator==(const ArrayIterator< ArrayType, ValueType > &rhs) const
Check equality.
static ValueType & deref_if_needed(ValueType *t)
Dereference if argument is a pointer to ValueType.
ValueType * pointer
Pointer.
std::ptrdiff_t difference_type
Difference type.
int m_index
Current index.
ValueType & reference
Reference.
ArrayIterator()
Default constructor (not that you can dereference these).
const ArrayType * m_array
Array to iterate over.
Optimizes class to iterate over TObjArray and classes inheriting from it.
Definition: ArrayIterator.h:23
ObjArrayIterator()=default
default constructor
ValueType & operator*() const
dereference.
Definition: ArrayIterator.h:82
ValueType * operator->() const
dereference.
Definition: ArrayIterator.h:84
ValueType value_type
Value type.
Definition: ArrayIterator.h:34
bool operator!=(const ObjArrayIterator< ArrayType, ValueType > &rhs) const
check inequality.
Definition: ArrayIterator.h:79
ObjArrayIterator(ArrayType &array, size_t index)
real constructor
Definition: ArrayIterator.h:48
std::forward_iterator_tag iterator_category
Iterator category.
Definition: ArrayIterator.h:28
bool operator==(const ObjArrayIterator< ArrayType, ValueType > &rhs) const
check equality.
Definition: ArrayIterator.h:77
ObjArrayIterator< ArrayType, ValueType > operator++(int)
postfix increment
Definition: ArrayIterator.h:75
ObjArrayIterator(ArrayType const *const *array, bool end=false)
Convenience constructor because we usually have a TClonesArray** member so this takes cares of the ch...
Definition: ArrayIterator.h:71
ValueType * pointer
Pointer.
Definition: ArrayIterator.h:37
TObject ** m_array
pointer to the fCont member of the TObjArray
Definition: ArrayIterator.h:87
std::ptrdiff_t difference_type
Difference type.
Definition: ArrayIterator.h:31
ObjArrayIterator(ArrayType const *array, bool end=false)
Convenience constructor in case of a TClonesArray pointer.
Definition: ArrayIterator.h:57
ValueType & reference
Reference.
Definition: ArrayIterator.h:40
ObjArrayIterator< ArrayType, ValueType > & operator++()
prefix increment
Definition: ArrayIterator.h:73
Abstract base class for different kinds of events.