OBBitVec Class Reference

A speed-optimized vector of bits. More...

#include <openbabel/bitvec.h>

List of all members.

Public Types

typedef std::vector< unsigned > word_vector

Public Member Functions

 OBBitVec ()
 OBBitVec (unsigned size_in_bits)
 OBBitVec (const OBBitVec &bv)
void SetBitOn (unsigned bit_offset)
void SetBitOff (unsigned bit_offset)
void SetRangeOn (unsigned lo_bit_offset, unsigned hi_bit_offset)
void SetRangeOff (unsigned lo_bit_offset, unsigned hi_bit_offset)
void Fold (unsigned new_bit_size)
int FirstBit (unsigned bit_offset=0) const
int NextBit (int last_bit_offset) const
int EndBit () const
unsigned GetSize () const
unsigned CountBits () const
bool Empty () const
bool IsEmpty () const
bool Resize (unsigned size_in_bits)
bool ResizeWords (unsigned size_in_words)
bool BitIsSet (unsigned bit_offset) const
bool BitIsOn (int bit_offset) const
void FromVecInt (const std::vector< int > &bit_offsets)
void FromString (const std::string &line, int bits)
void ToVecInt (std::vector< int > &bit_offsets) const
void Clear ()
void Negate ()
void GetWords (word_vector &vec)
OBBitVecoperator= (const OBBitVec &bv)
OBBitVecoperator &= (const OBBitVec &bv)
OBBitVecoperator|= (const OBBitVec &bv)
OBBitVecoperator|= (int bit_offset)
OBBitVecoperator^= (const OBBitVec &bv)
OBBitVecoperator-= (const OBBitVec &bv)
OBBitVecoperator+= (const OBBitVec &bv)
bool operator[] (int bit_offset) const

Friends

OBERROR OBBitVec operator| (const OBBitVec &bv1, const OBBitVec &bv2)
OBERROR OBBitVec operator & (const OBBitVec &bv1, const OBBitVec &bv2)
OBERROR OBBitVec operator^ (const OBBitVec &bv1, const OBBitVec &bv2)
OBERROR OBBitVec operator- (const OBBitVec &bv1, const OBBitVec &bv2)
OBERROR bool operator== (const OBBitVec &bv1, const OBBitVec &bv2)
OBERROR bool operator< (const OBBitVec &bv1, const OBBitVec &bv2)
OBERROR std::istream & operator>> (std::istream &is, OBBitVec &bv)
OBERROR std::ostream & operator<< (std::ostream &os, const OBBitVec &bv)


Detailed Description

A speed-optimized vector of bits.

Fast and efficient bitstring class.

This class implements a fast vector of bits using internally a vector of processor native unsigned words. Any bits which are out of reach of the current size are considered to be zero. Streamlined, corrected and documented by [email protected]

The OBBitVec class is a fast and efficient bitstring class that is handy to use as a truth table. Truth tables are an easy way to store whether a list of items has a particular property. Instances of OBBitVec can be dynamically resized, and have a number of overloaded operators that make code simple and readable. The following examples demonstrate uses of the OBBitVec class:

    OBBitVec bv1,bv2,bv3;
    bv1.SetBitOn(5);
    bv2.SetBitOff(200);
    bv1 |= bv2;
    bv1 = bv1 & bv2;
    if (bv1.IsEmpty()) // IsEmpty() returns true if no bits are set on
    {
       std::cout << "bv1 = " << bv1 << std::endl;
    }

    int bit;
    for (bit = bv1.NextBit(0);bit != bv1.EndBit();bit = bv1.NextBit(bit))
    {
       cout << "the next bit turned on is " << bit << endl;
    }

Member Typedef Documentation

typedef std::vector<unsigned> word_vector


Constructor & Destructor Documentation

OBBitVec (  )  [inline]

Construct a bit vector of the default size.

Construct a bit vector of STARTWORDS size, cleared to all zero bits.

OBBitVec ( unsigned  size_in_bits  )  [inline]

Construct a bit vector of maxbits bits.

Construct a bit vector with a size in bits of size_in_bits rounded up to the nearest word and cleared to all zero bits.

