26#ifndef DCCLBITSET20120424H 
   27#define DCCLBITSET20120424H 
   42class 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);
 
 
  375inline bool operator==(
const Bitset& a, 
const Bitset& b)
 
  377    return (a.size() == b.size()) && std::equal(a.begin(), a.end(), b.begin());
 
  380inline 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)
 
  395inline Bitset operator&(
const Bitset& b1, 
const Bitset& b2)
 
  402inline Bitset operator|(
const Bitset& b1, 
const Bitset& b2)
 
  409inline Bitset operator^(
const Bitset& b1, 
const Bitset& b2)
 
  416inline std::ostream& operator<<(std::ostream& os, 
const Bitset& b) { 
return (os << b.to_string()); }
 
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
 
size_t to_byte_string(char *buf, size_t max_len)
Generate a byte string representation of the Bitset, where each character represents 8 bits of the Bi...
 
Bitset & reset()
Set all bits false.
 
std::string to_string() const
Returns the value of the Bitset as a printable string, where each bit is represented by '1' or '0'....
 
void get_more_bits(size_type num_bits)
Retrieve more bits from the parent Bitset.
 
Bitset operator>>(size_type n) const
Right shift.
 
Bitset & operator>>=(size_type n)
Right shift in place.
 
Bitset operator<<(size_type n) const
Left shift.
 
Bitset & operator^=(const Bitset &rhs)
Logical XOR in place.
 
Bitset & append(const Bitset &bits)
Adds the bitset to the big end.
 
bool test(size_type n) const
Test a bit (return its value)
 
Bitset & flip(size_type n)
Flip (toggle) a bit.
 
Bitset & operator<<=(size_type n)
Left shift in place.
 
IntType to() const
Returns the value of the Bitset as a integer.
 
Bitset & operator|=(const Bitset &rhs)
Logical OR in place.
 
Bitset & flip()
Flip (toggle) all bits.
 
void from_ulong(unsigned long value, size_type num_bits=std::numeric_limits< unsigned long >::digits)
Sets value of the Bitset to the contents of an unsigned long integer. Equivalent to from<unsigned lon...
 
unsigned long to_ulong() const
Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>().
 
Bitset(Bitset *parent=nullptr)
Construct an empty Bitset.
 
Bitset & set()
Set all bits true.
 
void from_byte_string(const std::string &s)
Sets the value of the Bitset to the contents of a byte string, where each character represents 8 bits...
 
Bitset & operator&=(const Bitset &rhs)
Logical AND in place.
 
Bitset(size_type num_bits, unsigned long value=0, Bitset *parent=nullptr)
Construct a Bitset of a certain initial size and value.
 
std::string to_byte_string()
Returns the value of the Bitset to a byte string, where each character represents 8 bits of the Bitse...
 
Bitset & reset(size_type n)
Reset a bit (i.e. set it to false)
 
Bitset & set(size_type n, bool val=true)
Set a bit to a given value.
 
Bitset & prepend(const Bitset &bits)
Adds the bitset to the little end.
 
void from_byte_stream(CharIterator begin, CharIterator end)
Sets the value of the Bitset to the contents of a byte string, where each character represents 8 bits...
 
void from(IntType value, size_type num_bits=std::numeric_limits< IntType >::digits)
Sets value of the Bitset to the contents of an integer.
 
Exception class for DCCL.
 
Dynamic Compact Control Language namespace.