22 #ifndef DCCLBITSET20120424H 23 #define DCCLBITSET20120424H 31 #include "exception.h" 38 class Bitset :
public std::deque<bool>
53 explicit Bitset(size_type num_bits,
unsigned long value = 0,
Bitset* parent = 0)
54 :
std::deque<
bool>(num_bits, false),
56 {
from(value, num_bits); }
76 if(rhs.size() != size())
77 throw(
dccl::Exception(
"Bitset operator&= requires this->size() == rhs.size()"));
79 for (size_type i = 0; i != this->size(); ++i)
80 (*
this)[i] = (*this)[i] & rhs[i];
92 if(rhs.size() != size())
93 throw(
dccl::Exception(
"Bitset operator|= requires this->size() == rhs.size()"));
95 for (size_type i = 0; i != this->size(); ++i)
96 (*
this)[i] = (*this)[i] | rhs[i];
108 if(rhs.size() != size())
109 throw(
dccl::Exception(
"Bitset operator^= requires this->size() == rhs.size()"));
111 for (size_type i = 0; i != this->size(); ++i)
112 (*
this)[i] = (*this)[i] ^ rhs[i];
127 for(size_type i = 0; i < n; ++i)
143 for(size_type i = 0; i < n; ++i)
176 Bitset&
set(size_type n,
bool val =
true)
187 for(iterator it = begin(), n = end(); it != n; ++it)
197 {
return set(n,
false);}
204 for(iterator it = begin(), n = end(); it != n; ++it)
214 {
return set(n, !(*this)[n]); }
221 for(size_type i = 0, n = size(); i < n; ++i)
231 {
return (*
this)[n]; }
244 template<
typename IntType>
245 void from(IntType value, size_type num_bits = std::numeric_limits<IntType>::digits)
247 this->resize(num_bits);
248 for(
int i = 0, n = std::min<size_type>(std::numeric_limits<IntType>::digits, size());
251 if(value & (static_cast<IntType>(1) << i))
257 void from_ulong(
unsigned long value, size_type num_bits = std::numeric_limits<unsigned long>::digits)
259 from<unsigned long>(value, num_bits);
266 template<
typename IntType>
269 if(size() > static_cast<size_type>(std::numeric_limits<IntType>::digits))
270 throw(
Exception(
"Type IntType cannot represent current bitset (this->size() > std::numeric_limits<IntType>::digits)"));
273 for(
int i = 0, n = size(); i < n; ++i)
276 out |= (
static_cast<IntType
>(1) << i);
286 return to<unsigned long>();
292 std::string s(size(), 0);
294 for(Bitset::const_reverse_iterator it = rbegin(),
295 n = rend(); it != n; ++it)
297 s[i] = (*it) ?
'1' :
'0';
313 std::string s(this->size()/8 + (this->size()%8 ? 1 : 0), 0);
315 for(size_type i = 0, n = this->size(); i < n; ++i)
316 s[i/8] |= static_cast<char>((*
this)[i] << (i%8));
329 size_t len = this->size()/8 + (this->size()%8 ? 1 : 0);
333 throw std::length_error(
"max_len must be >= len");
337 std::fill_n(buf, len, 0);
339 for(size_type i = 0, n = this->size(); i < n; ++i)
340 buf[i/8] |= static_cast<char>((*
this)[i] << (i%8));
357 template<
typename CharIterator>
360 this->resize(std::distance(begin, end) * 8);
362 for(CharIterator it = begin; it != end; ++it)
364 for(size_type j = 0; j < 8; ++j)
365 (*
this)[i*8+j] = (*it) & (1 << j);
373 for(const_reverse_iterator it = bits.rbegin(),
374 n = bits.rend(); it != n; ++it)
383 for(const_iterator it = bits.begin(),
384 n = bits.end(); it != n; ++it)
392 Bitset relinquish_bits(size_type num_bits,
bool final_child);
400 inline bool operator==(
const Bitset& a,
const Bitset& b)
402 return (a.size() == b.size()) && std::equal(a.begin(), a.end(), b.begin());
407 for(
int i = (std::max(a.size(), b.size()) - 1); i >= 0; --i)
409 bool a_bit = (i < static_cast<int>(a.size())) ? a[i] : 0;
410 bool b_bit = (i < static_cast<int>(b.size())) ? b[i] : 0;
412 if(a_bit > b_bit)
return false;
413 else if(a_bit < b_bit)
return true;
449 relinquish_bits(num_bits,
true);
Bitset & flip(size_type n)
Flip (toggle) a bit.
Bitset & append(const Bitset &bits)
Adds the bitset to the big end.
void from(IntType value, size_type num_bits=std::numeric_limits< IntType >::digits)
Sets value of the Bitset to the contents of an integer.
bool test(size_type n) const
Test a bit (return its value)
unsigned long to_ulong() const
Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>()...
Bitset operator<<(size_type n) const
Left shift.
std::string to_string() const
Returns the value of the Bitset as a printable string, where each bit is represented by '1' or '0'...
Bitset & operator&=(const Bitset &rhs)
Logical AND in place.
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...
Bitset & operator|=(const Bitset &rhs)
Logical OR in place.
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...
Bitset & reset(size_type n)
Reset a bit (i.e. set it to false)
Bitset & operator^=(const Bitset &rhs)
Logical XOR in place.
Bitset & operator>>=(size_type n)
Right shift in place.
Bitset(Bitset *parent=0)
Construct an empty Bitset.
Bitset operator>>(size_type n) const
Right shift.
void get_more_bits(size_type num_bits)
Retrieve more bits from the parent Bitset.
Bitset & prepend(const Bitset &bits)
Adds the bitset to the little end.
Dynamic Compact Control Language namespace.
Bitset & operator<<=(size_type n)
Left shift in place.
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...
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 & flip()
Flip (toggle) all bits.
Exception class for DCCL.
IntType to() const
Returns the value of the Bitset as a integer.
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(size_type num_bits, unsigned long value=0, Bitset *parent=0)
Construct a Bitset of a certain initial size and value.
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy...
Bitset & reset()
Set all bits false.