bitvec.h Source File

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

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-2006 by Geoffrey R. Hutchison
    00006
    00007 This file is part of the Open Babel project.
    00008 For more information, see <http://openbabel.org/>
    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 <openbabel/babelconfig.h>
    00024
    00025 #include <vector>
    00026 #include <string>
    00027
    00028 #ifndef USE_64BIT_INTEGER
    00029 // For 32-bit architecture
    00030 #define SETWORD 32
    00031 // SETWORD = 2 ^ WORDROLL
    00032 #define WORDROLL 5
    00033 // WORDMASK = SETWORD - 1
    00034 #define WORDMASK 31
    00035 #else
    00036 // For 64-bit architecture
    00037 #define SETWORD 64
    00038 // SETWORD = 2 ^ WORDROLL
    00039 #define WORDROLL 6
    00040 // WORDMASK = SETWORD - 1
    00041 #define WORDMASK 63
    00042 #endif // 64 bit
    00043
    00044 #define WORDSIZE_OF_BITSIZE( bit_size ) ( ( bit_size >> WORDROLL ) + (( bit_size & WORDMASK ) ? 1 : 0) )
    00045
    00046 #ifndef STARTWORDS
    00047 #define STARTWORDS 10
    00048 #endif // STARTWORDS
    00049
    00050 namespace OpenBabel
    00051   {
    00053
    00061   class OBERROR OBBitVec
    00062     {
    00063     public:
    00064           typedef std::vector<unsigned> word_vector;
    00065
    00066         private:
    00068       size_t _size; //was unsigned
    00070       word_vector       _set;
    00071
    00072     public:
    00074
    00077       OBBitVec()
    00078           :_set(STARTWORDS, 0)
    00079         { _size = _set.size(); }
    00081
    00086       OBBitVec(unsigned size_in_bits)
    00087           :_set(WORDSIZE_OF_BITSIZE(size_in_bits), 0)
    00088         { _size = _set.size(); }
    00090
    00094       OBBitVec(const OBBitVec & bv)
    00095           :_size(0)
    00096                 { (*this) = bv; }
    00098       void SetBitOn(unsigned bit_offset);
    00100       void SetBitOff(unsigned bit_offset);
    00102       void SetRangeOn(unsigned lo_bit_offset, unsigned hi_bit_offset);
    00104       void SetRangeOff(unsigned lo_bit_offset, unsigned hi_bit_offset);
    00106       void Fold(unsigned new_bit_size);
    00108
    00112       int FirstBit(unsigned bit_offset = 0) const
    00113         {
    00114           return (BitIsSet(bit_offset) ? 0  : NextBit(bit_offset));
    00115         }
    00117       int NextBit(int last_bit_offset) const;
    00119       int EndBit() const {  return -1; }
    00121       size_t GetSize() const    { return(_size);    }
    00123       unsigned CountBits() const;
    00124
    00126       bool Empty() const   { return(IsEmpty()); }
    00128       bool IsEmpty() const;
    00130
    00134       bool Resize(unsigned size_in_bits)
    00135                 {
    00136                 return ResizeWords( WORDSIZE_OF_BITSIZE(size_in_bits) );
    00137                 }
    00139
    00143           bool ResizeWords(unsigned size_in_words)
    00144                 {
    00145                 if (size_in_words <= _size)
    00146                   return false;
    00147                 _set.resize(size_in_words, 0); // increase the vector with zeroed bits
    00148                 _size = _set.size();
    00149                 return true;
    00150                 }
    00152
    00156       bool BitIsSet(unsigned bit_offset) const
    00157         {
    00158                   bool rtn = false;
    00159                   unsigned word_offset = bit_offset >> WORDROLL;
    00160                   if (word_offset < GetSize())
    00161                         {
    00162                           bit_offset &= WORDMASK;
    00163                           rtn = (( _set[word_offset] >> bit_offset ) & 1);
    00164                         }
    00165           return rtn;
    00166         }
    00168       bool BitIsOn(int bit_offset) const
    00169         { return BitIsSet((unsigned)bit_offset); }
    00170
    00172           void FromVecInt(const std::vector<int> & bit_offsets);
    00174           void FromString(const std::string & line, int bits);
    00176           void ToVecInt(std::vector<int> & bit_offsets) const;
    00178       void Clear();
    00180
    00184       void Negate()
    00185         {
    00186                   for (word_vector::iterator wx = _set.begin(), wy = _set.end(); wx != wy; ++wx)
    00187                     * wx = ~(* wx);
    00188         }
    00190
    00194       void GetWords(word_vector & vec)
    00195         {
    00196                 vec.insert(vec.end(), _set.begin(),_set.end());
    00197         }
    00198
    00200       OBBitVec & operator= (const OBBitVec & bv);
    00202       OBBitVec & operator&= (const OBBitVec & bv);
    00204       OBBitVec & operator|= (const OBBitVec & bv);
    00206
    00208       OBBitVec & operator|= (int bit_offset)
    00209         {
    00210           SetBitOn(bit_offset);
    00211           return(*this);
    00212         }
    00214       OBBitVec & operator^= (const OBBitVec & bv);
    00216       OBBitVec & operator-= (const OBBitVec & bv);
    00218       OBBitVec & operator+= (const OBBitVec & bv);
    00220
    00224       bool operator[] (int bit_offset) const
    00225         { return BitIsSet(bit_offset); }
    00226
    00228       friend OBERROR OBBitVec operator| (const OBBitVec & bv1, const OBBitVec & bv2);
    00230       friend OBERROR OBBitVec operator& (const OBBitVec & bv1,const OBBitVec & bv2);
    00232       friend OBERROR OBBitVec operator^ (const OBBitVec & bv1,const OBBitVec & bv2);
    00234       friend OBERROR OBBitVec operator- (const OBBitVec & bv1,const OBBitVec & bv2);
    00236       friend OBERROR bool operator== (const OBBitVec & bv1,const OBBitVec & bv2);
    00238       friend OBERROR bool operator< (const OBBitVec & bv1, const OBBitVec & bv2);
    00239
    00241       friend OBERROR std::istream& operator>> ( std::istream & is, OBBitVec & bv );
    00243       friend OBERROR std::ostream& operator<< ( std::ostream & os, const OBBitVec & bv ) ;
    00244     };
    00245
    00247   OBERROR double Tanimoto(const OBBitVec & bv1, const OBBitVec & bv2);
    00248
    00249   } // end namespace OpenBabel
    00250
    00251 #endif // OB_BITVEC_H
    00252

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines