BitArray.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. data[dataIndexB] &= ~((1 << leftBits) - 1);
  34. data[dataIndexB] |= (value >> (INT_BITS - shifts));
  35. }
  36. }
  37. static int getArrayLength(int length, int bits) {
  38. return roundUpDivide(length * bits, sizeof(int) * 8);
  39. }
  40. BitArray::BitArray() : length(0), bits(0), data(nullptr) {
  41. }
  42. static int* allocate(int length, int bits) {
  43. int l = getArrayLength(length, bits);
  44. int* a = new int[l];
  45. memset(a, 0, static_cast<size_t>(l) * sizeof(int));
  46. return a;
  47. }
  48. BitArray::BitArray(int length_, int bits_)
  49. : length(length_), bits(bits_), data(nullptr) {
  50. if(length > 0 && bits > 0) {
  51. data = allocate(length, bits);
  52. }
  53. }
  54. BitArray::BitArray(const BitArray& other) : BitArray(other.length, other.bits) {
  55. for(int i = 0; i < length; i++) {
  56. set(i, other.get(i));
  57. }
  58. }
  59. BitArray::BitArray(BitArray&& other) : BitArray() {
  60. swap(other);
  61. }
  62. BitArray::~BitArray() {
  63. delete[] data;
  64. }
  65. BitArray& BitArray::operator=(BitArray other) {
  66. swap(other);
  67. return *this;
  68. }
  69. BitArray& BitArray::set(int index, int value) {
  70. setBits(data, index, bits, value);
  71. return *this;
  72. }
  73. int BitArray::get(int index) const {
  74. return readBits(data, index, bits);
  75. }
  76. int BitArray::getLength() const {
  77. return length;
  78. }
  79. int BitArray::getBits() const {
  80. return bits;
  81. }
  82. int BitArray::getInternalByteSize() const {
  83. if(bits <= 0 || length <= 0) {
  84. return 0;
  85. }
  86. return getArrayLength(length, bits) * static_cast<int>(sizeof(int));
  87. }
  88. int BitArray::select(int index) const {
  89. if(index <= 0) {
  90. return -1;
  91. }
  92. int found = 0;
  93. int end = getArrayLength(length, bits);
  94. for(int i = 0; i < end; i++) {
  95. int ones = __builtin_popcount(static_cast<unsigned int>(data[i]));
  96. found += ones;
  97. if(found >= index) {
  98. found -= ones;
  99. int a = i * 32 - 1;
  100. int d = data[i];
  101. while(found < index) {
  102. found += d & 1;
  103. d >>= 1;
  104. a++;
  105. }
  106. return a;
  107. }
  108. }
  109. return -1;
  110. }
  111. void BitArray::fill(int value) {
  112. for(int i = 0; i < length; i++) {
  113. set(i, value);
  114. }
  115. }
  116. void BitArray::resize(int newLength, int newBits) {
  117. int* newData = allocate(newLength, newBits);
  118. int end = Math::min(length, newLength);
  119. for(int i = 0; i < end; i++) {
  120. setBits(newData, i, newBits, get(i));
  121. }
  122. for(int i = end; i < newLength; i++) {
  123. setBits(newData, i, newBits, 0);
  124. }
  125. delete[] data;
  126. data = newData;
  127. bits = newBits;
  128. length = newLength;
  129. }
  130. void BitArray::swap(BitArray& other) {
  131. std::swap(length, other.length);
  132. std::swap(bits, other.bits);
  133. std::swap(data, other.data);
  134. }