From 206e98126d344f2427f1d40bd66dda7f7d7987ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Wed, 8 Oct 2014 21:12:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=B0=86bitmap=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E6=88=90string=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Bitmap.h | 78 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 11 deletions(-) diff --git a/TinySTL/Bitmap.h b/TinySTL/Bitmap.h index f25e588..c8bd46f 100644 --- a/TinySTL/Bitmap.h +++ b/TinySTL/Bitmap.h @@ -28,7 +28,15 @@ namespace TinySTL{ size_t count() const; size_t size() const{ return size_; } //Returns whether the bit at position pos is set (i.e., whether it is one). - bool test(size_t pos) const;//TODO + bool test(size_t pos) const{ + const auto nth = getNth(pos); + const auto mth = getMth(pos); + uint8_t *ptr = start_ + nth; + uint8_t temp = getMask(*ptr, mth); + if (temp) + return true; + return false; + } //Returns whether any of the bits is set (i.e., whether at least one bit in the bitset is set to one). bool any() const; //Returns whether none of the bits is set (i.e., whether all bits in the bitset have a value of zero). @@ -40,26 +48,26 @@ namespace TinySTL{ uninitialized_fill_n(start_, sizeOfUINT8_, ~0); return *this; } - bitmap& set(size_t pos, bool val = true);//TODO + bitmap& set(size_t pos, bool val = true); bitmap& reset(){ uninitialized_fill_n(start_, sizeOfUINT8_, 0); return *this; } - bitmap& reset(size_t pos);//TODO - bitmap& flip();//TODO - bitmap& flip(size_t pos);//TODO + bitmap& reset(size_t pos); + bitmap& flip(); + bitmap& flip(size_t pos); - std::string to_string() const;//TODO + std::string to_string() const; template - friend std::ostream& operator <<(std::ostream& os, const bitmap& bm);//TODO + friend std::ostream& operator <<(std::ostream& os, const bitmap& bm); private: size_t roundUp8(size_t bytes){ return ((bytes + EAlign::ALIGN - 1) & ~(EAlign::ALIGN - 1)); } //将i的第nth为置为newVal void setNthInInt8(uint8_t& i, size_t nth, bool newVal){//nth从0开始 - uint8_t temp = i & (1 << nth); //取出i的第nth位 + uint8_t temp = getMask(i, nth); if ((bool)temp == newVal){ return; }else{ @@ -71,10 +79,12 @@ namespace TinySTL{ } } } + //取出i的第nth位 + uint8_t getMask(uint8_t i, size_t nth)const{ return (i & (1 << nth)); } //将第n个位置转换为其在第几个uint8_t中 - size_t getNth(size_t n){ return (n / 8); } + size_t getNth(size_t n)const{ return (n / 8); } //将第n个位置转换为其在第N个uint8_t中的第几个bit上 - size_t getMth(size_t n){ return (n % EAlign::ALIGN); } + size_t getMth(size_t n)const{ return (n % EAlign::ALIGN); } void allocateAndFillN(size_t n, uint8_t val){ start_ = dataAllocator::allocate(n); finish_ = uninitialized_fill_n(start_, n, val); @@ -86,7 +96,7 @@ namespace TinySTL{ allocateAndFillN(sizeOfUINT8_, 0); } template - bitmap& bitmap::set(size_t pos, bool val = true){ + bitmap& bitmap::set(size_t pos, bool val){ assert(pos >= 0); const auto nth = getNth(pos); const auto mth = getMth(pos); @@ -95,6 +105,32 @@ namespace TinySTL{ return *this; } template + bitmap& bitmap::reset(size_t pos){ + set(pos, false); + return *this; + } + template + bitmap& bitmap::flip(){ + uint8_t *ptr = start_; + for (; ptr != finish_; ++ptr){ + uint8_t n = *ptr; + *ptr = ~n; + } + return *this; + } + template + bitmap& bitmap::flip(size_t pos){ + const auto nth = getNth(pos); + const auto mth = getMth(pos); + uint8_t *ptr = start_ + nth; + uint8_t temp = getMask(*ptr, mth); + if (temp) + setNthInInt8(*ptr, mth, false); + else + setNthInInt8(*ptr, mth, true); + return *this; + } + template size_t bitmap::count() const{ uint8_t *ptr = start_; size_t sum = 0; @@ -131,6 +167,26 @@ namespace TinySTL{ bool bitmap::none() const{ return !any(); } + template + std::string bitmap::to_string() const{ + std::string str; + uint8_t *ptr = start_; + for (; ptr != finish_; ++ptr){ + uint8_t n = *ptr; + for (int i = 0; i != 8; ++i){ + uint8_t t = getMask(n, i); + if (t) str += "1"; + else str += "0"; + } + } + return str; + } + + template + std::ostream& operator <<(std::ostream& os, const bitmap& bm){ + os << bm.to_string(); + return os; + } } #endif \ No newline at end of file