#include <rw/bitvec.h> RWBitVec v;
Class RWBitVec is a bitvector whose length can be changed at run time. Because this requires an extra level of indirection, this makes it slightly less efficient than classes RWGBitVec(size) or RWTBitVec<size>, whose lengths are fixed at compile time.
Simple
#include <rw/bitvec.h> #include <rw/rstream.h> main(){ // Allocate a vector with 20 bits, set to TRUE: RWBitVec av(20, TRUE); av(0) = FALSE; // Turn bit 0 off av.clearBit(7); // Turn bit 7 off av.setBit(2); // Turn bit 2 back on for(int i=11; i<=14; i++) av(i) = FALSE; cout << av << endl; // Print the vector out }
Program output:
[ 0 1 1 1 1 1 1 0 1 1 1 0 0 0 0 1 1 1 1 1 ]
RWBitVec();
Construct a zero lengthed (null) vector.
RWBitVec(size_t N);
Construct a vector with N bits. The initial value of the bits is undefined.
RWBitVec(size_t N, RWBoolean initVal);
Construct a vector with N bits, each set to the Boolean value initVal.
RWBitVec(const RWByte* bp, size_t N);
Construct a vector with N bits, initialized to the data in the array of bytes pointed to by bp. This array must be at least long enough to contain N bits. The identifier RWByte is a typedef for an unsigned char.
RWBitVec(const RWBitVec& v);
Copy constructor. Uses value semantics -- the constructed vector will be a copy of v.
~RWBitVec();
The destructor. Releases any allocated memory.
RWBitVec& operator=(const RWBitVec& v);
Assignment operator. Value semantics are used -- self will be a copy of v.
RWBitVec& operator=(RWBoolean b);
Assignment operator. Sets every bit in self to the boolean value b.
RWBitVec& operator&=(const RWBitVec& v); RWBitVec& operator^=(const RWBitVec& v); RWBitVec& operator|=(const RWBitVec& v);
Logical assignments. Set each element of self to the logical AND, XOR, or OR, respectively, of self and the corresponding bit in v. Self and v must have the same number of elements (i.e., be conformal) or an exception of type RWInternalErr will occur.
RWBitRef operator[](size_t i);
Returns a reference to bit i of self. A helper class, RWBitRef, is used. The result can be used as an lvalue. The index i must be between 0 and the length of the vector less one. Bounds checking is performed. If the index is out of range, then an exception of type RWBoundsErr will occur.
RWBitRef operator()(size_t i);
Returns a reference to bit i of self. A helper class, RWBitRef, is used. The result can be used as an lvalue. The index i must be between 0 and the length of the vector less one. Bounds checking is performed only if the preprocessor macro RWBOUNDS_CHECK has been defined before including the header file <rw/bitvec.h>. If so, and if the index is out of range, then an exception of type RWBoundsErr will occur.
RWBoolean operator[](size_t i) const;
Returns the boolean value of bit i. The result cannot be used as an lvalue. The index i must be between 0 and the length of the vector less one. Bounds checking is performed. If the index is out of range, then an exception of type RWBoundsErr will occur.
RWBoolean operator()(size_t i) const;
Returns the boolean value of bit i. The result cannot be used as an lvalue. The index i must be between 0 and the length of the vector less one. Bounds checking is performed only if the preprocessor macro RWBOUNDS_CHECK has been defined before including the header file <rw/bitvec.h>. If so, and if the index is out of range, then an exception of type RWBoundsErr will occur.
RWBoolean operator==(const RWBitVec& u) const;
Returns TRUE if self and v have the same length and if each bit of self is set to the same value as the corresponding bit in v. Otherwise, returns FALSE.
RWBoolean operator!=(const RWBitVec& u) const;
Returns FALSE if self and v have the same length and if each bit of self is set to the same value as the corresponding bit in v. Otherwise, returns TRUE.
RWBoolean operator==(RWBoolean b) const;
Returns TRUE if every bit of self is set to the boolean value b. Otherwise FALSE.
RWBoolean operator!=(RWBoolean b) const;
Returns FALSE if every bit of self is set to the boolean value b. Otherwise TRUE.
void clearBit(size_t i);
Clears (i.e., sets to FALSE) the bit with index i. The index i must be between 0 and the length of the vector less one. No bounds checking is performed. The following are equivalent, although clearBit(size_t) is slightly smaller and faster than using operator()(size_t):
a(i) = FALSE; a.clearBit(i);
const RWByte* data() const;
Returns a const pointer to the raw data of self. Should be used with care.
size_t firstFalse() const;
Returns the index of the first FALSE bit in self. Returns RW_NPOS if there is no FALSE bit.
size_t firstTrue() const;
Returns the index of the first TRUE bit in self. Returns RW_NPOS if there is no TRUE bit.
unsigned hash() const;
Returns a value suitable for hashing.
RWBoolean isEqual(const RWBitVec& v) const;
Returns TRUE if self and v have the same length and if each bit of self is set to the same value as the corresponding bit in v. Otherwise, returns FALSE.
size_t length() const;
Returns the number of bits in the vector.
ostream& printOn(ostream& s) const;
Print the vector v on the output stream s. See the example above for a sample of the format.
void resize(size_t N);
Resizes the vector to have length N. If this results in a lengthening of the vector, the additional bits will be set to FALSE.
istream& scanFrom(istream&);
Read the bit vector from the input stream s. The vector will dynamically be resized as necessary. The vector should be in the same format printed by member function printOn(ostream&).
void setBit(size_t i);
Sets (i.e., sets to TRUE) the bit with index i. The index i must be between 0 and size-1. No bounds checking is performed. The following are equivalent, although setBit(size_t) is slightly smaller and faster than using operator()(size_t):
a(i) = TRUE; a.setBit(i);
RWBoolean testBit(size_t i) const;
Tests the bit with index i. The index i must be between 0 and size-1. No bounds checking is performed. The following are equivalent, although testBit(size_t) is slightly smaller and faster than using operator()(size_t):
if( a(i) ) doSomething(); if( a.testBit(i) ) doSomething();
RWBitVec operator!(const RWBitVec& v);
Unary operator that returns the logical negation of vector v.
RWBitVec operator&(const RWBitVec&,const RWBitVec&); RWBitVec operator^(const RWBitVec&,const RWBitVec&); RWBitVec operator|(const RWBitVec&,const RWBitVec&);
Returns a vector that is the logical AND, XOR, or OR of the vectors v1 and v2. The two vectors must have the same length or an exception of type RWInternalErr will occur.
ostream& operator<<(ostream& s, const RWBitVec& v);
Calls v.printOn(s).
istream& operator>>(istream& s, RWBitVec& v);
Calls v.scanFrom(s).
RWvostream& operator<<(RWvostream&, const RWBitVec& vec); RWFile& operator<<(RWFile&, const RWBitVec& vec);
Saves the RWBitVec vec to a virtual stream or RWFile, respectively.
RWvistream& operator>>(RWvistream&, RWBitVec& vec); RWFile& operator>>(RWFile&, RWBitVec& vec);
Restores an RWBitVec into vec from a virtual stream or RWFile, respectively, replacing the previous contents of vec.
size_t sum(const RWBitVec& v);
Returns the total number of bits set in the vector v.