Class for representing square free monomials, or subsets of integers.
Similar to a C++ bitset
except that its size does not need to be
fixed at compile time (hence the adjective dynamic).
DynamicBitset(long n=0)
DynamicBitset(ConstRefPPMonoidElem pp)
DynamicBitset(const DynamicBitset&)
unsigned long size(const DynamicBitset& b) { return b.mySize(); }
std::ostream& operator<<(std::ostream&, const DynamicBitset&);
With sanity checks:
DynamicBitset operator|(const DynamicBitset&, const DynamicBitset&);
-- bitwise or
DynamicBitset operator&(const DynamicBitset&, const DynamicBitset&);
-- bitwise and
DynamicBitset operator-(const DynamicBitset&, const DynamicBitset&);
-- bitwise diff
DynamicBitset operator^(const DynamicBitset&, const DynamicBitset&);
-- bitwise xor
bool IsSubset(const DynamicBitset&, const DynamicBitset&);
bool IsDisjoint(const DynamicBitset&, const DynamicBitset&);
bool Is1At(const DynamicBitset&, std::size_t index);
PPMonoidElem NewPP(const PPMonoid&, const DynamicBitset&);
unsigned int mySize() const;
-- number of bits
bool IamAll0s() const;
-- *this = [00000...0000]
bool IamAll1s() const;
-- *this = [11111...1111]
Without sanity checks:
DynamicBitset& mySet(std::size_t index, bool val);
-- *this[index] = val
DynamicBitset& mySet(std::size_t index);
-- *this[index] = true``
DynamicBitset& operator=(const DynamicBitset& rhs);
DynamicBitset& operator&=(const DynamicBitset& rhs);
-- and
DynamicBitset& operator|=(const DynamicBitset& rhs);
-- or
DynamicBitset& operator^=(const DynamicBitset& rhs);
-- xor
DynamicBitset& operator-=(const DynamicBitset& rhs);
-- diff
bool Iam1At(std::size_t index) const;
-- *this[index] == 1
bool operator<(const DynamicBitset& rhs) const;
-- wrt Xel
bool IamSubset(const DynamicBitset& rhs) const;
-- subset of rhs
bool IamDisjoint(const DynamicBitset& rhs) const;
bool operator==(const DynamicBitset& rhs) const {return myVec==rhs.myVec;}
bool operator!=(const DynamicBitset& rhs) const {return myVec!=rhs.myVec;}
Default printing style is clean
, i.e. as an STL bitset of the same
size. Printing style can be changed by setting the variable
DynamicBitset::ourOutputStyle
(see ex-DynamicBitset1.C).
Member functions
void myOutputSelf(std::ostream& out) const;
-- as a bitset of same size
void myOutputSelf8(std::ostream& out) const;
-- blocks of 8/ourNumBitsInBlock, for readability
void myOutputSelfLong(std::ostream& out) const;
-- as reversed vector<unsigned long>
Member fields (private)
std::vector<BitBlock> |
myVec; |
unsigned long |
mySizeValue; |
The size_t
constant DynamicBitset::ourNumBitsInBlock
stores number of bits of an unsigned long
(normally 32 or 64).
So a DynamicBitset
stores a STL vector of STL bitsets of
(constant) size ourNumBitsInBlock
called myVec
.
The field mySizeValue
is the number of bits we intend to use.
(e.g. in a 32 bit machine a DynamicBitset
of size 60 is stored as
a vector with 2 BitBlock
s and will have 4 unused bits)
enum OutputStyle {clean, AsRevVecOfLong, WithSeparators};
Member functions (private)
void myResize(std::size_t n);
-- only for ctors
unsigned int myVecSize() const;
-- number of BitBlock
s in vector
This class is needed because C++ bitset
length has to be fixed at
compile time. There is a class in boost named dynamic_bitset
:
if/when we decide CoCoALib inlude boost DynamicBitset
will just
call the boost implementation.
DynamicBitset
s, unlike boost's dynamic_bitset
s, are not
stretchable: the resize function is private.
They are used to represent square-free power-products, therefore
changing size does not make sense. But there is no technical reason
to forbid it, so we might make it available.
2010
facet
from "TmpIsTree`` into
DynamicBitset.H,C
(and renamed).
Rearranged and changed names for similarity with bitsets in STL and
boost. Stuctured in safe or fast functions according to
coding conventions. Test and example.