BitArray.cpp 4.5 KB

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