Mercurial > dedupe
changeset 69:9161e9fd5b3f
Taking code out of class definition.
| author | Tom Fredrik Blenning Klaussen <bfg@bfgconsult.no> |
|---|---|
| date | Sat, 12 Jan 2013 11:50:42 +0100 |
| parents | f3d4aba620cc |
| children | c2ad34480216 |
| files | BitArray.hpp |
| diffstat | 1 files changed, 79 insertions(+), 69 deletions(-) [+] |
line wrap: on
line diff
--- a/BitArray.hpp Sat Jan 05 20:07:46 2013 +0100 +++ b/BitArray.hpp Sat Jan 12 11:50:42 2013 +0100 @@ -3,6 +3,33 @@ #include <ostream> +#include <cassert> + +typedef unsigned char uchar; +typedef unsigned int uint; + +class BitArray +{ + uint size_; + unsigned char* bits; + +public: + BitArray(uint size, bool val); + + BitArray(uint size); + BitArray(); + BitArray(const BitArray& ); + + ~BitArray(); + bool testBit(uint i) const; + void setBit(uint i, bool val); + uint size() const; + uchar getPaddedChar(size_t offset = 0) const; + + bool operator==(const BitArray& rhs) const; + BitArray& operator=(const BitArray& rhs); +}; + //We should remove these defines from the header, but we keep them for //now until we have stabilized the API and removed any bugs. #define HIGH(B) ((B) >> 3) @@ -12,77 +39,60 @@ #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: - 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)]) - { - //We need to initialize the bits beyond size, since they are - //implicitly used in certain functions. - bits[NUMCHARS(size) - 1] = 0; - } - - BitArray() : size_(0), bits(0) - { - } - - BitArray(const BitArray& ); - - ~BitArray(); - 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)); - } - } - - 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); -}; +inline BitArray::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; + } +} + +inline BitArray::BitArray(uint size) : size_(size), bits(new uchar[NUMCHARS(size)]) +{ + //We need to initialize the bits beyond size, since they are + //implicitly used in certain functions. + bits[NUMCHARS(size) - 1] = 0; +} + +inline BitArray::BitArray() : size_(0), bits(0) +{ +} + +inline bool BitArray::testBit(uint i) const +{ + assert(i < size_); + return bits[HIGH(i)] & MASK(LOW(i)); +} + +inline void BitArray::setBit(uint i, bool val) +{ + assert(i < size_); + if (val) { + bits[HIGH(i)] |= SELECTMASK(LOW(i)); + } + else { + bits[HIGH(i)] &= DESELECTMASK(LOW(i)); + } +} + +inline uint BitArray::size() const +{ + return size_; +} + +inline uchar BitArray::getPaddedChar(size_t offset) 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)); +} std::ostream& operator<<(std::ostream& out, const BitArray& rhs);
