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 "DLLInfo.h"
34#include <NDEVR/BaseValues.h>
35#include <NDEVR/BitFlag.h>
36#include <NDEVR/BitReference.h>
37#include <iterator>
38namespace NDEVR
39{
40 /**--------------------------------------------------------------------------------------------------
41 \brief Simple bit iterator, typically used for parsing Buffer of bools in loops.
42
43 **/
45 {
46 public:
47 BitIterator(BitFlag* flag, const sint04 offset)
48 : m_flag(flag)
49 , m_bit(offset)
50 {
51 checkBitLocationPositive();
52 }
53 BitIterator(const BitIterator& iter) = default;
54
55 bool operator!() const
56 {
57 return !(*m_flag)[m_bit_ref];
58 }
59 constexpr operator bool() const
60 {
61 return (*m_flag)[m_bit_ref];
62 }
64 {
65 return BitReference(*m_flag, m_bit_ref);
66 }
67 const BitReference operator*() const
68 {
69 return BitReference(*m_flag, m_bit_ref);
70 }
71
73 {
74 m_bit++;
75 checkBitLocationPositive();
76 return *this;
77 }
79 {
80 m_bit--;
81 checkBitLocationNegative();
82 return *this;
83 }
85 {
86 BitIterator old_value(*this);
87 m_bit++;
88 checkBitLocationPositive();
89 return old_value;
90 }
92 {
93 BitIterator old_value(*this);
94 m_bit--;
95 checkBitLocationNegative();
96 return old_value;
97 }
98 template<class t_type>
99 BitIterator& operator+=(const t_type& movement)
100 {
101 m_bit += cast<sint04>(movement);
102 checkBitLocationPositive();
103 return (*this);
104 }
105 template<class t_type>
106 BitIterator& operator-=(const t_type& movement)
107 {
108 m_bit -= cast<sint04>(movement);
109 checkBitLocationNegative();
110 return (*this);
111 }
112 bool operator>(const BitIterator& other) const
113 {
114 if (m_flag == other.m_flag)
115 return m_bit > other.m_bit;
116 else
117 return m_flag > other.m_flag;
118 }
119 bool operator<(const BitIterator& other) const
120 {
121 if (m_flag == other.m_flag)
122 return m_bit < other.m_bit;
123 else
124 return m_flag < other.m_flag;
125 }
126 ptrdiff_t operator-(const BitIterator& rawIterator)
127 {
128 return std::distance(rawIterator.m_flag, this->m_flag) + cast<ptrdiff_t>(rawIterator.m_bit - this->m_bit);
129 }
130 template<class t_type>
132 {
133 BitIterator sum = *this;
134 sum.m_bit += cast<sint04>(distance);
135 sum.checkBitLocationPositive();
136 return sum;
137 }
138 template<class t_type>
140 {
141 BitIterator sum = *this;
142 sum.m_bit -= cast<sint04>(distance);
143 sum.checkBitLocationNegative();
144 return sum;
145 }
146 public:
147 BitIterator& operator=(const BitIterator& rawIterator) = default;
148
149 bool operator==(const BitIterator& rawIterator) const { return (m_bit == rawIterator.m_bit && m_flag == rawIterator.m_flag); }
150 bool operator!=(const BitIterator& rawIterator) const { return (m_bit != rawIterator.m_bit || m_flag != rawIterator.m_flag); }
151
152 private:
153 void checkBitLocationPositive() const
154 {
155 if (m_bit >= 8)
156 {
157 m_flag += (m_bit / 8);
158 m_bit = m_bit % 8;
159 }
160 m_bit_ref = cast<uint01>(m_bit);
161 }
162 void checkBitLocationNegative() const
163 {
164 if (m_bit <= 0)
165 {
166 m_bit = -m_bit + 8;
167 m_flag -= (m_bit / 8);
168 m_bit = m_bit % 8;
169 }
170 m_bit_ref = cast<uint01>(m_bit);
171 }
172 mutable BitFlag* m_flag;
173 mutable sint04 m_bit;
174 mutable uint01 m_bit_ref = Constant<uint01>::Invalid;
175 };
176 /**--------------------------------------------------------------------------------------------------
177 \brief Simple bit iterator, typically used for parsing Buffer of bools in loops.
178
179 **/
181 {
182 public:
183 ConstBitIterator(const BitFlag* flag, sint04 offset)
184 : m_flag(flag)
185 , m_bit(offset)
186 {
187 checkBitLocationPositive();
188 }
189 ConstBitIterator(const BitFlag* flag, uint04 offset)
190 : m_flag(flag)
191 , m_bit(cast<sint04>(offset))
192 {
193 checkBitLocationPositive();
194 }
195 ConstBitIterator(const ConstBitIterator& iter) = default;
196
198 {
199 return (*m_flag)[m_bit_ref];
200 }
201 bool operator*() const
202 {
203 return (*m_flag)[m_bit_ref];
204 }
205 bool operator==(bool ref) const
206 {
207 return (*m_flag)[m_bit_ref] == ref;
208 }
209 bool operator!() const
210 {
211 return !(*m_flag)[m_bit_ref];
212 }
213 constexpr operator bool() const
214 {
215 return (*m_flag)[m_bit_ref];
216 }
218 {
219 m_bit++;
220 checkBitLocationPositive();
221 return *this;
222 }
224 {
225 m_bit--;
226 checkBitLocationNegative();
227 return *this;
228 }
230 {
231 ConstBitIterator old_value(*this);
232 m_bit++;
233 checkBitLocationPositive();
234 return old_value;
235 }
237 {
238 ConstBitIterator old_value(*this);
239 m_bit--;
240 checkBitLocationNegative();
241 return old_value;
242 }
243 template<class t_type>
244 ConstBitIterator& operator+=(const t_type& movement)
245 {
246 m_bit += cast<sint04>(movement);
247 checkBitLocationPositive();
248 return (*this);
249 }
250 template<class t_type>
251 ConstBitIterator& operator-=(const t_type& movement)
252 {
253 m_bit -= cast<sint04>(movement);
254 checkBitLocationNegative();
255 return (*this);
256 }
257
258 ptrdiff_t operator-(const ConstBitIterator& rawIterator)
259 {
260 return std::distance(rawIterator.m_flag, this->m_flag) + cast<ptrdiff_t>(rawIterator.m_bit - this->m_bit);
261 }
262
263 public:
264 ConstBitIterator& operator=(const ConstBitIterator& rawIterator) = default;
265
266 bool operator==(const ConstBitIterator& rawIterator) const { return (m_bit == rawIterator.m_bit && m_flag == rawIterator.m_flag); }
267 bool operator!=(const ConstBitIterator& rawIterator) const { return (m_bit != rawIterator.m_bit || m_flag != rawIterator.m_flag); }
268
269 template<class t_type>
270 ConstBitIterator operator+(const t_type& movement)
271 {
272 ConstBitIterator sum(*this);
273 sum += movement;
274 return sum;
275 }
276 template<class t_type>
277 ConstBitIterator operator-(const t_type& movement)
278 {
279 ConstBitIterator sum(*this);
280 sum -= movement;
281 return sum;
282 }
283 private:
284 void checkBitLocationPositive() const
285 {
286 if (m_bit >= 8)
287 {
288 m_flag += (m_bit / 8);
289 m_bit = m_bit % 8;
290 }
291 m_bit_ref = cast<uint01>(m_bit);
292 }
293 void checkBitLocationNegative() const
294 {
295 if (m_bit <= 0)
296 {
297 m_bit = -m_bit + 8;
298 m_flag -= (m_bit / 8);
299 m_bit = m_bit % 8;
300 }
301 m_bit_ref = cast<uint01>(m_bit);
302 }
303 mutable const BitFlag* m_flag;
304 mutable sint04 m_bit;
305 mutable uint01 m_bit_ref = Constant<uint01>::Invalid;
306 };
307}
308
A bitset that stores 8 bits (elements with only two possible values: 0 or 1, true or false,...
Definition BitFlag.hpp:55
Simple bit iterator, typically used for parsing Buffer of bools in loops.
Definition BitIterator.hpp:45
BitIterator operator--(int)
Definition BitIterator.hpp:91
BitIterator(BitFlag *flag, const sint04 offset)
Definition BitIterator.hpp:47
BitIterator & operator--()
Definition BitIterator.hpp:78
bool operator!=(const BitIterator &rawIterator) const
Definition BitIterator.hpp:150
BitIterator & operator-=(const t_type &movement)
Definition BitIterator.hpp:106
bool operator<(const BitIterator &other) const
Definition BitIterator.hpp:119
bool operator!() const
Definition BitIterator.hpp:55
bool operator==(const BitIterator &rawIterator) const
Definition BitIterator.hpp:149
BitIterator operator++(int)
Definition BitIterator.hpp:84
BitReference operator*()
Definition BitIterator.hpp:63
BitIterator operator+(t_type distance) const
Definition BitIterator.hpp:131
const BitReference operator*() const
Definition BitIterator.hpp:67
BitIterator & operator=(const BitIterator &rawIterator)=default
BitIterator & operator+=(const t_type &movement)
Definition BitIterator.hpp:99
BitIterator & operator++()
Definition BitIterator.hpp:72
bool operator>(const BitIterator &other) const
Definition BitIterator.hpp:112
ptrdiff_t operator-(const BitIterator &rawIterator)
Definition BitIterator.hpp:126
BitIterator(const BitIterator &iter)=default
BitIterator operator-(t_type distance) const
Definition BitIterator.hpp:139
A convenience class used with Buffers or Vectors of bools for referencing or acting on a single bit.
Definition BitReference.hpp:42
Simple bit iterator, typically used for parsing Buffer of bools in loops.
Definition BitIterator.hpp:181
bool operator*()
Definition BitIterator.hpp:197
ConstBitIterator & operator-=(const t_type &movement)
Definition BitIterator.hpp:251
bool operator*() const
Definition BitIterator.hpp:201
ConstBitIterator operator-(const t_type &movement)
Definition BitIterator.hpp:277
ConstBitIterator & operator+=(const t_type &movement)
Definition BitIterator.hpp:244
ConstBitIterator & operator=(const ConstBitIterator &rawIterator)=default
bool operator==(bool ref) const
Definition BitIterator.hpp:205
ConstBitIterator(const ConstBitIterator &iter)=default
bool operator!() const
Definition BitIterator.hpp:209
ConstBitIterator(const BitFlag *flag, uint04 offset)
Definition BitIterator.hpp:189
ConstBitIterator operator+(const t_type &movement)
Definition BitIterator.hpp:270
bool operator==(const ConstBitIterator &rawIterator) const
Definition BitIterator.hpp:266
ConstBitIterator & operator--()
Definition BitIterator.hpp:223
ConstBitIterator & operator++()
Definition BitIterator.hpp:217
ConstBitIterator(const BitFlag *flag, sint04 offset)
Definition BitIterator.hpp:183
ConstBitIterator operator--(int)
Definition BitIterator.hpp:236
bool operator!=(const ConstBitIterator &rawIterator) const
Definition BitIterator.hpp:267
ptrdiff_t operator-(const ConstBitIterator &rawIterator)
Definition BitIterator.hpp:258
ConstBitIterator operator++(int)
Definition BitIterator.hpp:229
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:64
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
constexpr t_type distance(const t_vertex &vertex, const LineSegment< t_dims, t_type, t_vertex > &line)
Definition Distance.hpp:171
static const t_type Invalid
Definition BaseValues.hpp:234