|
@@ -40,9 +40,14 @@ static void setBits(int* data, int index, int bits, int value) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+BitArray::BitArray() : length(0), bits(0), data(nullptr) {
|
|
|
+}
|
|
|
+
|
|
|
BitArray::BitArray(int length, int bits)
|
|
|
- : length(length), bits(bits),
|
|
|
- data(new int[roundUpDivide(length * bits, sizeof(int))]) {
|
|
|
+ : length(length), bits(bits), data(nullptr) {
|
|
|
+ if(length > 0 && bits > 0) {
|
|
|
+ data = new int[roundUpDivide(length * bits, sizeof(int))];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
BitArray::BitArray(const BitArray& other) : BitArray(other.length, other.bits) {
|
|
@@ -51,7 +56,7 @@ BitArray::BitArray(const BitArray& other) : BitArray(other.length, other.bits) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-BitArray::BitArray(BitArray&& other) : length(0), bits(0), data(nullptr) {
|
|
|
+BitArray::BitArray(BitArray&& other) : BitArray() {
|
|
|
swap(other);
|
|
|
}
|
|
|
|
|
@@ -87,14 +92,19 @@ void BitArray::fill(int value) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void BitArray::resize(int newBits) {
|
|
|
- int* newData = new int[roundUpDivide(length * newBits, sizeof(int))];
|
|
|
- for(int i = 0; i < length; i++) {
|
|
|
+void BitArray::resize(int newLength, int newBits) {
|
|
|
+ int* newData = new int[roundUpDivide(newLength * newBits, sizeof(int))];
|
|
|
+ int end = Math::min(length, newLength);
|
|
|
+ for(int i = 0; i < end; i++) {
|
|
|
setBits(newData, i, newBits, get(i));
|
|
|
}
|
|
|
+ for(int i = end; i < newLength; i++) {
|
|
|
+ setBits(newData, i, newBits, 0);
|
|
|
+ }
|
|
|
delete[] data;
|
|
|
data = newData;
|
|
|
bits = newBits;
|
|
|
+ length = newLength;
|
|
|
}
|
|
|
|
|
|
void BitArray::swap(BitArray& other) {
|