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 5.4KB

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