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)
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());
405 inline bool operator<(
const Bitset& a,
const Bitset& b)
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;
418 inline Bitset operator&(
const Bitset& b1,
const Bitset& b2)
425 inline Bitset operator|(
const Bitset& b1,
const Bitset& b2)
432 inline Bitset operator^(
const Bitset& b1,
const Bitset& b2)
440 inline std::ostream& operator<<(std::ostream& os,
const Bitset& b)
442 return (os << b.to_string());
449 relinquish_bits(num_bits,
true);