BitVector32.cs 3.8 KB

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