Visual Computing Library
Loading...
Searching...
No Matches
pointer_from_index_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_COMPONENTS_POINTER_FROM_INDEX_ITERATOR_H
24#define VCL_MESH_ITERATORS_COMPONENTS_POINTER_FROM_INDEX_ITERATOR_H
25
26#include <vclib/types.h>
27
28#include <iterator>
29
30namespace vcl {
31
38template<
39 typename Iterator,
40 typename ElementType,
41 typename ParentElement,
42 bool CNST = false>
44{
45 template<typename, typename, typename, bool>
46 friend class PointerFromIndexIterator;
47
48 static constexpr uint ELEM_ID = ElementType::ELEMENT_ID;
49
50 using VT = std::conditional_t<CNST, const ElementType, ElementType>;
51 using PE = std::conditional_t<CNST, const ParentElement*, ParentElement*>;
52
53 Iterator mIt;
54 PE parentElement = nullptr;
55
56public:
58 using value_type = VT*;
59 using reference = VT*;
60 using pointer = VT**;
61 using iterator_category = std::random_access_iterator_tag;
62
63 PointerFromIndexIterator() = default;
64
71 mIt(it), parentElement(pElem)
72 {
73 }
74
79 PointerFromIndexIterator(Iterator it) : mIt(it) {}
80
82
88 Iterator,
89 ElementType,
91 false>& oi) requires (CNST == true)
92 : mIt(oi.mIt), parentElement(oi.parentElement) {};
93
94 value_type operator*() const
95 {
96 uint e = *mIt;
97 if (e == UINT_NULL) [[unlikely]]
98 return nullptr;
99 else
100 return &(parentElement->parentMesh()->template element<ELEM_ID>(e));
101 }
102
103 auto operator->() const { return FakePointerWithValue(**this); }
104
105 bool operator==(const PointerFromIndexIterator& oi) const
106 {
107 return mIt == oi.mIt;
108 }
109
110 bool operator!=(const PointerFromIndexIterator& oi) const
111 {
112 return !(*this == oi);
113 }
114
115 PointerFromIndexIterator& operator++()
116 {
117 ++mIt;
118 return *this;
119 }
120
121 PointerFromIndexIterator operator++(int)
122 {
123 auto it = *this;
124 ++mIt;
125 return it;
126 }
127
128 PointerFromIndexIterator& operator--()
129 {
130 --mIt;
131 return *this;
132 }
133
134 PointerFromIndexIterator operator--(int)
135 {
136 auto it = *this;
137 --mIt;
138 return it;
139 }
140
141 PointerFromIndexIterator& operator+=(difference_type n)
142 {
143 mIt += n;
144 return *this;
145 }
146
147 PointerFromIndexIterator& operator-=(difference_type n)
148 {
149 mIt -= n;
150 return *this;
151 }
152
153 PointerFromIndexIterator operator+(difference_type n) const
154 {
155 auto it = *this;
156 it += n;
157 return it;
158 }
159
160 PointerFromIndexIterator operator-(difference_type n) const
161 {
162 auto it = *this;
163 it -= n;
164 return it;
165 }
166
167 difference_type operator-(const PointerFromIndexIterator& oi) const
168 {
169 return mIt - oi.mIt;
170 }
171
172 value_type operator[](difference_type i) { return *(*this + i); }
173
174 bool operator<(const PointerFromIndexIterator& oi) const
175 {
176 return mIt < oi.mIt;
177 }
178
179 bool operator>(const PointerFromIndexIterator& oi) const
180 {
181 return mIt > oi.mIt;
182 }
183
184 bool operator<=(const PointerFromIndexIterator& oi) const
185 {
186 return mIt <= oi.mIt;
187 }
188
189 bool operator>=(const PointerFromIndexIterator& oi) const
190 {
191 return mIt >= oi.mIt;
192 }
193};
194
195template<typename Iterator, typename ElementType, typename ParentElement>
196using ConstPointerFromIndexIterator =
197 PointerFromIndexIterator<Iterator, ElementType, ParentElement, true>;
198
199} // namespace vcl
200
201#endif // VCL_MESH_ITERATORS_COMPONENTS_POINTER_FROM_INDEX_ITERATOR_H
This iterator is used to iterate over a container of pointers to elements and return the index of the...
Definition pointer_from_index_iterator.h:44
PointerFromIndexIterator(Iterator it, PE pElem)
Constructor for the begin iterator - ParentElement required.
Definition pointer_from_index_iterator.h:70
PointerFromIndexIterator(Iterator it)
Constructor for the end iterator, ParentElement not required.
Definition pointer_from_index_iterator.h:79
PointerFromIndexIterator(const PointerFromIndexIterator< Iterator, ElementType, ParentElement, false > &oi)
Constructor from a non-const iterator.
Definition pointer_from_index_iterator.h:87
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:48