API Documentation
Loading...
Searching...
No Matches
BitIterator.hpp
Go to the documentation of this file.
1/**--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: BitIterator
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/BitFlag.h>
35#include <NDEVR/BitReference.h>
36#include <iterator>
37namespace NDEVR
38{
40 {
41 public:
42 BitIterator(BitFlag* flag, const sint04 offset)
43 : m_flag(flag)
44 , m_bit(offset)
45 {
46 checkBitLocationPositive();
47 }
48 BitIterator(const BitIterator& iter) = default;
49
50 bool operator!() const
51 {
52 return !(*m_flag)[m_bit_ref];
53 }
54 constexpr operator bool() const
55 {
56 return (*m_flag)[m_bit_ref];
57 }
59 {
60 return BitReference(*m_flag, m_bit_ref);
61 }
62 const BitReference operator*() const
63 {
64 return BitReference(*m_flag, m_bit_ref);
65 }
66
68 {
69 m_bit++;
70 checkBitLocationPositive();
71 return *this;
72 }
74 {
75 m_bit--;
76 checkBitLocationNegative();
77 return *this;
78 }
80 {
81 BitIterator old_value(*this);
82 m_bit++;
83 checkBitLocationPositive();
84 return old_value;
85 }
87 {
88 BitIterator old_value(*this);
89 m_bit--;
90 checkBitLocationNegative();
91 return old_value;
92 }
93 template<class t_type>
94 BitIterator& operator+=(const t_type& movement)
95 {
96 m_bit += cast<sint04>(movement);
97 checkBitLocationPositive();
98 return (*this);
99 }
100 template<class t_type>
101 BitIterator& operator-=(const t_type& movement)
102 {
103 m_bit -= cast<sint04>(movement);
104 checkBitLocationNegative();
105 return (*this);
106 }
107 bool operator>(const BitIterator& other) const
108 {
109 if (m_flag == other.m_flag)
110 return m_bit > other.m_bit;
111 else
112 return m_flag > other.m_flag;
113 }
114 bool operator<(const BitIterator& other) const
115 {
116 if (m_flag == other.m_flag)
117 return m_bit < other.m_bit;
118 else
119 return m_flag < other.m_flag;
120 }
121 ptrdiff_t operator-(const BitIterator& rawIterator)
122 {
123 return std::distance(rawIterator.m_flag, this->m_flag) + cast<ptrdiff_t>(rawIterator.m_bit - this->m_bit);
124 }
125 template<class t_type>
127 {
128 BitIterator sum = *this;
129 sum.m_bit += cast<sint04>(distance);
130 sum.checkBitLocationPositive();
131 return sum;
132 }
133 template<class t_type>
135 {
136 BitIterator sum = *this;
137 sum.m_bit -= cast<sint04>(distance);
138 sum.checkBitLocationNegative();
139 return sum;
140 }
141 public:
142 BitIterator& operator=(const BitIterator& rawIterator) = default;
143
144 bool operator==(const BitIterator& rawIterator) const { return (m_bit == rawIterator.m_bit && m_flag == rawIterator.m_flag); }
145 bool operator!=(const BitIterator& rawIterator) const { return (m_bit != rawIterator.m_bit || m_flag != rawIterator.m_flag); }
146
147 private:
148 void checkBitLocationPositive() const
149 {
150 if (m_bit >= 8)
151 {
152 m_flag += (m_bit / 8);
153 m_bit = m_bit % 8;
154 }
155 m_bit_ref = cast<uint01>(m_bit);
156 }
157 void checkBitLocationNegative() const
158 {
159 if (m_bit <= 0)
160 {
161 m_bit = -m_bit + 8;
162 m_flag -= (m_bit / 8);
163 m_bit = m_bit % 8;
164 }
165 m_bit_ref = cast<uint01>(m_bit);
166 }
167 mutable BitFlag* m_flag;
168 mutable sint04 m_bit;
169 mutable uint01 m_bit_ref = Constant<uint01>::NaN;
170 };
171
173 {
174 public:
175 ConstBitIterator(const BitFlag* flag, sint04 offset)
176 : m_flag(flag)
177 , m_bit(offset)
178 {
179 checkBitLocationPositive();
180 }
181 ConstBitIterator(const BitFlag* flag, uint04 offset)
182 : m_flag(flag)
183 , m_bit(cast<sint04>(offset))
184 {
185 checkBitLocationPositive();
186 }
187 ConstBitIterator(const ConstBitIterator& iter) = default;
188
190 {
191 return (*m_flag)[m_bit_ref];
192 }
193 bool operator*() const
194 {
195 return (*m_flag)[m_bit_ref];
196 }
197 bool operator==(bool ref) const
198 {
199 return (*m_flag)[m_bit_ref] == ref;
200 }
201 bool operator!() const
202 {
203 return !(*m_flag)[m_bit_ref];
204 }
205 constexpr operator bool() const
206 {
207 return (*m_flag)[m_bit_ref];
208 }
210 {
211 m_bit++;
212 checkBitLocationPositive();
213 return *this;
214 }
216 {
217 m_bit--;
218 checkBitLocationNegative();
219 return *this;
220 }
222 {
223 ConstBitIterator old_value(*this);
224 m_bit++;
225 checkBitLocationPositive();
226 return old_value;
227 }
229 {
230 ConstBitIterator old_value(*this);
231 m_bit--;
232 checkBitLocationNegative();
233 return old_value;
234 }
235 template<class t_type>
236 ConstBitIterator& operator+=(const t_type& movement)
237 {
238 m_bit += cast<sint04>(movement);
239 checkBitLocationPositive();
240 return (*this);
241 }
242 template<class t_type>
243 ConstBitIterator& operator-=(const t_type& movement)
244 {
245 m_bit -= cast<sint04>(movement);
246 checkBitLocationNegative();
247 return (*this);
248 }
249
250 ptrdiff_t operator-(const ConstBitIterator& rawIterator)
251 {
252 return std::distance(rawIterator.m_flag, this->m_flag) + cast<ptrdiff_t>(rawIterator.m_bit - this->m_bit);
253 }
254
255 public:
256 ConstBitIterator& operator=(const ConstBitIterator& rawIterator) = default;
257
258 bool operator==(const ConstBitIterator& rawIterator) const { return (m_bit == rawIterator.m_bit && m_flag == rawIterator.m_flag); }
259 bool operator!=(const ConstBitIterator& rawIterator) const { return (m_bit != rawIterator.m_bit || m_flag != rawIterator.m_flag); }
260
261 template<class t_type>
262 ConstBitIterator operator+(const t_type& movement)
263 {
264 ConstBitIterator sum(*this);
265 sum += movement;
266 return sum;
267 }
268 template<class t_type>
269 ConstBitIterator operator-(const t_type& movement)
270 {
271 ConstBitIterator sum(*this);
272 sum -= movement;
273 return sum;
274 }
275 private:
276 void checkBitLocationPositive() const
277 {
278 if (m_bit >= 8)
279 {
280 m_flag += (m_bit / 8);
281 m_bit = m_bit % 8;
282 }
283 m_bit_ref = cast<uint01>(m_bit);
284 }
285 void checkBitLocationNegative() const
286 {
287 if (m_bit <= 0)
288 {
289 m_bit = -m_bit + 8;
290 m_flag -= (m_bit / 8);
291 m_bit = m_bit % 8;
292 }
293 m_bit_ref = cast<uint01>(m_bit);
294 }
295 mutable const BitFlag* m_flag;
296 mutable sint04 m_bit;
297 mutable uint01 m_bit_ref = Constant<uint01>::NaN;
298 };
299}
300
A bitset that stores 8 bits (elements with only two possible values: 0 or 1, true or false,...
Definition BitFlag.hpp:68
Definition BitIterator.hpp:40
BitIterator operator--(int)
Definition BitIterator.hpp:86
BitIterator(BitFlag *flag, const sint04 offset)
Definition BitIterator.hpp:42
BitIterator & operator--()
Definition BitIterator.hpp:73
bool operator!=(const BitIterator &rawIterator) const
Definition BitIterator.hpp:145
BitIterator & operator-=(const t_type &movement)
Definition BitIterator.hpp:101
bool operator<(const BitIterator &other) const
Definition BitIterator.hpp:114
bool operator!() const
Definition BitIterator.hpp:50
bool operator==(const BitIterator &rawIterator) const
Definition BitIterator.hpp:144
BitIterator operator++(int)
Definition BitIterator.hpp:79
BitReference operator*()
Definition BitIterator.hpp:58
BitIterator operator+(t_type distance) const
Definition BitIterator.hpp:126
const BitReference operator*() const
Definition BitIterator.hpp:62
BitIterator & operator=(const BitIterator &rawIterator)=default
BitIterator & operator+=(const t_type &movement)
Definition BitIterator.hpp:94
BitIterator & operator++()
Definition BitIterator.hpp:67
bool operator>(const BitIterator &other) const
Definition BitIterator.hpp:107
ptrdiff_t operator-(const BitIterator &rawIterator)
Definition BitIterator.hpp:121
BitIterator(const BitIterator &iter)=default
BitIterator operator-(t_type distance) const
Definition BitIterator.hpp:134
Definition BitReference.hpp:38
Definition BitIterator.hpp:173
bool operator*()
Definition BitIterator.hpp:189
ConstBitIterator & operator-=(const t_type &movement)
Definition BitIterator.hpp:243
bool operator*() const
Definition BitIterator.hpp:193
ConstBitIterator operator-(const t_type &movement)
Definition BitIterator.hpp:269
ConstBitIterator & operator+=(const t_type &movement)
Definition BitIterator.hpp:236
ConstBitIterator & operator=(const ConstBitIterator &rawIterator)=default
bool operator==(bool ref) const
Definition BitIterator.hpp:197
ConstBitIterator(const ConstBitIterator &iter)=default
bool operator!() const
Definition BitIterator.hpp:201
ConstBitIterator(const BitFlag *flag, uint04 offset)
Definition BitIterator.hpp:181
ConstBitIterator operator+(const t_type &movement)
Definition BitIterator.hpp:262
bool operator==(const ConstBitIterator &rawIterator) const
Definition BitIterator.hpp:258
ConstBitIterator & operator--()
Definition BitIterator.hpp:215
ConstBitIterator & operator++()
Definition BitIterator.hpp:209
ConstBitIterator(const BitFlag *flag, sint04 offset)
Definition BitIterator.hpp:175
ConstBitIterator operator--(int)
Definition BitIterator.hpp:228
bool operator!=(const ConstBitIterator &rawIterator) const
Definition BitIterator.hpp:259
ptrdiff_t operator-(const ConstBitIterator &rawIterator)
Definition BitIterator.hpp:250
ConstBitIterator operator++(int)
Definition BitIterator.hpp:221
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:76
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:98
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr t_type distance(const t_vertex &vertex, const LineSegment< t_dims, t_type, t_vertex > &line)
Definition Distance.hpp:250
static const t_type NaN
Definition BaseValues.hpp:274