Visual Computing Library
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 }
99
100 reference operator*() const { return *mIt; }
101
102 pointer operator->() const { return &*mIt; }
103
104 bool operator==(const ElementContainerIterator& oi) const
105 {
106 return mIt == oi.mIt;
107 }
108
109 bool operator!=(const ElementContainerIterator& oi) const
110 {
111 return mIt != oi.mIt;
112 }
113
114 ElementContainerIterator& operator++() { return (this->*mIncrementFun)(); }
115
116 ElementContainerIterator operator++(int)
117 {
118 return (this->*mPostIncrementFun)();
119 }
120
121 ElementContainerIterator& operator--() { return (this->*mDecrementFun)(); }
122
123 ElementContainerIterator operator--(int)
124 {
125 return (this->*mPostDecrementFun)();
126 }
127
128 ElementContainerIterator& operator+=(difference_type n)
129 {
130 return (this->*mAssignSumFun)(n);
131 }
132
133 ElementContainerIterator& operator-=(difference_type n)
134 {
135 return (this->*mAssignSumFun)(-n);
136 }
137
138 ElementContainerIterator operator+(difference_type n) const
139 {
141 return temp += n;
142 }
143
144 ElementContainerIterator operator-(difference_type n) const
145 {
147 return temp += -n;
148 }
149
150 difference_type operator-(const ElementContainerIterator& oi) const
151 {
152 return (this->*mDiffFun)(oi);
153 }
154
155 reference operator[](difference_type i) { return *(*this + i); }
156
157 bool operator<(const ElementContainerIterator& oi) const
158 {
159 return mIt < oi.mIt;
160 }
161
162 bool operator>(const ElementContainerIterator& oi) const
163 {
164 return mIt > oi.mIt;
165 }
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
177private:
183 {
184 do {
185 ++mIt;
186 } while (mIt != mVec->end() && mIt->deleted());
187 return *this;
188 }
189
195 {
197 do {
198 ++mIt;
199 } while (mIt != mVec->end() && mIt->deleted());
200 return old;
201 }
202
208 {
209 ++mIt;
210 return *this;
211 }
212
218 {
220 ++mIt;
221 return old;
222 }
223
229 {
230 do {
231 --mIt;
232 } while (mIt != mVec->begin() && mIt->deleted());
233 return *this;
234 }
235
241 {
243 do {
244 --mIt;
245 } while (mIt != mVec->begin() && mIt->deleted());
246 return old;
247 }
248
254 {
255 --mIt;
256 return *this;
257 }
258
264 {
266 --mIt;
267 return old;
268 }
269
270 ElementContainerIterator& assignSumJump(difference_type n)
271 {
272 difference_type m = n;
273
274 if (m >= 0)
275 while (m--)
276 this->operator++();
277 else
278 while (m++)
279 this->operator--();
280 return *this;
281 }
282
283 ElementContainerIterator& assignSumFast(difference_type n)
284 {
285 mIt += n;
286 return *this;
287 }
288
289 difference_type diffJump(const ElementContainerIterator& oi) const
290 {
291 ElementContainerIterator i = oi;
292 difference_type diff = 0;
293 while (i != *this) {
294 ++diff;
295 ++i;
296 }
297 return diff;
298 }
299
300 difference_type diffFast(const ElementContainerIterator& oi) const
301 {
302 return this->mIt - oi.mIt;
303 }
304};
305
306template<template<typename, typename...> typename Container, typename T>
307using ConstElementContainerIterator =
308 ElementContainerIterator<Container, T, true>;
309
310} // namespace vcl
311
312template<template<typename, typename...> typename Container, typename T, bool C>
316{
317 return it + n;
318}
319
320#endif // VCL_MESH_ITERATORS_ELEMENT_CONTAINER_ITERATOR_H
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:194
ElementContainerIterator postDecrementFast()
Post decrement function that will be called if we don't need to jump deleted elements.
Definition element_container_iterator.h:263
ElementContainerIterator & decrementJump()
Decrement function that will be called if we need to jump deleted elements.
Definition element_container_iterator.h:228
ElementContainerIterator postIncrementFast()
Post increment function that will be called if we don't need to jump deleted elements.
Definition element_container_iterator.h:217
ElementContainerIterator & incrementJump()
Increment function that will be called if we need to jump deleted elements.
Definition element_container_iterator.h:182
ElementContainerIterator postDecrementJump()
Post decrement function that will be called if we need to jump deleted elements.
Definition element_container_iterator.h:240
ElementContainerIterator & decrementFast()
Decrement function that will be called if we don't need to jump deleted elements.
Definition element_container_iterator.h:253
ElementContainerIterator & incrementFast()
Increment function that will be called if we don't need to jump deleted elements.
Definition element_container_iterator.h:207
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43