SparseBitVect.h

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

Generated on Fri Apr 3 06:03:02 2009 for RDCode by  doxygen 1.5.6