SourcePro® API Reference Guide

 
List of all members | Public Member Functions
RWTBitVec< N > Class Template Reference

Parameterized bit vector of fixed length. More...

#include <rw/tbitvec.h>

Public Member Functions

 RWTBitVec ()
 
 RWTBitVec (bool val)
 
 RWTBitVec (const RWTBitVec< N > &v)
 
void clearBit (size_t i)
 
const RWBytedata () const
 
size_t firstFalse () const
 
size_t firstTrue () const
 
bool operator!= (bool b) const
 
bool operator!= (const RWTBitVec< N > &v) const
 
RWTBitVec< N > operator& (const RWTBitVec< N > &v2) const
 
RWTBitVec< N > & operator&= (const RWTBitVec< N > &v)
 
RWBitRef operator() (size_t i)
 
bool operator() (size_t i) const
 
bool operator< (const RWTBitVec< N > &v) const
 
RWTBitVec< N > & operator= (const RWTBitVec< N > &v)
 
RWTBitVec< N > & operator= (bool val)
 
bool operator== (bool b) const
 
bool operator== (const RWTBitVec< N > &v) const
 
RWBitRef operator[] (size_t i)
 
bool operator[] (size_t i) const
 
RWTBitVec< N > operator^ (const RWTBitVec< N > &v2) const
 
RWTBitVec< N > & operator^= (const RWTBitVec< N > &v)
 
RWTBitVec< N > operator| (const RWTBitVec< N > &v2) const
 
RWTBitVec< N > & operator|= (const RWTBitVec< N > &v)
 
void setBit (size_t i)
 
bool testBit (size_t i) const
 

Detailed Description

template<size_t N>
class RWTBitVec< N >

RWTBitVec is a parameterized bit vector of fixed length Size. Unlike class RWBitVec, its length cannot be changed at runtime. The advantage of RWTBitVec is its smaller size, and one less level of indirection, resulting in a slight speed advantage.

Bits are numbered from 0 through Size-1, inclusive.

The copy constructor and assignment operator use copy semantics.

Synopsis
#include <rw/tbitvec.h>
RWTBitVec<22> // A 22 bit long vector
Persistence
None
Example

In this example, two 8-bit vectors are exercised:

#include <rw/tbitvec.h>
#include <iostream>
const size_t VECSIZE = 8;
int main ()
{
RWTBitVec<VECSIZE> a, b; // Allocate two vectors.
a(2) = true; // Set bit 2 (the third bit) of a on.
b(3) = true; // Set bit 3 (the fourth bit) of b on.
RWTBitVec<VECSIZE> c = a ^ b; // Set c to the XOR of a and b.
std::cout << "V1" << "\t"
<< "V2" << "\t"
<< "V1 XOR V2"
<< std::endl;
for(size_t i = 0; i < VECSIZE; i++) {
std::cout << a[i] << "\t"
<< b[i] << "\t"
<< c[i] << "\n";
}
return 0;
}

Program output:

V1 V2 V1 XOR V2
0 0 0
0 0 0
1 0 1
0 1 1
0 0 0
0 0 0
0 0 0
0 0 0

Constructor & Destructor Documentation

template<size_t N>
RWTBitVec< N >::RWTBitVec ( )
inline

Constructs an instance with all bits set to false.

template<size_t N>
RWTBitVec< N >::RWTBitVec ( bool  val)
inline

Constructs an instance with all bits set to val.

template<size_t N>
RWTBitVec< N >::RWTBitVec ( const RWTBitVec< N > &  v)
inline

Constructs a copy of v.

Member Function Documentation

template<size_t N>
void RWTBitVec< N >::clearBit ( size_t  i)
inline

Clears (i.e., sets to false) the bit with index i. The index i must be between 0 and Size-1. No bounds checking is performed. The following two lines are equivalent, although using clearBit(size_t) is slightly smaller and faster than using operator()(size_t):

a(i) = false;
a.clearBit(i);
template<size_t N>
const RWByte* RWTBitVec< N >::data ( void  ) const
inline

Returns a const pointer to the raw data of self.

template<size_t N>
size_t RWTBitVec< N >::firstFalse ( ) const
inline

