API Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 \brief An boolean specifc Vector which stores vectors of bools in optimal space.
41 t_dims: The number of dimensions used in a vector.
42 **/
43 template<uint01 t_dims>
44 class Vector<t_dims, bool>
45 {
46 public:
47 constexpr Vector() = default;
48 constexpr Vector(const Vector<t_dims, bool>& vector)
49 {
50 for (uint01 dim = 0; dim < sizeof(Vector<t_dims, bool>); ++dim)
51 m_values[dim] = vector.m_values[dim];
52 }
53 /**
54 \brief Copy constructor.
55
56 Author: Tyler Parke
57
58 Date: 2017-11-13
59
60 Parameters:
61 vector - The vector to copy to this vector.
62 **/
63 template<class t_vec_type>
64 constexpr Vector(const Vector<t_dims, t_vec_type>& vector)
65 {
66 for (uint01 dim = 0; dim < t_dims; ++dim)
67 m_values[dim] = cast<bool>(vector[dim]);
68 }
69
70 /**
71 \brief Sets values in each dimension to the value in the passed in scaler
72
73 Author: Tyler Parke
74
75 Date: 2017-09-13
76
77 Parameters:
78 scaler - The bool to set all dimensions to.
79 **/
80
81 constexpr explicit Vector(const bool& value)
82 {
83 for (uint01 dim = 0; dim < ByteCount(); ++dim)
84 m_values[dim] = BitFlag(value);
85 }
86
87
88 Vector(std::initializer_list<bool> active_list)
89 {
90 uint04 i = 0;
91 for (auto val : active_list)
92 (*this)(i++, val);
93 }
94
95 template<class t_type>
96 Vector(std::initializer_list<t_type> active_list)
97 {
98 for (auto i : active_list)
99 (*this)(cast<uint01>(i), true);
100 }
101
102 constexpr void operator()(const uint01 dim, bool value)
103 {
104 m_values[dim / 8](dim % 8, value);
105 }
106
107 /**
108 \brief Sets values in each dimension to the respective value in the passed in scaler. Note the Vector must
109 be 2 dimensions to use this function.
110
111 Author: Tyler Parke
112
113 Date: 2017-09-13
114
115 Parameters:
116 x - The bool to set the 0th or X dimension to
117 y - The bool to set the 1st or Y dimension to
118 **/
119 constexpr Vector(const bool& x, const bool& y)
120 {
121 static_assert(t_dims == 2, "Unexpected Number of Dimensions. Vector Size Required: 2");
122 m_values[0](X, x);
123 m_values[0](Y, y);
124 }
125
126 /**
127 \brief Sets values in each dimension to the respective value in the passed in scaler. Note the Vector must
128 be 3 dimensions to use this function.
129 \param[in] x - The bool to set the 0th or X dimension to
130 \param[in] y - The bool to set the 1st or Y dimension to
131 \param[in] z - The bool to set the 2nd or Z dimension to
132 **/
133 constexpr Vector(const bool& x, const bool& y, const bool& z)
134 {
135 static_assert(t_dims == 3, "Unexpected Number of Dimensions. Vector Size Required: 3");
136 m_values[0](X, x);
137 m_values[0](Y, y);
138 m_values[0](Z, z);
139 }
140
141 /**
142 \brief Sets values in each dimension to the respective value in the passed in scaler. Note the Vector must
143 be 4 dimensions to use this function.
144 \param[in] x - The bool to set the 0th or X dimension to
145 \param[in] y - The bool to set the 1st or Y dimension to
146 \param[in] z - The bool to set the 2nd or Z dimension to
147 \param[in] w - The bool to set the 3rd or W dimension to
148 **/
149 constexpr Vector(const bool& x, const bool& y, const bool& z, const bool& w)
150 {
151 static_assert(t_dims == 4, "Unexpected Number of Dimensions. Vector Size Required: 4");
152 m_values[0](X, x);
153 m_values[0](Y, y);
154 m_values[0](Z, z);
155 m_values[0](W, w);
156 }
157
158 /**
159 \brief Given a container of statically determined array, transforms it to a vector
160 \param[in] vector - The vector container object.
161 **/
162 constexpr explicit Vector(const bool(&vector)[t_dims])
163 {
164 for (uint01 dim = 0; dim < t_dims; dim++)
165 m_values[dim / 8](dim % 8, vector[dim]);
166 }
167
168 /**
169 \brief Vectors. Creates a vector where the prefix vector is combined with the suffix boolean.
170 \param[in] vector - The vector.
171 \param[in] suffix A boolean to append to the given vector
172 **/
173 constexpr Vector(const Vector<t_dims - 1, bool>& vector, const bool& suffix)
174 {
175 static_assert(t_dims >= 2, "Unexpected Number of Dimensions. Vector Size Required: >= 2");
176 for (uint01 dim = 0; dim < t_dims - 1; ++dim)
177 m_values[dim / 8](dim % 8, vector[dim]);
178 m_values[(t_dims - 1) / 8]((t_dims - 1) % 8, suffix);
179 }
180
181 /**
182 \brief Creates a vector where the prefix vector is combined with the suffix scalers.
183 \param[in] vector - The vector.
184 \param[in] postfix_a - The postfix a.
185 \param[in] postfix_b - The postfix b.
186 **/
187 constexpr Vector(const Vector<getMax(t_dims - 2, 0), bool>& vector, const bool& suffix_a, const bool& suffix_b)
188 {
189 static_assert(t_dims >= 3, "Unexpected Number of Dimensions. Vector Size Required: >= 3");
190 for (uint01 dim = 0; dim < t_dims - 2; ++dim)
191 m_values[dim / 8](dim % 8, vector[dim]);
192 m_values[(t_dims - 2) / 8]((t_dims - 2) % 8, suffix_a);
193 m_values[(t_dims - 1) / 8]((t_dims - 1) % 8, suffix_b);
194 }
195
196 /**
197 \brief Creates a vector where the prefix scaler is combined with the suffix vector.
198 \param[in] prefix The prefix.
199 \param[in] vector The vector.
200 **/
201 constexpr Vector(const bool& prefix, const Vector<t_dims - 1, bool>& vector)
202 {
203 static_assert(t_dims >= 2, "Unexpected Number of Dimensions. Vector Size Required: >= 2");
204 m_values[0](0, prefix);
205 for (uint01 dim = 1; dim < t_dims; ++dim)
206 m_values[dim / 8](dim % 8, vector[dim - 1]);
207 }
208 /**
209 \brief Vectors. Creates a vector where the prefix scaler values are combined with the suffix vector.
210 \param[in] prefix_a - The first prefix.
211 \param[in] prefix_b - The second prefix.
212 \param[in] vector - The vector.
213 **/
214 constexpr Vector(const bool& prefix_a, const bool& prefix_b, const Vector<t_dims - 1, bool>& vector)
215 {
216 static_assert(t_dims >= 2, "Unexpected Number of Dimensions. Vector Size Required: of at least 1");
217 m_values[X] = prefix_a;
218 m_values[Y] = prefix_b;
219 for (uint01 dim = Z; dim < t_dims; ++dim)
220 m_values[dim] = vector[dim - 1];
221 }
222
223 /**
224 \brief As the given extra fill value.
225 \param[in] extra_fill_value - (Optional) The extra fill value, or value to set dimensions extra that may be
226 created when t_new_dim > t_dim.
227 \returns A Vector&lt;t_new_dim,t_new_type&gt;
228 **/
229 template<uint01 t_new_dim, class t_new_type>
230 constexpr Vector<t_new_dim, t_new_type> as(t_new_type extra_fill_value = 0) const
231 {
233 uint01 dim = 0;
234 const uint01 min = getMin(t_new_dim, t_dims);
235 for (; dim < min; dim++)
236 vec[dim] = cast<t_new_type>(m_values[dim / 8][dim % 8]);
237 for (; dim < t_new_dim; dim++)
238 vec[dim] = extra_fill_value;
239 return vec;
240 }
241
242
243
244 /**
245 \brief For Single dimensional objects, they may also be considered a scaler, thus allow implicit conversion
246 from a vector to a scaler when the dimension of the vector is 1.
247 \returns The result of the operation.
248 **/
249 constexpr operator bool() const
250 {
251 static_assert(t_dims == 1, "Cannot auto-convert from vector to scaler unless dimension of vector is 1.");
252 return m_values[0][0];
253 }
254
255
256
257 /**
258 \brief Accesses the value of a certain dimension.
259 \param[in] dimension_index - The dimensional index of the the value we wish to retrieve.
260 \returns The value at the given dimension_index.
261 **/
262
263 constexpr BitReference operator[](uint01 dimension_index)
264 {
265 lib_assert(dimension_index < t_dims, "Dimension requested Exceeded maximum vector dimension");
266 return BitReference(m_values[dimension_index / 8], dimension_index % 8);
267 }
268
269
270
271 constexpr bool operator[](const uint01 dimension_index) const
272 {
273 lib_assert(dimension_index < t_dims, "Dimension requested Exceeded maximum vector dimension");
274 return m_values[dimension_index / 8][dimension_index % 8];
275 }
276
277
278 /**
279 \brief Assignment operator.
280 \param[in] vector - The vector to set this vector to.
281 \returns A reference of this object (Useful for chaining '=' together).
282 **/
283
285 {
286 for (uint01 dim = 0; dim < ByteCount(); ++dim)
287 m_values[dim] = vector.m_values[dim];
288 return *this;
289 }
290
291 /**
292 \brief Assignment operator. Setes all values to the value provided in scaler argument.
293 \param[in] scaler The scaler to set each dimension to.
294 \returns A reference of this object (Useful for chaining '=' together).
295 **/
296 constexpr Vector<t_dims, bool>& operator=(const bool& scaler)
297 {
298
299 for (uint01 dim = 0; dim < ByteCount(); ++dim)
300 m_values[dim] = BitFlag(scaler);
301 return *this;
302 }
303
305 {
306 for (uint01 dim = 0; dim < ByteCount(); ++dim)
307 m_values[dim] &= vector.m_values[dim];
308 return *this;
309 }
310
312 {
313 for (uint01 dim = 0; dim < ByteCount(); ++dim)
314 m_values[dim] |= vector.m_values[dim];
315 return *this;
316 }
317
319 {
320 Vector<t_dims, bool> output(0);
321 for (uint01 dim = 0; dim < ByteCount(); ++dim)
322 output[dim] = m_values[dim] & vector.m_values[dim];
323 return output;
324 }
325
327 {
328 Vector<t_dims, bool> output(0);
329 for (uint01 dim = 0; dim < ByteCount(); ++dim)
330 output[dim] = m_values[dim] | vector.m_values[dim];
331 return output;
332 }
333
334 /**
335 \brief Number of dimensions in this vector class.
336 \returns The total number of dimensions in this vector space.
337 **/
338
339 static constexpr uint01 NumberOfDimensions() { return t_dims; }
340
341 /**
342 \brief Returns the type of this class. Useful for using decltype to instantiate a member of this class in a
343 static environment.
344 \returns The type of this class. Object is inavlid outside of decl.
345 **/
346 constexpr static bool Type() { return bool(); }
347
348 static constexpr uint04 ByteCount()
349 {
350 return (t_dims / 8) + (t_dims % 8 == 0 ? 0 : 1);
351 }
352 protected:
353 /** The values[t dims]. */
354 BitFlag m_values[(t_dims / 8) + (t_dims % 8 == 0 ? 0 : 1)];
355 };
356};
357
358
#define lib_assert(expression, message)
Definition LibAssert.h:61
A bitset that stores 8 bits (elements with only two possible values: 0 or 1, true or false,...
Definition BitFlag.hpp:55
A convenience class used with Buffers or Vectors of bools for referencing or acting on a single bit.
Definition BitReference.hpp:42
constexpr Vector(const bool &value)
Sets values in each dimension to the value in the passed in scaler.
Definition BoolVector.hpp:81
constexpr Vector(const bool &prefix, const Vector< t_dims - 1, bool > &vector)
Creates a vector where the prefix scaler is combined with the suffix vector.
Definition BoolVector.hpp:201
constexpr Vector< t_dims, bool > operator|(const Vector< t_dims, bool > &vector) const
Definition BoolVector.hpp:326
constexpr Vector< t_new_dim, t_new_type > as(t_new_type extra_fill_value=0) const
As the given extra fill value.
Definition BoolVector.hpp:230
constexpr Vector(const Vector< t_dims, t_vec_type > &vector)
Copy constructor.
Definition BoolVector.hpp:64
constexpr void operator()(const uint01 dim, bool value)
Definition BoolVector.hpp:102
static constexpr uint04 ByteCount()
Definition BoolVector.hpp:348
constexpr Vector()=default
static constexpr bool Type()
Returns the type of this class. Useful for using decltype to instantiate a member of this class in a ...
Definition BoolVector.hpp:346
Vector(std::initializer_list< t_type > active_list)
Definition BoolVector.hpp:96
constexpr Vector(const Vector< getMax(t_dims - 2, 0), bool > &vector, const bool &suffix_a, const bool &suffix_b)
Creates a vector where the prefix vector is combined with the suffix scalers.
Definition BoolVector.hpp:187
constexpr Vector(const bool &x, const bool &y, const bool &z)
Sets values in each dimension to the respective value in the passed in scaler. Note the Vector must b...
Definition BoolVector.hpp:133
constexpr Vector< t_dims, bool > & operator=(const bool &scaler)
Assignment operator. Setes all values to the value provided in scaler argument.
Definition BoolVector.hpp:296
constexpr Vector< t_dims, bool > & operator=(const Vector< t_dims, bool > &vector)
Assignment operator.
Definition BoolVector.hpp:284
constexpr Vector< t_dims, bool > & operator&=(const Vector< t_dims, bool > &vector)
Definition BoolVector.hpp:304
Vector(std::initializer_list< bool > active_list)
Definition BoolVector.hpp:88
constexpr Vector(const Vector< t_dims - 1, bool > &vector, const bool &suffix)
Vectors. Creates a vector where the prefix vector is combined with the suffix boolean.
Definition BoolVector.hpp:173
constexpr Vector(const bool &prefix_a, const bool &prefix_b, const Vector< t_dims - 1, bool > &vector)
Vectors. Creates a vector where the prefix scaler values are combined with the suffix vector.
Definition BoolVector.hpp:214
constexpr Vector(const bool &x, const bool &y, const bool &z, const bool &w)
Sets values in each dimension to the respective value in the passed in scaler. Note the Vector must b...
Definition BoolVector.hpp:149
static constexpr uint01 NumberOfDimensions()
Number of dimensions in this vector class.
Definition BoolVector.hpp:339
constexpr Vector< t_dims, bool > & operator|=(const Vector< t_dims, bool > &vector)
Definition BoolVector.hpp:311
constexpr Vector(const bool &x, const bool &y)
Sets values in each dimension to the respective value in the passed in scaler. Note the Vector must b...
Definition BoolVector.hpp:119
constexpr bool operator[](const uint01 dimension_index) const
Definition BoolVector.hpp:271
constexpr BitReference operator[](uint01 dimension_index)
Accesses the value of a certain dimension.
Definition BoolVector.hpp:263
constexpr Vector(const Vector< t_dims, bool > &vector)
Definition BoolVector.hpp:48
constexpr Vector< t_dims, bool > operator&(const Vector< t_dims, bool > &vector) const
Definition BoolVector.hpp:318
constexpr Vector(const bool(&vector)[t_dims])
Given a container of statically determined array, transforms it to a vector.
Definition BoolVector.hpp:162
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 The only requirement is that t_type have > ...
Definition BaseFunctions.hpp:94
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
@ Y
Definition BaseValues.hpp:169
@ X
Definition BaseValues.hpp:167
@ Z
Definition BaseValues.hpp:171
@ W
Definition BaseValues.hpp:173
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