BitArray.cpp 4.2 KB

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