Open Babel  3.0
Public Types | Public Member Functions | Friends | List of all members
OBBitVec Class Reference

#include <openbabel/bitvec.h>

Public Types

typedef std::vector< uint32_t > 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
 
size_t GetSize () const
 
unsigned CountBits () const
 
bool IsEmpty () const
 
bool Resize (unsigned size_in_bits)
 
bool ResizeWords (unsigned size_in_words)
 
bool BitIsSet (unsigned 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

OBBitVec operator| (const OBBitVec &bv1, const OBBitVec &bv2)
 
OBBitVec operator & (const OBBitVec &bv1, const OBBitVec &bv2)
 
OBBitVec operator^ (const OBBitVec &bv1, const OBBitVec &bv2)
 
OBBitVec operator- (const OBBitVec &bv1, const OBBitVec &bv2)
 
bool operator== (const OBBitVec &bv1, const OBBitVec &bv2)
 
bool operator< (const OBBitVec &bv1, const OBBitVec &bv2)
 
std::istream & operator>> (std::istream &is, OBBitVec &bv)
 
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 fixed size unsigned ints (uint32_t). Any bits which are out of reach of the current size are considered to be zero. Streamlined, corrected and documented by kshep.nosp@m.herd.nosp@m.1@use.nosp@m.rs.s.nosp@m.ource.nosp@m.forg.nosp@m.e.net

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

◆ word_vector

typedef std::vector<uint32_t> word_vector

Constructor & Destructor Documentation

◆ OBBitVec() [1/3]

OBBitVec ( )
inline

Construct a bit vector of the default size.

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

◆ OBBitVec() [2/3]

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_bitsThe number of bits for which to reserve space

◆ OBBitVec() [3/3]

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]bvThe other bit vector to copy to this

Member Function Documentation

◆ SetBitOn()

void SetBitOn ( unsigned  bit_offset)

◆ SetBitOff()

void SetBitOff ( unsigned  bit_offset)

◆ SetRangeOn()

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_offseta zero based offset into the bit vector
[in]hi_bit_offseta zero based offset into the bit vector

Referenced by OBBuilder::Connect(), OBMolAtomBFSIter::OBMolAtomBFSIter(), OBMolAtomDFSIter::OBMolAtomDFSIter(), and OBMolBondBFSIter::OBMolBondBFSIter().

◆ SetRangeOff()

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_offseta zero based offset into the bit vector
[in]hi_bit_offseta zero based offset into the bit vector

◆ Fold()

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_sizethe size of the resultant vector, in bits

◆ FirstBit()

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_offsetthe 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 OBMol::CopySubstructure(), OBMolAtomDFSIter::operator++(), OBMolAtomBFSIter::operator++(), and OBMolBondBFSIter::operator++().

◆ NextBit()

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_offsetthe 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(), OpenBabel::ComparePairSecond(), OBMol::ContigFragList(), OBMol::CopySubstructure(), OBMol::FindChildren(), OBMol::FindLargestFragment(), OpenBabel::GetDFFVector(), OBMol::GetGTDVector(), OpenBabel::operator<(), OBRotorList::SetEvalAtoms(), and OBBitVec::ToVecInt().

◆ EndBit()

int EndBit ( ) const
inline

◆ GetSize()

size_t GetSize ( ) const
inline

◆ CountBits()

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::CanonicalLabels(), OpenBabel::CompileMoleculeQuery(), OBMol::ContigFragList(), OpenBabel::FindAutomorphisms(), OBMol::FindLargestFragment(), OpenBabel::findMetalloceneBonds(), OpenBabel::Tanimoto(), and OBBitVec::ToVecInt().

◆ IsEmpty()

bool IsEmpty ( ) const

◆ Resize()

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_bitsthe number of bits
Returns
true if enlargement was necessary, false otherwise

Referenced by OpenBabel::ComparePairSecond(), OBMol::ContigFragList(), OBBitVec::FromString(), OBMolAtomBFSIter::OBMolAtomBFSIter(), OBMolAtomDFSIter::OBMolAtomDFSIter(), OBMolBondBFSIter::OBMolBondBFSIter(), OBRing::OBRing(), and OBAlign::SetRefMol().

◆ ResizeWords()

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_wordsthe 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().

◆ BitIsSet()

bool BitIsSet ( unsigned  bit_offset) const
inline

◆ FromVecInt()

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_offsetsA list of bit offsets

Referenced by OBSmartsPattern::GetUMapList(), OBSmartsPattern::Match(), and OBRing::OBRing().

◆ FromString()

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]lineA string containing positive integers
[in]new_bit_sizeThe size that the vector should become

◆ ToVecInt()

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_offsetsA list of bit offsets, in ascending order

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

◆ Clear()

void Clear ( void  )

◆ Negate()

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.

◆ GetWords()

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]veca vector of words to which to append the data

◆ operator=()

OBBitVec & operator= ( const OBBitVec bv)

Assignment operator.

Assign this vector to be a copy of bv

Parameters
[in]bvA bit vector
Returns
A reference to this

◆ operator &=()

OBBitVec& operator&= ( const OBBitVec bv)

And-equals operator.

Referenced by OBBitVec::operator=().

◆ operator|=() [1/2]

OBBitVec & operator|= ( const OBBitVec bv)

Or-equals operator.

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

Parameters
[in]bvA bit vector
Returns
A reference to this

◆ operator|=() [2/2]

OBBitVec& operator|= ( int  bit_offset)
inline

Or-equals operator for integer.

Or the bit at offset bit_offset with 1

◆ operator^=()

OBBitVec & operator^= ( const OBBitVec bv)

Exclusive-or-equals operator.

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

Parameters
[in]bvA bit vector
Returns
A reference to this

◆ operator-=()

OBBitVec & operator-= ( const OBBitVec bv)

Minus-equals operator.

Unset bits in this vector which are set in bv

Parameters
[in]bvA bit vector
Returns
A reference to this

◆ operator+=()

OBBitVec & operator+= ( const OBBitVec bv)

Plus-equals operator.

Append vector bv to the end if this vector

Parameters
[in]bvA bit vector
Returns
A reference to this

◆ operator[]()

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_offseta zero based offset into the bit vector
Returns
true if it is set, false otherwise

Friends And Related Function Documentation

◆ operator|

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]bv1A bit vector
[in]bv2Another bit vector
Returns
A bit vector

◆ operator &

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]bv1A bit vector
[in]bv2Another bit vector
Returns
A bit vector

◆ operator^

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]bv1A bit vector
[in]bv2Another bit vector
Returns
A bit vector

◆ operator-

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]bv1A bit vector
[in]bv2Another bit vector
Returns
A bit vector

◆ operator==

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]bv1A bit vector
[in]bv2Another bit vector
Returns
true if equal, false otherwise

◆ operator<

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]bv1A bit vector
[in]bv2Another bit vector
Returns
true if equal, false otherwise

◆ operator>>

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]isThe input stream
[out]bvThe bit vector to contain the result

◆ operator<<

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]osThe output stream
[in]bvThe bit vector to be output

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