首页 > 代码库 > 比特数组

比特数组

  1 class BitArray  2 {  3 public:  4     enum  5     {  6         bits_per_long = sizeof(unsigned long)*8,  7     };  8       9     BitArray(): array_(0) { } 10     BitArray(unsigned long nr): array_(0) 11     { 12         reset(nr); 13     } 14  15     ~BitArray() { if (array_) delete [] array_; } 16  17     BitArray(const BitArray& rhs) 18     { 19         if (rhs.array_) 20         { 21             array_ = new unsigned long[rhs.nr_long_]; 22             memcpy(array_, rhs.array_, rhs.nr_long_ * sizeof(unsigned long)); 23             nr_long_ = rhs.nr_long_; 24             nr_bits_ = rhs.nr_bits_; 25         } 26     } 27  28     BitArray& operator=(const BitArray& rhs) 29     { 30         if (this == &rhs) 31         { 32             return *this; 33         } 34  35         if (array_) 36         { 37             delete [] array_; 38         } 39              40         if (rhs.array_) 41         { 42             array_ = new unsigned long[rhs.nr_long_]; 43             memcpy(array_, rhs.array_, rhs.nr_long_ * sizeof(unsigned long)); 44             nr_long_ = rhs.nr_long_; 45             nr_bits_ = rhs.nr_bits_; 46         } 47         return *this; 48     } 49  50     bool operator[](std::size_t nr) 51     { 52         if (nr >= nr_bits_) 53             return false; 54  55         unsigned long ret = array_[bitWord(nr)] & bitMask(nr); 56         if (ret) 57             return true; 58         else 59             return false;             60     } 61  62     bool testBit(std::size_t nr) 63     { 64         return this->operator[](nr); 65     } 66  67     void setBit(unsigned long nr) 68     { 69         if (nr >= nr_bits_) 70         { 71             return; 72         } 73  74         array_[bitWord(nr)] |= bitMask(nr); 75     } 76      77     void clearBit(unsigned long nr) 78     { 79         if (nr >= nr_bits_) 80         { 81             return; 82         } 83  84         array_[bitWord(nr)] &= ~bitMask(nr); 85     } 86  87     void reset(unsigned long nr_bits) 88     { 89         if (array_) 90         { 91             delete [] array_; 92         } 93  94         unsigned long real_nr_bits = align(nr_bits); 95         nr_long_ = getNrLong(real_nr_bits); 96         array_ = new unsigned long[nr_long_]; 97         nr_bits_ = nr_bits; 98         memset(array_, 0, nr_long_*sizeof(unsigned long)); 99     }100 101 private:102 103     unsigned long getNrLong(unsigned long nr) { return nr/bits_per_long; }104     unsigned long align(unsigned long nr)105     {106         return (nr + (bits_per_long-1)) & (~(bits_per_long-1));107     }108 109     unsigned long bitWord(unsigned long nr)110     {111         return nr/bits_per_long;112     }113     114     unsigned long bitMask(unsigned long nr)115     {116         return nr%bits_per_long;117     }118 119     unsigned long nr_bits_;120     unsigned long nr_long_;121     unsigned long * array_;122 };

使用如下,

#include <iostream>#include <string>#include <string.h>#include "BitArray.h"int main(void){    BitArray a(10);    a.setBit(1);    cout << a[1] << endl;    a.clearBit(1);    cout << a[1] << endl;    a.setBit(10);    cout << a[10] << endl;        return 0;}

 

比特数组