Parameters:
[in] size_in_bits The number of bits for which to reserve space

OBBitVec ( const OBBitVec bv  )  [inline]

Copy constructor (result has same number of bits).

Construct a bit vector which is an exact duplicate of bv.

Parameters:
[in] bv The other bit vector to copy to this


Member Function Documentation

void SetBitOn ( unsigned  bit_offset  ) 

void SetBitOff ( unsigned  bit_offset  ) 

Set the bit_offset 'th bit to 0.

Set the bit_offset 'th bit to 0

Parameters:
[in] bit_offset a zero based offset into the bit vector

Referenced by OBFFConstraints::DeleteConstraint(), OBMol::FindChildren(), OBMolAtomBFSIter::OBMolAtomBFSIter(), OBMolAtomDFSIter::OBMolAtomDFSIter(), OBMolAtomBFSIter::operator++(), OBMolAtomDFSIter::operator++(), OBBitVec::SetRangeOff(), and OBForceField::UpdatePairsSimple().

void SetRangeOn ( unsigned  lo_bit_offset,
unsigned  hi_bit_offset 
)

Set the range of bits from lo_bit_offset to hi_bit_offset to 1.

Set the range of bits from lo_bit_offset to hi_bit_offset to 1 Increases the size of this bit vector if necessary

Parameters:
[in] lo_bit_offset a zero based offset into the bit vector
[in] hi_bit_offset a zero based offset into the bit vector

void SetRangeOff ( unsigned  lo_bit_offset,
unsigned  hi_bit_offset 
)

Set the range of bits from lo_bit_offset to hi_bit_offset to 0.

Set the range of bits from lo_bit_offset to hi_bit_offset to 0

Parameters:
[in] lo_bit_offset a zero based offset into the bit vector
[in] hi_bit_offset a zero based offset into the bit vector

void Fold ( unsigned  new_bit_size  ) 

Reduce the size of the vector by or-ing the excess bits over the start.

Reduce the size of the vector to new_bit_size by or-ing the excess bits over the start of the vector

Parameters:
[in] new_bit_size the size of the resultant vector, in bits

int FirstBit ( unsigned  bit_offset = 0  )  const [inline]

Find the first true bit at or after bit_offset.

Searches the vector for the first true value, starting at the bit_offset 'th bit

Parameters:
[in] bit_offset the first bit to consider
Returns:
the bit offset of the first true bit at or after bit_offset, or -1 if there is none

Referenced by OBMolAtomBFSIter::operator++(), and OBMolAtomDFSIter::operator++().

int NextBit ( int  last_bit_offset  )  const

Find the next true bit after last_bit_offset.

Searches the vector for the first true value, starting at the last_bit_offset 'th bit

Parameters:
[in] last_bit_offset the bit before the first to consider
Returns:
the bit offset of the first true bit after last_bit_offset, or -1 if there is none

Referenced by OpenBabel::BuildOBRTreeVector(), OBMol::ContigFragList(), OBMol::FindChildren(), OBMol::FindLargestFragment(), OpenBabel::GetDFFVector(), OBMol::GetGTDVector(), OpenBabel::GetGTDVector(), OpenBabel::operator<(), OBRotorList::SetEvalAtoms(), and OBBitVec::ToVecInt().

int EndBit (  )  const [inline]

unsigned GetSize (  )  const [inline]

unsigned CountBits (  )  const

Return the number of bits which are set to 1 in the vector.

Count the number of bits which are set in this vector

Returns:
the bit count

Referenced by OpenBabel::CalculateSymmetry(), OpenBabel::CanonicalLabels(), OBMol::ContigFragList(), OBMol::FindLargestFragment(), OBBond::IsClosure(), OpenBabel::Tanimoto(), and OBBitVec::ToVecInt().

bool Empty (  )  const [inline]

bool IsEmpty (  )  const

Are there no bits set to 1 in this vector?

Are there no bits set to 1 in this vector?

Returns:
true for "is empty", false if not empty

Referenced by OBMol::ContigFragList(), OBMol::FindChildren(), OBMol::FindLargestFragment(), OpenBabel::GetDFFVector(), OBMol::GetGTDVector(), and OpenBabel::GetGTDVector().

