Visual Computing Library  devel
Loading...
Searching...
No Matches
element_container_iterator.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MESH_ITERATORS_ELEMENT_CONTAINER_ITERATOR_H
24#define VCL_MESH_ITERATORS_ELEMENT_CONTAINER_ITERATOR_H
25
26#include <iterator>
27#include <type_traits>
28
29namespace vcl {
30
31template<
32 template<typename, typename...>
33 typename Container,
34 typename T,
35 bool CNST = false>
37{
38 using ContIt = std::conditional_t<
39 CNST,
40 typename Container<T>::const_iterator,
41 typename Container<T>::iterator>;
42
43public:
44 using difference_type = ptrdiff_t;
45 using value_type = T;
46 using reference = std::conditional_t<CNST, const T&, T&>;
47 using pointer = std::conditional_t<CNST, const T*, T*>;
48 using iterator_category = std::random_access_iterator_tag;
49
50private:
51 ContIt mIt; // the actual iterator
52
53 const Container<T>* mVec = nullptr; // need to check end when jump elements
54
55 /* Pointers to functions */
56
57 // pointer to mIncrementFun function
60
61 // pointer to post increment function
64
65 // pointer to decrement function
68
69 // pointer to post decrement function
72
73 // pointer to assignment sum function
75 difference_type) = &ElementContainerIterator::assignSumJump;
76
77 // pointer to diff between iterators function
78 difference_type (ElementContainerIterator::*mDiffFun)(
79 const ElementContainerIterator& oi) const =
80 &ElementContainerIterator::diffJump;
81
82public:
83 ElementContainerIterator() = default;
84
86 ContIt it,
87 const Container<T>& vec,
88 bool jumpDeleted = true) : mIt(it), mVec(&vec)
89 {
90 if (!jumpDeleted) {
95 mAssignSumFun = &ElementContainerIterator::assignSumFast;
96 mDiffFun = &ElementContainerIterator::diffFast;
97 }
98 else {
99 // if the user asked to jump the deleted elements, and the first
100 // element is deleted, we need to move forward until we find the
101 // first non-deleted element
102 while (mIt != mVec->end() && mIt->deleted()) {
103 ++mIt;
104 }
105 }
106 }
107
108 reference operator*() const { return *mIt; }
109
110 pointer operator->() const { return &*mIt; }
111
112 bool operator==(const ElementContainerIterator& oi) const
113 {
114 return mIt == oi.mIt;
115 }
116
117 bool operator!=(const ElementContainerIterator& oi) const
118 {
119 return mIt != oi.mIt;
120 }
121
122 ElementContainerIterator& operator++() { return (this->*mIncrementFun)(); }
123
124 ElementContainerIterator operator++(int)
125 {
126 return (this->*mPostIncrementFun)();
127 }
128
129 ElementContainerIterator& operator--() { return (this->*mDecrementFun)(); }
130
131 ElementContainerIterator operator--(int)
132 {
133 return (this->*mPostDecrementFun)();
134 }
135
136 ElementContainerIterator& operator+=(difference_type n)
137 {
138 return (this->*mAssignSumFun)(n);
139 }
140
141 ElementContainerIterator& operator-=(difference_type n)
142 {
143 return (this->*mAssignSumFun)(-n);
144 }
145
146 ElementContainerIterator operator+(difference_type n) const
147 {
149 return temp += n;
150 }
151
152 ElementContainerIterator operator-(difference_type n) const
153 {
155 return temp += -n;
156 }
157
158 difference_type operator-(const ElementContainerIterator& oi) const
159 {
160 return (this->*mDiffFun)(oi);
161 }
162
163 const reference operator[](difference_type i) const { return *(*this + i); }
164
165 reference operator[](difference_type i) { return *(*this + i); }
166
167 bool operator<(const ElementContainerIterator& oi) const
168 {
169 return mIt < oi.mIt;
170 }
171
172 bool operator>(const ElementContainerIterator& oi) const
173 {
174 return mIt > oi.mIt;
175 }
176
177 bool operator<=(const ElementContainerIterator& oi) const
178 {
179 return mIt <= oi.mIt;
180 }
181
182 bool operator>=(const ElementContainerIterator& oi) const
183 {
184 return mIt >= oi.mIt;
185 }
186
187private:
193 {
194 do {
195 ++mIt;
196 } while (mIt != mVec->end() && mIt->deleted());
197 return *this;
198 }
199
205 {
207 do {
208 ++mIt;
209 } while (mIt != mVec->end() && mIt->deleted());
210 return old;
211 }
212
218 {
219 ++mIt;
220 return *this;
221 }
222
228 {
230 ++mIt;
231 return old;
232 }
233
239 {
240 do {
241 --mIt;
242 } while (mIt != mVec->begin() && mIt->deleted());
243 return *this;
244 }
245
251 {
253 do {
254 --mIt;
255 } while (mIt != mVec->begin() && mIt->deleted());
256 return old;
257 }
258
264 {
265 --mIt;
266 return *this;
267 }
268
274 {
276 --mIt;
277 return old;
278 }
279
280 ElementContainerIterator& assignSumJump(difference_type n)
281 {
282 difference_type m = n;
283
284 if (m >= 0)
285 while (m--)
286 this->operator++();
287 else
288 while (m++)
289 this->operator--();
290 return *this;
291 }
292
293 ElementContainerIterator& assignSumFast(difference_type n)
294 {
295 mIt += n;
296 return *this;
297 }
298
299 difference_type diffJump(const ElementContainerIterator& oi) const
300 {
301 ElementContainerIterator i = oi;
302 difference_type diff = 0;
303 while (i != *this) {
304 ++diff;
305 ++i;
306 }
307 return diff;
308 }
309
310 difference_type diffFast(const ElementContainerIterator& oi) const
311 {
312 return this->mIt - oi.mIt;
313 }
314};
315
316template<
317 template<typename, typename...>
318 typename Container,
319 typename T,
320 bool C = false>
321ElementContainerIterator<Container, T, C> operator+(
322 typename ElementContainerIterator<Container, T, C>::difference_type n,
323 const ElementContainerIterator<Container, T, C>& it)
324{
325 return it + n;
326}
327
328template<template<typename, typename...> typename Container, typename T>
329using ConstElementContainerIterator =
330 ElementContainerIterator<Container, T, true>;
331
332} // namespace vcl
333
334#endif // VCL_MESH_ITERATORS_ELEMENT_CONTAINER_ITERATOR_H
A class representing a box in N-dimensional space.
Definition box.h:46
Definition element_container_iterator.h:37
ElementContainerIterator postIncrementJump()
Post increment function that will be called if we need to jump deleted elements.
Definition element_container_iterator.h:204
ElementContainerIterator postDecrementFast()
Post decrement function that will be called if we don't need to jump deleted elements.
Definition element_container_iterator.h:273
ElementContainerIterator & decrementJump()
Decrement function that will be called if we need to jump deleted elements.
Definition element_container_iterator.h:238
ElementContainerIterator postIncrementFast()
Post increment function that will be called if we don't need to jump deleted elements.
Definition element_container_iterator.h:227
ElementContainerIterator & incrementJump()
Increment function that will be called if we need to jump deleted elements.
Definition element_container_iterator.h:192
ElementContainerIterator postDecrementJump()
Post decrement function that will be called if we need to jump deleted elements.
Definition element_container_iterator.h:250
ElementContainerIterator & decrementFast()
Decrement function that will be called if we don't need to jump deleted elements.
Definition element_container_iterator.h:263
ElementContainerIterator & incrementFast()
Increment function that will be called if we don't need to jump deleted elements.
Definition element_container_iterator.h:217