Open Babel  3.0
bitvec.h
Go to the documentation of this file.
1 /**********************************************************************
2 bitvec.h - Vector of bits.
3 
4 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
5 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
6 
7 This file is part of the Open Babel project.
8 For more information, see <http://openbabel.org/>
9 
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation version 2 of the License.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 ***********************************************************************/
19 
20 #ifndef OB_BITVEC_H
21 #define OB_BITVEC_H
22 
23 #include <openbabel/babelconfig.h>
24 
25 #include <vector>
26 #include <string>
27 
28 #if defined(_MSC_VER) && _MSC_VER <= 1600
29  // Assuming 32bit integer
30  typedef unsigned uint32_t;
31 #else
32  #include <inttypes.h>
33 #endif
34 
35 // Use uint32_t
36 #define SETWORD 32
37 // SETWORD = 2 ^ WORDROLL
38 #define WORDROLL 5
39 // WORDMASK = SETWORD - 1
40 #define WORDMASK 31
41 
42 #define WORDSIZE_OF_BITSIZE( bit_size ) ( ( bit_size >> WORDROLL ) + (( bit_size & WORDMASK ) ? 1 : 0) )
43 
44 #ifndef STARTWORDS
45 #define STARTWORDS 10
46 #endif // STARTWORDS
47 
48 namespace OpenBabel
49  {
51 
57  class OBERROR OBBitVec
58  {
59  public:
60  typedef std::vector<uint32_t> word_vector;
61 
62  private:
64  size_t _size; //was unsigned
66  word_vector _set;
67 
68  public:
70 
74  :_set(STARTWORDS, 0)
75  { _size = _set.size(); }
77 
82  OBBitVec(unsigned size_in_bits)
83  :_set(WORDSIZE_OF_BITSIZE(size_in_bits), 0)
84  { _size = _set.size(); }
86 
90  OBBitVec(const OBBitVec & bv)
91  :_size(0)
92  { (*this) = bv; }
94  void SetBitOn(unsigned bit_offset);
96  void SetBitOff(unsigned bit_offset);
98  void SetRangeOn(unsigned lo_bit_offset, unsigned hi_bit_offset);
100  void SetRangeOff(unsigned lo_bit_offset, unsigned hi_bit_offset);
102  void Fold(unsigned new_bit_size);
104 
108  int FirstBit(unsigned bit_offset = 0) const
109  {
110  return (BitIsSet(bit_offset) ? 0 : NextBit(bit_offset));
111  }
113  int NextBit(int last_bit_offset) const;
115  int EndBit() const { return -1; }
117  size_t GetSize() const { return(_size); }
119  unsigned CountBits() const;
120 
122  bool IsEmpty() const;
124 
128  bool Resize(unsigned size_in_bits)
129  {
130  return ResizeWords( WORDSIZE_OF_BITSIZE(size_in_bits) );
131  }
133 
137  bool ResizeWords(unsigned size_in_words)
138  {
139  if (size_in_words <= _size)
140  return false;
141  _set.resize(size_in_words, 0); // increase the vector with zeroed bits
142  _size = _set.size();
143  return true;
144  }
146 
150  bool BitIsSet(unsigned bit_offset) const
151  {
152  bool rtn = false;
153  unsigned word_offset = bit_offset >> WORDROLL;
154  if (word_offset < GetSize())
155  {
156  bit_offset &= WORDMASK;
157  rtn = (( _set[word_offset] >> bit_offset ) & 1);
158  }
159  return rtn;
160  }
162  void FromVecInt(const std::vector<int> & bit_offsets);
164  void FromString(const std::string & line, int bits);
166  void ToVecInt(std::vector<int> & bit_offsets) const;
168  void Clear();
170 
174  void Negate()
175  {
176  for (word_vector::iterator wx = _set.begin(), wy = _set.end(); wx != wy; ++wx)
177  * wx = ~(* wx);
178  }
180 
184  void GetWords(word_vector & vec)
185  {
186  vec.insert(vec.end(), _set.begin(),_set.end());
187  }
188 
190  OBBitVec & operator= (const OBBitVec & bv);
192  OBBitVec & operator&= (const OBBitVec & bv);
194  OBBitVec & operator|= (const OBBitVec & bv);
196 
198  OBBitVec & operator|= (int bit_offset)
199  {
200  SetBitOn(bit_offset);
201  return(*this);
202  }
204  OBBitVec & operator^= (const OBBitVec & bv);
206  OBBitVec & operator-= (const OBBitVec & bv);
208  OBBitVec & operator+= (const OBBitVec & bv);
210 
214  bool operator[] (int bit_offset) const
215  { return BitIsSet(bit_offset); }
216 
218  friend OBERROR OBBitVec operator| (const OBBitVec & bv1, const OBBitVec & bv2);
220  friend OBERROR OBBitVec operator& (const OBBitVec & bv1,const OBBitVec & bv2);
222  friend OBERROR OBBitVec operator^ (const OBBitVec & bv1,const OBBitVec & bv2);
224  friend OBERROR OBBitVec operator- (const OBBitVec & bv1,const OBBitVec & bv2);
226  friend OBERROR bool operator== (const OBBitVec & bv1,const OBBitVec & bv2);
228  friend OBERROR bool operator< (const OBBitVec & bv1, const OBBitVec & bv2);
229 
231  friend OBERROR std::istream& operator>> ( std::istream & is, OBBitVec & bv );
233  friend OBERROR std::ostream& operator<< ( std::ostream & os, const OBBitVec & bv ) ;
234  };
235 
237  OBERROR double Tanimoto(const OBBitVec & bv1, const OBBitVec & bv2);
238 
239  } // end namespace OpenBabel
240 
241 #endif // OB_BITVEC_H
242 
OBBitVec()
Construct a bit vector of the default size.
Definition: bitvec.h:73
#define WORDSIZE_OF_BITSIZE(bit_size)
Definition: bitvec.h:42
#define WORDROLL
Definition: bitvec.h:38
OBBitVec operator &(const OBBitVec &bv1, const OBBitVec &bv2)
Definition: bitvec.cpp:487
bool operator<(const OBBitVec &bv1, const OBBitVec &bv2)
Definition: bitvec.cpp:556
bool Resize(unsigned size_in_bits)
Reserve space for size_in_bits bits.
Definition: bitvec.h:128
bool BitIsSet(unsigned bit_offset) const
Asks if the bit_offset &#39;th bit is set.
Definition: bitvec.h:150
void Negate()
Inverts every bit in the vector.
Definition: bitvec.h:174
OBBitVec operator|(const OBBitVec &bv1, const OBBitVec &bv2)
Definition: bitvec.cpp:475
bool ResizeWords(unsigned size_in_words)
Reserve space for size_in_words words.
Definition: bitvec.h:137
OBBitVec(const OBBitVec &bv)
Copy constructor (result has same number of bits)
Definition: bitvec.h:90
#define STARTWORDS
Definition: bitvec.h:45
A speed-optimized vector of bits.
Definition: bitvec.h:57
vector3 operator-(const vector3 &v1, const vector3 &v2)
Vector subtraction.
Definition: vector3.h:320
size_t GetSize() const
Return the number of words ( NOT the number of bits ).
Definition: bitvec.h:117
OBBitVec(unsigned size_in_bits)
Construct a bit vector of maxbits bits.
Definition: bitvec.h:82
OBBitVec operator^(const OBBitVec &bv1, const OBBitVec &bv2)
Definition: bitvec.cpp:499
void GetWords(word_vector &vec)
Return a copy of the internal vector of words, at the end of vec.
Definition: bitvec.h:184
#define WORDMASK
Definition: bitvec.h:40
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
std::ostream & operator<<(std::ostream &, const vector3 &)
Prints a representation of the vector as a row vector of the form "<0.1,1,2>".
Definition: vector3.cpp:109
std::istream & operator>>(std::istream &is, OBBitVec &bv)
Definition: bitvec.cpp:592
int EndBit() const
Return the bit offset of the last bit (for iterating) i.e. -1.
Definition: bitvec.h:115
std::vector< uint32_t > word_vector
Definition: bitvec.h:60
int FirstBit(unsigned bit_offset=0) const
Find the first true bit at or after bit_offset.
Definition: bitvec.h:108
bool operator==(const OBBitVec &bv1, const OBBitVec &bv2)
Definition: bitvec.cpp:525
Global namespace for all Open Babel code.
Definition: alias.h:22