Visual Computing Library
Loading...
Searching...
No Matches
index_from_pointer_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_INDEX_FROM_POINTER_ITERATOR_H
24#define VCL_MESH_ITERATORS_COMPONENTS_INDEX_FROM_POINTER_ITERATOR_H
25
26#include <vclib/types.h>
27
28#include <iterator>
29
30namespace vcl {
31
38template<typename Iterator>
40{
41 Iterator mIt;
42
43public:
45 using value_type = uint;
46 using reference = const uint&;
47 using pointer = const uint*;
48 using iterator_category = std::random_access_iterator_tag;
49
50 IndexFromPointerIterator() = default;
51
52 IndexFromPointerIterator(const Iterator& it) : mIt(it) {}
53
54 value_type operator*() const
55 {
56 auto e = *mIt;
57 if (e == nullptr) [[unlikely]]
58 return UINT_NULL;
59 else
60 return e->index();
61 }
62
63 auto operator->() const { return FakePointerWithValue(**this); }
64
65 bool operator==(const IndexFromPointerIterator& oi) const
66 {
67 return mIt == oi.mIt;
68 }
69
70 bool operator!=(const IndexFromPointerIterator& oi) const
71 {
72 return !(*this == oi);
73 }
74
75 IndexFromPointerIterator& operator++()
76 {
77 ++mIt;
78 return *this;
79 }
80
81 IndexFromPointerIterator operator++(int)
82 {
83 auto it = *this;
84 ++mIt;
85 return it;
86 }
87
88 IndexFromPointerIterator& operator--()
89 {
90 --mIt;
91 return *this;
92 }
93
94 IndexFromPointerIterator operator--(int)
95 {
96 auto it = *this;
97 --mIt;
98 return it;
99 }
100
102 {
103 mIt += n;
104 return *this;
105 }
106
108 {
109 mIt -= n;
110 return *this;
111 }
112
114 {
115 auto it = *this;
116 it += n;
117 return it;
118 }
119
121 {
122 auto it = *this;
123 it -= n;
124 return it;
125 }
126
127 difference_type operator-(const IndexFromPointerIterator& oi) const
128 {
129 return mIt - oi.mIt;
130 }
131
132 value_type operator[](difference_type i) { return *(*this + i); }
133
134 bool operator<(const IndexFromPointerIterator& oi) const
135 {
136 return mIt < oi.mIt;
137 }
138
139 bool operator>(const IndexFromPointerIterator& oi) const
140 {
141 return mIt > oi.mIt;
142 }
143
144 bool operator<=(const IndexFromPointerIterator& oi) const
145 {
146 return mIt <= oi.mIt;
147 }
148
149 bool operator>=(const IndexFromPointerIterator& oi) const
150 {
151 return mIt >= oi.mIt;
152 }
153};
154
155} // namespace vcl
156
157#endif // VCL_MESH_ITERATORS_COMPONENTS_INDEX_FROM_POINTER_ITERATOR_H
A simple utility class to represent a pointer with a value.
Definition base.h:121
This iterator is used to iterate over a container of pointers to elements and return the index of the...
Definition index_from_pointer_iterator.h:40
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