SourcePro C++ 12.0 |
SourcePro® C++ API Reference Guide |
SourcePro C++ Documentation Home |
Represents a bit vector whose length can be changed at runtime. More...
#include <rw/bitvec.h>
Inherits RWMemoryPool.
Public Member Functions | |
RWBitVec () | |
RWBitVec (size_t N) | |
RWBitVec (size_t N, bool initVal) | |
RWBitVec (const RWByte *bp, size_t N) | |
RWBitVec (const RWBitVec &v) | |
RWBitVec (RWBitVec &&v) | |
~RWBitVec () | |
RWBitVec & | operator= (const RWBitVec &v) |
RWBitVec & | operator= (RWBitVec &&v) |
RWBitVec & | operator= (bool b) |
RWBitVec & | operator&= (const RWBitVec &v) |
RWBitVec & | operator^= (const RWBitVec &v) |
RWBitVec & | operator|= (const RWBitVec &v) |
RWBitRef | operator[] (size_t i) |
RWBitRef | operator() (size_t i) |
bool | operator[] (size_t i) const |
bool | operator() (size_t i) const |
bool | operator== (const RWBitVec &v) const |
bool | operator!= (const RWBitVec &v) const |
bool | operator== (bool b) const |
bool | operator!= (bool b) const |
void | clearBit (size_t i) |
const RWByte * | data () const |
size_t | firstFalse () const |
size_t | firstTrue () const |
unsigned | hash () const |
bool | isEqual (const RWBitVec &v) const |
size_t | length () const |
std::ostream & | printOn (std::ostream &s) const |
void | resize (size_t N) |
std::istream & | scanFrom (std::istream &s) |
void | setBit (size_t i) |
bool | testBit (size_t i) const |
void | swap (RWBitVec &v) |
Friends | |
RWBitVec | operator! (const RWBitVec &v) |
RWBitVec | operator& (const RWBitVec &v1, const RWBitVec &v2) |
RWBitVec | operator^ (const RWBitVec &v1, const RWBitVec &v2) |
RWBitVec | operator| (const RWBitVec &v1, const RWBitVec &v2) |
std::ostream & | operator<< (std::ostream &s, const RWBitVec &v) |
std::istream & | operator>> (std::istream &s, RWBitVec &v) |
size_t | sum (const RWBitVec &v) |
Related Functions | |
(Note that these are not member functions.) | |
RWvistream & | operator>> (RWvistream &str, RWBitVec &vec) |
RWFile & | operator>> (RWFile &file, RWBitVec &vec) |
RWvostream & | operator<< (RWvostream &str, const RWBitVec &vec) |
RWFile & | operator<< (RWFile &file, const RWBitVec &vec) |
Class RWBitVec is a bit vector whose length can be changed at runtime. Because this requires an extra level of indirection, this makes it slightly less efficient than RWTBitVec<Size> , whose lengths are fixed at compile time.
#include <rw/bitvec.h> RWBitVec v;
Simple
#include <rw/bitvec.h> #include <iostream> int main(){ // Allocate a vector with 20 bits, set to true: RWBitVec av(20, true); av(0) = false; // Turn bit 0 off av(2) = false; // Turn bit 2 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; std::cout << av << std::endl; // Print the vector out return 0; }
Program output:
[ 0 1 1 1 1 1 1 0 1 1 1 0 0 0 0 1 1 1 1 1 ]
RWBitVec::RWBitVec | ( | ) | [inline] |
Constructs a zero length (null) vector.
RWBitVec::RWBitVec | ( | size_t | N | ) |
Constructs a vector with N bits. The initial value of the bits is undefined.
RWBitVec::RWBitVec | ( | size_t | N, | |
bool | initVal | |||
) |
Constructs a vector with N bits, each set to the Boolean value initVal.
RWBitVec::RWBitVec | ( | const RWByte * | bp, | |
size_t | N | |||
) |
Constructs 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::RWBitVec | ( | const RWBitVec & | v | ) |
Copy constructor. Uses value semantics. The constructed vector is a copy of v.
RWBitVec::RWBitVec | ( | RWBitVec && | v | ) |
Move constructor. The constructed vector takes ownership of the data owned by v.
RWBitVec::~RWBitVec | ( | ) |
The destructor. Releases any allocated memory.
void RWBitVec::clearBit | ( | size_t | i | ) | [inline] |
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* RWBitVec::data | ( | ) | const [inline] |
Returns a const pointer to the raw data of self. Should be used with care.
size_t RWBitVec::firstFalse | ( | ) | const [inline] |
Returns the index of the first false
bit in self. Returns RW_NPOS if there is no false
bit.
size_t RWBitVec::firstTrue | ( | ) | const [inline] |
Returns the index of the first true
bit in self. Returns RW_NPOS if there is no true
bit.
unsigned RWBitVec::hash | ( | ) | const |
Returns a value suitable for hashing.
bool RWBitVec::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 RWBitVec::length | ( | ) | const [inline] |
Returns the number of bits in the vector.
bool RWBitVec::operator!= | ( | bool | b | ) | const [inline] |
Returns false
if every bit of self is set to the boolean value b. Otherwise, returns true
.
bool RWBitVec::operator!= | ( | const RWBitVec & | v | ) | const [inline] |
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
.
Logical assignment. Sets each element of self to the logical AND
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 is thrown.
bool RWBitVec::operator() | ( | size_t | i | ) | const [inline] |
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 is thrown.
RWBitRef RWBitVec::operator() | ( | size_t | i | ) | [inline] |
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 is thrown.
RWBitVec& RWBitVec::operator= | ( | bool | b | ) |
Assignment operator. Sets every bit in self to the boolean value b.
Move assignment. Self takes ownership of the data owned by v.
Assignment operator. Uses value semantics. Self is a copy of v.
bool RWBitVec::operator== | ( | bool | b | ) | const |
Returns true
if every bit of self is set to the boolean value b. Otherwise, returns false
.
bool RWBitVec::operator== | ( | const RWBitVec & | v | ) | const [inline] |
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
.
bool RWBitVec::operator[] | ( | size_t | i | ) | const [inline] |
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 is thrown.
RWBitRef RWBitVec::operator[] | ( | size_t | i | ) | [inline] |
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 is thrown.
Logical assignment. Sets each element of self to the logical XOR
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 is thrown.
Logical assignment. Sets each element of self to the logical OR
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 is thrown.
std::ostream& RWBitVec::printOn | ( | std::ostream & | s | ) | const |
Prints the bit vector on the output stream s. See the example above for a sample of the format.
void RWBitVec::resize | ( | size_t | N | ) |
Resizes the vector to have length N. If this results in a lengthening of the vector, the additional bits are set to false
.
std::istream& RWBitVec::scanFrom | ( | std::istream & | s | ) |
Reads the bit vector from the input stream s. The vector is dynamically resized as necessary. The vector should be in the same format printed by member function printOn(std::ostream&).
void RWBitVec::setBit | ( | size_t | i | ) | [inline] |
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);
void RWBitVec::swap | ( | RWBitVec & | v | ) |
Swaps the data owned by self with the data owned by v.
bool RWBitVec::testBit | ( | size_t | i | ) | const [inline] |
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();
Unary operator that returns the logical negation of vector v.
Returns a vector that is the logical AND
of the vectors v1 and v2. The two vectors must have the same length or an exception of type RWInternalErr is thrown.
RWvostream & operator<< | ( | RWvostream & | str, | |
const RWBitVec & | vec | |||
) | [related] |
Saves the RWBitVec vec to a virtual stream.
std::ostream& operator<< | ( | std::ostream & | s, | |
const RWBitVec & | v | |||
) | [friend] |
Prints the bit vector v on the output stream s.
RWvistream & operator>> | ( | RWvistream & | str, | |
RWBitVec & | vec | |||
) | [related] |
Restores an RWBitVec into vec from a virtual stream, replacing the previous contents of vec.
std::istream& operator>> | ( | std::istream & | s, | |
RWBitVec & | v | |||
) | [friend] |
Reads the bit vector v from the input stream s.
Returns a vector that is the logical XOR
of the vectors v1 and v2. The two vectors must have the same length or an exception of type RWInternalErr is thrown.
Returns a vector that is the logical OR
of the vectors v1 and v2. The two vectors must have the same length or an exception of type RWInternalErr is thrown.
size_t sum | ( | const RWBitVec & | v | ) | [friend] |
Returns the total number of bits set in the vector v.
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.