SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches

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

#include <rw/bitvec.h>

Public Member Functions

 RWBitVec ()
 
 RWBitVec (const RWBitVec &v)
 
 RWBitVec (const RWByte *bp, size_t N)
 
 RWBitVec (RWBitVec &&v)
 
 RWBitVec (size_t N)
 
 RWBitVec (size_t N, bool initVal)
 
 ~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!= (bool b) const
 
bool operator!= (const RWBitVec &v) const
 
RWBitVecoperator&= (const RWBitVec &v)
 
RWBitRef operator() (size_t i)
 
bool operator() (size_t i) const
 
RWBitVecoperator= (bool b)
 
RWBitVecoperator= (const RWBitVec &v)
 
RWBitVecoperator= (RWBitVec &&v)
 
bool operator== (bool b) const
 
bool operator== (const RWBitVec &v) 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 Symbols

(Note that these are not member symbols.)

RWFileoperator<< (RWFile &file, const RWBitVec &vec)
 
RWvostreamoperator<< (RWvostream &str, const RWBitVec &vec)
 
RWFileoperator>> (RWFile &file, RWBitVec &vec)
 
RWvistreamoperator>> (RWvistream &str, 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>
Represents a bit vector whose length can be changed at runtime.
Definition bitvec.h:87
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() [1/6]

RWBitVec::RWBitVec ( )
inline

Constructs a zero length (null) vector.

◆ RWBitVec() [2/6]

RWBitVec::RWBitVec ( size_t N)

Constructs a vector with N bits. The initial value of the bits is undefined.

◆ RWBitVec() [3/6]

RWBitVec::RWBitVec ( size_t N,
bool initVal )

Constructs a vector with N bits, each set to the Boolean value initVal.

◆ RWBitVec() [4/6]

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() [5/6]

RWBitVec::RWBitVec ( const RWBitVec & v)

Copy constructor. Uses value semantics. The constructed vector is a copy of v.

◆ RWBitVec() [6/6]

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::~RWBitVec ( )

The destructor. Releases any allocated memory.

Member Function Documentation

◆ clearBit()

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

◆ data()

const RWByte * RWBitVec::data ( ) const
inline

Returns a const pointer to the raw data of self. Should be used with care.

◆ firstFalse()

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.

◆ firstTrue()

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.

◆ hash()

unsigned RWBitVec::hash ( ) const

Returns a value suitable for hashing.

◆ isEqual()

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.

◆ length()

size_t RWBitVec::length ( ) const
inline

Returns the number of bits in the vector.

◆ operator!=() [1/2]

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

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

◆ operator!=() [2/2]

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.

◆ operator&=()

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.

◆ operator()() [1/2]

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.

◆ operator()() [2/2]

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.

◆ operator=() [1/3]

RWBitVec & RWBitVec::operator= ( bool b)

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

◆ operator=() [2/3]

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

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

◆ operator=() [3/3]

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.

◆ operator==() [1/2]

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

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

◆ operator==() [2/2]

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.

◆ operator[]() [1/2]

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.

◆ operator[]() [2/2]

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.

◆ operator^=()

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.

◆ operator|=()

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.

◆ printOn()

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.

◆ resize()

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.

◆ scanFrom()

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.

◆ setBit()

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

◆ swap()

void RWBitVec::swap ( RWBitVec & v)

Swaps the data owned by self with the data owned by v.

◆ testBit()

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 Symbol Documentation

◆ operator!

RWBitVec operator! ( const RWBitVec & v)
friend

Unary operator that returns the logical negation of vector v.

◆ operator&

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.

◆ operator<<() [1/3]

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

Saves the RWBitVec vec to an RWFile.

◆ operator<<() [2/3]

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

Saves the RWBitVec vec to a virtual stream.

◆ operator<< [3/3]

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

Prints the bit vector v on the output stream s.

◆ operator>>() [1/3]

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

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

◆ operator>>() [2/3]

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

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

◆ operator>> [3/3]

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

Reads the bit vector v from the input stream s.

◆ operator^

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.

◆ operator|

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.

◆ sum

size_t sum ( const RWBitVec & v)
friend

Returns the total number of bits set in the vector v.

Copyright © 2024 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.