Belle II Software  release-05-02-19
ArrayIterator.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Christian Pulvermacher *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <iterator>
14 
15 class TObject;
16 
17 namespace Belle2 {
25  template <class ArrayType, class ValueType> class ObjArrayIterator: public std::iterator<std::forward_iterator_tag, ValueType> {
26  public:
28  ObjArrayIterator() = default;
33  ObjArrayIterator(ArrayType& array, size_t index): m_array{array.GetObjectRef() + index} {}
34 
42  explicit ObjArrayIterator(ArrayType const* array, bool end = false)
43  {
44  if (array) {
45  m_array = array->GetObjectRef() + (end ? array->GetEntriesFast() : 0);
46  }
47  }
48 
56  explicit ObjArrayIterator(ArrayType const* const* array, bool end = false): ObjArrayIterator(array ? * array : nullptr, end) {}
60  ObjArrayIterator<ArrayType, ValueType> operator++(int) { auto old = *this; ++(*this); return old; }
62  bool operator==(const ObjArrayIterator<ArrayType, ValueType>& rhs) const { return m_array == rhs.m_array; }
64  bool operator!=(const ObjArrayIterator<ArrayType, ValueType>& rhs) const { return !(*this == rhs); }
65 
67  ValueType& operator*() const { return *static_cast<ValueType*>(*m_array); }
69  ValueType* operator->() const { return &(operator*()); }
70  private:
72  TObject** m_array{nullptr};
73  };
74 
87  template <class ArrayType, class ValueType>
88  class ArrayIterator : public std::iterator<std::forward_iterator_tag, ValueType> {
90  static ValueType& deref_if_needed(ValueType& t) { return t; }
92  static ValueType& deref_if_needed(ValueType* t) { return *t; }
93  public:
95  ArrayIterator(): m_array(NULL), m_index(-1) {}
96 
98  explicit ArrayIterator(const ArrayType* array, int index):
99  m_array(array),
100  m_index(index)
101  {}
102 
105 
108  {
110  ++(*this);
111  return old;
112  }
113 
115  bool operator==(const ArrayIterator<ArrayType, ValueType>& rhs) const { return m_index == rhs.m_index && m_array == rhs.m_array; }
117  bool operator!=(const ArrayIterator<ArrayType, ValueType>& rhs) const { return !(*this == rhs); }
118 
120  ValueType& operator*() const { return deref_if_needed((*m_array)[m_index]); }
122  ValueType* operator->() const { return &(operator*()); }
123 
124  private:
125  const ArrayType* m_array;
126  int m_index;
127  };
129 }
Belle2::ObjArrayIterator::operator==
bool operator==(const ObjArrayIterator< ArrayType, ValueType > &rhs) const
check equality.
Definition: ArrayIterator.h:62
Belle2::ObjArrayIterator::ObjArrayIterator
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:56
Belle2::ArrayIterator::deref_if_needed
static ValueType & deref_if_needed(ValueType *t)
dereference if argument is a pointer to ValueType.
Definition: ArrayIterator.h:92
Belle2::ArrayIterator::operator++
ArrayIterator< ArrayType, ValueType > & operator++()
prefix increment.
Definition: ArrayIterator.h:104
Belle2::ObjArrayIterator::operator!=
bool operator!=(const ObjArrayIterator< ArrayType, ValueType > &rhs) const
check inequality.
Definition: ArrayIterator.h:64
Belle2::ObjArrayIterator::ObjArrayIterator
ObjArrayIterator()=default
default constructor
Belle2::ArrayIterator::ArrayIterator
ArrayIterator()
Default constructor (not that you can dereference these).
Definition: ArrayIterator.h:95
Belle2::ObjArrayIterator::m_array
TObject ** m_array
pointer to the fCont member of the TObjArray
Definition: ArrayIterator.h:72
Belle2::ObjArrayIterator
Optimizes class to iterate over TObjArray and classes inheriting from it.
Definition: ArrayIterator.h:25
Belle2::ArrayIterator
Generic iterator class for arrays, allowing use of STL algorithms, range-based for etc.
Definition: ArrayIterator.h:88
Belle2::ArrayIterator::deref_if_needed
static ValueType & deref_if_needed(ValueType &t)
dereference if argument is a pointer to ValueType.
Definition: ArrayIterator.h:90
Belle2::ArrayIterator::operator->
ValueType * operator->() const
dereference.
Definition: ArrayIterator.h:122
Belle2::ObjArrayIterator::operator++
ObjArrayIterator< ArrayType, ValueType > operator++(int)
postfix increment
Definition: ArrayIterator.h:60
Belle2::ArrayIterator::ArrayIterator
ArrayIterator(const ArrayType *array, int index)
Constructor.
Definition: ArrayIterator.h:98
Belle2::ArrayIterator::operator*
ValueType & operator*() const
dereference.
Definition: ArrayIterator.h:120
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ArrayIterator::m_index
int m_index
Current index.
Definition: ArrayIterator.h:126
Belle2::ArrayIterator::m_array
const ArrayType * m_array
Array to iterate over.
Definition: ArrayIterator.h:125
Belle2::ObjArrayIterator::ObjArrayIterator
ObjArrayIterator(ArrayType const *array, bool end=false)
Convenience constructor in case of a TClonesArray pointer.
Definition: ArrayIterator.h:42
Belle2::ArrayIterator::operator!=
bool operator!=(const ArrayIterator< ArrayType, ValueType > &rhs) const
check inequality.
Definition: ArrayIterator.h:117
Belle2::ObjArrayIterator::operator->
ValueType * operator->() const
dereference.
Definition: ArrayIterator.h:69
Belle2::ObjArrayIterator::operator*
ValueType & operator*() const
dereference.
Definition: ArrayIterator.h:67
Belle2::ArrayIterator::operator==
bool operator==(const ArrayIterator< ArrayType, ValueType > &rhs) const
check equality.
Definition: ArrayIterator.h:115
Belle2::ObjArrayIterator::ObjArrayIterator
ObjArrayIterator(ArrayType &array, size_t index)
real constructor
Definition: ArrayIterator.h:33
Belle2::ArrayIterator::operator++
ArrayIterator< ArrayType, ValueType > operator++(int)
postfix increment.
Definition: ArrayIterator.h:107
Belle2::ObjArrayIterator::operator++
ObjArrayIterator< ArrayType, ValueType > & operator++()
prefix increment
Definition: ArrayIterator.h:58