BitVector32.cs 8.3 KB

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