VPTissue Reference Manual
SVIterator.h
Go to the documentation of this file.
1 #ifndef CONTAINER_S_V_ITERATOR_H_
2 #define CONTAINER_S_V_ITERATOR_H_
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
7  * the European Commission - subsequent versions of the EUPL (the "Licence");
8  * You may not use this work except in compliance with the Licence.
9  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the Licence is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the Licence for the specific language governing
15  * permissions and limitations under the Licence.
16  */
22 #include <cassert>
23 #include <cstddef>
24 #include <iterator>
25 #include <limits>
26 #include <type_traits>
27 
28 namespace SimPT_Sim {
29 namespace Container {
30 
31 template <typename T, size_t N>
32 class SegmentedVector;
33 
55 template <
56  typename T,
57  std::size_t N,
58  typename P = const T*,
59  typename R = const T&,
60  bool is_const_iterator = true
61  >
62 class SVIterator : public std::iterator<
63  std::random_access_iterator_tag, T, std::ptrdiff_t, P, R>
64 {
65 public:
66  // ==================================================================
67  // Member types (in addition to those introduced by the std::iterator
68  // base class (i.e. value_type, difference_type, pointer, reference,
69  // iterator_category).
70  // ==================================================================
72 
73  // ==================================================================
74  // Construction / Copy / Move / Destruction
75  // ==================================================================
77  SVIterator() : m_p(m_end), m_c(nullptr) {}
78 
80  SVIterator(const self_type& other) : m_p(other.m_p), m_c(other.m_c) {}
81 
82  // ==================================================================
83  // Bidirectional iterator methods
84  // ==================================================================
85 
87  R operator*() const
88  {
89  assert(m_c != nullptr && m_p < m_c->size());
90  size_t b = m_p / N; // index of buffer
91  size_t i = m_p % N; // index in buffer b
92  return *static_cast<T*>(static_cast<void*>(&(m_c->m_blocks[b][i])));
93  }
94 
96  P operator->() const
97  {
98  assert(m_c != nullptr && m_p < m_c->size());
99  size_t b = m_p / N; // index of buffer
100  size_t i = m_p % N; // index in buffer b
101  return static_cast<T*>(static_cast<void*>(&(m_c->m_blocks[b][i])));
102  }
103 
106  {
107  if (m_c != nullptr) { // This is a nullptr only when default constructed
108  if (m_p < m_c->m_size - 1)
109  ++m_p;
110  else
111  m_p = m_end;
112  }
113  return *this;
114  }
115 
118  {
119  self_type tmp(*this);
120  operator++();
121  return tmp;
122  }
123 
126  {
127  if (m_c != nullptr) { // This is a nullptr only when default constructed
128  if (m_p > 0 && m_p != m_end)
129  --m_p;
130  else if (m_p == m_end)
131  m_p = m_c->m_size-1;
132  }
133  return *this;
134  }
135 
138  {
139  self_type tmp(*this);
140  operator--();
141  return tmp;
142  }
143 
145  bool operator==(const self_type& other) const
146  {
147  return (m_p == other.m_p) && (m_c == other.m_c);
148  }
149 
151  bool operator!=(const self_type& other) const
152  {
153  return (m_p != other.m_p) || (m_c != other.m_c);
154  }
155 
156  // ==================================================================
157  // Random-Access iterator methods
158  // ==================================================================
159 
161  R operator[](std::size_t n) const
162  {
163  assert(m_p + n != m_end);
164  size_t b = (m_p + n) / N; // index of buffer
165  size_t i = (m_p + n) % N; // index in buffer b
166  return *static_cast<T*>(static_cast<void*>(&(m_c->m_blocks[b][i])));
167  }
168 
170  self_type& operator+=(std::ptrdiff_t n)
171  {
172  m_p += n;
173  if (m_p > m_c->m_size)
174  m_p = m_end;
175  return *this;
176  }
177 
179  self_type& operator-=(std::ptrdiff_t n)
180  {
181  m_p -= n;
182  if (m_p < 0)
183  m_p = m_end;
184  return *this;
185  }
186 
188  self_type operator+(std::ptrdiff_t n)
189  {
190  return self_type(m_p + n, m_c);
191  }
192 
194  self_type operator-(std::ptrdiff_t);
195 
197  long int operator-(const self_type& other) const
198  {
199  return m_p - other.m_p;
200  }
201 
203  bool operator<(const self_type& other) const
204  {
205  return m_p < other.m_p;
206  }
207 
209  bool operator<=(const self_type& other) const
210  {
211  return m_p <= other.m_p;
212  }
213 
215  bool operator>(const self_type& other) const
216  {
217  return m_p > other.m_p;
218  }
219 
221  bool operator>=(const self_type& other) const
222  {
223  return m_p >= other.m_p;
224  }
225 
226 private:
227  friend class SegmentedVector<T, N>;
228 
229 private:
231  std::size_t m_p;
232 
234  constexpr static std::size_t m_end = std::numeric_limits<size_t>::max();
235 
236 private:
238  using container_pointer_type = typename std::conditional<
239  is_const_iterator, const SegmentedVector<T, N>*, SegmentedVector<T, N>*>::type;
240 
241  // Container that the iterator points into.
242  container_pointer_type m_c;
243 
244 private:
246  SVIterator(std::size_t p, container_pointer_type c) : m_p(p), m_c(c) {}
247 
249  bool IsDefaultContructed()
250  {
251  return m_c == nullptr && m_p == m_end;
252  }
253 
255  bool IsPastTheEnd()
256  {
257  return m_c != nullptr && m_p == m_end;
258  }
259 
261  bool IsDereferencable()
262  {
263  return m_c != nullptr && m_p < m_c->size();
264  }
265 };
266 
267 } // namespace
268 } // namespace
269 
270 #endif
SVIterator(const self_type &other)
Copy constructor.
Definition: SVIterator.h:80
SVIterator()
Default constructor.
Definition: SVIterator.h:77
bool operator<(const self_type &other) const
Returns whether iterator is before other.
Definition: SVIterator.h:203
self_type & operator-=(std::ptrdiff_t n)
Set iterator to n-th previous element.
Definition: SVIterator.h:179
self_type & operator+=(std::ptrdiff_t n)
Set iterator to n-th next element.
Definition: SVIterator.h:170
bool operator>=(const self_type &other) const
Returns whether iterator is not after other.
Definition: SVIterator.h:221
self_type operator+(std::ptrdiff_t n)
Return iterator pointing to n-th next element.
Definition: SVIterator.h:188
see the online C++11 documentation
Namespace for the core simulator.
self_type & operator++()
Pre-increment (returns position after increment)
Definition: SVIterator.h:105
bool operator==(const self_type &other) const
Iterator equality.
Definition: SVIterator.h:145
R operator[](std::size_t n) const
Direct access to n-th element.
Definition: SVIterator.h:161
self_type operator++(int)
Post-increment (returns position prior to increment)
Definition: SVIterator.h:117
self_type & operator--()
Pre-decrement (returns position after decrement)
Definition: SVIterator.h:125
self_type operator--(int)
Pre-increment (returns position after decrement)
Definition: SVIterator.h:137
long int operator-(const self_type &other) const
Return distance between iterators.
Definition: SVIterator.h:197
bool operator>(const self_type &other) const
Returns whether iterator is after other.
Definition: SVIterator.h:215
R operator*() const
Element access.
Definition: SVIterator.h:87
P operator->() const
Member of element access.
Definition: SVIterator.h:96
bool operator<=(const self_type &other) const
Returns whether iterator is not after other.
Definition: SVIterator.h:209
Implementation of iterator for SegmentedVector.
Definition: SVIterator.h:62
bool operator!=(const self_type &other) const
Iterator inequality.
Definition: SVIterator.h:151
Container that stores objects "almost contiguously" and guarantees that pointers/iterators are not in...
self_type operator-(std::ptrdiff_t)
Return iterator pointing to n-th previous element.