Visual Computing Library  devel
Loading...
Searching...
No Matches
bit_set.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_CORE_BIT_SET_H
24#define VCL_SPACE_CORE_BIT_SET_H
25
26#include "bit_set/bit_proxy.h"
27
28#include <vclib/base.h>
29
30#include <stdexcept>
31#include <string>
32
33namespace vcl {
34
50template<std::integral T>
51class BitSet
52{
53 T mBits = static_cast<T>(0);
54
55public:
59 using UnderlyingType = T;
60
64 static constexpr std::size_t SIZE = sizeof(T) * 8;
65
69 BitSet() = default;
70
81 template<NonBoolIntegralOrEnum I>
82 BitSet(std::initializer_list<I> l)
83 {
84 for (const auto& i : l) {
85 if constexpr (std::is_enum_v<I>)
86 at(toUnderlying(i)) = true;
87 else
88 at(i) = true;
89 }
90 }
91
101 template<typename B>
102 BitSet(std::initializer_list<B> l) requires std::same_as<bool, B>
103 {
104 if (l.size() > SIZE)
105 throw std::invalid_argument(
106 "BitSet: list size is greater than the number of bits of the "
107 "BitSet");
108
109 for (uint i = 0; const auto& b : l)
110 at(i++) = b;
111 }
112
117 constexpr std::size_t size() const { return SIZE; }
118
124 bool operator[](uint i) const
125 {
126 assert(i < SIZE);
127 return mBits & (1 << i);
128 }
129
136 {
137 assert(i < SIZE);
138 return BitProxy(mBits, i);
139 }
140
147 bool at(uint i) const
148 {
149 if (i < SIZE)
150 return mBits & (1 << i);
151 else
152 throw std::out_of_range(std::to_string(i) + " out of range.");
153 }
154
163 {
164 if (i < SIZE)
165 return BitProxy(mBits, i);
166 else
167 throw std::out_of_range(std::to_string(i) + " out of range.");
168 }
169
174 bool all() const { return mBits == ~static_cast<T>(0); }
175
180 bool any() const { return !none(); }
181
186 bool none() const { return mBits == static_cast<T>(0); }
187
193 {
194 mBits = ~static_cast<T>(0);
195 return *this;
196 }
197
205 {
206 at(i) = b;
207 return *this;
208 }
209
215 {
216 mBits = 0;
217 return *this;
218 }
219
226 {
227 at(i) = false;
228 return *this;
229 }
230
236 {
237 mBits = ~mBits;
238 return *this;
239 }
240
247 {
248 at(i) = !at(i);
249 return *this;
250 }
251
256 T underlying() const { return mBits; }
257
267 {
268 return BitSet<T>(mBits & other.mBits);
269 }
270
280 {
281 return BitSet<T>(mBits | other.mBits);
282 }
283
293 {
294 return BitSet<T>(mBits ^ other.mBits);
295 }
296
303 BitSet<T> operator~() const { return BitSet<T>(~mBits); }
304
313 {
314 mBits &= other.mBits;
315 return *this;
316 }
317
326 {
327 mBits |= other.mBits;
328 return *this;
329 }
330
339 {
340 mBits ^= other.mBits;
341 return *this;
342 }
343
348 void serialize(std::ostream& os) const { vcl::serialize(os, mBits); }
349
354 void deserialize(std::istream& is) { vcl::deserialize(is, mBits); }
355
356 auto operator<=>(const BitSet<T>&) const = default;
357
359 template<typename U>
360 friend std::ostream& operator<<(std::ostream& os, const BitSet<U>& bs);
361
362private:
363 // constructor to initialize the BitSet with a given integral value
364 BitSet(T bits) : mBits(bits) {}
365};
366
373template<typename T>
374std::ostream& operator<<(std::ostream& os, const BitSet<T>& bs)
375{
376 os << "BitSet<" << typeid(T).name() << ">(";
377 for (uint i = 0; i < bs.size(); i++)
378 os << bs[i];
379 os << ")";
380 return os;
381}
382
383/* Specialization Aliases */
384
391
398
405
412
413/* Concepts */
414
425template<typename T>
426concept BitSetConcept = std::derived_from< // same type or derived type
427 std::remove_cvref_t<T>,
428 BitSet<typename RemoveRef<T>::UnderlyingType>>;
429
430} // namespace vcl
431
432#endif // VCL_SPACE_CORE_BIT_SET_H
The BitProxy class allows to access to a bool reference from a bit saved in a mask,...
Definition bit_proxy.h:43
The BitSet class allows to treat an integral type as an array of booleans of a guaranteed size.
Definition bit_set.h:52
bool all() const
Returns true if all the bits of the BitSet are set to true.
Definition bit_set.h:174
BitSet< T > set()
Sets all the bits to true.
Definition bit_set.h:192
BitSet< T > reset(uint i)
Sets the bit at position i to false.
Definition bit_set.h:225
bool any() const
Returns true if any of the bits of the BitSet are set to true.
Definition bit_set.h:180
BitSet< T > flip()
Flips all the bits of the BitSet.
Definition bit_set.h:235
BitSet< T > operator^(const BitSet< T > &other) const
Returns a BitSet that is the result of the bitwise XOR between this BitSet and another BitSet.
Definition bit_set.h:292
constexpr std::size_t size() const
Returns the number of bits of the BitSet.
Definition bit_set.h:117
BitSet< T > operator~() const
Returns a BitSet that is the result of the bitwise NOT of this BitSet.
Definition bit_set.h:303
BitProxy< T > operator[](uint i)
Returns a reference of the i-th bit value of the BitSet.
Definition bit_set.h:135
bool operator[](uint i) const
Returns the i-th bit value of the BitSet.
Definition bit_set.h:124
BitSet< T > operator&(const BitSet< T > &other) const
Returns a BitSet that is the result of the bitwise AND between this BitSet and another BitSet.
Definition bit_set.h:266
static constexpr std::size_t SIZE
The number of the bits of the BitSet.
Definition bit_set.h:64
BitSet< T > & operator|=(const BitSet< T > &other)
Compound assignment operator that performs the bitwise OR between this BitSet and another BitSet.
Definition bit_set.h:325
BitSet< T > & operator&=(const BitSet< T > &other)
Compound assignment operator that performs the bitwise AND between this BitSet and another BitSet.
Definition bit_set.h:312
BitSet< T > operator|(const BitSet< T > &other) const
Returns a BitSet that is the result of the bitwise OR between this BitSet and another BitSet.
Definition bit_set.h:279
BitSet< T > set(bool b, uint i)
Sets the bit at position i to b.
Definition bit_set.h:204
void serialize(std::ostream &os) const
Serializes the BitSet to the given output stream.
Definition bit_set.h:348
BitSet(std::initializer_list< I > l)
Constructor from list of integral (or enum) values that represent the indices of the true bits,...
Definition bit_set.h:82
bool at(uint i) const
Returns the i-th bit value of the BitSet, with bounds checking.
Definition bit_set.h:147
BitSet< T > reset()
Sets all the bits to false.
Definition bit_set.h:214
BitSet< T > flip(uint i)
Flips the bit at position i.
Definition bit_set.h:246
T UnderlyingType
The type of the underlying integral value used to store the bits.
Definition bit_set.h:59
bool none() const
Returns true if none of the bits of the BitSet is set to true.
Definition bit_set.h:186
void deserialize(std::istream &is)
Deserializes the BitSet from the given input stream.
Definition bit_set.h:354
T underlying() const
Returns the underlying integral value of the BitSet.
Definition bit_set.h:256
BitProxy< T > at(uint i)
Returns a reference of the i-th bit value of the BitSet, with bounds checking.
Definition bit_set.h:162
BitSet< T > & operator^=(const BitSet< T > &other)
Compound assignment operator that performs the bitwise XOR between this BitSet and another BitSet.
Definition bit_set.h:338
BitSet(std::initializer_list< B > l)
Constructor from a list of boolean values, allowing braces initialization.
Definition bit_set.h:102
BitSet()=default
Empty constructor. All the Bits of the BitSet are set to false.
A class representing a box in N-dimensional space.
Definition box.h:46
PointT size() const
Computes the size of the box.
Definition box.h:267
A concept representing a BitSet.
Definition bit_set.h:426