You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AccessBitsDbo.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //
  2. // Created by robin on 7/21/16.
  3. //
  4. #include <cstring>
  5. #include <iostream>
  6. #include "AccessBitsDbo.h"
  7. #include "StringUtils.h"
  8. #include "ArrayUtils.h"
  9. const char AccessBitsDbo::nonInvertedBitPosition[4][4] = {
  10. {0, 0, 0, 0 },
  11. {11, 10, 9, 8 },
  12. {23, 22, 21, 20},
  13. {19, 18, 17, 16}};
  14. const char AccessBitsDbo::invertedBitPosition[4][4] = {
  15. {0, 0, 0, 0 },
  16. {7, 6, 5, 4 },
  17. {3, 2, 1, 0},
  18. {15, 14, 13, 12}};
  19. AccessBitsDbo::AccessBitsDbo()
  20. : _bits("\xff\x0f\00\x00", 4)
  21. {
  22. }
  23. AccessBitsDbo::AccessBitsDbo(const std::string &bits)
  24. : _bits(StringUtils::ensureSize(bits, 4))
  25. {
  26. }
  27. char AccessBitsDbo::getUserData() const
  28. {
  29. return _bits[_bits.length() - 1];
  30. }
  31. void AccessBitsDbo::setUserData(const char &data)
  32. {
  33. _bits[_bits.length() - 1] = data;
  34. }
  35. void AccessBitsDbo::setBit(int i, int j, const bool& value)
  36. {
  37. char buf[_bits.length()];
  38. memcpy(buf, _bits.c_str(), _bits.length());
  39. ArrayUtils::setArrayBit(buf, nonInvertedBitPosition[i][j], value);
  40. ArrayUtils::setArrayBit(buf, invertedBitPosition[i][j], !value);
  41. _bits = std::string(buf, _bits.length());
  42. }
  43. bool AccessBitsDbo::getBit(int i, int j) const
  44. {
  45. const char* buf = _bits.c_str();
  46. return ArrayUtils::getArrayBit(buf, nonInvertedBitPosition[i][j]) && !ArrayUtils::getArrayBit(buf, invertedBitPosition[i][j]);
  47. }
  48. std::string AccessBitsDbo::getBits() const
  49. {
  50. return _bits;
  51. }
  52. bool AccessBitsDbo::canKeyAReadBlock(int block) const
  53. {
  54. bool c1 = getBit(1, block);
  55. bool c2 = getBit(2, block);
  56. bool c3 = getBit(3, block);
  57. return !c3 || (!c1 && !c2 && c3);
  58. }
  59. bool AccessBitsDbo::canKeyBReadBlock(int block) const
  60. {
  61. bool c1 = getBit(1, block);
  62. bool c2 = getBit(2, block);
  63. bool c3 = getBit(3, block);
  64. return !c1 || !c2 || !c3;
  65. }
  66. bool AccessBitsDbo::canKeyAWriteBlock(int block) const
  67. {
  68. bool c1 = getBit(1, block);
  69. bool c2 = getBit(2, block);
  70. bool c3 = getBit(3, block);
  71. return !c1 && !c2 && !c3;
  72. }
  73. bool AccessBitsDbo::canKeyBWriteBlock(int block) const
  74. {
  75. bool c1 = getBit(1, block);
  76. bool c2 = getBit(2, block);
  77. bool c3 = getBit(3, block);
  78. return (!c2 && !c3) || (c1 && !c3) || (!c1 && c2 && c3);
  79. }
  80. bool AccessBitsDbo::canKeyAIncrementBlock(int block) const
  81. {
  82. bool c1 = getBit(1, block);
  83. bool c2 = getBit(2, block);
  84. bool c3 = getBit(3, block);
  85. return !c1 && !c2 && !c3;
  86. }
  87. bool AccessBitsDbo::canKeyBIncrementBlock(int block) const
  88. {
  89. bool c1 = getBit(1, block);
  90. bool c2 = getBit(2, block);
  91. bool c3 = getBit(3, block);
  92. return (!c1 && !c2 && !c3) || (c1 && c2 && !c3);
  93. }
  94. bool AccessBitsDbo::canKeyADecrementBlock(int block) const
  95. {
  96. bool c1 = getBit(1, block);
  97. bool c2 = getBit(2, block);
  98. bool c3 = getBit(3, block);
  99. return (!c1 && !c2) || (c1 && c2 && !c3);
  100. }
  101. bool AccessBitsDbo::canKeyBDecrementBlock(int block) const
  102. {
  103. bool c1 = getBit(1, block);
  104. bool c2 = getBit(2, block);
  105. bool c3 = getBit(3, block);
  106. return (!c1 && !c2) || (c1 && c2 && !c3);
  107. }
  108. bool AccessBitsDbo::canKeyAReadKeyATrailer() const
  109. {
  110. return false;
  111. }
  112. bool AccessBitsDbo::canKeyBReadKeyATrailer() const
  113. {
  114. return false;
  115. }
  116. bool AccessBitsDbo::canKeyAWriteKeyATrailer() const
  117. {
  118. bool c1 = getBit(1, 3);
  119. bool c2 = getBit(2, 3);
  120. bool c3 = getBit(3, 3);
  121. return !c1 && !c2;
  122. }
  123. bool AccessBitsDbo::canKeyBWriteKeyATrailer() const
  124. {
  125. bool c1 = getBit(1, 3);
  126. bool c2 = getBit(2, 3);
  127. bool c3 = getBit(3, 3);
  128. return (!c1 && c2 && c3) || (c1 && !c2 && !c3);
  129. }
  130. bool AccessBitsDbo::canKeyAReadAccessBitsTrailer() const
  131. {
  132. return true;
  133. }
  134. bool AccessBitsDbo::canKeyBReadAccessBitsTrailer() const
  135. {
  136. bool c1 = getBit(1, 3);
  137. bool c2 = getBit(2, 3);
  138. bool c3 = getBit(3, 3);
  139. return c1 || (c2 && c3);
  140. }
  141. bool AccessBitsDbo::canKeyAWriteAccessBitsTrailer() const
  142. {
  143. bool c1 = getBit(1, 3);
  144. bool c2 = getBit(2, 3);
  145. bool c3 = getBit(3, 3);
  146. return !c1 && !c2 && c3;
  147. }
  148. bool AccessBitsDbo::canKeyBWriteAccessBitsTrailer() const
  149. {
  150. bool c1 = getBit(1, 3);
  151. bool c2 = getBit(2, 3);
  152. bool c3 = getBit(3, 3);
  153. return (!c1 && c2 && c3) || (c1 && !c2 && c3);
  154. }
  155. bool AccessBitsDbo::canKeyAReadKeyBTrailer() const
  156. {
  157. bool c1 = getBit(1, 3);
  158. bool c2 = getBit(2, 3);
  159. bool c3 = getBit(3, 3);
  160. return (!c1 && !c2) || (!c1 && !c3);
  161. }
  162. bool AccessBitsDbo::canKeyBReadKeyBTrailer() const
  163. {
  164. return false;
  165. }
  166. bool AccessBitsDbo::canKeyAWriteKeyBTrailer() const
  167. {
  168. bool c1 = getBit(1, 3);
  169. bool c2 = getBit(2, 3);
  170. bool c3 = getBit(3, 3);
  171. return !c1 && !c2;
  172. }
  173. bool AccessBitsDbo::canKeyBWriteKeyBTrailer() const
  174. {
  175. bool c1 = getBit(1, 3);
  176. bool c2 = getBit(2, 3);
  177. bool c3 = getBit(3, 3);
  178. return (!c1 && c2 && c3) || (c1 && !c2 && !c3);
  179. }