26 #ifndef DCCLBITSET20120424H
27 #define DCCLBITSET20120424H
35 #include "exception.h"
42 class Bitset :
public std::deque<bool>
55 explicit Bitset(size_type num_bits,
unsigned long value = 0,
Bitset* parent =
nullptr)
56 : std::deque<
bool>(num_bits, false), parent_(parent)
58 from(value, num_bits);
79 if (rhs.size() != size())
80 throw(
dccl::Exception(
"Bitset operator&= requires this->size() == rhs.size()"));
82 for (size_type i = 0; i != this->size(); ++i) (*
this)[i] &= rhs[i];
94 if (rhs.size() != size())
95 throw(
dccl::Exception(
"Bitset operator|= requires this->size() == rhs.size()"));
97 for (size_type i = 0; i != this->size(); ++i) (*
this)[i] |= rhs[i];
109 if (rhs.size() != size())
110 throw(
dccl::Exception(
"Bitset operator^= requires this->size() == rhs.size()"));
112 for (size_type i = 0; i != this->size(); ++i) (*
this)[i] ^= rhs[i];
126 for (size_type i = 0; i < n; ++i)
142 for (size_type i = 0; i < n; ++i)
186 for (
bool& it : *
this) it =
true;
201 for (
bool& it : *
this) it =
false;
216 for (size_type i = 0, n = size(); i < n; ++i)
flip(i);
224 bool test(size_type n)
const {
return (*
this)[n]; }
235 template <
typename IntType>
236 void from(IntType value, size_type num_bits = std::numeric_limits<IntType>::digits)
238 this->resize(num_bits);
239 for (
int i = 0, n = std::min<size_type>(std::numeric_limits<IntType>::digits, size());
242 if (value & (
static_cast<IntType
>(1) << i))
249 size_type num_bits = std::numeric_limits<unsigned long>::digits)
251 from<unsigned long>(value, num_bits);
258 template <
typename IntType> IntType
to()
const
260 if (size() >
static_cast<size_type
>(std::numeric_limits<IntType>::digits))
261 throw(
Exception(
"Type IntType cannot represent current bitset (this->size() > "
262 "std::numeric_limits<IntType>::digits)"));
265 for (
int i = 0, n = size(); i < n; ++i)
268 out |= (
static_cast<IntType
>(1) << i);
275 unsigned long to_ulong()
const {
return to<unsigned long>(); }
280 std::string s(size(), 0);
282 for (
auto it = rbegin(), n = rend(); it != n; ++it)
284 s[i] = (*it) ?
'1' :
'0';
300 std::string s(this->size() / 8 + (this->size() % 8 ? 1 : 0), 0);
302 for (size_type i = 0, n = this->size(); i < n; ++i)
303 s[i / 8] |=
static_cast<char>((*
this)[i] << (i % 8));
316 size_t len = this->size() / 8 + (this->size() % 8 ? 1 : 0);
320 throw std::length_error(
"max_len must be >= len");
324 std::fill_n(buf, len, 0);
326 for (size_type i = 0, n = this->size(); i < n; ++i)
327 buf[i / 8] |=
static_cast<char>((*
this)[i] << (i % 8));
341 template <
typename CharIterator>
void from_byte_stream(CharIterator begin, CharIterator end)
343 this->resize(std::distance(begin, end) * 8);
345 for (CharIterator it = begin; it != end; ++it)
347 for (size_type j = 0; j < 8; ++j) (*
this)[i * 8 + j] = (*it) & (1 << j);
355 for (
auto it = bits.rbegin(), n = bits.rend(); it != n; ++it) push_front(*it);
363 for (
bool bit : bits) push_back(bit);
369 Bitset relinquish_bits(size_type num_bits,
bool final_child);
375 inline bool operator==(
const Bitset& a,
const Bitset& b)
377 return (a.size() == b.size()) && std::equal(a.begin(), a.end(), b.begin());
380 inline bool operator<(
const Bitset& a,
const Bitset& b)
382 for (
int i = (std::max(a.size(), b.size()) - 1); i >= 0; --i)
384 bool a_bit = (i < static_cast<int>(a.size())) ? a[i] : 0;
385 bool b_bit = (i < static_cast<int>(b.size())) ? b[i] : 0;
389 else if (a_bit < b_bit)
395 inline Bitset operator&(
const Bitset& b1,
const Bitset& b2)
402 inline Bitset operator|(
const Bitset& b1,
const Bitset& b2)
409 inline Bitset operator^(
const Bitset& b1,
const Bitset& b2)
416 inline std::ostream& operator<<(std::ostream& os,
const Bitset& b) {
return (os << b.to_string()); }