comparison 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
comparison
equal deleted inserted replaced
51:0bd3c1c46251 52:725b0d776f3c
1 #include "BitArray.hpp" 1 #include "BitArray.hpp"
2 2
3 #include <algorithm>
4
5 BitArray::~BitArray()
6 {
7 delete bits;
8 }
9
10 BitArray::BitArray(const BitArray& other) : size_(other.size()), bits(new uchar[NUMCHARS(size_)])
11 {
12 memcpy(bits, other.bits, NUMCHARS(size_));
13 }
14
15 BitArray& BitArray::operator=(const BitArray& other)
16 {
17 if (NUMCHARS(size_) != NUMCHARS(other.size_)) {
18 delete bits;
19 bits = new uchar[NUMCHARS(other.size_)];
20 }
21 size_ = other.size_;
22 memcpy(bits, other.bits, NUMCHARS(size_));
23 return *this;
24 }
25
26 bool BitArray::operator==(const BitArray& rhs) const
27 {
28 if (size() != rhs.size())
29 return false;
30 for(size_t i = 0; i < size(); ++i) {
31 if (testBit(i) != rhs.testBit(i))
32 return false;
33 }
34 return true;
35 }
36
37 std::ostream& operator<<(std::ostream& out, const BitArray& rhs)
38 {
39 for (size_t i = 0; i < rhs.size(); ++i) {
40 out << (rhs.testBit(i) ? "1" : "0");
41 }
42 return out;
43 }