API Documentation
Loading...
Searching...
No Matches
BoolVector.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: BoolVector
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <NDEVR/LibAssert.h>
35#include <NDEVR/Vector.h>
36#include <NDEVR/BitReference.h>
37namespace NDEVR
38{
39 /**--------------------------------------------------------------------------------------------------
40 Class: Vector<t_dims, bool>
41 \brief An boolean specifc Vector which stores vectors of bools in optimal space.
42
43 t_dims: The number of dimensions used in a vector.
44
45 Author: Tyler Parke
46
47 Date: 2017-09-13
48 *-----------------------------------------------------------------------------------------------**/
49 template<uint01 t_dims>
50 class Vector<t_dims, bool>
51 {
52 public:
53 constexpr Vector() = default;
54 constexpr Vector(const Vector<t_dims, bool>& vector)
55 {
56 for (uint01 dim = 0; dim < sizeof(Vector<t_dims, bool>); ++dim)
57 m_values[dim] = vector.m_values[dim];
58 }
59 /**--------------------------------------------------------------------------------------------------
60 Copy constructor.
61
62 Author: Tyler Parke
63
64 Date: 2017-11-13
65
66 Parameters:
67 vector - The vector to copy to this vector.
68 *-----------------------------------------------------------------------------------------------**/
69 template<class t_vec_type>
70 constexpr Vector(const Vector<t_dims, t_vec_type>& vector)
71 {
72 for (uint01 dim = 0; dim < t_dims; ++dim)
73 m_values[dim] = cast<bool>(vector[dim]);
74 }
75
76 /**--------------------------------------------------------------------------------------------------
77 Sets values in each dimension to the value in the passed in scaler
78
79 Author: Tyler Parke
80
81 Date: 2017-09-13
82
83 Parameters:
84 scaler - The bool to set all dimensions to.
85 *-----------------------------------------------------------------------------------------------**/
86
87 constexpr explicit Vector(const bool& value)
88 {
89 for (uint01 dim = 0; dim < ByteCount(); ++dim)
90 m_values[dim] = BitFlag(value);
91 }
92
93
94 Vector(std::initializer_list<bool> active_list)
95 {
96 uint04 i = 0;
97 for (auto val : active_list)
98 (*this)(i++, val);
99 }
100
101 template<class t_type>
102 Vector(std::initializer_list<t_type> active_list)
103 {
104 for (auto i : active_list)
105 (*this)(cast<uint01>(i), true);
106 }
107
108 constexpr void operator()(const uint01 dim, bool value)
109 {
110 m_values[dim / 8](dim % 8, value);
111 }
112
113 /**--------------------------------------------------------------------------------------------------
114 Sets values in each dimension to the respective value in the passed in scaler. Note the Vector must
115 be 2 dimensions to use this function.
116
117 Author: Tyler Parke
118
119 Date: 2017-09-13
120
121 Parameters:
122 x - The bool to set the 0th or X dimension to
123 y - The bool to set the 1st or Y dimension to
124 *-----------------------------------------------------------------------------------------------**/
125 constexpr Vector(const bool& x, const bool& y)
126 {
127 static_assert(t_dims == 2, "Unexpected Number of Dimensions. Vector Size Required: 2");
128 m_values[0](X, x);
129 m_values[0](Y, y);
130 }
131
132 /**--------------------------------------------------------------------------------------------------
133 Sets values in each dimension to the respective value in the passed in scaler. Note the Vector must
134 be 3 dimensions to use this function.
135
136 Author: Tyler Parke
137
138 Date: 2017-09-13
139
140 Parameters:
141 x - The bool to set the 0th or X dimension to
142 y - The bool to set the 1st or Y dimension to
143 z - The bool to set the 2nd or Z dimension to
144 *-----------------------------------------------------------------------------------------------**/
145 constexpr Vector(const bool& x, const bool& y, const bool& z)
146 {
147 static_assert(t_dims == 3, "Unexpected Number of Dimensions. Vector Size Required: 3");
148 m_values[0](X, x);
149 m_values[0](Y, y);
150 m_values[0](Z, z);
151 }
152
153 /**--------------------------------------------------------------------------------------------------
154 Sets values in each dimension to the respective value in the passed in scaler. Note the Vector must
155 be 4 dimensions to use this function.
156
157 Author: Tyler Parke
158
159 Date: 2017-09-13
160
161 Parameters:
162 x - The bool to set the 0th or X dimension to
163 y - The bool to set the 1st or Y dimension to
164 z - The bool to set the 2nd or Z dimension to
165 w - The bool to set the 3rd or W dimension to
166 *-----------------------------------------------------------------------------------------------**/
167 constexpr Vector(const bool& x, const bool& y, const bool& z, const bool& w)
168 {
169 static_assert(t_dims == 4, "Unexpected Number of Dimensions. Vector Size Required: 4");
170 m_values[0](X, x);
171 m_values[0](Y, y);
172 m_values[0](Z, z);
173 m_values[0](W, w);
174 }
175
176 /**--------------------------------------------------------------------------------------------------
177 Given a container of statically determined array, transforms it to a vector
178
179 Author: Tyler Parke
180
181 Date: 2017-11-13
182
183 Parameters:
184 vector - The vector container object.
185 *-----------------------------------------------------------------------------------------------**/
186 constexpr explicit Vector(const bool(&vector)[t_dims])
187 {
188 for (uint01 dim = 0; dim < t_dims; dim++)
189 m_values[dim / 8](dim % 8, vector[dim]);
190 }
191
192 /**--------------------------------------------------------------------------------------------------
193 Vectors. Creates a vector where the prefix vector is combined with the suffix scalers.
194
195 Author: Tyler Parke
196
197 Date: 2017-11-13
198
199 Parameters:
200 vector - The vector.
201 postfix - The postfix.
202 *-----------------------------------------------------------------------------------------------**/
203 constexpr Vector(const Vector<t_dims - 1, bool>& vector, const bool& suffix)
204 {
205 static_assert(t_dims >= 2, "Unexpected Number of Dimensions. Vector Size Required: >= 2");
206 for (uint01 dim = 0; dim < t_dims - 1; ++dim)
207 m_values[dim / 8](dim % 8, vector[dim]);
208 m_values[(t_dims - 1) / 8]((t_dims - 1) % 8, suffix);
209 }
210
211 /**--------------------------------------------------------------------------------------------------
212
213 Vectors. Creates a vector where the prefix vector is combined with the suffix scalers.
214
215 Author: Tyler Parke
216
217 Date: 2017-11-13
218
219 Parameters:
220 vector - The vector.
221 postfix_a - The postfix a.
222 postfix_b - The postfix b.
223 *-----------------------------------------------------------------------------------------------**/
224 constexpr Vector(const Vector<getMax(t_dims - 2, 0), bool>& vector, const bool& suffix_a, const bool& suffix_b)
225 {
226 static_assert(t_dims >= 3, "Unexpected Number of Dimensions. Vector Size Required: >= 3");
227 for (uint01 dim = 0; dim < t_dims - 2; ++dim)
228 m_values[dim / 8](dim % 8, vector[dim]);
229 m_values[(t_dims - 2) / 8]((t_dims - 2) % 8, suffix_a);
230 m_values[(t_dims - 1) / 8]((t_dims - 1) % 8, suffix_b);
231 }
232
233 /**--------------------------------------------------------------------------------------------------
234 Vectors. Creates a vector where the prefix scaler is combined with the suffix vector.
235
236 Author: Tyler Parke
237
238 Date: 2017-11-13
239
240 Parameters:
241 prefix - The prefix.
242 vector - The vector.
243 *-----------------------------------------------------------------------------------------------**/
244 constexpr Vector(const bool& prefix, const Vector<t_dims - 1, bool>& vector)
245 {
246 static_assert(t_dims >= 2, "Unexpected Number of Dimensions. Vector Size Required: >= 2");
247 m_values[0](0, prefix);
248 for (uint01 dim = 1; dim < t_dims; ++dim)
249 m_values[dim / 8](dim % 8, vector[dim - 1]);
250 }
251 /**--------------------------------------------------------------------------------------------------
252 Vectors. Creates a vector where the prefix scaler values are combined with the suffix vector.
253
254 Author: Tyler Parke
255
256 Date: 2017-11-13
257
258 Parameters:
259 prefix_a - The first prefix.
260 prefix_b - The second prefix.
261 vector - The vector.
262 *-----------------------------------------------------------------------------------------------**/
263 constexpr Vector(const bool& prefix_a, const bool& prefix_b, const Vector<t_dims - 1, bool>& vector)
264 {
265 static_assert(t_dims >= 2, "Unexpected Number of Dimensions. Vector Size Required: of at least 1");
266 m_values[X] = prefix_a;
267 m_values[Y] = prefix_b;
268 for (uint01 dim = Z; dim < t_dims; ++dim)
269 m_values[dim] = vector[dim - 1];
270 }
271
272 /**--------------------------------------------------------------------------------------------------
273 As the given extra fill value.
274
275 Author: Tyler Parke
276
277 Date: 2017-11-13
278
279 Parameters:
280 extra_fill_value - (Optional) The extra fill value, or value to set dimensions extra that may be
281 created when t_new_dim > t_dim.
282
283 Returns: A Vector&lt;t_new_dim,t_new_type&gt;
284 *-----------------------------------------------------------------------------------------------**/
285 template<uint01 t_new_dim, class t_new_type>
286 constexpr Vector<t_new_dim, t_new_type> as(t_new_type extra_fill_value = 0) const
287 {
289 uint01 dim = 0;
290 const uint01 min = getMin(t_new_dim, t_dims);
291 for (; dim < min; dim++)
292 vec[dim] = cast<t_new_type>(m_values[dim / 8][dim % 8]);
293 for (; dim < t_new_dim; dim++)
294 vec[dim] = extra_fill_value;
295 return vec;
296 }
297
298
299
300 /**--------------------------------------------------------------------------------------------------
301 For Single dimensional objects, they may also be considered a scaler, thus allow implicit conversion
302 from a vector to a scaler when the dimension of the vector is 1.
303
304 Author: Tyler Parke
305
306 Date: 2017-11-13
307
308 Returns: The result of the operation.
309 *-----------------------------------------------------------------------------------------------**/
310 constexpr operator bool() const
311 {
312 static_assert(t_dims == 1, "Cannot auto-convert from vector to scaler unless dimension of vector is 1.");
313 return m_values[0][0];
314 }
315
316
317
318 /**--------------------------------------------------------------------------------------------------
319 Accesses the value of a certain dimension.
320
321 Author: Tyler Parke
322
323 Date: 2017-11-13
324
325 Parameters:
326 dimension_index - The dimensional index of the the value we wish to retrieve.
327
328 Returns: The value at the given dimension_index.
329 *-----------------------------------------------------------------------------------------------**/
330
331 constexpr BitReference operator[](uint01 dimension_index)
332 {
333 lib_assert(dimension_index < t_dims, "Dimension requested Exceeded maximum vector dimension");
334 return BitReference(m_values[dimension_index / 8], dimension_index % 8);
335 }
336
337
338
339 constexpr bool operator[](const uint01 dimension_index) const
340 {
341 lib_assert(dimension_index < t_dims, "Dimension requested Exceeded maximum vector dimension");
342 return m_values[dimension_index / 8][dimension_index % 8];
343 }
344
345
346 /**--------------------------------------------------------------------------------------------------
347 Assignment operator.
348
349 Author: Tyler Parke
350
351 Date: 2017-11-13
352
353 Parameters:
354 vector - The vector to set this vector to.
355
356 Returns: A reference of this object (Useful for chaining '=' together).
357 *-----------------------------------------------------------------------------------------------**/
358
360 {
361 for (uint01 dim = 0; dim < ByteCount(); ++dim)
362 m_values[dim] = vector.m_values[dim];
363 return *this;
364 }
365
366 /**--------------------------------------------------------------------------------------------------
367 Assignment operator. Setes all values to the value provided in scaler argument.
368
369 Author: Tyler Parke
370
371 Date: 2017-11-13
372
373 Parameters:
374 scaler - The scaler to set each dimension to.
375
376 Returns: A reference of this object (Useful for chaining '=' together).
377 *-----------------------------------------------------------------------------------------------**/
378
379 constexpr Vector<t_dims, bool>& operator=(const bool& scaler)
380 {
381
382 for (uint01 dim = 0; dim < ByteCount(); ++dim)
383 m_values[dim] = BitFlag(scaler);
384 return *this;
385 }
386
388 {
389 for (uint01 dim = 0; dim < ByteCount(); ++dim)
390 m_values[dim] &= vector.m_values[dim];
391 return *this;
392 }
393
395 {
396 for (uint01 dim = 0; dim < ByteCount(); ++dim)
397 m_values[dim] |= vector.m_values[dim];
398 return *this;
399 }
400
402 {
403 Vector<t_dims, bool> output(0);
404 for (uint01 dim = 0; dim < ByteCount(); ++dim)
405 output[dim] = m_values[dim] & vector.m_values[dim];
406 return output;
407 }
408
410 {
411 Vector<t_dims, bool> output(0);
412 for (uint01 dim = 0; dim < ByteCount(); ++dim)
413 output[dim] = m_values[dim] | vector.m_values[dim];
414 return output;
415 }
416
417 /**--------------------------------------------------------------------------------------------------
418 Number of dimensions in this vector class.
419
420 Author: Tyler Parke
421
422 Date: 2017-11-13
423
424 Returns: The total number of dimensions in this vector space.
425 *-----------------------------------------------------------------------------------------------**/
426
427 static constexpr uint01 NumberOfDimensions() { return t_dims; }
428
429 /**--------------------------------------------------------------------------------------------------
430 Returns the type of this class. Useful for using decltype to instantiate a member of this class in a
431 static environment.
432
433 Author: Tyler Parke
434
435 Date: 2020-05-03
436
437 Returns: The type of this class. Object is inavlid outside of decl.
438 *-----------------------------------------------------------------------------------------------**/
439 constexpr static bool Type() { return bool(); }
440
441 static constexpr uint04 ByteCount()
442 {
443 return (t_dims / 8) + (t_dims % 8 == 0 ? 0 : 1);
444 }
445 protected:
446 /** The values[t dims]. */
447 BitFlag m_values[(t_dims / 8) + (t_dims % 8 == 0 ? 0 : 1)];
448 };
449};
450
451
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:68
A bitset that stores 8 bits (elements with only two possible values: 0 or 1, true or false,...
Definition BitFlag.hpp:62
A convenience class used with Buffers or Vectors of bools for referencing or acting on a.
Definition BitReference.hpp:42
constexpr Vector(const bool &value)
Definition BoolVector.hpp:87
constexpr Vector(const bool &prefix, const Vector< t_dims - 1, bool > &vector)
Definition BoolVector.hpp:244
constexpr Vector< t_dims, bool > operator|(const Vector< t_dims, bool > &vector) const
Definition BoolVector.hpp:409
constexpr Vector< t_new_dim, t_new_type > as(t_new_type extra_fill_value=0) const
Definition BoolVector.hpp:286
constexpr Vector(const Vector< t_dims, t_vec_type > &vector)
Definition BoolVector.hpp:70
constexpr void operator()(const uint01 dim, bool value)
Definition BoolVector.hpp:108
static constexpr uint04 ByteCount()
Definition BoolVector.hpp:441
constexpr Vector()=default
static constexpr bool Type()
Definition BoolVector.hpp:439
Vector(std::initializer_list< t_type > active_list)
Definition BoolVector.hpp:102
constexpr Vector(const Vector< getMax(t_dims - 2, 0), bool > &vector, const bool &suffix_a, const bool &suffix_b)
Definition BoolVector.hpp:224
constexpr Vector(const bool &x, const bool &y, const bool &z)
Definition BoolVector.hpp:145
constexpr Vector< t_dims, bool > & operator=(const bool &scaler)
Definition BoolVector.hpp:379
constexpr Vector< t_dims, bool > & operator=(const Vector< t_dims, bool > &vector)
Definition BoolVector.hpp:359
constexpr Vector< t_dims, bool > & operator&=(const Vector< t_dims, bool > &vector)
Definition BoolVector.hpp:387
Vector(std::initializer_list< bool > active_list)
Definition BoolVector.hpp:94
constexpr Vector(const Vector< t_dims - 1, bool > &vector, const bool &suffix)
Definition BoolVector.hpp:203
constexpr Vector(const bool &prefix_a, const bool &prefix_b, const Vector< t_dims - 1, bool > &vector)
Definition BoolVector.hpp:263
constexpr Vector(const bool &x, const bool &y, const bool &z, const bool &w)
Definition BoolVector.hpp:167
static constexpr uint01 NumberOfDimensions()
Definition BoolVector.hpp:427
constexpr Vector< t_dims, bool > & operator|=(const Vector< t_dims, bool > &vector)
Definition BoolVector.hpp:394
constexpr Vector(const bool &x, const bool &y)
Definition BoolVector.hpp:125
constexpr bool operator[](const uint01 dimension_index) const
Definition BoolVector.hpp:339
constexpr BitReference operator[](uint01 dimension_index)
Definition BoolVector.hpp:331
constexpr Vector(const Vector< t_dims, bool > &vector)
Definition BoolVector.hpp:54
constexpr Vector< t_dims, bool > operator&(const Vector< t_dims, bool > &vector) const
Definition BoolVector.hpp:401
constexpr Vector(const bool(&vector)[t_dims])
Definition BoolVector.hpp:186
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
t_type m_values[t_dims]
Definition Vector.hpp:623
Definition ACIColor.h:37
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator.
Definition BaseFunctions.hpp:101
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:78
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:94
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:379
@ Y
Definition BaseValues.hpp:165
@ X
Definition BaseValues.hpp:163
@ Z
Definition BaseValues.hpp:167
@ W
Definition BaseValues.hpp:169
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator Author: Tyler Parke Date: 2017-11-05...
Definition BaseFunctions.hpp:56