-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitarray.cpp
More file actions
179 lines (145 loc) · 4.13 KB
/
bitarray.cpp
File metadata and controls
179 lines (145 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// Created by Ole on 10.09.2017.
//
#include "bitarray.h"
namespace vivid { namespace util {
BitArray::BitArray() {
current = new std::bitset<BITSET_SIZE>();
bitsets.push_back(current);
}
BitArray::BitArray(const BitArray& other) {
current = new std::bitset<BITSET_SIZE>();
bitsets.push_back(current);
for (unsigned int i = 0; i < other.getSize(); i++)
pushBack(other.getBit(i));
}
BitArray::BitArray(const unsigned int& length) {
current = new std::bitset<BITSET_SIZE>();
bitsets.push_back(current);
for (unsigned int i = 0; i < length; i++)
pushBack((bool) 0);
}
BitArray::BitArray(const int& value, unsigned int length) {
current = new std::bitset<BITSET_SIZE>();
bitsets.push_back(current);
if (length > sizeof(int) * 8)
length = sizeof(int) * 8;
for (unsigned int i = 0; i < length; i++)
pushBack((bool) ((value >> i) & 0x1));
}
BitArray::BitArray(const std::string& bits) {
current = new std::bitset<BITSET_SIZE>();
bitsets.push_back(current);
pushBack(bits);
}
BitArray::BitArray(const bool* const bits, const unsigned int& size)
: localSize(0), totalSize(0) {
current = new std::bitset<BITSET_SIZE>();
bitsets.push_back(current);
for (int i = 0; i < size; i++)
pushBack(bits[i]);
}
BitArray::~BitArray() {
for (auto bitset : bitsets)
delete bitset;
}
void BitArray::pushBack(const bool& one) {
if (localSize == BITSET_SIZE) {
current = new std::bitset<BITSET_SIZE>();
bitsets.push_back(current);
localSize = 0;
}
(*current)[localSize] = one;
localSize++;
updateSize();
}
void BitArray::pushBack(const unsigned char& byte) {
for (unsigned int i = 0; i < sizeof(unsigned char) * 8; i++) {
pushBack((bool) ((byte >> i) & 1));
}
}
void BitArray::pushBack(const std::string& bits) {
for (char c : bits) {
if (c == '0')
pushBack((bool) 0);
else if (c == '1')
pushBack((bool) 1);
}
}
void BitArray::pushFront(const bool& one) {
bool last = one;
for (unsigned int i = 0; i < getSize(); i++) {
bool lastTemp = getBit(i);
setBit(i, last);
last = lastTemp;
}
pushBack(last);
}
void BitArray::setBit(const unsigned int& pos, bool one) {
if (pos >= getSize())
return;
bitsets[pos / BITSET_SIZE]->set(pos % BITSET_SIZE, one);
}
bool BitArray::getBit(const unsigned int& position) const {
if (position >= getSize())
return false;
return (*(bitsets[position / BITSET_SIZE]))[position % BITSET_SIZE];
}
void BitArray::addBit(const unsigned int& position) {
if (position >= getSize())
return;
if (getBit(position)) {
setBit(position, false);
addBit(position + 1);
} else {
setBit(position, true);
}
}
void BitArray::add(const unsigned int& value) {
for (unsigned int i = 0; i < sizeof(int) * 8; i++) {
if ((value >> i) & 0x1)
addBit(i);
}
}
void BitArray::add(const BitArray& value) {
const unsigned int count = (value.getSize() < getSize() ? value.getSize() : getSize());
for (unsigned int i = 0; i < count; i++)
if (value.getBit(i))
addBit(i);
}
unsigned int BitArray::read(const unsigned int& position, const unsigned int& length, const bool& reverse) {
unsigned int result = 0;
for (unsigned int i = 0; i < length; i++) {
if (getBit(position + (reverse ? length - 1 - i : i)))
result |= 1 << i;
}
return result;
}
void BitArray::updateSize() {
totalSize = (unsigned int) (BITSET_SIZE * (bitsets.size() - 1) + localSize);
}
const std::string BitArray::toString() const {
std::string out;
for (unsigned int i = 0; i < getSize(); i++) {
out += (getBit(i) ? "1" : "0");
}
return out;
}
bool BitArray::operator==(const BitArray& right) const {
if (getSize() != right.getSize())
return false;
for (unsigned int i = 0; i < getSize(); i++) {
if (getBit(i) && (!right.getBit(i)))
return false;
if ((!getBit(i)) && right.getBit(i))
return false;
}
return true;
}
std::ostream& operator<<(std::ostream& stream, const BitArray& array) {
return stream << array.toString();
}
std::ostream& operator<<(std::ostream& stream, const BitArray* const array) {
return stream << array->toString();
}
}}