Mercurial > dedupe
diff BitArray.cpp @ 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 | 247adcbbaf8b |
line wrap: on
line diff
--- a/BitArray.cpp Thu Sep 13 23:14:40 2012 +0200 +++ b/BitArray.cpp Thu Sep 13 23:23:14 2012 +0200 @@ -1,2 +1,43 @@ #include "BitArray.hpp" +#include <algorithm> + +BitArray::~BitArray() +{ + delete bits; +} + +BitArray::BitArray(const BitArray& other) : size_(other.size()), bits(new uchar[NUMCHARS(size_)]) +{ + memcpy(bits, other.bits, NUMCHARS(size_)); +} + +BitArray& BitArray::operator=(const BitArray& other) +{ + if (NUMCHARS(size_) != NUMCHARS(other.size_)) { + delete bits; + bits = new uchar[NUMCHARS(other.size_)]; + } + size_ = other.size_; + memcpy(bits, other.bits, NUMCHARS(size_)); + return *this; +} + +bool BitArray::operator==(const BitArray& rhs) const +{ + if (size() != rhs.size()) + return false; + for(size_t i = 0; i < size(); ++i) { + if (testBit(i) != rhs.testBit(i)) + return false; + } + return true; +} + +std::ostream& operator<<(std::ostream& out, const BitArray& rhs) +{ + for (size_t i = 0; i < rhs.size(); ++i) { + out << (rhs.testBit(i) ? "1" : "0"); + } + return out; +}
