Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
SVIterator.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This file is part of the gobelijn software.
4  * Gobelijn is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or any later
7  * version. Gobelijn is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  * or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for details. You should have received
11  * a copy of the GNU General Public License along with the software. If not,
12  * see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright 2012, Jan Broeckhove, CoMP research group, Universiteit Antwerpen.
15  */
21 #include <cassert>
22 #include <cstddef>
23 #include <iterator>
24 #include <limits>
25 #include <type_traits>
26 
27 namespace UA_CoMP {
28 namespace Container {
29 
30 template <typename T, size_t N>
31 class SegmentedVector;
32 
54 template <typename T, std::size_t N, typename P = const T*, typename R = const T&, bool is_const_iterator = true>
55 class SVIterator : public std::iterator<std::random_access_iterator_tag, T, std::ptrdiff_t, P, R>
56 {
57 public:
58  // ==================================================================
59  // Member types (in addition to those introduced by the std::iterator
60  // base class (i.e. value_type, difference_type, pointer, reference,
61  // iterator_category).
62  // ==================================================================
64 
65  // ==================================================================
66  // Construction / Copy / Move / Destruction
67  // ==================================================================
69  SVIterator() : m_p(m_end), m_c(nullptr) {}
70 
72  SVIterator(const self_type& other) : m_p(other.m_p), m_c(other.m_c) {}
73 
74  // ==================================================================
75  // Bidirectional iterator methods
76  // ==================================================================
77 
79  R operator*() const
80  {
81  assert(m_c != nullptr && m_p < m_c->size());
82  size_t b = m_p / N; // index of buffer
83  size_t i = m_p % N; // index in buffer b
84  return *static_cast<T*>(static_cast<void*>(&(m_c->m_blocks[b][i])));
85  }
86 
88  P operator->() const
89  {
90  assert(m_c != nullptr && m_p < m_c->size());
91  size_t b = m_p / N; // index of buffer
92  size_t i = m_p % N; // index in buffer b
93  return static_cast<T*>(static_cast<void*>(&(m_c->m_blocks[b][i])));
94  }
95 
98  {
99  if (m_c != nullptr) { // This is a nullptr only when default constructed
100  if (m_p < m_c->m_size - 1)
101  ++m_p;
102  else
103  m_p = m_end;
104  }
105  return *this;
106  }
107 
110  {
111  self_type tmp(*this);
112  operator++();
113  return tmp;
114  }
115 
118  {
119  if (m_c != nullptr) { // This is a nullptr only when default constructed
120  if (m_p > 0 && m_p != m_end)
121  --m_p;
122  else if (m_p == m_end)
123  m_p = m_c->m_size - 1;
124  }
125  return *this;
126  }
127 
130  {
131  self_type tmp(*this);
132  operator--();
133  return tmp;
134  }
135 
137  bool operator==(const self_type& other) const { return (m_p == other.m_p) && (m_c == other.m_c); }
138 
140  bool operator!=(const self_type& other) const { return (m_p != other.m_p) || (m_c != other.m_c); }
141 
142  // ==================================================================
143  // Random-Access iterator methods
144  // ==================================================================
145 
147  R operator[](std::size_t n) const
148  {
149  assert(m_p + n != m_end);
150  size_t b = (m_p + n) / N; // index of buffer
151  size_t i = (m_p + n) % N; // index in buffer b
152  return *static_cast<T*>(static_cast<void*>(&(m_c->m_blocks[b][i])));
153  }
154 
156  self_type& operator+=(std::ptrdiff_t n)
157  {
158  m_p += n;
159  if (m_p > m_c->m_size)
160  m_p = m_end;
161  return *this;
162  }
163 
165  self_type& operator-=(std::ptrdiff_t n)
166  {
167  m_p -= n;
168  if (m_p < 0)
169  m_p = m_end;
170  return *this;
171  }
172 
174  self_type operator+(std::ptrdiff_t n) { return self_type(m_p + n, m_c); }
175 
177  self_type operator-(std::ptrdiff_t);
178 
180  long int operator-(const self_type& other) const { return m_p - other.m_p; }
181 
183  bool operator<(const self_type& other) const { return m_p < other.m_p; }
184 
186  bool operator<=(const self_type& other) const { return m_p <= other.m_p; }
187 
189  bool operator>(const self_type& other) const { return m_p > other.m_p; }
190 
192  bool operator>=(const self_type& other) const { return m_p >= other.m_p; }
193 
194 private:
195  friend class SegmentedVector<T, N>;
196 
197 private:
199  std::size_t m_p;
200 
202  constexpr static std::size_t m_end = std::numeric_limits<size_t>::max();
203 
204 private:
206  using container_pointer_type =
207  typename std::conditional<is_const_iterator, const SegmentedVector<T, N>*, SegmentedVector<T, N>*>::type;
208 
209  // Container that the iterator points into.
211 
212 private:
214  SVIterator(std::size_t p, container_pointer_type c) : m_p(p), m_c(c) {}
215 
217  bool IsDefaultContructed() { return m_c == nullptr && m_p == m_end; }
218 
220  bool IsPastTheEnd() { return m_c != nullptr && m_p == m_end; }
221 
223  bool IsDereferencable() { return m_c != nullptr && m_p < m_c->size(); }
224 };
225 } // namespace Container
226 } // namespace UA_CoMP
self_type operator-(std::ptrdiff_t)
Return iterator pointing to n-th previous element.
R operator[](std::size_t n) const
Direct access to n-th element.
Definition: SVIterator.h:147
long int operator-(const self_type &other) const
Return distance between iterators.
Definition: SVIterator.h:180
bool operator<(const self_type &other) const
Returns whether iterator is before other.
Definition: SVIterator.h:183
container_pointer_type m_c
Definition: SVIterator.h:210
self_type operator++(int)
Post-increment (returns position prior to increment)
Definition: SVIterator.h:109
SVIterator(std::size_t p, container_pointer_type c)
Private constructor, currently only container itself can create iterators.
Definition: SVIterator.h:214
bool IsDefaultContructed()
See class description.
Definition: SVIterator.h:217
self_type & operator++()
Pre-increment (returns position after increment)
Definition: SVIterator.h:97
self_type operator--(int)
Pre-increment (returns position after decrement)
Definition: SVIterator.h:129
typename std::conditional< is_const_iterator, const SegmentedVector< T, N > *, SegmentedVector< T, N > * >::type container_pointer_type
Type of pointer-to-container (i.e. its const qualification).
Definition: SVIterator.h:207
bool operator!=(const self_type &other) const
Iterator inequality.
Definition: SVIterator.h:140
bool operator>(const self_type &other) const
Returns whether iterator is after other.
Definition: SVIterator.h:189
bool IsDereferencable()
See class description.
Definition: SVIterator.h:223
bool IsPastTheEnd()
See class description.
Definition: SVIterator.h:220
T const & max(T const &a, T const &b)
Definition: max1.hpp:9
self_type & operator-=(std::ptrdiff_t n)
Set iterator to n-th previous element.
Definition: SVIterator.h:165
self_type operator+(std::ptrdiff_t n)
Return iterator pointing to n-th next element.
Definition: SVIterator.h:174
self_type & operator--()
Pre-decrement (returns position after decrement)
Definition: SVIterator.h:117
bool operator<=(const self_type &other) const
Returns whether iterator is not after other.
Definition: SVIterator.h:186
Implementation of iterator for SegmentedVector.
Definition: SVIterator.h:55
P operator->() const
Member of element access.
Definition: SVIterator.h:88
bool operator>=(const self_type &other) const
Returns whether iterator is not after other.
Definition: SVIterator.h:192
SVIterator()
Default constructor.
Definition: SVIterator.h:69
Container that stores objects "almost contiguously" and guarantees that pointers/iterators are not in...
self_type & operator+=(std::ptrdiff_t n)
Set iterator to n-th next element.
Definition: SVIterator.h:156
bool operator==(const self_type &other) const
Iterator equality.
Definition: SVIterator.h:137
SVIterator(const self_type &other)
Copy constructor.
Definition: SVIterator.h:72
R operator*() const
Element access.
Definition: SVIterator.h:79
SVIterator< T, N, P, R, is_const_iterator > self_type
Definition: SVIterator.h:63
std::size_t m_p
Current iterator position in the container.
Definition: SVIterator.h:199
static constexpr std::size_t m_end
One past the last element iterator position.
Definition: SVIterator.h:202