From 5587bc66a76d86f36e5ddb030593dc908a73a6b2 Mon Sep 17 00:00:00 2001 From: Greg Landrum Date: Sat, 24 Mar 2007 05:43:22 +0000 Subject: [PATCH] store an explicit member with the number of on bits; this enables some forthcoming optimizations --- Code/DataStructs/ExplicitBitVect.cpp | 18 +++++++++++++----- Code/DataStructs/ExplicitBitVect.h | 7 ++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Code/DataStructs/ExplicitBitVect.cpp b/Code/DataStructs/ExplicitBitVect.cpp index bd826e15c..40a51de8d 100755 --- a/Code/DataStructs/ExplicitBitVect.cpp +++ b/Code/DataStructs/ExplicitBitVect.cpp @@ -18,23 +18,25 @@ ExplicitBitVect::ExplicitBitVect(const std::string s) { - d_size=0;dp_bits = 0; + d_size=0;dp_bits = 0;d_numOnBits=0; InitFromText(s.c_str(),s.length()); } ExplicitBitVect::ExplicitBitVect(const char *data,const unsigned int dataLen) { - d_size=0;dp_bits = 0; + d_size=0;dp_bits = 0;d_numOnBits=0; InitFromText(data,dataLen); } ExplicitBitVect::ExplicitBitVect(const ExplicitBitVect& other){ d_size = other.d_size; dp_bits = new boost::dynamic_bitset<>(*(other.dp_bits)); + d_numOnBits=other.d_numOnBits; }; ExplicitBitVect& ExplicitBitVect::operator=(const ExplicitBitVect& other){ d_size = other.d_size; dp_bits = new boost::dynamic_bitset<>(*(other.dp_bits)); + d_numOnBits=other.d_numOnBits; return *this; }; bool ExplicitBitVect::operator[] (const unsigned int which) const { @@ -51,6 +53,7 @@ ExplicitBitVect::ExplicitBitVect(const char *data,const unsigned int dataLen) return true; } else { (*dp_bits)[which] = 1; + ++d_numOnBits; return false; } }; @@ -60,6 +63,7 @@ ExplicitBitVect::ExplicitBitVect(const char *data,const unsigned int dataLen) } if((bool)(*dp_bits)[which]){ (*dp_bits)[which] = 0; + --d_numOnBits; return true; } else { return false; @@ -75,24 +79,28 @@ ExplicitBitVect::ExplicitBitVect(const char *data,const unsigned int dataLen) ExplicitBitVect ExplicitBitVect::operator^ (const ExplicitBitVect &other) const { ExplicitBitVect ans(d_size); *(ans.dp_bits) = (*dp_bits) ^ *(other.dp_bits); + ans.d_numOnBits=dp_bits->count(); return(ans); }; ExplicitBitVect ExplicitBitVect::operator& (const ExplicitBitVect &other) const { ExplicitBitVect ans(d_size); *(ans.dp_bits) = (*dp_bits) & *(other.dp_bits); + ans.d_numOnBits=dp_bits->count(); return(ans); }; ExplicitBitVect ExplicitBitVect::operator| (const ExplicitBitVect &other) const { ExplicitBitVect ans(d_size); *(ans.dp_bits) = (*dp_bits) | *(other.dp_bits); + ans.d_numOnBits=dp_bits->count(); return(ans); }; ExplicitBitVect ExplicitBitVect::operator~ () const { ExplicitBitVect ans(d_size); *(ans.dp_bits) = ~(*dp_bits); + ans.d_numOnBits=dp_bits->count(); return(ans); }; @@ -100,10 +108,10 @@ ExplicitBitVect::ExplicitBitVect(const char *data,const unsigned int dataLen) return d_size; }; const unsigned int ExplicitBitVect::GetNumOnBits() const { - return dp_bits->count(); + return d_numOnBits; }; const unsigned int ExplicitBitVect::GetNumOffBits() const { - return d_size - dp_bits->count(); + return d_size - d_numOnBits; }; // the contents of v are blown out @@ -120,9 +128,9 @@ ExplicitBitVect::ExplicitBitVect(const char *data,const unsigned int dataLen) d_size = size; if(dp_bits) delete dp_bits; dp_bits = new boost::dynamic_bitset<>(size); + d_numOnBits=0; }; - ExplicitBitVect::~ExplicitBitVect() { if(dp_bits) delete dp_bits; }; diff --git a/Code/DataStructs/ExplicitBitVect.h b/Code/DataStructs/ExplicitBitVect.h index 100e6bfc8..f0fcb7a28 100755 --- a/Code/DataStructs/ExplicitBitVect.h +++ b/Code/DataStructs/ExplicitBitVect.h @@ -20,9 +20,9 @@ */ class ExplicitBitVect : public BitVect { public: - ExplicitBitVect() : dp_bits(0), d_size(0) {}; + ExplicitBitVect() : dp_bits(0), d_size(0), d_numOnBits(0) {}; //! initialize with a particular size; - explicit ExplicitBitVect(unsigned int size) : dp_bits(0), d_size(0) {_InitForSize(size);}; + explicit ExplicitBitVect(unsigned int size) : dp_bits(0), d_size(0), d_numOnBits(0) {_InitForSize(size);}; ExplicitBitVect(const ExplicitBitVect& other); //! construct from a string pickle ExplicitBitVect(const std::string); @@ -48,12 +48,13 @@ public: void GetOnBits (IntVect& v) const; // FIX: complete these - void ClearBits() { ; }; + void ClearBits() { dp_bits->reset(); }; std::string ToString() const; boost::dynamic_bitset<> *dp_bits; //!< our raw storage private: unsigned int d_size; + unsigned int d_numOnBits; void _InitForSize(const unsigned int size); };