Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RIndexIter.hxx
Go to the documentation of this file.
1 /// \file ROOT/RIndexIter.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_RIndexIter
17 #define ROOT7_RIndexIter
18 
19 #include <iterator>
20 
21 namespace ROOT {
22 namespace Experimental {
23 namespace Internal {
24 
25 /**
26  \class RIndexIter
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,
34  class POINTER = typename std::add_pointer<typename std::remove_reference<REFERENCE>::type>::type>
35 class RIndexIter: 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  RIndexIter(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  RIndexIter &operator++() noexcept
51  {
52  ++fIndex;
53  return *this;
54  }
55 
56  /// --i
57  RIndexIter &operator--() noexcept
58  {
59  if (fIndex != fgEndIndex)
60  --fIndex;
61  return *this;
62  }
63 
64  /// i++
65  RIndexIter operator++(int)noexcept
66  {
67  RIndexIter old(*this);
68  ++(*this);
69  return old;
70  }
71 
72  // i--
73  RIndexIter operator--(int)noexcept
74  {
75  RIndexIter old(*this);
76  --(*this);
77  return old;
78  }
79 
80  RIndexIter &operator+=(int d) noexcept
81  {
82  fIndex += d;
83  return *this;
84  }
85 
86  RIndexIter &operator-=(int d) noexcept
87  {
88  if (d > fIndex) {
89  fIndex = fgEndIndex;
90  } else {
91  fIndex -= d;
92  }
93  return *this;
94  }
95 
96  RIndexIter operator+(int d) noexcept
97  {
98  RIndexIter ret(*this);
99  ret += d;
100  return ret;
101  }
102 
103  RIndexIter operator-(int d) noexcept
104  {
105  RIndexIter 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<(RIndexIter<REFERENCE, POINTER> lhs, RIndexIter<REFERENCE, POINTER> rhs) noexcept
116 {
117  return lhs.GetIndex() < rhs.GetIndex();
118 }
119 
120 template <class REFERENCE, class POINTER>
121 bool operator>(RIndexIter<REFERENCE, POINTER> lhs, RIndexIter<REFERENCE, POINTER> rhs) noexcept
122 {
123  return lhs.GetIndex() > rhs.GetIndex();
124 }
125 
126 template <class REFERENCE, class POINTER>
127 bool operator<=(RIndexIter<REFERENCE, POINTER> lhs, RIndexIter<REFERENCE, POINTER> rhs) noexcept
128 {
129  return lhs.GetIndex() <= rhs.GetIndex();
130 }
131 
132 template <class REFERENCE, class POINTER>
133 inline bool operator>=(RIndexIter<REFERENCE, POINTER> lhs, RIndexIter<REFERENCE, POINTER> rhs) noexcept
134 {
135  return lhs.GetIndex() >= rhs.GetIndex();
136 }
137 
138 template <class REFERENCE, class POINTER>
139 inline bool operator==(RIndexIter<REFERENCE, POINTER> lhs, RIndexIter<REFERENCE, POINTER> rhs) noexcept
140 {
141  return lhs.GetIndex() == rhs.GetIndex();
142 }
143 
144 template <class REFERENCE, class POINTER>
145 inline bool operator!=(RIndexIter<REFERENCE, POINTER> lhs, RIndexIter<REFERENCE, POINTER> rhs) noexcept
146 {
147  return lhs.GetIndex() != rhs.GetIndex();
148 }
149 ///\}
150 
151 } // namespace Internal
152 } // namespace Experimental
153 } // namespace ROOT
154 
155 #endif // ROOT7_RIndexIter