CVB++ 15.0
image_plane_contiguous_iterator.hpp
1#pragma once
2
3#include <iterator>
4#include <cassert>
5
6#include "size_2d.hpp"
7
8namespace Cvb
9{
10
11 CVB_BEGIN_INLINE_NS
12
13 template <class Type>
14 class ContiguousConstIterator
15 {
16 private:
17 using Allocator = std::allocator<Type>;
18 using AllocType = typename std::allocator_traits<Allocator>::template rebind_alloc<Type>;
19 using AllocTraits = std::allocator_traits<AllocType>;
20
21 public:
22 using iterator_category = std::random_access_iterator_tag;
23 using value_type = Type;
24 using pointer = typename AllocTraits::pointer;
25 using reference = const value_type &;
26 using difference_type = typename AllocTraits::difference_type;
27
28 ContiguousConstIterator() = default;
29
30 explicit ContiguousConstIterator(pointer ptr, intptr_t index) noexcept
31 : ptr_(ptr)
32 , index_(index)
33 {
34 assert(("index must not be negative", index >= 0)); // NOLINT
35 }
36
37 reference operator*() const noexcept
38 {
39 return ptr_[index_];
40 }
41
42 pointer operator->() const noexcept
43 {
44 return ptr_ + index_;
45 }
46
47 ContiguousConstIterator<Type> &operator++() noexcept
48 {
49 ++index_;
50 return *this;
51 }
52
53 ContiguousConstIterator<Type> operator++(int) noexcept // NOLINT(cert-dcl21-cpp)
54 {
55 auto tmp = *this;
56 ++*this;
57 return tmp;
58 }
59
60 ContiguousConstIterator<Type> &operator--() noexcept
61 {
62 --index_;
63 assert(("operator-- results in negative index", index_ >= 0));
64 return *this;
65 }
66
67 ContiguousConstIterator<Type> operator--(int) noexcept // NOLINT(cert-dcl21-cpp)
68 {
69 auto tmp = *this;
70 --*this;
71 return tmp;
72 }
73
74 ContiguousConstIterator<Type> &operator+=(const difference_type offset) noexcept
75 {
76 index_ += offset;
77 assert(("operator+= results in negative index", index_ >= 0));
78 return *this;
79 }
80
81 ContiguousConstIterator<Type> operator+(const difference_type offset) const noexcept
82 {
83 auto tmp = *this;
84 return tmp += offset;
85 }
86
87 ContiguousConstIterator<Type> &operator-=(const difference_type offset) noexcept
88 {
89 return *this += -offset;
90 }
91
92 ContiguousConstIterator<Type> operator-(const difference_type offset) const noexcept
93 {
94 auto tmp = *this;
95 return tmp -= offset;
96 }
97
98 difference_type operator-(const ContiguousConstIterator<Type> &rhs) const noexcept
99 {
100 assert(("operator- results in negative index", index_ - rhs.index_ >= 0));
101 return index_ - rhs.index_;
102 }
103
104 reference operator[](const difference_type offset) const noexcept
105 {
106 return *(*this + offset);
107 }
108
109 bool operator==(const ContiguousConstIterator<Type> &rhs) const noexcept
110 {
111 assert(("iter to different buffer", ptr_ == rhs.ptr_));
112 return index_ == rhs.index_;
113 }
114
115 bool operator!=(const ContiguousConstIterator<Type> &rhs) const noexcept
116 {
117 return !(*this == rhs);
118 }
119
120 bool operator<(const ContiguousConstIterator<Type> &rhs) const noexcept
121 {
122 return index_ < rhs.index_;
123 }
124
125 bool operator>(const ContiguousConstIterator<Type> &rhs) const noexcept
126 {
127 return rhs < *this;
128 }
129
130 bool operator<=(const ContiguousConstIterator<Type> &rhs) const noexcept
131 {
132 return !(rhs < *this);
133 }
134
135 bool operator>=(const ContiguousConstIterator<Type> &rhs) const noexcept
136 {
137 return !(*this < rhs);
138 }
139
140 private:
141 pointer ptr_ = nullptr;
142 intptr_t index_ = 0;
143 };
144
145 template <class Type>
146 inline ContiguousConstIterator<Type> operator+(typename ContiguousConstIterator<Type>::difference_type offset,
147 ContiguousConstIterator<Type> next) noexcept
148 {
149 return next += offset;
150 }
151
152 template <class Type>
153 class ContiguousIterator : public ContiguousConstIterator<Type>
154 {
155 private:
156 using Allocator = std::allocator<Type>;
157 using AllocType = typename std::allocator_traits<Allocator>::template rebind_alloc<Type>;
158 using AllocTraits = std::allocator_traits<AllocType>;
159
160 using Base = ContiguousConstIterator<Type>;
161
162 public:
163 using iterator_category = std::random_access_iterator_tag;
164 using value_type = Type;
165 using pointer = typename AllocTraits::pointer;
166 using reference = value_type &;
167 using difference_type = typename AllocTraits::difference_type;
168
169 using Base::Base;
170
171 reference operator*() const noexcept
172 {
173 return const_cast<reference>(Base::operator*()); // NOLINT(cppcoreguidelines-pro-type-const-cast)
174 }
175
176 pointer operator->() const noexcept
177 {
178 return const_cast<pointer>(Base::operator->()); // NOLINT(cppcoreguidelines-pro-type-const-cast)
179 }
180
181 ContiguousIterator<Type> &operator++() noexcept
182 {
183 Base::operator++();
184 return *this;
185 }
186
187 ContiguousIterator<Type> operator++(int) noexcept // NOLINT(cert-dcl21-cpp)
188 {
189 auto tmp = *this;
190 Base::operator++();
191 return tmp;
192 }
193
194 ContiguousIterator<Type> &operator--() noexcept
195 {
196 Base::operator--();
197 return *this;
198 }
199
200 ContiguousIterator<Type> operator--(int) noexcept // NOLINT(cert-dcl21-cpp)
201 {
202 auto tmp = *this;
203 Base::operator--();
204 return tmp;
205 }
206
207 ContiguousIterator<Type> &operator+=(const difference_type offset) noexcept
208 {
209 Base::operator+=(offset);
210 return *this;
211 }
212
213 ContiguousIterator<Type> operator+(const difference_type offset) const noexcept
214 {
215 auto tmp = *this;
216 return tmp += offset;
217 }
218
219 ContiguousIterator<Type> &operator-=(const difference_type offset) noexcept
220 {
221 Base::operator-=(offset);
222 return *this;
223 }
224
225 using Base::operator-;
226
227 ContiguousIterator<Type> operator-(const difference_type offset) const noexcept
228 {
229 auto tmp = *this;
230 return tmp -= offset;
231 }
232
233 reference operator[](const difference_type offset) const noexcept
234 {
235 return const_cast<reference>(Base::operator[](offset)); // NOLINT(cppcoreguidelines-pro-type-const-cast)
236 }
237 };
238
239 template <class Type>
240 inline ContiguousIterator<Type> operator+(typename ContiguousIterator<Type>::difference_type offset,
241 ContiguousIterator<Type> next) noexcept
242 {
243 return next += offset;
244 }
245
246 CVB_END_INLINE_NS
247
248} // namespace Cvb
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
AffineMatrix2D operator+(const AffineMatrix2D &lhs, const AffineMatrix2D &rhs) noexcept
Add two affine matrices.
Definition affine_matrix_2d.hpp:223
T next(T... args)