SourcePro® API Reference Guide

 
List of all members | Public Member Functions | Friends | Related Functions

Represents a bit vector whose length can be changed at runtime. More...

#include <rw/bitvec.h>

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 ()
 
void clearBit (size_t i)
 
const RWBytedata () const
 
size_t firstFalse () const
 
size_t firstTrue () const
 
unsigned hash () const
 
bool isEqual (const RWBitVec &v) const
 
size_t length () const
 
bool operator!= (const RWBitVec &v) const
 
bool operator!= (bool b) const
 
RWBitVecoperator&= (const RWBitVec &v)
 
RWBitRef operator() (size_t i)
 
bool operator() (size_t i) const
 
RWBitVecoperator= (const RWBitVec &v)
 
RWBitVecoperator= (RWBitVec &&v)
 
RWBitVecoperator= (bool b)
 
bool operator== (const RWBitVec &v) const
 
bool operator== (bool b) const
 
RWBitRef operator[] (size_t i)
 
bool operator[] (size_t i) const
 
RWBitVecoperator^= (const RWBitVec &v)
 
RWBitVecoperator|= (const RWBitVec &v)
 
std::ostream & printOn (std::ostream &s) const
 
void resize (size_t N)
 
std::istream & scanFrom (std::istream &s)
 
void setBit (size_t i)
 
void swap (RWBitVec &v)
 
bool testBit (size_t i) const
 

Friends

RWBitVec operator! (const RWBitVec &v)
 
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)
 
RWBitVec operator^ (const RWBitVec &v1, const RWBitVec &v2)
 
RWBitVec operator| (const RWBitVec &v1, const RWBitVec &v2)
 
size_t sum (const RWBitVec &v)
 

Related Functions

(Note that these are not member functions.)

RWvostreamoperator<< (RWvostream &str, const RWBitVec &vec)
 
RWFileoperator<< (RWFile &file, const RWBitVec &vec)
 
RWvistreamoperator>> (RWvistream &str, RWBitVec &vec)
 
RWFileoperator>> (RWFile &file, RWBitVec &vec)
 

Detailed Description

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, whose lengths are fixed at compile time.

Synopsis
#include <rw/bitvec.h>
Persistence
Simple
Example
#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
]

Constructor & Destructor Documentation

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.

Condition:
This method is available only on platforms with rvalue reference support.
RWBitVec::~RWBitVec ( )

The destructor. Releases any allocated memory.

Member Function Documentation

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!= ( 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.

bool RWBitVec::operator!= ( bool  b) const
inline

Returns false if every bit of self is set to the boolean value b. Otherwise, returns true.

RWBitVec& RWBitVec::operator&= ( const RWBitVec v)
inline

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.

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.

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.

RWBitVec& RWBitVec::operator= ( const RWBitVec v)

Assignment operator. Uses value semantics. Self is a copy of v.

RWBitVec& RWBitVec::operator= ( RWBitVec &&  v)

Move assignment. Self takes ownership of the data owned by v.

Condition:
This method is available only on platforms with rvalue reference support.
RWBitVec& RWBitVec::operator= ( bool  b)

Assignment operator. Sets every bit in self to the boolean value b.

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== ( bool  b) const

Returns true if every bit of self is set to the boolean value b. Otherwise, returns false.

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.

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.

RWBitVec& RWBitVec::operator^= ( const RWBitVec v)
inline

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.

RWBitVec& RWBitVec::operator|= ( const RWBitVec v)
inline

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&) const.

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();

Friends And Related Function Documentation

RWBitVec operator! ( const RWBitVec v)
friend

Unary operator that returns the logical negation of vector v.

RWBitVec operator& ( const RWBitVec v1,
const RWBitVec v2 
)
friend

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.

std::ostream& operator<< ( std::ostream &  s,
const RWBitVec v 
)
friend

Prints the bit vector v on the output stream s.

RWvostream & operator<< ( RWvostream str,
const RWBitVec vec 
)
related

Saves the RWBitVec vec to a virtual stream.

RWFile & operator<< ( RWFile file,
const RWBitVec vec 
)
related

Saves the RWBitVec vec to an RWFile.

std::istream& operator>> ( std::istream &  s,
RWBitVec v 
)
friend

Reads the bit vector v from the input stream s.

RWvistream & operator>> ( RWvistream str,
RWBitVec vec 
)
related

Restores an RWBitVec into vec from a virtual stream, replacing the previous contents of vec.

RWFile & operator>> ( RWFile file,
RWBitVec vec 
)
related

Restores an RWBitVec into vec from an RWFile, replacing the previous contents of vec.

RWBitVec operator^ ( const RWBitVec v1,
const RWBitVec v2 
)
friend

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.

RWBitVec operator| ( const RWBitVec v1,
const RWBitVec v2 
)
friend

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 © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.