ECSTASY
All in the name
Loading...
Searching...
No Matches
BitSet.cpp
Go to the documentation of this file.
1
11
12#include "BitSet.hpp"
13
14#include <algorithm>
15#include <cstddef>
16#include <cstdint>
17#include <math.h>
18#include <ostream>
19#include <vector>
20#include <string_view>
21
22namespace util
23{
24 BitSet::BitSet(std::size_t initialSize) : _size(initialSize), _store(getStoreWordCount(initialSize))
25 {
26 }
27
28 BitSet::BitSet(std::string_view bitString) : BitSet(bitString.size())
29 {
30 for (std::size_t i = 0, s = bitString.size(); i < s; ++i)
31 this->set(i, bitString[s - i - 1] == '1');
32 }
33
34 BitSet &BitSet::setAll(bool value) noexcept
35 {
36 for (auto &word : this->_store)
37 word = value ? ~std::uint64_t(0) : std::uint64_t(0);
38 this->normalize();
39 return *this;
40 }
41
42 bool BitSet::operator==(BitSet const &other) const noexcept
43 {
44 return this->_store == other._store;
45 }
46
47 bool BitSet::operator!=(BitSet const &other) const noexcept
48 {
49 return this->_store != other._store;
50 }
51
52 BitSet &BitSet::flip() noexcept
53 {
54 for (auto &word : this->_store)
55 word = ~word;
56 this->normalize();
57 return *this;
58 }
59
61 {
62 BitSet copy(*this);
63
64 copy.flip();
65 return copy;
66 }
67
68 BitSet BitSet::operator&(BitSet const &other) const
69 {
70 BitSet result(std::min(this->_size, other._size));
71
72 for (std::size_t i = 0, s = result._store.size(); i < s; ++i)
73 result._store[i] = this->_store[i] & other._store[i];
74 result.normalize();
75 return result;
76 }
77
78 BitSet BitSet::operator|(BitSet const &other) const
79 {
80 BitSet result(std::min(this->_size, other._size));
81
82 for (std::size_t i = 0, s = result._store.size(); i < s; ++i)
83 result._store[i] = this->_store[i] | other._store[i];
84 result.normalize();
85 return result;
86 }
87
88 BitSet BitSet::operator^(BitSet const &other) const
89 {
90 BitSet result(std::min(this->_size, other._size));
91
92 for (std::size_t i = 0, s = result._store.size(); i < s; ++i)
93 result._store[i] = this->_store[i] ^ other._store[i];
94 result.normalize();
95 return result;
96 }
97
98 BitSet &BitSet::operator&=(BitSet const &other) noexcept
99 {
100 this->_resize(std::min(this->_size, other._size));
101 for (std::size_t i = 0, s = this->_store.size(); i < s; ++i)
102 this->_store[i] &= other._store[i];
103 this->normalize();
104 return *this;
105 }
106
107 BitSet &BitSet::operator|=(BitSet const &other) noexcept
108 {
109 this->_resize(std::min(this->_size, other._size));
110 for (std::size_t i = 0, s = this->_store.size(); i < s; ++i)
111 this->_store[i] |= other._store[i];
112 this->normalize();
113 return *this;
114 }
115
116 BitSet &BitSet::operator^=(BitSet const &other) noexcept
117 {
118 this->_resize(std::min(this->_size, other._size));
119 for (std::size_t i = 0, s = this->_store.size(); i < s; ++i)
120 this->_store[i] ^= other._store[i];
121 this->normalize();
122 return *this;
123 }
124
125 BitSet &BitSet::inplaceAnd(BitSet const &other) noexcept
126 {
127 std::size_t s = std::min(this->_store.size(), other._store.size());
128
129 for (std::size_t i = 0; i + 1 < s; ++i)
130 this->_store[i] &= other._store[i];
131 this->_store[s - 1] &= other._store[s - 1] | (~std::uint64_t(0) << (other.size() & 0b111111));
132 this->normalize();
133 return *this;
134 }
135
136 BitSet &BitSet::inplaceOr(BitSet const &other) noexcept
137 {
138 for (std::size_t i = 0, s = std::min(this->_store.size(), other._store.size()); i < s; ++i)
139 this->_store[i] |= other._store[i];
140 this->normalize();
141 return *this;
142 }
143
144 BitSet &BitSet::inplaceXor(BitSet const &other) noexcept
145 {
146 for (std::size_t i = 0, s = std::min(this->_store.size(), other._store.size()); i < s; ++i)
147 this->_store[i] ^= other._store[i];
148 this->normalize();
149 return *this;
150 }
151
152 void BitSet::normalize() noexcept
153 {
154 if ((this->_size & 0b111111) != 0) {
155 std::uint64_t &last = *this->_store.rbegin();
156 uint64_t mask = ~((~std::uint64_t(0)) << (this->_size & 0b111111));
157 last &= mask;
158 }
159 }
160
162 {
163 this->_store.resize(getStoreWordCount(size));
164 this->_size = size;
165 }
166
167 void BitSet::resizeSentinel(std::size_t size, bool sentinelValue)
168 {
169 if (this->_size > 0)
170 this->set(this->_size - 1, !sentinelValue);
171 this->resize(size + 1);
172 this->set(this->_size - 1, sentinelValue);
173 }
174
175 BitSet &BitSet::push(bool value)
176 {
177 this->resize(this->_size + 1);
178 this->set(this->_size - 1, value);
179 return *this;
180 }
181
183 {
184 bool value = this->test(this->_size - 1);
185 this->resize(this->_size - 1);
186 return value;
187 }
188
190 {
191 for (std::size_t s = set.size(), i = s; i > 0; --i)
192 output << (set.test(i - 1) ? '1' : '0');
193 return output;
194 }
195} // namespace util
Mimics the API of std::bitset but with the dynamic properties of std::vector<bool>
Definition BitSet.hpp:35
BitSet operator|(BitSet const &other) const
Performs a bitwise OR with this set and other.
Definition BitSet.cpp:78
static constexpr std::size_t getStoreWordCount(std::size_t bitCount)
Definition BitSet.hpp:319
void resize(std::size_t size)
Changes the number of bits stored in this set.
Definition BitSet.hpp:199
BitSet & push(bool value)
Adds the given bit to the end of the set.
Definition BitSet.cpp:175
bool operator==(BitSet const &other) const noexcept
Definition BitSet.cpp:42
constexpr std::size_t size() const noexcept
Definition BitSet.hpp:87
BitSet operator^(BitSet const &other) const
Performs a bitwise XOR with this set and other.
Definition BitSet.cpp:88
BitSet & setAll(bool value=true) noexcept
Sets all bits to the given value.
Definition BitSet.cpp:34
BitSet(std::size_t initialSize=0)
Definition BitSet.cpp:24
static constexpr std::uint64_t mask(std::size_t pos)
Definition BitSet.hpp:313
BitSet & inplaceXor(BitSet const &other) noexcept
Performs a bitwise XOR with this set and other in place without resizing this.
Definition BitSet.cpp:144
BitSet & operator^=(BitSet const &other) noexcept
Performs a bitwise XOR with this set and other in place.
Definition BitSet.cpp:116
std::size_t _size
Definition BitSet.hpp:309
void resizeSentinel(std::size_t size, bool sentinelValue)
Changes the number of bits stored in this set, and sets the final bit past size to the value of senti...
Definition BitSet.cpp:167
void normalize() noexcept
Unset the trailing bits.
Definition BitSet.cpp:152
BitSet operator~() const
Definition BitSet.cpp:60
BitSet & operator|=(BitSet const &other) noexcept
Performs a bitwise OR with this set and other in place.
Definition BitSet.cpp:107
std::vector< std::uint64_t > _store
Definition BitSet.hpp:310
BitSet & inplaceAnd(BitSet const &other) noexcept
Performs a bitwise AND with this set and other in place without resizing this.
Definition BitSet.cpp:125
BitSet & inplaceOr(BitSet const &other) noexcept
Performs a bitwise OR with this set and other in place without resizing this.
Definition BitSet.cpp:136
bool operator!=(BitSet const &other) const noexcept
Definition BitSet.cpp:47
void _resize(std::size_t size)
Resize without normalizing.
Definition BitSet.cpp:161
BitSet operator&(BitSet const &other) const
Performs a bitwise AND with this set and other.
Definition BitSet.cpp:68
BIT_SET_CONSTEXPR BitSet & set(std::size_t pos, bool value=true) noexcept
Definition BitSet.hpp:111
bool pop()
Removes one bit from the end of the set.
Definition BitSet.cpp:182
BitSet & operator&=(BitSet const &other) noexcept
Performs a bitwise AND with this set and other in place.
Definition BitSet.cpp:98
BIT_SET_CONSTEXPR bool test(std::size_t pos) const noexcept
Definition BitSet.hpp:95
BitSet & flip() noexcept
Flips all the bits in the set.
Definition BitSet.cpp:52
T min(T... args)
Namespace regrouping helpers used by ecstasy but not specific to ecstasy.
Definition Queryable.hpp:21
std::ostream & operator<<(std::ostream &output, BitSet const &set)
Prints the contents of set into output.
Definition BitSet.cpp:189
T rbegin(T... args)
T resize(T... args)
T size(T... args)