BitArray.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "data/BitArray.h"
  2. #include "math/Math.h"
  3. static int roundUpDivide(int a, int b) {
  4. if(a % b == 0) {
  5. return a / b;
  6. }
  7. return a / b + 1;
  8. }
  9. static constexpr int INT_BITS = sizeof(int) * 8;
  10. static constexpr int DIVIDE_BITS = Math::roundUpLog2(INT_BITS);
  11. static int readBits(const int* data, int index, int bits) {
  12. int dataIndexA = (index * bits) >> DIVIDE_BITS;
  13. int dataIndexB = ((index + 1) * bits) >> DIVIDE_BITS;
  14. int shifts = (index * bits) & (INT_BITS - 1);
  15. if(dataIndexA == dataIndexB) {
  16. return (data[dataIndexA] >> shifts) & ((1 << bits) - 1);
  17. }
  18. int bitsInA = INT_BITS - shifts;
  19. int r = (data[dataIndexA] >> shifts) & ((1 << bitsInA) - 1);
  20. r |= (data[dataIndexB] & ((1 << (bits - bitsInA)) - 1)) << bitsInA;
  21. return r;
  22. }
  23. static void setBits(int* data, int index, int bits, int value) {
  24. int mask = (1 << bits) - 1;
  25. value &= mask;
  26. int dataIndexA = (index * bits) >> DIVIDE_BITS;
  27. int dataIndexB = ((index + 1) * bits) >> DIVIDE_BITS;
  28. int shifts = (index * bits) & (INT_BITS - 1);
  29. data[dataIndexA] &= ~(mask << shifts);
  30. data[dataIndexA] |= (value << shifts);
  31. if(dataIndexA != dataIndexB) {
  32. int leftBits = bits - (INT_BITS - shifts);
  33. int mask = (1 << leftBits) - 1;
  34. data[dataIndexB] &= ~mask;
  35. data[dataIndexB] |= (value >> (INT_BITS - shifts));
  36. }
  37. }
  38. BitArray::BitArray() : length(0), bits(0), data(nullptr) {
  39. }
  40. BitArray::BitArray(int length, int bits)
  41. : length(length), bits(bits), data(nullptr) {
  42. if(length > 0 && bits > 0) {
  43. data = new int[roundUpDivide(length * bits, sizeof(int))];
  44. }
  45. }
  46. BitArray::BitArray(const BitArray& other) : BitArray(other.length, other.bits) {
  47. for(int i = 0; i < length; i++) {
  48. set(i, other.get(i));
  49. }
  50. }
  51. BitArray::BitArray(BitArray&& other) : BitArray() {
  52. swap(other);
  53. }
  54. BitArray::~BitArray() {
  55. delete[] data;
  56. }
  57. BitArray& BitArray::operator=(BitArray other) {
  58. swap(other);
  59. return *this;
  60. }
  61. BitArray& BitArray::set(int index, int value) {
  62. setBits(data, index, bits, value);
  63. return *this;
  64. }
  65. int BitArray::get(int index) const {
  66. return readBits(data, index, bits);
  67. }
  68. int BitArray::getLength() const {
  69. return length;
  70. }
  71. int BitArray::getBits() const {
  72. return bits;
  73. }
  74. void BitArray::fill(int value) {
  75. for(int i = 0; i < length; i++) {
  76. set(i, value);
  77. }
  78. }
  79. void BitArray::resize(int newLength, int newBits) {
  80. int* newData = new int[roundUpDivide(newLength * newBits, sizeof(int))];
  81. int end = Math::min(length, newLength);
  82. for(int i = 0; i < end; i++) {
  83. setBits(newData, i, newBits, get(i));
  84. }
  85. for(int i = end; i < newLength; i++) {
  86. setBits(newData, i, newBits, 0);
  87. }
  88. delete[] data;
  89. data = newData;
  90. bits = newBits;
  91. length = newLength;
  92. }
  93. void BitArray::swap(BitArray& other) {
  94. std::swap(length, other.length);
  95. std::swap(bits, other.bits);
  96. std::swap(data, other.data);
  97. }