count bug fix

This commit is contained in:
邹晓航
2014-10-09 10:29:18 +08:00
parent 0ef6057041
commit fce433c674

View File

@@ -3,6 +3,7 @@
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>
#include "Allocator.h"
@@ -10,6 +11,8 @@
namespace TinySTL{
//bitmap<61>ὫN<E1BDAB>ϵ<EFBFBD><CFB5><EFBFBD>8<EFBFBD>ı<EFBFBD><C4B1><EFBFBD>
//<2F><><EFBFBD><EFBFBD>bitmapʵ<70><CAB5><EFBFBD>ϻ<EFBFBD><CFBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bit<69><74><EFBFBD>Ǵ<EFBFBD><C7B4>ڵ<EFBFBD><DAB5><EFBFBD>N<EFBFBD><4E>
template<size_t N>
class bitmap{
public:
@@ -28,6 +31,7 @@ namespace TinySTL{
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{
THROW(pos);
const auto nth = getNth(pos);
const auto mth = getMth(pos);
uint8_t *ptr = start_ + nth;
@@ -88,6 +92,10 @@ namespace TinySTL{
start_ = dataAllocator::allocate(n);
finish_ = uninitialized_fill_n(start_, n, val);
}
void THROW(size_t n)const{
if (!(0 <= n && n < size()))
throw std::out_of_range("Out Of Range");
}
};// end of bitmap
template<size_t N>
@@ -96,7 +104,7 @@ namespace TinySTL{
}
template<size_t N>
bitmap<N>& bitmap<N>::set(size_t pos, bool val){
assert(pos >= 0);
THROW(pos);
const auto nth = getNth(pos);
const auto mth = getMth(pos);
uint8_t *ptr = start_ + nth;//get the nth uint8_t
@@ -119,6 +127,7 @@ namespace TinySTL{
}
template<size_t N>
bitmap<N>& bitmap<N>::flip(size_t pos){
THROW(pos);
const auto nth = getNth(pos);
const auto mth = getMth(pos);
uint8_t *ptr = start_ + nth;
@@ -134,10 +143,11 @@ namespace TinySTL{
uint8_t *ptr = start_;
size_t sum = 0;
for (; ptr != finish_; ++ptr){
uint8_t n = *ptr;
while (n){
++sum;
n = n >> 1;
for (int i = 0; i != 8; ++i){
uint8_t t = getMask(*ptr, i);
if (t){
++sum;
}
}
}
return sum;