Logo ROOT   6.12/07
Reference Guide
TIndexIter.hxx
Go to the documentation of this file.
1 /// \file ROOT/TIndexIter.h
2 /// \ingroup Base ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2016-01-19
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6 /// is welcome!
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2016, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #ifndef ROOT7_TIndexIter
17 #define ROOT7_TIndexIter
18 
19 #include <iterator>
20 
21 namespace ROOT {
22 namespace Experimental {
23 namespace Internal {
24 
25 /**
26  \class TIndexIter
27  Iterates over an index; the REFERENCE is defined by the REFERENCE template parameter.
28 
29  Derived classes are expected to implement `const REFERENCE& operator*()` and
30  `const POINTER operator->()`.
31  */
32 
33 template <class REFERENCE,
35 class TIndexIter: public std::iterator<std::random_access_iterator_tag, REFERENCE, POINTER> {
36  size_t fIndex;
37 
38 protected:
39  static constexpr size_t fgEndIndex = (size_t)-1;
40 
41 public:
42  TIndexIter(size_t idx): fIndex(idx) {}
43 
44  /// Get the current index value.
45  size_t GetIndex() const noexcept { return fIndex; }
46 
47  ///\{
48  ///\name Index modifiers
49  /// ++i
50  TIndexIter &operator++() noexcept
51  {
52  ++fIndex;
53  return *this;
54  }
55 
56  /// --i
57  TIndexIter &operator--() noexcept
58  {
59  if (fIndex != fgEndIndex)
60  --fIndex;
61  return *this;
62  }
63 
64  /// i++
65  TIndexIter operator++(int)noexcept
66  {
67  TIndexIter old(*this);
68  ++(*this);
69  return old;
70  }
71 
72  // i--
73  TIndexIter operator--(int)noexcept
74  {
75  TIndexIter old(*this);
76  --(*this);
77  return old;
78  }
79 
80  TIndexIter &operator+=(int d) noexcept
81  {
82  fIndex += d;
83  return *this;
84  }
85 
86  TIndexIter &operator-=(int d) noexcept
87  {
88  if (d > fIndex) {
89  fIndex = fgEndIndex;
90  } else {
91  fIndex -= d;
92  }
93  return *this;
94  }
95 
96  TIndexIter operator+(int d) noexcept
97  {
98  TIndexIter ret(*this);
99  ret += d;
100  return ret;
101  }
102 
103  TIndexIter operator-(int d) noexcept
104  {
105  TIndexIter ret(*this);
106  ret -= d;
107  return ret;
108  }
109  ///\}
110 };
111 
112 ///\{
113 ///\name Relational operators.
114 template <class REFERENCE, class POINTER>
115 bool operator<(TIndexIter<REFERENCE, POINTER> lhs, TIndexIter<REFERENCE, POINTER> rhs) noexcept
116 {
117  return lhs.GetIndex() < rhs.GetIndex();
118 }
119 
120 template <class REFERENCE, class POINTER>
122 {
123  return lhs.GetIndex() > rhs.GetIndex();
124 }
125 
126 template <class REFERENCE, class POINTER>
127 bool operator<=(TIndexIter<REFERENCE, POINTER> lhs, TIndexIter<REFERENCE, POINTER> rhs) noexcept
128 {
129  return lhs.GetIndex() <= rhs.GetIndex();
130 }
131 
132 template <class REFERENCE, class POINTER>
134 {
135  return lhs.GetIndex() >= rhs.GetIndex();
136 }
137 
138 template <class REFERENCE, class POINTER>
140 {
141  return lhs.GetIndex() == rhs.GetIndex();
142 }
143 
144 template <class REFERENCE, class POINTER>
146 {
147  return lhs.GetIndex() != rhs.GetIndex();
148 }
149 ///\}
150 
151 } // namespace Internal
152 } // namespace Experimental
153 } // namespace ROOT
154 
155 #endif // ROOT7_TIndexIter
Iterates over an index; the REFERENCE is defined by the REFERENCE template parameter.
Definition: TIndexIter.hxx:35
TIndexIter operator--(int) noexcept
Definition: TIndexIter.hxx:73
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
TIndexIter & operator-=(int d) noexcept
Definition: TIndexIter.hxx:86
TIndexIter operator+(int d) noexcept
Definition: TIndexIter.hxx:96
bool operator==(TIndexIter< REFERENCE, POINTER > lhs, TIndexIter< REFERENCE, POINTER > rhs) noexcept
Definition: TIndexIter.hxx:139
bool operator>(TIndexIter< REFERENCE, POINTER > lhs, TIndexIter< REFERENCE, POINTER > rhs) noexcept
Definition: TIndexIter.hxx:121
TIndexIter & operator--() noexcept
–i
Definition: TIndexIter.hxx:57
TIndexIter & operator++() noexcept
Definition: TIndexIter.hxx:50
TIndexIter & operator+=(int d) noexcept
Definition: TIndexIter.hxx:80
bool operator!=(TIndexIter< REFERENCE, POINTER > lhs, TIndexIter< REFERENCE, POINTER > rhs) noexcept
Definition: TIndexIter.hxx:145
int type
Definition: TGX11.cxx:120
bool operator>=(TIndexIter< REFERENCE, POINTER > lhs, TIndexIter< REFERENCE, POINTER > rhs) noexcept
Definition: TIndexIter.hxx:133
TIndexIter operator-(int d) noexcept
Definition: TIndexIter.hxx:103
static constexpr size_t fgEndIndex
Definition: TIndexIter.hxx:39
TIndexIter operator++(int) noexcept
i++
Definition: TIndexIter.hxx:65
size_t GetIndex() const noexcept
Get the current index value.
Definition: TIndexIter.hxx:45