BitArray.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, "wusi");
  16. static u64 readBits(const u64* data, u64 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, u64 index, u64 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 u64 getArrayLength(u64 length, u64 bits) {
  43. return roundUpDivide(length * bits, U64_BITS);
  44. }
  45. Core::BitArray::BitArray() : lengthBits(0), data(nullptr) {
  46. }
  47. check_return Core::Error Core::BitArray::copyFrom(const BitArray& other) {
  48. CORE_RETURN_ERROR(resize(other.getLength(), other.getBits()));
  49. u64 length = getLength();
  50. for(u64 i = 0; i < length; i++) {
  51. set(i, other.get(i));
  52. }
  53. return ErrorCode::NONE;
  54. }
  55. Core::BitArray::BitArray(BitArray&& other) : BitArray() {
  56. swap(other);
  57. }
  58. Core::BitArray::~BitArray() {
  59. delete[] data;
  60. }
  61. Core::BitArray& Core::BitArray::operator=(BitArray&& other) {
  62. swap(other);
  63. return *this;
  64. }
  65. Core::BitArray& Core::BitArray::set(u64 index, u64 value) {
  66. if(data == nullptr || index >= getLength()) {
  67. return *this;
  68. }
  69. setBits(data, index, getBits(), value);
  70. return *this;
  71. }
  72. u64 Core::BitArray::get(u64 index) const {
  73. if(data == nullptr || index >= getLength()) {
  74. return 0;
  75. }
  76. return readBits(data, index, getBits());
  77. }
  78. u64 Core::BitArray::getLength() const {
  79. return lengthBits & LENGTH_MASK;
  80. }
  81. u64 Core::BitArray::getBits() const {
  82. return (lengthBits & ~LENGTH_MASK) >> LENGTH_BITS;
  83. }
  84. u64 Core::BitArray::getInternalByteSize() const {
  85. if(getLength() <= 0 || getBits() <= 0) {
  86. return 0;
  87. }
  88. return getArrayLength(getLength(), getBits()) * CORE_SIZE(u64);
  89. }
  90. i64 Core::BitArray::select(u64 index) const {
  91. if(index <= 0) {
  92. return -1;
  93. }
  94. u64 found = 0;
  95. u64 end = getArrayLength(getLength(), getBits());
  96. for(u64 i = 0; i < end; i++) {
  97. u64 ones = Core::popCount<u64, u64>(data[i]);
  98. found += ones;
  99. if(found >= index) {
  100. found -= ones;
  101. u64 a = i * U64_BITS - 1;
  102. u64 d = data[i];
  103. while(found < index) {
  104. found += d & 1;
  105. d >>= 1;
  106. a++;
  107. }
  108. return static_cast<i64>(a);
  109. }
  110. }
  111. return -1;
  112. }
  113. void Core::BitArray::fill(u64 value) {
  114. u64 length = getLength();
  115. for(u64 i = 0; i < length; i++) {
  116. set(i, value);
  117. }
  118. }
  119. check_return Core::Error Core::BitArray::resize(u64 newLength, u64 newBits) {
  120. if(newLength == 0 || newBits == 0 || newBits > 64) {
  121. return ErrorCode::INVALID_ARGUMENT;
  122. }
  123. u64 arrayLength = getArrayLength(newLength, newBits);
  124. u64* newData = new(noThrow) u64[arrayLength];
  125. if(newData == nullptr) {
  126. return ErrorCode::OUT_OF_MEMORY;
  127. }
  128. memset(newData, 0, arrayLength * sizeof(int));
  129. u64 end = Math::min(getLength(), newLength);
  130. for(u64 i = 0; i < end; i++) {
  131. setBits(newData, i, newBits, get(i));
  132. }
  133. for(u64 i = end; i < newLength; i++) {
  134. setBits(newData, i, newBits, 0);
  135. }
  136. delete[] data;
  137. data = newData;
  138. lengthBits = newLength | (newBits << LENGTH_BITS);
  139. return ErrorCode::NONE;
  140. }
  141. void Core::BitArray::swap(BitArray& other) {
  142. Core::swap(lengthBits, other.lengthBits);
  143. Core::swap(data, other.data);
  144. }