Returns the index of the first OFF (false) bit in self. Returns RW_NPOS if there is no OFF bit.

template<size_t N>
size_t RWTBitVec< N >::firstTrue ( ) const
inline

Returns the index of the first ON (true) bit in self. Returns RW_NPOS if there is no ON bit.

template<size_t N>
bool RWTBitVec< N >::operator!= ( bool  b) const
inline

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

template<size_t N>
bool RWTBitVec< N >::operator!= ( const RWTBitVec< N > &  v) const
inline

Returns true if any bit of self is not set to the same value as the corresponding bit in v. Otherwise, returns false.

template<size_t N>
RWTBitVec<N> RWTBitVec< N >::operator& ( const RWTBitVec< N > &  v2) const
inline

Returns the logical AND of self and v2.

template<size_t N>
RWTBitVec<N>& RWTBitVec< N >::operator&= ( const RWTBitVec< N > &  v)
inline

Logical assignments. Sets each bit of self to the logical AND of self and the corresponding bit in v.

template<size_t N>
RWBitRef RWTBitVec< N >::operator() ( size_t  i)
inline

Returns a reference to the ith bit of self. This reference can be used as an lvalue. The index i must be between 0 and Size-1, inclusive. No bounds checking is performed.

template<size_t N>
bool RWTBitVec< N >::operator() ( size_t  i) const
inline

Returns the value of the i th bit of self. The index i must be between 0 and Size-1, inclusive. No bounds checking is performed.

template<size_t N>
bool RWTBitVec< N >::operator< ( const RWTBitVec< N > &  v) const

Returns true if self is logically less than v. Otherwise, returns false.

RWTBitVec<2> v1, v2;
v1.setBit(0); // the value 1 in binary
v2.setBit(1); // the value 2 in binary
assert (v1 < v2); // 1 is less than 2
template<size_t N>
RWTBitVec<N>& RWTBitVec< N >::operator= ( const RWTBitVec< N > &  v)
inline

Sets self to a copy of v.

template<size_t N>
RWTBitVec<N>& RWTBitVec< N >::operator= ( bool  val)
inline

Sets all bits in self to the value val.

template<size_t N>
bool RWTBitVec< N >::operator== ( bool  b) const

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

template<size_t N>
bool RWTBitVec< N >::operator== ( const RWTBitVec< N > &  v) const

Returns true if each bit of self is set to the same value as the corresponding bit in v. Otherwise, returns false.

template<size_t N>
RWBitRef RWTBitVec< N >::operator[] ( size_t  i)
inline

Returns a reference to the i th bit of self. This reference can be used as an lvalue.

Exceptions
RWBoundsErrthrown if index i is not between 0 and Size-1, inclusive.
template<size_t N>
bool RWTBitVec< N >::operator[] ( size_t  i) const
inline

Returns the value of the i th bit of self. Bounds checking is performed.

Exceptions
RWBoundsErris thrown if index i is not between 0 and Size-1, inclusive.
template<size_t N>
RWTBitVec<N> RWTBitVec< N >::operator^ ( const RWTBitVec< N > &  v2) const
inline

Returns the logical XOR of self and v2.

template<size_t N>
RWTBitVec<N>& RWTBitVec< N >::operator^= ( const RWTBitVec< N > &  v)
inline

Logical assignments. Sets each bit of self to the logical XOR of self and the corresponding bit in v.

template<size_t N>
RWTBitVec<N> RWTBitVec< N >::operator| ( const RWTBitVec< N > &  v2) const
inline

Returns the logical OR of self and v2.

template<size_t N>
RWTBitVec<N>& RWTBitVec< N >::operator|= ( const RWTBitVec< N > &  v)
inline

Logical assignments. Sets each bit of self to the logical OR of self and the corresponding bit in v.

template<size_t N>
void RWTBitVec< N >::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 two lines are equivalent, although using setBit(size_t) is slightly smaller and faster than using operator()(size_t).

a(i) = true;
a.setBit(i);
template<size_t N>
bool RWTBitVec< N >::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 using testBit(size_t) is slightly smaller and faster than using operator()(size_t):

if( a(i) ) doSomething();
if( a.testBit(i) ) doSomething();

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