bitvec.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 bitvec.h - Vector of bits.
00003  
00004 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
00005 Some portions Copyright (C) 2001-2005 by Geoffrey R. Hutchison
00006  
00007 This file is part of the Open Babel project.
00008 For more information, see <http://openbabel.sourceforge.net/>
00009  
00010 This program is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation version 2 of the License.
00013  
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 ***********************************************************************/
00019 
00020 #ifndef OB_BITVEC_H
00021 #define OB_BITVEC_H
00022 
00023 #include "babelconfig.h"
00024 
00025 #ifdef WIN32
00026 #pragma warning (disable : 4786)
00027 #endif
00028 
00029 #if HAVE_IOSTREAM
00030 #include <iostream>
00031 #elif HAVE_IOSTREAM_H
00032 #include <iostream.h>
00033 #endif
00034 
00035 #include <algorithm>
00036 #include <vector>
00037 #include <string>
00038 
00039 #ifndef SETWORD
00040 #define SETWORD 32
00041 #endif
00042 
00043 #ifndef STARTWORDS
00044 #define STARTWORDS 10
00045 #endif //STARTBITS
00046 
00047 namespace OpenBabel
00048 {
00049 
00050 // class introduction in bitvec.cpp
00051 class OBAPI OBBitVec
00052 {
00053     int _size;
00054     std::vector<int> _set;
00055 public:
00056     OBBitVec()
00057     {
00058         _set.resize(STARTWORDS);
00059         _size=_set.size();
00060         Clear();
00061     }
00062     OBBitVec(int bits)
00063     {
00064         _set.resize(bits/SETWORD);
00065         _size=_set.size();
00066         Clear();
00067     }
00069     OBBitVec(const OBBitVec&);
00070     void SetBitOn(int);
00071     void SetBitOff(int);
00072     void SetRangeOn(int, int);
00073     void SetRangeOff(int, int);
00074     void Fold(int);
00075 
00077     int FirstBit(int)
00078     {
00079         return (BitIsSet(0) ? 0  : NextBit(0));
00080     }
00081     int NextBit(int);
00083     int EndBit()    {        return(-1);    }
00085     int GetSize() const    { return(_size);    }
00087     int CountBits();
00088 
00090     bool Empty()   { return(IsEmpty()); }
00091     bool IsEmpty();
00093     bool Resize(int maxbits);
00094 
00095     bool BitIsSet(int bit)
00096     {
00097         return((bit/SETWORD >= GetSize()) ?
00098                false : _set[bit/SETWORD]>>(bit%SETWORD)&1);
00099     }
00100     bool BitIsOn(int bit)
00101     {
00102         return((bit/SETWORD >= GetSize()) ?
00103                false : _set[bit/SETWORD]>>(bit%SETWORD)&1);
00104     }
00105 
00106     void FromVecInt(std::vector<int>&);
00107     void FromString(std::string&,int);
00108     void ToVecInt(std::vector<int>&);
00109     void Clear(void);
00111     void Negate()
00112     {
00113         for (int i= 0; i != _size; i++)
00114         {
00115             _set[i] = ~_set[i];
00116         }
00117     }
00118 
00120     OBBitVec &operator= (const OBBitVec &);
00121     OBBitVec &operator&= (OBBitVec &);
00122     OBBitVec &operator|= (OBBitVec &);
00123     OBBitVec &operator|= (const int i)
00124     {
00125         SetBitOn(i);
00126         return(*this);
00127     }
00128     OBBitVec &operator^= (OBBitVec &);
00129     OBBitVec &operator-= (OBBitVec &);
00130     OBBitVec &operator+= (OBBitVec &bv);
00131     bool operator[] (int bit)
00132     {
00133         return((bit/SETWORD >= GetSize()) ?
00134                false : _set[bit/SETWORD]>>(bit%SETWORD)&1);
00135     }
00136 
00137     friend OBBitVec operator| (OBBitVec &, OBBitVec &);
00138     friend OBBitVec operator& (OBBitVec &,OBBitVec &);
00139     friend OBBitVec operator^ (OBBitVec &,OBBitVec &);
00140     friend OBBitVec operator- (OBBitVec &,OBBitVec &);
00141     friend bool operator== (const OBBitVec &,const OBBitVec &);
00142 
00143     friend std::istream& operator>> ( std::istream&, OBBitVec& );
00144     friend std::ostream& operator<< ( std::ostream&, const OBBitVec& ) ;
00145         
00147     void GetWords(std::vector<unsigned int>& vec)
00148       {
00149         std::vector<int>::iterator itr;
00150         for(itr=_set.begin();itr!=_set.end();itr++)
00151           vec.push_back(*itr);
00152       }
00153 };
00154 
00155 OBAPI extern void ThrowError(char *);
00157 OBAPI double Tanimoto(OBBitVec&,OBBitVec&);
00158 
00159 }
00160 
00161 #endif // OB_BITVEC_H
00162