3 #ifndef DYNAMIC_BITSET_H_
4 #define DYNAMIC_BITSET_H_
8 #include <initializer_list>
38 auto binary = int_to_binary(value);
39 int number_of_padding = bits_.size() - binary.size();
40 add_padding(binary, number_of_padding);
41 bits_ = std::move(binary);
50 int number_of_padding = bits_.size() - binaries.size();
51 add_padding(binaries, number_of_padding);
52 bits_ = std::move(binaries);
63 int number_of_padding = bits_.size() - binaries.size();
64 bits_ = std::move(binaries);
65 add_padding(bits_, number_of_padding);
76 bits_ = std::move(string_to_bitset(binary_string));
87 int number_of_padding = bits_.size() - strlen(binary);
88 std::vector<bool> binaries;
90 while (*binary !=
'\0')
91 binaries.emplace_back(*binary++ ==
'1');
93 add_padding(binaries, number_of_padding);
94 bits_ = std::move(binaries);
124 if (
this != &other) {
125 bits_ = std::move(other.bits_);
137 int number_of_padding = bits_.size() - binaries.size();
138 bits_ = std::move(binaries);
139 add_padding(bits_, number_of_padding);
149 std::reverse(std::begin(bits_), std::end(bits_));
158 const std::vector<bool> &
get()
const {
return bits_; }
165 bool operator[](std::size_t index)
const {
return bits_.at(index); }
172 return std::all_of(std::begin(bits_), std::end(bits_),
173 [](
bool b) {
return b; });
181 return std::any_of(std::begin(bits_), std::end(bits_),
182 [](
bool b) {
return b; });
190 return std::none_of(std::begin(bits_), std::end(bits_),
191 [](
bool b) {
return b; });
198 inline std::vector<bool>::reference
operator[](std::size_t index) {
199 return bits_.at(index);
206 constexpr std::size_t
size()
const {
return bits_.size(); }
213 std::fill(std::begin(bits_), std::end(bits_), value);
229 for (
const auto &bit : bits_)
230 str.push_back(bit ==
true ?
'1' :
'0');
247 std::size_t value = 0;
248 std::size_t power = 1;
249 for (
auto it = bits_.rbegin(); it != bits_.rend(); ++it) {
261 operator std::size_t()
const {
return to_ulong(); }
267 operator unsigned long const() {
268 return static_cast<unsigned long>(
to_ulong());
276 operator unsigned long long const() {
277 return static_cast<unsigned long>(
to_ulong());
286 std::size_t
size = std::min(bits_.size(), other.
size());
287 std::vector<bool>
set;
288 for (
int i = 0; i <
size; ++i)
289 set.emplace_back(other[i] && bits_[i]);
299 std::size_t
size = std::min(bits_.size(), other.
size());
300 for (
int i = 0; i <
size; ++i)
301 bits_[i] = bits_[i] && other.bits_[i];
310 std::size_t
size = std::min(bits_.size(), other.
size());
311 std::vector<bool>
set;
312 for (
int i = 0; i <
size; ++i)
313 set.emplace_back(other[i] || bits_[i]);
323 std::size_t
size = std::min(bits_.size(), other.
size());
324 std::vector<bool>
set;
325 for (
int i = 0; i <
size; ++i)
326 bits_[i] = bits_[i] || other.bits_[i];
335 std::size_t
size = std::min(bits_.size(), other.
size());
336 std::vector<bool>
set;
337 for (
int i = 0; i <
size; ++i)
338 set.emplace_back(other[i] ^ bits_[i]);
347 std::size_t
size = std::min(bits_.size(), other.
size());
348 std::vector<bool>
set;
349 for (
int i = 0; i <
size; ++i)
350 bits_[i] = bits_[i] ^ other.bits_[i];
360 if (shift_amount >= bits_.size())
361 bits_.assign(bits_.size(),
false);
364 for (std::size_t i = 0; i < bits_.size(); ++i)
365 bits_[i] = bits_[i + shift_amount];
367 for (std::size_t i = bits_.size() - shift_amount; i < bits_.size(); ++i)
379 if (shift_amount >= bits_.size())
380 bits_.assign(bits_.size(),
false);
383 for (std::size_t i = bits_.size() - 1; i >= shift_amount; --i)
384 bits_[i] = bits_[i - shift_amount];
386 for (std::size_t i = 0; i < shift_amount; ++i)
396 std::vector<bool>::iterator
begin() {
return bits_.begin(); }
402 std::vector<bool>::iterator
end() {
return bits_.end(); }
411 for (
const auto &bit :
set.bits_) {
425 set.bits_ = std::move(
set.string_to_bitset(str));
435 std::vector<bool> string_to_bitset(
const std::string &binary_string) {
436 int number_of_padding = bits_.size() - binary_string.size();
437 std::vector<bool> binaries;
438 for (
const char &c : binary_string)
439 binaries.emplace_back(c ==
'1');
440 add_padding(binaries, number_of_padding);
448 void add_padding(std::vector<bool> &base,
int number_of_padding) {
449 for (
int i = 0; i < number_of_padding; ++i)
450 base.insert(base.begin(), 0);
457 std::vector<bool> int_to_binary(
int value) {
458 std::vector<bool> temp;
460 temp.insert(temp.begin(),
static_cast<bool>(value % 2));
464 temp.emplace_back(value);
468 std::vector<bool> bits_;
A dynamic bitset implementation that can be resized at runtime.
dynamic_bitset(dynamic_bitset &&other)
Move constructor for dynamic_bitset.
dynamic_bitset(const char *binary)
Constructor that initializes the bitset with a C-style string of binary digits.
dynamic_bitset(int value)
Constructor that initializes the bitset with a given integer value.
friend std::ostream & operator<<(std::ostream &out, const dynamic_bitset &set)
ostream operator to print dynamic_bitset
const std::vector< bool > & get() const
Get the underlying std::vector<bool> object.
bool none() const
Check if none of the bits in dynamic_bitset are true.
~dynamic_bitset()
Destructor for dynamic_bitset.
dynamic_bitset & operator&=(const dynamic_bitset &other)
and operator between two dynamic_bitset and change orginal set
dynamic_bitset & reverse()
Reverse the bits in dynamic_bitset.
bool all() const
Check if all bits in dynamic_bitset are true.
dynamic_bitset & reset()
Set all value to 0.
dynamic_bitset & operator>>=(std::size_t shift_amount)
Right Shift operator.
dynamic_bitset(std::vector< bool > binaries)
Constructor that initializes the bitset with a vector of bools.
std::size_t to_ulong() const
bitset to std::size_t
dynamic_bitset operator&(const dynamic_bitset &other) const
and operator between two dynamic_bitset
dynamic_bitset & operator=(const std::vector< bool > &binaries)
Assignment operator to assign a std::vector<bool> to dynamic_bitset.
dynamic_bitset & operator|=(const dynamic_bitset &other)
or operator between two dynamic_bitset and change orginal set
dynamic_bitset(const dynamic_bitset &)=delete
Delete the copy constructor for dynamic_bitset.
dynamic_bitset & operator<<=(std::size_t shift_amount)
Left Shift operator.
dynamic_bitset & operator=(const dynamic_bitset &)=delete
Delete the copy assignment operator for dynamic_bitset.
std::vector< bool >::reference operator[](std::size_t index)
Return reference bit on the given index.
dynamic_bitset & set(bool value)
Set all value of the bitset.
constexpr std::size_t size() const
Return size of the bitset.
std::vector< bool >::iterator end()
begin iterator
dynamic_bitset(const std::string &binary_string)
Constructor that initializes the bitset with a string of binary digits.
bool any() const
Check if any bit in dynamic_bitset is true.
std::vector< bool >::iterator begin()
begin iterator
dynamic_bitset & operator=(dynamic_bitset &&other)
Move assignment operator for dynamic_bitset.
dynamic_bitset operator^(const dynamic_bitset &other) const
xor operator between two dynamic_bitset
dynamic_bitset operator|(const dynamic_bitset &other) const
or operator between two dynamic_bitset
friend std::istream & operator>>(std::istream &input, dynamic_bitset &set)
istream operator to input of dynamic_bitset
bool operator[](std::size_t index) const
Get the value of a bit at a given index.
dynamic_bitset & operator^=(const dynamic_bitset &other)
xor operator between two dynamic_bitset and change orginal set
dynamic_bitset()
Default constructor that initializes the bitset with the default size.
std::string to_string() const
convert bitset to string bitset
dynamic_bitset(std::initializer_list< bool > binaries)
Constructor that initializes the bitset with an initializer list of bools.