bool Resize ( unsigned  size_in_bits  )  [inline]

Reserve space for size_in_bits bits.

Reserve space for size_in_bits bits rounded up

Parameters:
[in] size_in_bits the number of bits
Returns:
true if enlargement was necessary, false otherwise

Referenced by OpenBabel::BreakChiralTies(), OBMol::ContigFragList(), OpenBabel::FindRingAtoms(), OBMol::FindRingAtomsAndBonds(), OBBitVec::FromString(), OBBond::IsClosure(), OBMol::NewPerceiveKekuleBonds(), OBMolAtomBFSIter::OBMolAtomBFSIter(), OBMolAtomDFSIter::OBMolAtomDFSIter(), and OBRing::OBRing().

bool ResizeWords ( unsigned  size_in_words  )  [inline]

Reserve space for size_in_words words.

Reserve space for size_in_words words

Parameters:
[in] size_in_words the number of words
Returns:
true if enlargement was necessary, false otherwise

Referenced by OBBitVec::Fold(), OBBitVec::operator-=(), OBBitVec::operator^=(), OBBitVec::operator|=(), OBBitVec::SetBitOn(), and OBBitVec::SetRangeOn().

bool BitIsSet ( unsigned  bit_offset  )  const [inline]

Asks if the bit_offset 'th bit is set.

Is the bit_offset 'th bit set ?

Parameters:
[in] bit_offset a zero based offset into the bit vector
Returns:
true if it is set, false otherwise

Referenced by OBBuilder::AddNbrs(), OBBuilder::Build(), OBBuilder::Connect(), OpenBabel::GetHvyBondSum(), OpenBabel::GetHvyValence(), OpenBabel::GetValence(), OBFFConstraints::IsFixed(), OBFFConstraints::IsIgnored(), OBFFConstraints::IsXFixed(), OBFFConstraints::IsYFixed(), and OBFFConstraints::IsZFixed().

bool BitIsOn ( int  bit_offset  )  const [inline]

void FromVecInt ( const std::vector< int > &  bit_offsets  ) 

Sets the bits listed as bit offsets.

Sets bits on, listed as bit offsets

Parameters:
[in] bit_offsets A list of bit offsets

Referenced by OpenBabel::DetermineFRJ(), OBSmartsPattern::GetUMapList(), and OBRing::OBRing().

void FromString ( const std::string &  line,
int  new_bit_size 
)

Sets the bits listed as a string of integers.

Sets bits on, listed as a string of character-represented integers This bit vector is first cleared. The format is "[ n0 n1 n2 n3 ... ]". The square brackets are optional. The whitespace can be SPACE, NEWLINE or HTAB For example "[ 1 5 6 9 ]"

Parameters:
[in] line A string containing positive integers
[in] new_bit_size The size that the vector should become

void ToVecInt ( std::vector< int > &  bit_offsets  )  const

List the offsets of the bits which are set.

Retrieve a list of bit offsets The bit_offsets vector is first cleared.

Parameters:
[out] bit_offsets A list of bit offsets, in ascending order

Referenced by OBMol::ContigFragList(), and OBMol::FindChildren().

void Clear ( void   ) 

void Negate (  )  [inline]

Inverts every bit in the vector.

Inverts the entire vector. Note that this may give unexpected results, as the vector can be considered to end in an arbitrary number of zero bits.

Referenced by OBMolAtomBFSIter::OBMolAtomBFSIter(), and OBMolAtomDFSIter::OBMolAtomDFSIter().

void GetWords ( word_vector vec  )  [inline]

Return a copy of the internal vector of words, at the end of vec.

Copy the internal word vector. The copy is appended to vec.

Parameters:
[out] vec a vector of words to which to append the data

OBBitVec & operator= ( const OBBitVec bv  ) 

Assignment operator.

Assign this vector to be a copy of bv

Parameters:
[in] bv A bit vector
Returns:
A reference to this

OBBitVec & operator &= ( const OBBitVec bv  ) 

And-equals operator.

Assign this vector to the result of And-ing it with bv

Parameters:
[in] bv A bit vector
Returns:
A reference to this

OBBitVec & operator|= ( const OBBitVec bv  ) 

