BitArray.cpp 4.1 KB

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