comparison BitArray.hpp @ 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 f339499ecd79
children
comparison
equal deleted inserted replaced
68:f3d4aba620cc 69:9161e9fd5b3f
1 #ifndef BITARRAY_HPP 1 #ifndef BITARRAY_HPP
2 #define BITARRAY_HPP 2 #define BITARRAY_HPP
3 3
4 #include <ostream> 4 #include <ostream>
5
6 #include <cassert>
7
8 typedef unsigned char uchar;
9 typedef unsigned int uint;
10
11 class BitArray
12 {
13 uint size_;
14 unsigned char* bits;
15
16 public:
17 BitArray(uint size, bool val);
18
19 BitArray(uint size);
20 BitArray();
21 BitArray(const BitArray& );
22
23 ~BitArray();
24 bool testBit(uint i) const;
25 void setBit(uint i, bool val);
26 uint size() const;
27 uchar getPaddedChar(size_t offset = 0) const;
28
29 bool operator==(const BitArray& rhs) const;
30 BitArray& operator=(const BitArray& rhs);
31 };
5 32
6 //We should remove these defines from the header, but we keep them for 33 //We should remove these defines from the header, but we keep them for
7 //now until we have stabilized the API and removed any bugs. 34 //now until we have stabilized the API and removed any bugs.
8 #define HIGH(B) ((B) >> 3) 35 #define HIGH(B) ((B) >> 3)
9 #define LOW(B) ((B) & 0x07) 36 #define LOW(B) ((B) & 0x07)
10 #define MASK(X) (0x1 << (7 - X)) 37 #define MASK(X) (0x1 << (7 - X))
11 #define SELECTMASK(X) (uchar(MASK(X))) 38 #define SELECTMASK(X) (uchar(MASK(X)))
12 #define DESELECTMASK(X) (uchar(~MASK(X))) 39 #define DESELECTMASK(X) (uchar(~MASK(X)))
13 #define NUMCHARS(X) HIGH(X + 7) 40 #define NUMCHARS(X) HIGH(X + 7)
14 41
15 #include <cassert>
16
17 class QBitArray;
18 class BitArray
19 {
20 typedef unsigned char uchar;
21 typedef unsigned int uint;
22 uint size_;
23 unsigned char* bits;
24
25 public:
26 BitArray(uint size, bool val) : size_(size), bits(new uchar[NUMCHARS(size)])
27 {
28 uint n = NUMCHARS(size);
29 for (uint i = 0; i < n; ++i) {
30 bits[i] = (val) ? 0xff : 0;
31 }
32 }
33
34 BitArray(uint size) : size_(size), bits(new uchar[NUMCHARS(size)])
35 {
36 //We need to initialize the bits beyond size, since they are
37 //implicitly used in certain functions.
38 bits[NUMCHARS(size) - 1] = 0;
39 }
40
41 BitArray() : size_(0), bits(0)
42 {
43 }
44
45 BitArray(const BitArray& );
46
47 ~BitArray();
48 bool testBit(uint i) const
49 {
50 assert(i < size_);
51 return bits[HIGH(i)] & MASK(LOW(i));
52 }
53
54 void setBit(uint i, bool val)
55 {
56 assert(i < size_);
57 if (val) {
58 bits[HIGH(i)] |= SELECTMASK(LOW(i));
59 }
60 else {
61 bits[HIGH(i)] &= DESELECTMASK(LOW(i));
62 }
63 }
64
65 uint size() const
66 {
67 return size_;
68 }
69
70 uchar getPaddedChar(size_t offset = 0) const
71 {
72 assert(size_ > 0);
73 assert(HIGH(offset) < NUMCHARS(size_));
74 if (LOW(offset) == 0)
75 return bits[HIGH(offset)];
76
77 uchar next = ((HIGH(offset) + 1) < NUMCHARS(size_)) ?
78 bits[HIGH(offset) + 1] : 0;
79 return ((bits[HIGH(offset)] << 8) | next) >> (8 - LOW(offset));
80 }
81 42
82 43
83 bool operator==(const BitArray& rhs) const; 44 inline BitArray::BitArray(uint size, bool val) : size_(size), bits(new uchar[NUMCHARS(size)])
84 BitArray& operator=(const BitArray& rhs); 45 {
85 }; 46 uint n = NUMCHARS(size);
47 for (uint i = 0; i < n; ++i) {
48 bits[i] = (val) ? 0xff : 0;
49 }
50 }
51
52 inline BitArray::BitArray(uint size) : size_(size), bits(new uchar[NUMCHARS(size)])
53 {
54 //We need to initialize the bits beyond size, since they are
55 //implicitly used in certain functions.
56 bits[NUMCHARS(size) - 1] = 0;
57 }
58
59 inline BitArray::BitArray() : size_(0), bits(0)
60 {
61 }
62
63 inline bool BitArray::testBit(uint i) const
64 {
65 assert(i < size_);
66 return bits[HIGH(i)] & MASK(LOW(i));
67 }
68
69 inline void BitArray::setBit(uint i, bool val)
70 {
71 assert(i < size_);
72 if (val) {
73 bits[HIGH(i)] |= SELECTMASK(LOW(i));
74 }
75 else {
76 bits[HIGH(i)] &= DESELECTMASK(LOW(i));
77 }
78 }
79
80 inline uint BitArray::size() const
81 {
82 return size_;
83 }
84
85 inline uchar BitArray::getPaddedChar(size_t offset) const
86 {
87 assert(size_ > 0);
88 assert(HIGH(offset) < NUMCHARS(size_));
89 if (LOW(offset) == 0)
90 return bits[HIGH(offset)];
91
92 uchar next = ((HIGH(offset) + 1) < NUMCHARS(size_)) ?
93 bits[HIGH(offset) + 1] : 0;
94 return ((bits[HIGH(offset)] << 8) | next) >> (8 - LOW(offset));
95 }
86 96
87 std::ostream& operator<<(std::ostream& out, const BitArray& rhs); 97 std::ostream& operator<<(std::ostream& out, const BitArray& rhs);
88 98
89 #endif //BITARRAY_HPP 99 #endif //BITARRAY_HPP