00001 // 00002 // Copyright (c) 2003-2008 greg Landrum and Rational Discovery LLC 00003 // 00004 // @@ All Rights Reserved @@ 00005 // This file is part of the RDKit. 00006 // The contents are covered by the terms of the BSD license 00007 // which is included in the file license.txt, found at the root 00008 // of the RDKit source tree. 00009 // 00010 #ifndef __RD_SPARSEBITVECTS_H__ 00011 #define __RD_SPARSEBITVECTS_H__ 00012 00013 #include "BitVect.h" 00014 00015 #include <set> 00016 using std::set; 00017 #include <iterator> 00018 #include <algorithm> 00019 00020 00021 00022 typedef set<int> IntSet; 00023 typedef IntSet::iterator IntSetIter; 00024 typedef IntSet::const_iterator IntSetConstIter; 00025 00026 //! a class for bit vectors that are sparsely occupied. 00027 /*! 00028 SparseBitVect objects store only their on bits, in an 00029 std::set. 00030 00031 They are, as you might expect, quite memory efficient for sparsely populated 00032 vectors but become rather a nightmare if they need to be negated. 00033 00034 */ 00035 class SparseBitVect : public BitVect{ 00036 public: 00037 SparseBitVect() : dp_bits(0), d_size(0) {}; 00038 //! initialize with a particular size; 00039 explicit SparseBitVect(unsigned int size): dp_bits(0), d_size(0) {_initForSize(size); }; 00040 00041 //! copy constructor 00042 SparseBitVect(const SparseBitVect& other){ 00043 d_size=0;dp_bits = 0; 00044 _initForSize(other.getNumBits()); 00045 IntSet *bv=other.dp_bits; 00046 std::copy(bv->begin(),bv->end(),std::inserter(*dp_bits,dp_bits->end())); 00047 } 00048 //! construct from a string pickle 00049 SparseBitVect(const std::string &); 00050 //! construct from a text pickle 00051 SparseBitVect(const char *data,const unsigned int dataLen); 00052 00053 SparseBitVect& operator=(const SparseBitVect&); 00054 ~SparseBitVect(){ delete dp_bits; }; 00055 00056 bool operator[](const unsigned int which) const; 00057 SparseBitVect operator| (const SparseBitVect&) const; 00058 SparseBitVect operator& (const SparseBitVect&) const; 00059 SparseBitVect operator^ (const SparseBitVect&) const; 00060 SparseBitVect operator~ () const; 00061 00062 //! returns a (const) pointer to our raw storage 00063 const IntSet *getBitSet() const { return dp_bits;} 00064 00065 unsigned int getNumBits() const { return d_size; }; 00066 bool setBit(const unsigned int which); 00067 bool setBit(const IntSetIter which); 00068 bool unsetBit(const unsigned int which); 00069 bool getBit (const unsigned int which) const; 00070 bool getBit(const IntVectIter which) const; 00071 bool getBit(const IntSetIter which) const; 00072 00073 unsigned int getNumOnBits() const { return dp_bits->size(); }; 00074 unsigned int getNumOffBits() const { return d_size - dp_bits->size(); }; 00075 00076 std::string toString() const; 00077 00078 void getOnBits (IntVect& v) const; 00079 void clearBits() { dp_bits->clear(); }; 00080 IntSet *dp_bits; //!< our raw data, exposed for the sake of efficiency 00081 00082 bool operator==(const SparseBitVect &o) const { 00083 return *dp_bits==*o.dp_bits; 00084 } 00085 bool operator!=(const SparseBitVect &o) const { 00086 return *dp_bits!=*o.dp_bits; 00087 } 00088 00089 00090 private: 00091 unsigned int d_size; 00092 void _initForSize(const unsigned int size); 00093 }; 00094 00095 #endif
1.7.1