|
| 1 | +// |
| 2 | +// Created by Ole on 10.09.2017. |
| 3 | +// |
| 4 | + |
| 5 | +#include "bitarray.h" |
| 6 | +#include <string> |
| 7 | + |
| 8 | +BitArray::BitArray() |
| 9 | + : localSize(0), totalSize(0) { |
| 10 | + current = new std::bitset<BITSET_SIZE>(); |
| 11 | + bitsets.push_back(current); |
| 12 | +} |
| 13 | + |
| 14 | +BitArray::BitArray(const int& value, unsigned int length) |
| 15 | + : localSize(0), totalSize(0) { |
| 16 | + current = new std::bitset<BITSET_SIZE>(); |
| 17 | + bitsets.push_back(current); |
| 18 | + |
| 19 | + if (length > sizeof(int) * 8) |
| 20 | + length = sizeof(int) * 8; |
| 21 | + |
| 22 | + for (unsigned int i = 0; i < length; i++) { |
| 23 | + pushBack((bool) ((value >> i) & 0x1)); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +BitArray::BitArray(std::string& bits) |
| 28 | + : localSize(0), totalSize(0) { |
| 29 | + current = new std::bitset<BITSET_SIZE>(); |
| 30 | + bitsets.push_back(current); |
| 31 | + |
| 32 | + pushBack(bits); |
| 33 | +} |
| 34 | + |
| 35 | +BitArray::BitArray(const bool* const bits, const unsigned int& size) |
| 36 | + : localSize(0), totalSize(0) { |
| 37 | + current = new std::bitset<BITSET_SIZE>(); |
| 38 | + bitsets.push_back(current); |
| 39 | + |
| 40 | + for (int i = 0; i < size; i++) |
| 41 | + pushBack(bits[i]); |
| 42 | +} |
| 43 | + |
| 44 | +BitArray::~BitArray() { |
| 45 | + for (auto bitset : bitsets) |
| 46 | + delete bitset; |
| 47 | +} |
| 48 | + |
| 49 | +void BitArray::pushBack(const bool& one) { |
| 50 | + if (localSize == BITSET_SIZE) { |
| 51 | + current = new std::bitset<BITSET_SIZE>(); |
| 52 | + localSize = 0; |
| 53 | + } |
| 54 | + (*current)[localSize] = one; |
| 55 | + localSize++; |
| 56 | + updateSize(); |
| 57 | +} |
| 58 | + |
| 59 | +void BitArray::pushBack(const unsigned char& byte) { |
| 60 | + for (unsigned int i = 0; i < sizeof(byte) * 8; i++) { |
| 61 | + pushBack((bool) ((byte >> i) & 1)); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +void BitArray::pushBack(const std::string& bits) { |
| 66 | + for (char c : bits) { |
| 67 | + if (c == '0') |
| 68 | + pushBack((bool) 0); |
| 69 | + else if (c == '1') |
| 70 | + pushBack((bool) 1); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void BitArray::setBit(const unsigned int& pos, bool one) { |
| 75 | + if (pos >= getSize()) |
| 76 | + return; |
| 77 | + bitsets[pos / BITSET_SIZE]->set(pos % BITSET_SIZE, one); |
| 78 | +} |
| 79 | + |
| 80 | +bool BitArray::getBit(const unsigned int& position) const { |
| 81 | + if (position >= getSize()) |
| 82 | + return false; |
| 83 | + return (*(bitsets[position / BITSET_SIZE]))[position % BITSET_SIZE]; |
| 84 | +} |
| 85 | + |
| 86 | +void BitArray::addBit(const unsigned int& position) { |
| 87 | + if (position >= getSize()) |
| 88 | + return; |
| 89 | + if (getBit(position)) { |
| 90 | + setBit(position, false); |
| 91 | + addBit(position + 1); |
| 92 | + } else { |
| 93 | + setBit(position, true); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +void BitArray::add(const unsigned int& value) { |
| 98 | + for (unsigned int i = 0; i < sizeof(int) * 8; i++) { |
| 99 | + if ((value >> i) & 0x1) |
| 100 | + addBit(i); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void BitArray::add(const BitArray& value) { |
| 105 | + const unsigned int count = (value.getSize() < getSize() ? value.getSize() : getSize()); |
| 106 | + |
| 107 | + for (unsigned int i = 0; i < count; i++) |
| 108 | + if (value.getBit(i)) |
| 109 | + addBit(i); |
| 110 | +} |
| 111 | + |
| 112 | +unsigned char BitArray::read(const unsigned int& position, const unsigned int& length, const bool& reverse) { |
| 113 | + unsigned char result = 0; |
| 114 | + for(unsigned int i = 0; i < length; i++) { |
| 115 | + if(getBit(position + (reverse ? length - 1 - i : i))) |
| 116 | + result |= 1 << i; |
| 117 | + } |
| 118 | + return result; |
| 119 | +} |
| 120 | + |
| 121 | +void BitArray::updateSize() { |
| 122 | + totalSize = (unsigned int) (BITSET_SIZE * (bitsets.size() - 1) + localSize); |
| 123 | +} |
| 124 | + |
| 125 | +const std::string BitArray::toString() const { |
| 126 | + std::string out; |
| 127 | + for (unsigned int i = 0; i < getSize(); i++) { |
| 128 | + out = (getBit(i) ? "1" : "0") + out; |
| 129 | + } |
| 130 | + return out; |
| 131 | +} |
| 132 | + |
| 133 | +std::ostream& operator<<(std::ostream& stream, const BitArray& array) { |
| 134 | + return stream << array.toString(); |
| 135 | +} |
| 136 | + |
| 137 | +std::ostream& operator<<(std::ostream& stream, const BitArray* const array) { |
| 138 | + return stream << array->toString(); |
| 139 | +} |
0 commit comments