BitArray.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "core/data/BitArray.hpp"
  2. #include "core/math/Math.hpp"
  3. #include "core/utils/New.hpp"
  4. static u64 roundUpDivide(u64 a, u64 b) {
  5. if(a % b == 0) {
  6. return a / b;
  7. }
  8. return a / b + 1;
  9. }
  10. static constexpr u64 U64_BITS = 64;
  11. static constexpr u64 DIVIDE_BITS = Core::Math::roundUpLog2(U64_BITS);
  12. static constexpr u64 LENGTH_MASK = 0x01FF'FFFF'FFFF'FFFF;
  13. static constexpr u64 LENGTH_BITS = Core::Math::roundUpLog2(LENGTH_MASK);
  14. static_assert(LENGTH_BITS == 57, "wusi");
  15. static u64 readBits(const u64* data, u64 index, u64 bits) {
  16. u64 dataIndexA = (index * bits) >> DIVIDE_BITS;
  17. u64 dataIndexB = ((index * bits) + (bits - 1lu)) >> DIVIDE_BITS;
  18. u64 shifts = (index * bits) & (U64_BITS - 1lu);
  19. if(dataIndexA == dataIndexB) {
  20. return (data[dataIndexA] >> shifts) & ((1lu << bits) - 1lu);
  21. }
  22. u64 bitsInA = U64_BITS - shifts;
  23. u64 r = (data[dataIndexA] >> shifts) & ((1lu << bitsInA) - 1lu);
  24. r |= (data[dataIndexB] & ((1lu << (bits - bitsInA)) - 1lu)) << bitsInA;
  25. return r;
  26. }
  27. static void setBits(u64* data, u64 index, u64 bits, u64 value) {
  28. u64 mask = (1lu << bits) - 1lu;
  29. value &= mask;
  30. u64 dataIndexA = (index * bits) >> DIVIDE_BITS;
  31. u64 dataIndexB = ((index * bits) + (bits - 1lu)) >> DIVIDE_BITS;
  32. u64 shifts = (index * bits) & (U64_BITS - 1lu);
  33. data[dataIndexA] &= ~(mask << shifts);
  34. data[dataIndexA] |= (value << shifts);
  35. if(dataIndexA != dataIndexB) {
  36. u64 leftBits = bits - (U64_BITS - shifts);
  37. data[dataIndexB] &= ~((1lu << leftBits) - 1lu);
  38. data[dataIndexB] |= (value >> (U64_BITS - shifts));
  39. }
  40. }
  41. static u64 getArrayLength(u64 length, u64 bits) {
  42. return roundUpDivide(length * bits, U64_BITS);
  43. }
  44. Core::BitArray::BitArray() : lengthBits(0), data(nullptr) {
  45. }
  46. check_return Core::Error Core::BitArray::copyFrom(const BitArray& other) {
  47. CORE_RETURN_ERROR(resize(other.getLength(), other.getBits()));
  48. u64 length = getLength();
  49. for(u64 i = 0; i < length; i++) {
  50. set(i, other.get(i));
  51. }
  52. return Error::NONE;
  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(u64 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(u64 index) const {
  72. if(data == nullptr || index >= getLength()) {
  73. return 0;
  74. }
  75. return readBits(data, index, getBits());
  76. }
  77. u64 Core::BitArray::getLength() const {
  78. return lengthBits & LENGTH_MASK;
  79. }
  80. u64 Core::BitArray::getBits() const {
  81. return (lengthBits & ~LENGTH_MASK) >> LENGTH_BITS;
  82. }
  83. u64 Core::BitArray::getInternalByteSize() const {
  84. if(getLength() <= 0 || getBits() <= 0) {
  85. return 0;
  86. }
  87. return getArrayLength(getLength(), getBits()) * CORE_SIZE(u64);
  88. }
  89. i64 Core::BitArray::select(u64 index) const {
  90. if(index <= 0) {
  91. return -1;
  92. }
  93. u64 found = 0;
  94. u64 end = getArrayLength(getLength(), getBits());
  95. for(u64 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. u64 length = getLength();
  114. for(u64 i = 0; i < length; i++) {
  115. set(i, value);
  116. }
  117. }
  118. check_return Core::Error Core::BitArray::resize(u64 newLength, u64 newBits) {
  119. if(newLength == 0 || newBits == 0 || newBits > 64) {
  120. return Error::INVALID_ARGUMENT;
  121. }
  122. u64 arrayLength = getArrayLength(newLength, newBits);
  123. u64* newData = new u64[arrayLength];
  124. if(newData == nullptr) {
  125. return Error::OUT_OF_MEMORY;
  126. }
  127. Core::memorySet(newData, 0, static_cast<i64>(arrayLength) * CORE_SIZE(int));
  128. u64 end = Math::min(getLength(), newLength);
  129. for(u64 i = 0; i < end; i++) {
  130. setBits(newData, i, newBits, get(i));
  131. }
  132. for(u64 i = end; i < newLength; i++) {
  133. setBits(newData, i, newBits, 0);
  134. }
  135. delete[] data;
  136. data = newData;
  137. lengthBits = newLength | (newBits << LENGTH_BITS);
  138. return Error::NONE;
  139. }
  140. void Core::BitArray::swap(BitArray& other) {
  141. Core::swap(lengthBits, other.lengthBits);
  142. Core::swap(data, other.data);
  143. }