Stride Reference Manual  - generated for commit 9643b11
SVIterator.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #pragma once
22 
23 #include <cassert>
24 #include <cmath>
25 #include <iterator>
26 #include <type_traits>
27 
28 namespace stride {
29 namespace util {
30 
31 template <typename T, std::size_t N, bool Safe>
32 class SegmentedVector;
33 
55 template <typename T, std::size_t N, bool Safe, typename P = const T*, typename R = const T&,
56  bool is_const_iterator = true>
58 {
59 public:
60  // C++17 deprecates std::iterator so we need to define these traits ourselves.
61  using iterator_category = std::random_access_iterator_tag;
62  using value_type = T;
63  using difference_type = std::ptrdiff_t;
64  using pointer = P;
65  using reference = R;
66 
67 public:
68  // ==================================================================
69  // Member types (in addition to those introduced by the std::iterator
70  // base class (i.e. value_type, difference_type, pointer, reference,
71  // iterator_category).
72  // ==================================================================
74 
75  // ==================================================================
76  // Construction / Copy / Move / Destruction
77  // ==================================================================
79  SVIterator() : m_p(0), m_c(nullptr) {}
80 
82  SVIterator(const self_type& other) : m_p(other.m_p), m_c(other.m_c) {}
83 
84  // ==================================================================
85  // Bidirectional iterator methods
86  // ==================================================================
87 
89  R operator*() const
90  {
91  // assert(m_c != nullptr && 0 <= m_p && m_p < m_c->m_size);
92  assert(m_c != nullptr && 0 <= m_p && m_p < m_c->m_size);
93  return *static_cast<T*>(static_cast<void*>(&(m_c->m_blocks[m_p / N][m_p % N])));
94  }
95 
97  P operator->() const
98  {
99  assert(m_c != nullptr && 0 <= m_p && m_p < m_c->m_size);
100  return static_cast<T*>(static_cast<void*>(&(m_c->m_blocks[m_p / N][m_p % N])));
101  }
102 
105  {
106  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
107  // defensive: m_p = std::max(++m_p, mc->m_size);
108  ++m_p;
109  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
110  return *this;
111  }
112 
115  {
116  self_type tmp(*this);
117  operator++();
118  return tmp;
119  }
120 
123  {
124  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
125  // defensive: m_p = std::max(--m_p, 0);
126  --m_p;
127  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
128  return *this;
129  }
130 
133  {
134  self_type tmp(*this);
135  operator--();
136  return tmp;
137  }
138 
140  bool operator==(const self_type& other) const { return (m_p == other.m_p) && (m_c == other.m_c); }
141 
143  bool operator!=(const self_type& other) const { return (m_p != other.m_p) || (m_c != other.m_c); }
144 
145  // ==================================================================
146  // Random-Access iterator methods
147  // ==================================================================
148 
150  R operator[](std::size_t n) const
151  {
152  assert(m_c != nullptr && 0 <= m_p + n && m_p + n < m_c->m_size);
153  return *static_cast<T*>(static_cast<void*>(&(m_c->m_blocks[(m_p + n) / N][(m_p + n) % N])));
154  }
155 
157  self_type& operator+=(std::ptrdiff_t n)
158  {
159  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
160  // defensive: m_p = std::max((m_p+=n), mc->m_size);
161  m_p += n;
162  return *this;
163  }
164 
166  self_type& operator-=(std::ptrdiff_t n)
167  {
168  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
169  // defensive: m_p = std::max((m_p -= n), 0);
170  m_p -= n;
171  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
172  return *this;
173  }
174 
176  self_type operator+(std::ptrdiff_t n)
177  {
178  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
179  assert(m_c != nullptr && 0 <= m_p + n && m_p + n <= m_c->m_size);
180  return self_type(m_p + n, m_c);
181  }
182 
184  self_type operator-(std::ptrdiff_t n)
185  {
186  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
187  assert(m_c != nullptr && 0 <= m_p - n && m_p - n <= m_c->m_size);
188  return self_type(m_p - n, m_c);
189  };
190 
192  long int operator-(const self_type& other) const
193  {
194  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
195  assert(other.m_c != nullptr && 0 <= other.m_p && other.m_p <= other.m_c->m_size);
196  return m_p - other.m_p;
197  }
198 
200  bool operator<(const self_type& other) const
201  {
202  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
203  assert(other.m_c != nullptr && 0 <= other.m_p && other.m_p <= other.m_c->m_size);
204  return m_p < other.m_p;
205  }
206 
208  bool operator<=(const self_type& other) const
209  {
210  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
211  assert(other.m_c != nullptr && 0 <= other.m_p && other.m_p <= other.m_c->m_size);
212  return m_p <= other.m_p;
213  }
214 
216  bool operator>(const self_type& other) const
217  {
218  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
219  assert(other.m_c != nullptr && 0 <= other.m_p && other.m_p <= other.m_c->m_size);
220  return m_p > other.m_p;
221  }
222 
224  bool operator>=(const self_type& other) const
225  {
226  assert(m_c != nullptr && 0 <= m_p && m_p <= m_c->m_size);
227  assert(other.m_c != nullptr && 0 <= other.m_p && other.m_p <= other.m_c->m_size);
228  return m_p >= other.m_p;
229  }
230 
231 private:
232  friend class SegmentedVector<T, N, Safe>;
233 
234 private:
236  using container_pointer_type = typename std::conditional<is_const_iterator, const SegmentedVector<T, N, Safe>*,
238 
239 private:
241  SVIterator(std::size_t p, container_pointer_type c) : m_p(p), m_c(c) {}
242 
243 private:
244  std::size_t m_p;
246 };
247 
248 //------------------------------------
249 // Helpers
250 //------------------------------------
251 template <typename T, std::size_t N, bool Safe, typename P = const T*, typename R = const T&,
252  bool is_const_iterator = true>
255 {
256  return p.operator+(i);
257 }
258 
259 template <typename T, std::size_t N, bool Safe, typename P = const T*, typename R = const T&,
260  bool is_const_iterator = true>
263 {
264  return p.operator-(i);
265 }
266 
267 } // namespace util
268 } // namespace stride
bool operator<=(const self_type &other) const
Returns whether iterator is not after other.
Definition: SVIterator.h:208
SVIterator(const self_type &other)
Copy constructor.
Definition: SVIterator.h:82
self_type & operator+=(std::ptrdiff_t n)
Set iterator to n-th next element.
Definition: SVIterator.h:157
std::random_access_iterator_tag iterator_category
Definition: SVIterator.h:61
self_type operator-(std::ptrdiff_t n)
Return iterator pointing to n-th previous element.
Definition: SVIterator.h:184
SVIterator(std::size_t p, container_pointer_type c)
Private constructor, currently only container itself can create iterators.
Definition: SVIterator.h:241
bool operator>=(const self_type &other) const
Returns whether iterator is not after other.
Definition: SVIterator.h:224
bool operator<(const self_type &other) const
Returns whether iterator is before other.
Definition: SVIterator.h:200
R operator[](std::size_t n) const
Direct access to n-th element.
Definition: SVIterator.h:150
self_type & operator-=(std::ptrdiff_t n)
Set iterator to n-th previous element.
Definition: SVIterator.h:166
typename std::conditional< is_const_iterator, const SegmentedVector< T, N, Safe > *, SegmentedVector< T, N, Safe > * >::type container_pointer_type
Type of pointer-to-container (i.e. its const qualification).
Definition: SVIterator.h:237
P operator->() const
Member of element access.
Definition: SVIterator.h:97
long int operator-(const self_type &other) const
Return distance between iterators.
Definition: SVIterator.h:192
const self_type operator++(int)
Post-increment (returns position prior to increment)
Definition: SVIterator.h:114
std::size_t m_p
Current iterator position in the container.
Definition: SVIterator.h:244
SVIterator< T, N, Safe, P, R, is_const_iterator > self_type
Definition: SVIterator.h:73
Container that stores objects "almost contiguously" (in a chain of blocks) and guarantees that pointe...
bool operator==(const self_type &other) const
Iterator equality.
Definition: SVIterator.h:140
self_type operator+(std::ptrdiff_t n)
Return iterator pointing to n-th next element.
Definition: SVIterator.h:176
std::ptrdiff_t difference_type
Definition: SVIterator.h:63
self_type & operator--()
Pre-decrement (returns position after decrement)
Definition: SVIterator.h:122
const self_type operator--(int)
Pre-increment (returns position after decrement)
Definition: SVIterator.h:132
self_type & operator++()
Pre-increment (returns position after increment)
Definition: SVIterator.h:104
bool operator!=(const self_type &other) const
Iterator inequality.
Definition: SVIterator.h:143
Implementation of iterator for SegmentedVector.
Definition: SVIterator.h:57
bool operator>(const self_type &other) const
Returns whether iterator is after other.
Definition: SVIterator.h:216
container_pointer_type m_c
Container that the iterator points into.
Definition: SVIterator.h:245
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
R operator*() const
Element access.
Definition: SVIterator.h:89
SVIterator()
Default constructor.
Definition: SVIterator.h:79