dynamic_bitset
dynamic_bitset.hpp
Go to the documentation of this file.
1 
2 
3 #ifndef DYNAMIC_BITSET_H_
4 #define DYNAMIC_BITSET_H_
5 #include <algorithm>
6 #include <cmath>
7 #include <cstring>
8 #include <initializer_list>
9 #include <ostream>
10 #include <vector>
11 
24 template <std::size_t N = 0> class dynamic_bitset {
25 public:
30  dynamic_bitset() : bits_(N) {}
31 
37  dynamic_bitset(int value) : bits_(N) {
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);
42  }
43 
49  dynamic_bitset(std::vector<bool> binaries) : bits_(N) {
50  int number_of_padding = bits_.size() - binaries.size();
51  add_padding(binaries, number_of_padding);
52  bits_ = std::move(binaries);
53  }
54 
62  dynamic_bitset(std::initializer_list<bool> binaries) : bits_(N) {
63  int number_of_padding = bits_.size() - binaries.size();
64  bits_ = std::move(binaries);
65  add_padding(bits_, number_of_padding);
66  }
67 
75  dynamic_bitset(const std::string &binary_string) : bits_(N) {
76  bits_ = std::move(string_to_bitset(binary_string));
77  }
78 
86  dynamic_bitset(const char *binary) : bits_(N) {
87  int number_of_padding = bits_.size() - strlen(binary);
88  std::vector<bool> binaries;
89 
90  while (*binary != '\0')
91  binaries.emplace_back(*binary++ == '1');
92 
93  add_padding(binaries, number_of_padding);
94  bits_ = std::move(binaries);
95  }
96 
101 
105  dynamic_bitset(const dynamic_bitset &) = delete;
106 
111 
116  dynamic_bitset(dynamic_bitset &&other) : bits_(std::move(other.bits_)) {}
117 
124  if (this != &other) {
125  bits_ = std::move(other.bits_);
126  }
127  return *this;
128  }
129 
136  dynamic_bitset &operator=(const std::vector<bool> &binaries) {
137  int number_of_padding = bits_.size() - binaries.size();
138  bits_ = std::move(binaries);
139  add_padding(bits_, number_of_padding);
140  return *this;
141  }
142 
149  std::reverse(std::begin(bits_), std::end(bits_));
150  return *this;
151  }
152 
158  const std::vector<bool> &get() const { return bits_; }
159 
165  bool operator[](std::size_t index) const { return bits_.at(index); }
166 
171  bool all() const {
172  return std::all_of(std::begin(bits_), std::end(bits_),
173  [](bool b) { return b; });
174  }
175 
180  bool any() const {
181  return std::any_of(std::begin(bits_), std::end(bits_),
182  [](bool b) { return b; });
183  }
184 
189  bool none() const {
190  return std::none_of(std::begin(bits_), std::end(bits_),
191  [](bool b) { return b; });
192  }
193 
198  inline std::vector<bool>::reference operator[](std::size_t index) {
199  return bits_.at(index);
200  }
201 
206  constexpr std::size_t size() const { return bits_.size(); }
207 
212  dynamic_bitset &set(bool value) {
213  std::fill(std::begin(bits_), std::end(bits_), value);
214  return *this;
215  }
216 
221  inline dynamic_bitset &reset() { return set(false); }
222 
227  std::string to_string() const {
228  std::string str;
229  for (const auto &bit : bits_)
230  str.push_back(bit == true ? '1' : '0');
231 
232  return str;
233  }
234 
239  operator std::string() const { return to_string(); }
240 
246  std::size_t to_ulong() const {
247  std::size_t value = 0;
248  std::size_t power = 1;
249  for (auto it = bits_.rbegin(); it != bits_.rend(); ++it) {
250  if (*it)
251  value += power;
252  power *= 2;
253  }
254  return value;
255  }
256 
261  operator std::size_t() const { return to_ulong(); }
262 
267  operator unsigned long const() {
268  return static_cast<unsigned long>(to_ulong());
269  }
270 
276  operator unsigned long long const() {
277  return static_cast<unsigned long>(to_ulong());
278  }
279 
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]);
290  return dynamic_bitset(set);
291  }
292 
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];
302  return *this;
303  }
304 
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]);
314  return dynamic_bitset(set);
315  }
316 
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];
327  return *this;
328  }
329 
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]);
339  return dynamic_bitset(set);
340  }
341 
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];
351  return *this;
352  }
353 
359  dynamic_bitset &operator<<=(std::size_t shift_amount) {
360  if (shift_amount >= bits_.size())
361  bits_.assign(bits_.size(), false);
362  else {
363  // shift all bits to left
364  for (std::size_t i = 0; i < bits_.size(); ++i)
365  bits_[i] = bits_[i + shift_amount];
366  // fill shifted position
367  for (std::size_t i = bits_.size() - shift_amount; i < bits_.size(); ++i)
368  bits_[i] = false;
369  }
370  return *this;
371  }
372 
378  dynamic_bitset &operator>>=(std::size_t shift_amount) {
379  if (shift_amount >= bits_.size())
380  bits_.assign(bits_.size(), false);
381  else {
382  // shift all bits to right
383  for (std::size_t i = bits_.size() - 1; i >= shift_amount; --i)
384  bits_[i] = bits_[i - shift_amount];
385  // fill shifted position
386  for (std::size_t i = 0; i < shift_amount; ++i)
387  bits_[i] = false;
388  }
389  return *this;
390  }
391 
396  std::vector<bool>::iterator begin() { return bits_.begin(); }
397 
402  std::vector<bool>::iterator end() { return bits_.end(); }
403 
409  friend std::ostream &operator<<(std::ostream &out,
410  const dynamic_bitset &set) {
411  for (const auto &bit : set.bits_) {
412  out << bit;
413  }
414  return out;
415  }
416 
422  friend std::istream &operator>>(std::istream &input, dynamic_bitset &set) {
423  std::string str;
424  input >> str;
425  set.bits_ = std::move(set.string_to_bitset(str));
426  return input;
427  }
428 
429 private:
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);
441  return binaries;
442  }
443 
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);
451  }
452 
457  std::vector<bool> int_to_binary(int value) {
458  std::vector<bool> temp;
459  while (value > 0) {
460  temp.insert(temp.begin(), static_cast<bool>(value % 2));
461  value /= 2;
462  }
463  if (temp.empty())
464  temp.emplace_back(value);
465  return temp;
466  }
467 
468  std::vector<bool> bits_;
469 };
470 
471 #endif
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.