#include <openbabel/bitvec.h>
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) |
OBBitVec & | operator= (const OBBitVec &bv) |
OBBitVec & | operator &= (const OBBitVec &bv) |
OBBitVec & | operator|= (const OBBitVec &bv) |
OBBitVec & | operator|= (int bit_offset) |
OBBitVec & | operator^= (const OBBitVec &bv) |
OBBitVec & | operator-= (const OBBitVec &bv) |
OBBitVec & | operator+= (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) |
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; }
typedef std::vector<unsigned> word_vector |
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.
[in] | size_in_bits | The number of bits for which to reserve space |
Copy constructor (result has same number of bits).
Construct a bit vector which is an exact duplicate of bv
.
[in] | bv | The other bit vector to copy to this |
void SetBitOn | ( | unsigned | bit_offset | ) |
Set the bit_offset
'th bit to 1.
Set the bit_offset
'th bit to 1 Increases the size of this bit vector if necessary
[in] | bit_offset | a zero based offset into the bit vector |
Referenced by OBFFConstraints::AddAtomConstraint(), OBFFConstraints::AddAtomXConstraint(), OBFFConstraints::AddAtomYConstraint(), OBFFConstraints::AddAtomZConstraint(), OBFFConstraints::AddIgnore(), OBBuilder::AddNbrs(), OBRingSearch::AddRingFromClosure(), OpenBabel::BreakChiralTies(), OBBuilder::Build(), OBMol::ContigFragList(), OBMol::expandcycle(), OBMol::FindChildren(), OpenBabel::FindConjugatedEZBonds(), OBMol::FindLargestFragment(), OpenBabel::FindRings(), OBBitVec::FromString(), OBBitVec::FromVecInt(), OBResidue::GetBonds(), OpenBabel::GetDFFVector(), OBBuilder::GetFragment(), OBMol::GetGTDVector(), OpenBabel::GetGTDVector(), OpenBabel::MarkGrowBonds(), OpenBabel::MinimumPairRMS(), OBMol::NewPerceiveKekuleBonds(), OpenBabel::operator>>(), OBRingSearch::SaveUniqueRing(), OBRotorList::SetEvalAtoms(), OBBitVec::SetRangeOn(), and OBForceField::UpdatePairsSimple().
void SetBitOff | ( | unsigned | bit_offset | ) |
Set the bit_offset
'th bit to 0.
Set the bit_offset
'th bit to 0
[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
[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
[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
[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
[in] | bit_offset | the first bit to consider |
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
[in] | last_bit_offset | the bit before the first to consider |
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] |
Return the bit offset of the last bit (for iterating) i.e. -1.
Referenced by OpenBabel::BuildOBRTreeVector(), OBMol::ContigFragList(), OBMol::FindChildren(), OBMol::FindLargestFragment(), OpenBabel::GetDFFVector(), OBMol::GetGTDVector(), OpenBabel::GetGTDVector(), OBMolAtomBFSIter::operator++(), OBMolAtomDFSIter::operator++(), and OBRotorList::SetEvalAtoms().
unsigned GetSize | ( | ) | const [inline] |
Return the number of words ( NOT the number of bits ).
Referenced by OBBitVec::NextBit(), OBBitVec::operator &=(), OBBitVec::operator-=(), OpenBabel::operator==(), OBBitVec::operator^=(), OBBitVec::operator|=(), OBBitVec::SetBitOff(), OBBitVec::SetBitOn(), OBBitVec::SetRangeOff(), and OBBitVec::SetRangeOn().
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
Referenced by OpenBabel::CalculateSymmetry(), OpenBabel::CanonicalLabels(), OBMol::ContigFragList(), OBMol::FindLargestFragment(), OBBond::IsClosure(), OpenBabel::Tanimoto(), and OBBitVec::ToVecInt().
bool Empty | ( | ) | const [inline] |
Referenced by OpenBabel::BuildOBRTreeVector(), OBMol::FindChildren(), OBMol::FindLargestFragment(), and OBRotorList::SetEvalAtoms().
bool IsEmpty | ( | ) | const |
Are there no bits set to 1 in this vector?
Are there no bits set to 1 in this vector?
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
[in] | size_in_bits | the number of bits |
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
[in] | size_in_words | the number of words |
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 ?
[in] | bit_offset | a zero based offset into the bit vector |
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] |
Referenced by OpenBabel::CalculateSymmetry(), OpenBabel::CanonicalLabels(), OBMol::ContigFragList(), OpenBabel::CreateNewClassVector(), OpenBabel::DetermineFRJ(), OBMol::FindChildren(), OBMol::FindLargestFragment(), OBResidue::GetBonds(), OpenBabel::GetDFFVector(), OpenBabel::GetGIVector(), OBMol::GetGTDVector(), OpenBabel::GetGTDVector(), and OBRing::IsMember().
void FromVecInt | ( | const std::vector< int > & | bit_offsets | ) |
Sets the bits listed as bit offsets.
Sets bits on, listed as bit offsets
[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 ]"
[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.
[out] | bit_offsets | A list of bit offsets, in ascending order |
Referenced by OBMol::ContigFragList(), and OBMol::FindChildren().
void Clear | ( | void | ) |
Set all bits to zero.
Set all the bits in this vector to zero Does not currently change the size of the vector.
Referenced by OpenBabel::BreakChiralTies(), OpenBabel::BuildOBRTreeVector(), OBFFConstraints::Clear(), OBMol::ContigFragList(), OpenBabel::DetermineFRJ(), OBMol::FindChildren(), OBMol::FindLargestFragment(), OpenBabel::FindRingAtoms(), OBBitVec::FromString(), OpenBabel::GetDFFVector(), OBMol::GetGTDVector(), OpenBabel::GetGTDVector(), OBSmartsPattern::GetUMapList(), OBMol::NewPerceiveKekuleBonds(), OBRingSearch::RemoveRedundant(), OBRotorList::SetEvalAtoms(), and OBForceField::UpdatePairsSimple().
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
.
[out] | vec | a vector of words to which to append the data |
Assignment operator.
Assign this vector to be a copy of bv
[in] | bv | A bit vector |
And-equals operator.
Assign this vector to the result of And-ing it with bv
[in] | bv | A bit vector |
Or-equals operator.
Assign this vector to the result of Or-ing it with bv
[in] | bv | A bit vector |
OBBitVec& operator|= | ( | int | bit_offset | ) | [inline] |
Or-equals operator for integer.
Or the bit at offset bit_offset
with 1
Exclusive-or-equals operator.
Assign this vector to the result of Exclusive-or-ing it with bv
[in] | bv | A bit vector |
Minus-equals operator.
Unset bits in this vector which are set in bv
[in] | bv | A bit vector |
Plus-equals operator.
Append vector bv
to the end if this vector
[in] | bv | A bit vector |
bool operator[] | ( | int | bit_offset | ) | const [inline] |
Asks if the bit_offset
'th bit is set.
Is the bit_offset
'th bit set ?
[in] | bit_offset | a zero based offset into the bit vector |
Or operator.
Return a bit vector of the results of Or-ing each bit in bv1
with the corresponding bit in bv2
[in] | bv1 | A bit vector |
[in] | bv2 | Another bit vector |
And operator.
Return a bit vector of the results of And-ing each bit in bv1
with the corresponding bit in bv2
[in] | bv1 | A bit vector |
[in] | bv2 | Another bit vector |
Exclusive-or operator.
Return a bit vector of the results of Exclusive-or-ing each bit in bv1
with the corresponding bit in bv2
[in] | bv1 | A bit vector |
[in] | bv2 | Another bit vector |
Minus operator.
Return a bit vector of the results of clearing each bit in bv1
which is set in bv2
[in] | bv1 | A bit vector |
[in] | bv2 | Another bit vector |
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.
[in] | bv1 | A bit vector |
[in] | bv2 | Another bit vector |
Smaller-than operator.
Return true if bv1
i less than bv2
Lexicographical order, with bit vectors written LSB first.
[in] | bv1 | A bit vector |
[in] | bv2 | Another bit vector |
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 ]"
[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 ]"
[out] | os | The output stream |
[in] | bv | The bit vector to be output |