Mercurial > dedupe
view 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 |
line wrap: on
line source
#ifndef BITARRAY_HPP #define BITARRAY_HPP #if 0 #include <QtCore/QBitArray> typedef QBitArray BitArray; #else #define HIGH(B) ((B) >> 3) #define LOW(B) ((B) & 0x07) #define MASK(X) (0x1 << X) #define SELECTMASK(X) (uchar(MASK(X))) #define DESELECTMASK(X) (uchar(~MASK(X))) #define NUMCHARS(X) HIGH(X + 7) #include <cassert> class QBitArray; class BitArray { typedef unsigned char uchar; typedef unsigned int uint; uint size_; unsigned char* bits; public: bool testBit(uint i) const { assert(i < size_); return bits[HIGH(i)] & MASK(LOW(i)); } void setBit(uint i, bool val) { assert(i < size_); if (val) { bits[HIGH(i)] |= SELECTMASK(LOW(i)); } else { bits[HIGH(i)] &= DESELECTMASK(LOW(i)); } } BitArray(uint size) { size_ = size; bits = new uchar[NUMCHARS(size)]; } BitArray(uint size, bool val) : size_(size), bits(new uchar[NUMCHARS(size)]) { uint n = NUMCHARS(size); for (uint i = 0; i < n; ++i) { bits[i] = (val) ? 0xff : 0; } } BitArray() : size_(0), bits(0) { } uint size() const { return size_; } }; #endif #endif //BITARRAY_HPP
