BitArray.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "data/BitArray.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "math/Math.h"
  5. #include "utils/Utility.h"
  6. static int roundUpDivide(int a, int b) {
  7. if(a % b == 0) {
  8. return a / b;
  9. }
  10. return a / b + 1;
  11. }
  12. static constexpr int INT_BITS = sizeof(int) * 8;
  13. static constexpr int DIVIDE_BITS = Core::Math::roundUpLog2(INT_BITS);
  14. static int readBits(const int* data, int index, int bits) {
  15. int dataIndexA = (index * bits) >> DIVIDE_BITS;
  16. int dataIndexB = ((index + 1) * bits) >> DIVIDE_BITS;
  17. int shifts = (index * bits) & (INT_BITS - 1);
  18. if(dataIndexA == dataIndexB) {
  19. return (data[dataIndexA] >> shifts) & ((1 << bits) - 1);
  20. }
  21. int bitsInA = INT_BITS - shifts;
  22. int r = (data[dataIndexA] >> shifts) & ((1 << bitsInA) - 1);
  23. r |= (data[dataIndexB] & ((1 << (bits - bitsInA)) - 1)) << bitsInA;
  24. return r;
  25. }
  26. static void setBits(int* data, int index, int bits, int value) {
  27. int mask = (1 << bits) - 1;
  28. value &= mask;
  29. int dataIndexA = (index * bits) >> DIVIDE_BITS;
  30. int dataIndexB = ((index + 1) * bits) >> DIVIDE_BITS;
  31. int shifts = (index * bits) & (INT_BITS - 1);
  32. data[dataIndexA] &= ~(mask << shifts);
  33. data[dataIndexA] |= (value << shifts);
  34. if(dataIndexA != dataIndexB) {
  35. int leftBits = bits - (INT_BITS - shifts);
  36. data[dataIndexB] &= ~((1 << leftBits) - 1);
  37. data[dataIndexB] |= (value >> (INT_BITS - shifts));
  38. }
  39. }
  40. static int getArrayLength(int length, int bits) {
  41. return roundUpDivide(length * bits, sizeof(int) * 8);
  42. }
  43. Core::BitArray::BitArray() : length(0), bits(0), data(nullptr) {
  44. }
  45. check_return bool Core::BitArray::copyFrom(const BitArray& other) {
  46. if(resize(other.length, other.bits)) {
  47. return true;
  48. }
  49. for(int i = 0; i < length; i++) {
  50. set(i, other.get(i));
  51. }
  52. return false;
  53. }
  54. Core::BitArray::BitArray(BitArray&& other) : BitArray() {
  55. swap(other);
  56. }
  57. Core::BitArray::~BitArray() {
  58. delete[] data;
  59. }
  60. Core::BitArray& Core::BitArray::operator=(BitArray&& other) {
  61. swap(other);
  62. return *this;
  63. }
  64. Core::BitArray& Core::BitArray::set(int index, int value) {
  65. if(data == nullptr || index < 0 || index >= length) {
  66. return *this;
  67. }
  68. setBits(data, index, bits, value);
  69. return *this;
  70. }
  71. int Core::BitArray::get(int index) const {
  72. if(data == nullptr || index < 0 || index >= length) {
  73. return 0;
  74. }
  75. return readBits(data, index, bits);
  76. }
  77. int Core::BitArray::getLength() const {
  78. return length;
  79. }
  80. int Core::BitArray::getBits() const {
  81. return bits;
  82. }
  83. int Core::BitArray::getInternalByteSize() const {
  84. if(bits <= 0 || length <= 0) {
  85. return 0;
  86. }
  87. return getArrayLength(length, bits) * CORE_SIZE(int);
  88. }
  89. int Core::BitArray::select(int index) const {
  90. if(index <= 0) {
  91. return -1;
  92. }
  93. int found = 0;
  94. int end = getArrayLength(length, bits);
  95. for(int i = 0; i < end; i++) {
  96. int ones = Core::popCount(data[i]);
  97. found += ones;
  98. if(found >= index) {
  99. found -= ones;
  100. int a = i * 32 - 1;
  101. int d = data[i];
  102. while(found < index) {
  103. found += d & 1;
  104. d >>= 1;
  105. a++;
  106. }
  107. return a;
  108. }
  109. }
  110. return -1;
  111. }
  112. void Core::BitArray::fill(int value) {
  113. for(int i = 0; i < length; i++) {
  114. set(i, value);
  115. }
  116. }
  117. check_return bool Core::BitArray::resize(int newLength, int newBits) {
  118. if(newLength <= 0 || newBits <= 0) {
  119. return true;
  120. }
  121. int arrayLength = getArrayLength(newLength, newBits);
  122. int* newData = new int[arrayLength];
  123. if(newData == nullptr) {
  124. return true;
  125. }
  126. memset(newData, 0, static_cast<size_t>(arrayLength) * sizeof(int));
  127. int end = Math::min(length, newLength);
  128. for(int i = 0; i < end; i++) {
  129. setBits(newData, i, newBits, get(i));
  130. }
  131. for(int i = end; i < newLength; i++) {
  132. setBits(newData, i, newBits, 0);
  133. }
  134. delete[] data;
  135. data = newData;
  136. bits = newBits;
  137. length = newLength;
  138. return false;
  139. }
  140. void Core::BitArray::swap(BitArray& other) {
  141. Core::swap(length, other.length);
  142. Core::swap(bits, other.bits);
  143. Core::swap(data, other.data);
  144. }