Loading...
Searching...
No Matches
bitvec.h
Go to the documentation of this file.
/**********************************************************************
bitvec.h - Vector of bits.
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#ifndef OB_BITVEC_H
#define OB_BITVEC_H
#include <openbabel/babelconfig.h>
#include <vector>
#include <string>
#if defined(_MSC_VER) && _MSC_VER <= 1600
// Assuming 32bit integer
typedef unsigned uint32_t;
#else
#include <inttypes.h>
#endif
// Use uint32_t
#define SETWORD 32
// SETWORD = 2 ^ WORDROLL
#define WORDROLL 5
// WORDMASK = SETWORD - 1
#define WORDMASK 31
#define WORDSIZE_OF_BITSIZE( bit_size ) ( ( bit_size >> WORDROLL ) + (( bit_size & WORDMASK ) ? 1 : 0) )
#ifndef STARTWORDS
#define STARTWORDS 10
#endif // STARTWORDS
namespace OpenBabel
{
class OBERROR OBBitVec
{
public:
typedef std::vector<uint32_t> word_vector;
private:
size_t _size; //was unsigned
word_vector _set;
public:
OBBitVec()
:_set(STARTWORDS, 0)
{ _size = _set.size(); }
OBBitVec(unsigned size_in_bits)
:_set(WORDSIZE_OF_BITSIZE(size_in_bits), 0)
{ _size = _set.size(); }
OBBitVec(const OBBitVec & bv)
:_size(0)
{ (*this) = 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
{
return (BitIsSet(bit_offset) ? 0 : NextBit(bit_offset));
}
int NextBit(int last_bit_offset) const;
int EndBit() const { return -1; }
size_t GetSize() const { return(_size); }
unsigned CountBits() const;
bool IsEmpty() const;
bool Resize(unsigned size_in_bits)
{
return ResizeWords( WORDSIZE_OF_BITSIZE(size_in_bits) );
}
bool ResizeWords(unsigned size_in_words)
{
if (size_in_words <= _size)
return false;
_set.resize(size_in_words, 0); // increase the vector with zeroed bits
_size = _set.size();
return true;
}
bool BitIsSet(unsigned bit_offset) const
{
bool rtn = false;
unsigned word_offset = bit_offset >> WORDROLL;
if (word_offset < GetSize())
{
bit_offset &= WORDMASK;
rtn = (( _set[word_offset] >> bit_offset ) & 1);
}
return rtn;
}
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()
{
for (word_vector::iterator wx = _set.begin(), wy = _set.end(); wx != wy; ++wx)
* wx = ~(* wx);
}
void GetWords(word_vector & vec)
{
vec.insert(vec.end(), _set.begin(),_set.end());
}
OBBitVec & operator= (const OBBitVec & bv);
OBBitVec & operator&= (const OBBitVec & bv);
OBBitVec & operator|= (const OBBitVec & bv);
OBBitVec & operator|= (int bit_offset)
{
SetBitOn(bit_offset);
return(*this);
}
OBBitVec & operator^= (const OBBitVec & bv);
OBBitVec & operator-= (const OBBitVec & bv);
OBBitVec & operator+= (const OBBitVec & bv);
bool operator[] (int bit_offset) const
{ return BitIsSet(bit_offset); }
friend OBERROR OBBitVec operator| (const OBBitVec & bv1, const OBBitVec & bv2);
friend OBERROR OBBitVec operator& (const OBBitVec & bv1,const OBBitVec & bv2);
friend OBERROR OBBitVec operator^ (const OBBitVec & bv1,const OBBitVec & bv2);
friend OBERROR OBBitVec operator- (const OBBitVec & bv1,const OBBitVec & bv2);
friend OBERROR bool operator== (const OBBitVec & bv1,const OBBitVec & bv2);
friend OBERROR bool operator< (const OBBitVec & bv1, const OBBitVec & bv2);
friend OBERROR std::istream& operator>> ( std::istream & is, OBBitVec & bv );
friend OBERROR std::ostream& operator<< ( std::ostream & os, const OBBitVec & bv ) ;
};
OBERROR double Tanimoto(const OBBitVec & bv1, const OBBitVec & bv2);
} // end namespace OpenBabel
#endif // OB_BITVEC_H
WORDMASK
#define WORDMASK
Definition bitvec.h:40
WORDROLL
#define WORDROLL
Definition bitvec.h:38
STARTWORDS
#define STARTWORDS
Definition bitvec.h:45
WORDSIZE_OF_BITSIZE
#define WORDSIZE_OF_BITSIZE(bit_size)
Definition bitvec.h:42
OpenBabel::OBBitVec
A speed-optimized vector of bits.
Definition bitvec.h:58
OpenBabel::OBBitVec::Negate
void Negate()
Inverts every bit in the vector.
Definition bitvec.h:174
OpenBabel::OBBitVec::SetBitOn
void SetBitOn(unsigned bit_offset)
Set the bit_offset 'th bit to 1.
Definition bitvec.cpp:86
OpenBabel::OBBitVec::GetSize
size_t GetSize() const
Return the number of words ( NOT the number of bits ).
Definition bitvec.h:117
OpenBabel::OBBitVec::word_vector
std::vector< uint32_t > word_vector
Definition bitvec.h:60
OpenBabel::OBBitVec::OBBitVec
OBBitVec(const OBBitVec &bv)
Copy constructor (result has same number of bits).
Definition bitvec.h:90
OpenBabel::OBBitVec::EndBit
int EndBit() const
Return the bit offset of the last bit (for iterating) i.e. -1.
Definition bitvec.h:115
OpenBabel::OBBitVec::NextBit
int NextBit(int last_bit_offset) const
Find the next true bit after last_bit_offset.
Definition bitvec.cpp:217
OpenBabel::OBBitVec::GetWords
void GetWords(word_vector &vec)
Return a copy of the internal vector of words, at the end of vec.
Definition bitvec.h:184
OpenBabel::OBBitVec::ResizeWords
bool ResizeWords(unsigned size_in_words)
Reserve space for size_in_words words.
Definition bitvec.h:137
OpenBabel::OBBitVec::Resize
bool Resize(unsigned size_in_bits)
Reserve space for size_in_bits bits.
Definition bitvec.h:128
OpenBabel::OBBitVec::OBBitVec
OBBitVec(unsigned size_in_bits)
Construct a bit vector of maxbits bits.
Definition bitvec.h:82
OpenBabel::OBBitVec::BitIsSet
bool BitIsSet(unsigned bit_offset) const
Asks if the bit_offset 'th bit is set.
Definition bitvec.h:150
OpenBabel::OBBitVec::FirstBit
int FirstBit(unsigned bit_offset=0) const
Find the first true bit at or after bit_offset.
Definition bitvec.h:108
OpenBabel::OBBitVec::OBBitVec
OBBitVec()
Construct a bit vector of the default size.
Definition bitvec.h:73
OpenBabel
Global namespace for all Open Babel code.
Definition alias.h:23
OpenBabel::Tanimoto
double Tanimoto(const OBBitVec &bv1, const OBBitVec &bv2)
The Tanimoto coefficient, which may be regarded as the proportion of the "on-bits" which are shared.
Definition bitvec.cpp:660