Visual Computing Library
Loading...
Searching...
No Matches
const_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_MISC_ITERATORS_CONST_POINTER_ITERATOR_H
24#define VCL_MISC_ITERATORS_CONST_POINTER_ITERATOR_H
25
26#include <vclib/concepts/pointers.h>
27#include <vclib/types/const_correctness.h>
28
29#include <iterator>
30
31namespace vcl {
32
54template<typename It>
55requires (IsAnyPointer<typename std::iterator_traits<It>::value_type>)
57{
58 using Base = It;
59
60 It mIt;
61
62public:
63 using difference_type = std::iterator_traits<It>::difference_type;
64 using value_type =
66 using reference = value_type&;
67 using pointer = value_type*;
68 using iterator_category = std::iterator_traits<It>::iterator_category;
69
70 ConstPointerIterator() = default;
71
72 ConstPointerIterator(It it) : mIt(it) {}
73
74 value_type operator*() const { return *mIt; }
75
76 pointer operator->() const { return &(*mIt); }
77
78 bool operator==(const ConstPointerIterator& oi) const
79 {
80 return mIt == oi.mIt;
81 }
82
83 bool operator!=(const ConstPointerIterator& oi) const
84 {
85 return mIt != oi.mIt;
86 }
87
88 ConstPointerIterator& operator++()
89 {
90 ++mIt;
91 return *this;
92 }
93
94 ConstPointerIterator operator++(int)
95 {
97 ++mIt;
98 return tmp;
99 }
100
101 ConstPointerIterator& operator--()
102 requires (std::bidirectional_iterator<It>)
103 {
104 --mIt;
105 return *this;
106 }
107
108 ConstPointerIterator operator--(int)
109 requires (std::bidirectional_iterator<It>)
110 {
112 --mIt;
113 return tmp;
114 }
115
116 ConstPointerIterator& operator+=(difference_type n)
117 requires (std::random_access_iterator<It>)
118 {
119 mIt += n;
120 return *this;
121 }
122
123 ConstPointerIterator operator+(difference_type n) const
124 requires (std::random_access_iterator<It>)
125 {
126 return ConstPointerIterator(mIt + n);
127 }
128
129 friend ConstPointerIterator operator+(
130 difference_type n,
132 requires (std::random_access_iterator<It>)
133 {
134 return ConstPointerIterator(n + it.mIt);
135 }
136
137 ConstPointerIterator& operator-=(difference_type n)
138 requires (std::random_access_iterator<It>)
139 {
140 mIt -= n;
141 return *this;
142 }
143
144 ConstPointerIterator operator-(difference_type n) const
145 requires (std::random_access_iterator<It>)
146 {
147 return ConstPointerIterator(mIt - n);
148 }
149
150 difference_type operator-(const ConstPointerIterator& oi) const
151 requires (std::random_access_iterator<It>)
152 {
153 return mIt - oi.mIt;
154 }
155
156 value_type operator[](difference_type n) const
157 requires (std::random_access_iterator<It>)
158 {
159 return mIt[n];
160 }
161};
162
163} // namespace vcl
164
165#endif // VCL_MISC_ITERATORS_CONST_POINTER_ITERATOR_H
The ConstPointerIterator class is a utility class that wraps an iterator of a container of [shared] p...
Definition const_pointer_iterator.h:57
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43