Mercurial > dedupe
diff BitArray.hpp @ 52:725b0d776f3c
Fix a lot of problems with BitArray and reorganize which functions are
located in header and file.
| author | Tom Fredrik Blenning Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 13 Sep 2012 23:23:14 +0200 |
| parents | b23f04d4a276 |
| children | f339499ecd79 |
line wrap: on
line diff
--- a/BitArray.hpp Thu Sep 13 23:14:40 2012 +0200 +++ b/BitArray.hpp Thu Sep 13 23:23:14 2012 +0200 @@ -6,10 +6,12 @@ #include <QtCore/QBitArray> typedef QBitArray BitArray; #else +#include <ostream> + #define HIGH(B) ((B) >> 3) #define LOW(B) ((B) & 0x07) -#define MASK(X) (0x1 << X) +#define MASK(X) (0x1 << (7 - X)) #define SELECTMASK(X) (uchar(MASK(X))) #define DESELECTMASK(X) (uchar(~MASK(X))) @@ -24,7 +26,28 @@ typedef unsigned int uint; uint size_; unsigned char* bits; + public: + 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(uint size) : size_(size), bits(new uchar[NUMCHARS(size)]) + { + bits[NUMCHARS(size) - 1] = 0; + } + + BitArray() : size_(0), bits(0) + { + } + + BitArray(const BitArray& ); + + ~BitArray(); bool testBit(uint i) const { assert(i < size_); @@ -42,31 +65,30 @@ } } - 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_; - } + } + + uchar getPaddedChar(size_t offset = 0) const + { + assert(size_ > 0); + assert(HIGH(offset) < NUMCHARS(size_)); + if (LOW(offset) == 0) + return bits[HIGH(offset)]; + + uchar next = ((HIGH(offset) + 1) < NUMCHARS(size_)) ? + bits[HIGH(offset) + 1] : 0; + return ((bits[HIGH(offset)] << 8) | next) >> (8 - LOW(offset)); + } + + + bool operator==(const BitArray& rhs) const; + BitArray& operator=(const BitArray& rhs); }; + +std::ostream& operator<<(std::ostream& out, const BitArray& rhs); + #endif #endif //BITARRAY_HPP - -
