BitArray.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "utils/BitArray.h"
  2. #include "utils/Utils.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 = Utils::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(int length, int bits, int value)
  39. : length(length), bits(bits), data(new int[roundUpDivide(length * bits, sizeof(int))]) {
  40. fill(value);
  41. }
  42. BitArray::~BitArray() {
  43. delete[] data;
  44. }
  45. BitArray::BitArray(const BitArray& other) : BitArray(other.length, other.bits) {
  46. copyData(other);
  47. }
  48. BitArray::BitArray(BitArray&& other) : length(other.length), bits(other.bits), data(other.data) {
  49. other.reset();
  50. }
  51. BitArray& BitArray::operator=(const BitArray& other) {
  52. if(this == &other) {
  53. return *this;
  54. } else if(length == other.length && bits == other.bits) {
  55. copyData(other);
  56. return *this;
  57. }
  58. delete[] data;
  59. length = other.length;
  60. bits = other.bits;
  61. data = new int[roundUpDivide(length * bits, sizeof(int))];
  62. copyData(other);
  63. return *this;
  64. }
  65. BitArray& BitArray::operator=(BitArray&& other) {
  66. if(this == &other) {
  67. return *this;
  68. }
  69. delete[] data;
  70. length = other.length;
  71. bits = other.bits;
  72. data = other.data;
  73. other.reset();
  74. return *this;
  75. }
  76. BitArray& BitArray::set(int index, int value) {
  77. setBits(data, index, bits, value);
  78. return *this;
  79. }
  80. int BitArray::get(int index) const {
  81. return readBits(data, index, bits);
  82. }
  83. int BitArray::getLength() const {
  84. return length;
  85. }
  86. int BitArray::getBits() const {
  87. return bits;
  88. }
  89. void BitArray::fill(int value) {
  90. for(int i = 0; i < length; i++) {
  91. set(i, value);
  92. }
  93. }
  94. void BitArray::resize(int newBits) {
  95. int* newData = new int[roundUpDivide(length * newBits, sizeof(int))];
  96. for(int i = 0; i < length; i++) {
  97. setBits(newData, i, newBits, get(i));
  98. }
  99. delete[] data;
  100. data = newData;
  101. bits = newBits;
  102. }
  103. void BitArray::copyData(const BitArray& other) {
  104. for(int i = 0; i < length; i++) {
  105. set(i, other.get(i));
  106. }
  107. }
  108. void BitArray::reset() {
  109. length = 0;
  110. bits = 0;
  111. data = nullptr;
  112. }