Mercurial > dedupe
comparison BitArray.hpp @ 47:b23f04d4a276
Test a custom BitArray.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Mon, 10 Sep 2012 23:59:05 +0200 |
| parents | |
| children | 725b0d776f3c |
comparison
equal
deleted
inserted
replaced
| 46:877327e9061a | 47:b23f04d4a276 |
|---|---|
| 1 #ifndef BITARRAY_HPP | |
| 2 #define BITARRAY_HPP | |
| 3 | |
| 4 | |
| 5 #if 0 | |
| 6 #include <QtCore/QBitArray> | |
| 7 typedef QBitArray BitArray; | |
| 8 #else | |
| 9 #define HIGH(B) ((B) >> 3) | |
| 10 #define LOW(B) ((B) & 0x07) | |
| 11 | |
| 12 #define MASK(X) (0x1 << X) | |
| 13 #define SELECTMASK(X) (uchar(MASK(X))) | |
| 14 #define DESELECTMASK(X) (uchar(~MASK(X))) | |
| 15 | |
| 16 #define NUMCHARS(X) HIGH(X + 7) | |
| 17 | |
| 18 #include <cassert> | |
| 19 | |
| 20 class QBitArray; | |
| 21 class BitArray | |
| 22 { | |
| 23 typedef unsigned char uchar; | |
| 24 typedef unsigned int uint; | |
| 25 uint size_; | |
| 26 unsigned char* bits; | |
| 27 public: | |
| 28 bool testBit(uint i) const | |
| 29 { | |
| 30 assert(i < size_); | |
| 31 return bits[HIGH(i)] & MASK(LOW(i)); | |
| 32 } | |
| 33 | |
| 34 void setBit(uint i, bool val) | |
| 35 { | |
| 36 assert(i < size_); | |
| 37 if (val) { | |
| 38 bits[HIGH(i)] |= SELECTMASK(LOW(i)); | |
| 39 } | |
| 40 else { | |
| 41 bits[HIGH(i)] &= DESELECTMASK(LOW(i)); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 BitArray(uint size) | |
| 46 { | |
| 47 size_ = size; | |
| 48 bits = new uchar[NUMCHARS(size)]; | |
| 49 } | |
| 50 | |
| 51 BitArray(uint size, bool val) : size_(size), bits(new uchar[NUMCHARS(size)]) | |
| 52 { | |
| 53 uint n = NUMCHARS(size); | |
| 54 for (uint i = 0; i < n; ++i) { | |
| 55 bits[i] = (val) ? 0xff : 0; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 BitArray() : size_(0), bits(0) | |
| 60 { | |
| 61 } | |
| 62 | |
| 63 uint size() const | |
| 64 { | |
| 65 return size_; | |
| 66 } | |
| 67 }; | |
| 68 #endif | |
| 69 | |
| 70 #endif //BITARRAY_HPP | |
| 71 | |
| 72 |
