1 |
/********************************************************************** |
2 |
bitvec.h - Vector of bits. |
3 |
|
4 |
Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc. |
5 |
Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison |
6 |
|
7 |
This file is part of the Open Babel project. |
8 |
For more information, see <http://openbabel.sourceforge.net/> |
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 "config.h" |
24 |
|
25 |
#ifdef WIN32 |
26 |
#pragma warning (disable : 4786) |
27 |
#endif |
28 |
|
29 |
#if HAVE_IOSTREAM |
30 |
#include <iostream> |
31 |
#elif HAVE_IOSTREAM_H |
32 |
#include <iostream.h> |
33 |
#endif |
34 |
|
35 |
#include <algorithm> |
36 |
#include <vector> |
37 |
#include <string> |
38 |
|
39 |
#ifndef SETWORD |
40 |
#define SETWORD 32 |
41 |
#endif |
42 |
|
43 |
#ifndef STARTWORDS |
44 |
#define STARTWORDS 10 |
45 |
#endif //STARTBITS |
46 |
|
47 |
namespace OpenBabel |
48 |
{ |
49 |
|
50 |
// class introduction in bitvec.cpp |
51 |
class OBAPI OBBitVec |
52 |
{ |
53 |
int _size; |
54 |
std::vector<int> _set; |
55 |
public: |
56 |
OBBitVec() |
57 |
{ |
58 |
_set.resize(STARTWORDS); |
59 |
_size=_set.size(); |
60 |
Clear(); |
61 |
} |
62 |
OBBitVec(int bits) |
63 |
{ |
64 |
_set.resize(bits/SETWORD); |
65 |
_size=_set.size(); |
66 |
Clear(); |
67 |
} |
68 |
/// Copy constructor (result has same number of bits) |
69 |
OBBitVec(const OBBitVec&); |
70 |
void SetBitOn(int); |
71 |
void SetBitOff(int); |
72 |
void SetRangeOn(int, int); |
73 |
void SetRangeOff(int, int); |
74 |
void Fold(int); |
75 |
|
76 |
//! \return the index of the first bit that is set to true |
77 |
int FirstBit(int) |
78 |
{ |
79 |
return (BitIsSet(0) ? 0 : NextBit(0)); |
80 |
} |
81 |
int NextBit(int); |
82 |
//! \return the index of the last bit (for iterating) |
83 |
int EndBit() { return(-1); } |
84 |
/// \return number of 32 bit words. NOT number of bits. |
85 |
int GetSize() const { return(_size); } |
86 |
/// \return the number of bits |
87 |
int CountBits(); |
88 |
|
89 |
/// \deprecated Use IsEmpty() instead. |
90 |
bool Empty() { return(IsEmpty()); } |
91 |
bool IsEmpty(); |
92 |
///Number of bits increased if necessary but never decreased |
93 |
bool Resize(int maxbits); |
94 |
|
95 |
bool BitIsSet(int bit) |
96 |
{ |
97 |
return((bit/SETWORD >= GetSize()) ? |
98 |
false : _set[bit/SETWORD]>>(bit%SETWORD)&1); |
99 |
} |
100 |
bool BitIsOn(int bit) |
101 |
{ |
102 |
return((bit/SETWORD >= GetSize()) ? |
103 |
false : _set[bit/SETWORD]>>(bit%SETWORD)&1); |
104 |
} |
105 |
|
106 |
void FromVecInt(std::vector<int>&); |
107 |
void FromString(std::string&,int); |
108 |
void ToVecInt(std::vector<int>&); |
109 |
void Clear(void); |
110 |
//! Inverts every bit in the vector |
111 |
void Negate() |
112 |
{ |
113 |
for (int i= 0; i != _size; i++) |
114 |
{ |
115 |
_set[i] = ~_set[i]; |
116 |
} |
117 |
} |
118 |
|
119 |
///Assignment operator but number of bits is not reduced |
120 |
OBBitVec &operator= (const OBBitVec &); |
121 |
OBBitVec &operator&= (OBBitVec &); |
122 |
OBBitVec &operator|= (OBBitVec &); |
123 |
OBBitVec &operator|= (const int i) |
124 |
{ |
125 |
SetBitOn(i); |
126 |
return(*this); |
127 |
} |
128 |
OBBitVec &operator^= (OBBitVec &); |
129 |
OBBitVec &operator-= (OBBitVec &); |
130 |
OBBitVec &operator+= (OBBitVec &bv); |
131 |
bool operator[] (int bit) |
132 |
{ |
133 |
return((bit/SETWORD >= GetSize()) ? |
134 |
false : _set[bit/SETWORD]>>(bit%SETWORD)&1); |
135 |
} |
136 |
|
137 |
friend OBBitVec operator| (OBBitVec &, OBBitVec &); |
138 |
friend OBBitVec operator& (OBBitVec &,OBBitVec &); |
139 |
friend OBBitVec operator^ (OBBitVec &,OBBitVec &); |
140 |
friend OBBitVec operator- (OBBitVec &,OBBitVec &); |
141 |
friend bool operator== (const OBBitVec &,const OBBitVec &); |
142 |
|
143 |
friend std::istream& operator>> ( std::istream&, OBBitVec& ); |
144 |
friend std::ostream& operator<< ( std::ostream&, const OBBitVec& ) ; |
145 |
|
146 |
///Access to data in word size pieces CM |
147 |
void GetWords(std::vector<unsigned int>& vec) |
148 |
{ |
149 |
std::vector<int>::iterator itr; |
150 |
for(itr=_set.begin();itr!=_set.end();itr++) |
151 |
vec.push_back(*itr); |
152 |
} |
153 |
}; |
154 |
|
155 |
OBAPI extern void ThrowError(char *); |
156 |
///This function can change the size of second parameter. There is an alternative with different parameters. |
157 |
OBAPI double Tanimoto(OBBitVec&,OBBitVec&); |
158 |
|
159 |
} |
160 |
|
161 |
#endif // OB_BITVEC_H |
162 |
|
163 |
//! \file bitvec.h |
164 |
//! \brief Fast and efficient bitstring class |