Or-equals operator.

Assign this vector to the result of Or-ing it with bv

Parameters:
[in] bv A bit vector
Returns:
A reference to this

OBBitVec& operator|= ( int  bit_offset  )  [inline]

Or-equals operator for integer.

Or the bit at offset bit_offset with 1

OBBitVec & operator^= ( const OBBitVec bv  ) 

Exclusive-or-equals operator.

Assign this vector to the result of Exclusive-or-ing it with bv

Parameters:
[in] bv A bit vector
Returns:
A reference to this

OBBitVec & operator-= ( const OBBitVec bv  ) 

Minus-equals operator.

Unset bits in this vector which are set in bv

Parameters:
[in] bv A bit vector
Returns:
A reference to this

OBBitVec & operator+= ( const OBBitVec bv  ) 

Plus-equals operator.

Append vector bv to the end if this vector

Parameters:
[in] bv A bit vector
Returns:
A reference to this

bool operator[] ( int  bit_offset  )  const [inline]

Asks if the bit_offset 'th bit is set.

Is the bit_offset 'th bit set ?

Parameters:
[in] bit_offset a zero based offset into the bit vector
Returns:
true if it is set, false otherwise


Friends And Related Function Documentation

OBERROR OBBitVec operator| ( const OBBitVec bv1,
const OBBitVec bv2 
) [friend]

Or operator.

Return a bit vector of the results of Or-ing each bit in bv1 with the corresponding bit in bv2

Parameters:
[in] bv1 A bit vector
[in] bv2 Another bit vector
Returns:
A bit vector

OBERROR OBBitVec operator & ( const OBBitVec bv1,
const OBBitVec bv2 
) [friend]

And operator.

Return a bit vector of the results of And-ing each bit in bv1 with the corresponding bit in bv2

Parameters:
[in] bv1 A bit vector
[in] bv2 Another bit vector
Returns:
A bit vector

OBERROR OBBitVec operator^ ( const OBBitVec bv1,
const OBBitVec bv2 
) [friend]

Exclusive-or operator.

Return a bit vector of the results of Exclusive-or-ing each bit in bv1 with the corresponding bit in bv2

Parameters:
[in] bv1 A bit vector
[in] bv2 Another bit vector
Returns:
A bit vector

OBERROR OBBitVec operator- ( const OBBitVec bv1,
const OBBitVec bv2 
) [friend]

Minus operator.

Return a bit vector of the results of clearing each bit in bv1 which is set in bv2

Parameters:
[in] bv1 A bit vector
[in] bv2 Another bit vector
Returns:
A bit vector

OBERROR bool operator== ( const OBBitVec bv1,
const OBBitVec bv2 
) [friend]

Equivalency operator.

Return true if bv1 and bv2 are equivalent Not that they may be of different size, and still equivalent provided that the extra bits are all zero.

Parameters:
[in] bv1 A bit vector
[in] bv2 Another bit vector
Returns:
true if equal, false otherwise

OBERROR bool operator< ( const OBBitVec bv1,
const OBBitVec bv2 
) [friend]

Smaller-than operator.

Return true if bv1 i less than bv2 Lexicographical order, with bit vectors written LSB first.

Parameters:
[in] bv1 A bit vector
[in] bv2 Another bit vector
Returns:
true if equal, false otherwise

OBERROR std::istream& operator>> ( std::istream &  is,
OBBitVec bv 
) [friend]

Input from a stream.

Sets bits on, listed as a string of character-represented integers in a stream Only reads one line of input The format is "[ n0 n1 n2 n3 ... ]". The square brackets are optional. The whitespace can be SPACE or HTAB For example "[ 1 5 6 9 ]"

Parameters:
[in,out] is The input stream
[out] bv The bit vector to contain the result

OBERROR std::ostream& operator<< ( std::ostream &  os,
const OBBitVec bv 
) [friend]

Output to a stream.

Output this bit vector to a stream The format is "[ n0 n1 n2 n3 ... ]". The whitespace is SPACE For example "[ 1 5 6 9 ]"

Parameters:
[out] os The output stream
[in] bv The bit vector to be output


The documentation for this class was generated from the following files: