BitVector32.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // System.Collections.Specialized.BitVector32.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Lawrence Pit ([email protected])
  7. // Andrew Birkett ([email protected])
  8. // Andreas Nahr ([email protected])
  9. //
  10. // (C) Ximian, Inc. http://www.ximian.com
  11. //
  12. using System;
  13. using System.Text;
  14. namespace System.Collections.Specialized {
  15. public struct BitVector32 {
  16. int bits;
  17. public struct Section {
  18. private short mask;
  19. private short offset;
  20. internal Section (short mask, short offset) {
  21. this.mask = mask;
  22. this.offset = offset;
  23. }
  24. public short Mask {
  25. get { return mask; }
  26. }
  27. public short Offset {
  28. get { return offset; }
  29. }
  30. public override bool Equals (object o)
  31. {
  32. if (! (o is Section))
  33. return false;
  34. Section section = (Section) o;
  35. return this.mask == section.mask &&
  36. this.offset == section.offset;
  37. }
  38. public override int GetHashCode ()
  39. {
  40. return (((Int16) mask).GetHashCode () << 16) +
  41. ((Int16) offset).GetHashCode ();
  42. }
  43. public override string ToString ()
  44. {
  45. return "Section{0x" + Convert.ToString(mask, 16) +
  46. ", 0x" + Convert.ToString(offset, 16) + "}";
  47. }
  48. public static string ToString (Section value)
  49. {
  50. StringBuilder b = new StringBuilder ();
  51. b.Append ("Section{0x");
  52. b.Append (Convert.ToString(value.Mask,16));
  53. b.Append (", 0x");
  54. b.Append (Convert.ToString(value.Offset,16));
  55. b.Append ("}");
  56. return b.ToString ();
  57. }
  58. }
  59. // Constructors
  60. public BitVector32 (BitVector32 source)
  61. {
  62. bits = source.bits;
  63. }
  64. public BitVector32 (int init)
  65. {
  66. bits = init;
  67. }
  68. // Properties
  69. public int Data {
  70. get { return bits; }
  71. }
  72. public int this [BitVector32.Section section] {
  73. get {
  74. return ((bits >> section.Offset) & section.Mask);
  75. }
  76. set {
  77. if (value < 0)
  78. throw new ArgumentException ("Section can't hold negative values");
  79. if (value > section.Mask)
  80. throw new ArgumentException ("Value too large to fit in section");
  81. bits &= ~(section.Mask << section.Offset);
  82. bits |= (value << section.Offset);
  83. }
  84. }
  85. public bool this [int mask] {
  86. get {
  87. long tmp = (uint)bits;
  88. return (tmp & (long)mask) == (long)mask;
  89. }
  90. set {
  91. if (value)
  92. bits |= mask;
  93. else
  94. bits &= ~mask;
  95. }
  96. }
  97. // Methods
  98. public static int CreateMask ()
  99. {
  100. return 1;
  101. }
  102. public static int CreateMask (int prev)
  103. {
  104. if (prev == 0)
  105. return 1;
  106. if (prev == Int32.MinValue)
  107. throw new InvalidOperationException ("all bits set");
  108. return prev << 1;
  109. }
  110. public static Section CreateSection (short maxValue)
  111. {
  112. return CreateSection (maxValue, new Section (0, 0));
  113. }
  114. public static Section CreateSection (short maxValue, BitVector32.Section previous)
  115. {
  116. if (maxValue < 1)
  117. throw new ArgumentException ("maxValue");
  118. int bit = HighestSetBit(maxValue) + 1;
  119. int mask = (1 << bit) - 1;
  120. int offset = previous.Offset + NumberOfSetBits (previous.Mask);
  121. if (offset > 32) {
  122. throw new ArgumentException ("Sections cannot exceed 32 bits in total");
  123. }
  124. return new Section ((short) mask, (short) offset);
  125. }
  126. public override bool Equals (object o)
  127. {
  128. if (!(o is BitVector32))
  129. return false;
  130. return bits == ((BitVector32) o).bits;
  131. }
  132. public override int GetHashCode ()
  133. {
  134. return bits.GetHashCode ();
  135. }
  136. public override string ToString ()
  137. {
  138. return ToString (this);
  139. }
  140. public static string ToString (BitVector32 value)
  141. {
  142. StringBuilder b = new StringBuilder ();
  143. b.Append ("BitVector32{");
  144. long mask = (long) 0x80000000;
  145. while (mask > 0) {
  146. b.Append (((value.bits & mask) == 0) ? '0' : '1');
  147. mask >>= 1;
  148. }
  149. b.Append ('}');
  150. return b.ToString ();
  151. }
  152. // Private utilities
  153. private static int NumberOfSetBits (int i)
  154. {
  155. int count = 0;
  156. for (int bit = 0; bit < 32; bit++) {
  157. int mask = 1 << bit;
  158. if ((i & mask) != 0)
  159. count++;
  160. }
  161. return count;
  162. }
  163. private static int HighestSetBit (int i)
  164. {
  165. for (int bit = 31; bit >= 0; bit--) {
  166. int mask = 1 << bit;
  167. if ((mask & i) != 0) {
  168. return bit;
  169. }
  170. }
  171. return -1;
  172. }
  173. }
  174. }