2
0

BitVector32.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace BDInfo
  7. {
  8. using System.Diagnostics;
  9. using System.Text;
  10. using System;
  11. using Microsoft.Win32;
  12. /// <devdoc>
  13. /// <para>Provides a simple light bit vector with easy integer or Boolean access to
  14. /// a 32 bit storage.</para>
  15. /// </devdoc>
  16. public struct BitVector32
  17. {
  18. private uint data;
  19. /// <devdoc>
  20. /// <para>Initializes a new instance of the BitVector32 structure with the specified internal data.</para>
  21. /// </devdoc>
  22. public BitVector32(int data)
  23. {
  24. this.data = (uint)data;
  25. }
  26. /// <devdoc>
  27. /// <para>Initializes a new instance of the BitVector32 structure with the information in the specified
  28. /// value.</para>
  29. /// </devdoc>
  30. public BitVector32(BitVector32 value)
  31. {
  32. this.data = value.data;
  33. }
  34. /// <devdoc>
  35. /// <para>Gets or sets a value indicating whether all the specified bits are set.</para>
  36. /// </devdoc>
  37. public bool this[int bit]
  38. {
  39. get
  40. {
  41. return (data & bit) == (uint)bit;
  42. }
  43. set
  44. {
  45. if (value)
  46. {
  47. data |= (uint)bit;
  48. }
  49. else
  50. {
  51. data &= ~(uint)bit;
  52. }
  53. }
  54. }
  55. /// <devdoc>
  56. /// <para>Gets or sets the value for the specified section.</para>
  57. /// </devdoc>
  58. public int this[Section section]
  59. {
  60. get
  61. {
  62. return (int)((data & (uint)(section.Mask << section.Offset)) >> section.Offset);
  63. }
  64. set
  65. {
  66. #if DEBUG
  67. if ((value & section.Mask) != value) {
  68. Debug.Fail("Value out of bounds on BitVector32 Section Set!");
  69. }
  70. #endif
  71. value <<= section.Offset;
  72. int offsetMask = (0xFFFF & (int)section.Mask) << section.Offset;
  73. data = (data & ~(uint)offsetMask) | ((uint)value & (uint)offsetMask);
  74. }
  75. }
  76. /// <devdoc>
  77. /// returns the raw data stored in this bit vector...
  78. /// </devdoc>
  79. public int Data
  80. {
  81. get
  82. {
  83. return (int)data;
  84. }
  85. }
  86. private static short CountBitsSet(short mask)
  87. {
  88. // yes, I know there are better algorithms, however, we know the
  89. // bits are always right aligned, with no holes (i.e. always 00000111,
  90. // never 000100011), so this is just fine...
  91. //
  92. short value = 0;
  93. while ((mask & 0x1) != 0)
  94. {
  95. value++;
  96. mask >>= 1;
  97. }
  98. return value;
  99. }
  100. /// <devdoc>
  101. /// <para> Creates the first mask in a series.</para>
  102. /// </devdoc>
  103. public static int CreateMask()
  104. {
  105. return CreateMask(0);
  106. }
  107. /// <devdoc>
  108. /// Creates the next mask in a series.
  109. /// </devdoc>
  110. public static int CreateMask(int previous)
  111. {
  112. if (previous == 0)
  113. {
  114. return 1;
  115. }
  116. if (previous == unchecked((int)0x80000000))
  117. {
  118. throw new InvalidOperationException("Bit vector full");
  119. }
  120. return previous << 1;
  121. }
  122. /// <devdoc>
  123. /// Given a highValue, creates the mask
  124. /// </devdoc>
  125. private static short CreateMaskFromHighValue(short highValue)
  126. {
  127. short required = 16;
  128. while ((highValue & 0x8000) == 0)
  129. {
  130. required--;
  131. highValue <<= 1;
  132. }
  133. ushort value = 0;
  134. while (required > 0)
  135. {
  136. required--;
  137. value <<= 1;
  138. value |= 0x1;
  139. }
  140. return unchecked((short)value);
  141. }
  142. /// <devdoc>
  143. /// <para>Creates the first section in a series, with the specified maximum value.</para>
  144. /// </devdoc>
  145. public static Section CreateSection(short maxValue)
  146. {
  147. return CreateSectionHelper(maxValue, 0, 0);
  148. }
  149. /// <devdoc>
  150. /// <para>Creates the next section in a series, with the specified maximum value.</para>
  151. /// </devdoc>
  152. public static Section CreateSection(short maxValue, Section previous)
  153. {
  154. return CreateSectionHelper(maxValue, previous.Mask, previous.Offset);
  155. }
  156. private static Section CreateSectionHelper(short maxValue, short priorMask, short priorOffset)
  157. {
  158. if (maxValue < 1)
  159. {
  160. throw new ArgumentOutOfRangeException("maxValue");
  161. }
  162. #if DEBUG
  163. int maskCheck = CreateMaskFromHighValue(maxValue);
  164. int offsetCheck = priorOffset + CountBitsSet(priorMask);
  165. Debug.Assert(maskCheck <= short.MaxValue && offsetCheck < 32, "Overflow on BitVector32");
  166. #endif
  167. short offset = (short)(priorOffset + CountBitsSet(priorMask));
  168. if (offset >= 32)
  169. {
  170. throw new InvalidOperationException("Bit vector full");
  171. }
  172. return new Section(CreateMaskFromHighValue(maxValue), offset);
  173. }
  174. public override bool Equals(object o)
  175. {
  176. if (!(o is BitVector32))
  177. {
  178. return false;
  179. }
  180. return data == ((BitVector32)o).data;
  181. }
  182. public override int GetHashCode()
  183. {
  184. return base.GetHashCode();
  185. }
  186. /// <devdoc>
  187. /// </devdoc>
  188. public static string ToString(BitVector32 value)
  189. {
  190. StringBuilder sb = new StringBuilder(/*"BitVector32{".Length*/12 + /*32 bits*/32 + /*"}".Length"*/1);
  191. sb.Append("BitVector32{");
  192. int locdata = (int)value.data;
  193. for (int i = 0; i < 32; i++)
  194. {
  195. if ((locdata & 0x80000000) != 0)
  196. {
  197. sb.Append("1");
  198. }
  199. else
  200. {
  201. sb.Append("0");
  202. }
  203. locdata <<= 1;
  204. }
  205. sb.Append("}");
  206. return sb.ToString();
  207. }
  208. /// <devdoc>
  209. /// </devdoc>
  210. public override string ToString()
  211. {
  212. return BitVector32.ToString(this);
  213. }
  214. /// <devdoc>
  215. /// <para>
  216. /// Represents an section of the vector that can contain a integer number.</para>
  217. /// </devdoc>
  218. public struct Section
  219. {
  220. private readonly short mask;
  221. private readonly short offset;
  222. internal Section(short mask, short offset)
  223. {
  224. this.mask = mask;
  225. this.offset = offset;
  226. }
  227. public short Mask
  228. {
  229. get
  230. {
  231. return mask;
  232. }
  233. }
  234. public short Offset
  235. {
  236. get
  237. {
  238. return offset;
  239. }
  240. }
  241. public override bool Equals(object o)
  242. {
  243. if (o is Section)
  244. return Equals((Section)o);
  245. else
  246. return false;
  247. }
  248. public bool Equals(Section obj)
  249. {
  250. return obj.mask == mask && obj.offset == offset;
  251. }
  252. public static bool operator ==(Section a, Section b)
  253. {
  254. return a.Equals(b);
  255. }
  256. public static bool operator !=(Section a, Section b)
  257. {
  258. return !(a == b);
  259. }
  260. public override int GetHashCode()
  261. {
  262. return base.GetHashCode();
  263. }
  264. /// <devdoc>
  265. /// </devdoc>
  266. public static string ToString(Section value)
  267. {
  268. return "Section{0x" + Convert.ToString(value.Mask, 16) + ", 0x" + Convert.ToString(value.Offset, 16) + "}";
  269. }
  270. /// <devdoc>
  271. /// </devdoc>
  272. public override string ToString()
  273. {
  274. return Section.ToString(this);
  275. }
  276. }
  277. }
  278. }