/mcs/class/System/System.Collections.Specialized/BitVector32.cs

https://bitbucket.org/luobailiang/mono · C# · 233 lines · 162 code · 36 blank · 35 comment · 27 complexity · 49158db4fef6f7a1de1e45a3c340dd34 MD5 · raw file

  1. //
  2. // System.Collections.Specialized.BitVector32.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza (miguel@ximian.com)
  6. // Lawrence Pit (loz@cable.a2000.nl)
  7. // Andrew Birkett (adb@tardis.ed.ac.uk)
  8. // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
  9. //
  10. // (C) Ximian, Inc. http://www.ximian.com
  11. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Text;
  33. namespace System.Collections.Specialized {
  34. public struct BitVector32 {
  35. int bits;
  36. public struct Section {
  37. private short mask;
  38. private short offset;
  39. internal Section (short mask, short offset) {
  40. this.mask = mask;
  41. this.offset = offset;
  42. }
  43. public short Mask {
  44. get { return mask; }
  45. }
  46. public short Offset {
  47. get { return offset; }
  48. }
  49. #if NET_2_0
  50. public static bool operator == (Section v1, Section v2)
  51. {
  52. return v1.mask == v2.mask &&
  53. v1.offset == v2.offset;
  54. }
  55. public static bool operator != (Section v1, Section v2)
  56. {
  57. return v1.mask != v2.mask ||
  58. v1.offset != v2.offset;
  59. }
  60. public bool Equals (Section obj)
  61. {
  62. return this.mask == obj.mask &&
  63. this.offset == obj.offset;
  64. }
  65. #endif
  66. public override bool Equals (object o)
  67. {
  68. if (! (o is Section))
  69. return false;
  70. Section section = (Section) o;
  71. return this.mask == section.mask &&
  72. this.offset == section.offset;
  73. }
  74. public override int GetHashCode ()
  75. {
  76. return mask << offset;
  77. }
  78. public override string ToString ()
  79. {
  80. return ToString (this);
  81. }
  82. public static string ToString (Section value)
  83. {
  84. StringBuilder b = new StringBuilder ();
  85. b.Append ("Section{0x");
  86. b.Append (Convert.ToString(value.Mask,16));
  87. b.Append (", 0x");
  88. b.Append (Convert.ToString(value.Offset,16));
  89. b.Append ("}");
  90. return b.ToString ();
  91. }
  92. }
  93. // Constructors
  94. public BitVector32 (BitVector32 source)
  95. {
  96. bits = source.bits;
  97. }
  98. public BitVector32 (int init)
  99. {
  100. bits = init;
  101. }
  102. // Properties
  103. public int Data {
  104. get { return bits; }
  105. }
  106. public int this [BitVector32.Section section] {
  107. get {
  108. return ((bits >> section.Offset) & section.Mask);
  109. }
  110. set {
  111. if (value < 0)
  112. throw new ArgumentException ("Section can't hold negative values");
  113. if (value > section.Mask)
  114. throw new ArgumentException ("Value too large to fit in section");
  115. bits &= ~(section.Mask << section.Offset);
  116. bits |= (value << section.Offset);
  117. }
  118. }
  119. public bool this [int mask] {
  120. get {
  121. #if NET_2_0
  122. return (bits & mask) == mask;
  123. #else
  124. long tmp = (uint)bits;
  125. return (tmp & (long)mask) == (long)mask;
  126. #endif
  127. }
  128. set {
  129. if (value)
  130. bits |= mask;
  131. else
  132. bits &= ~mask;
  133. }
  134. }
  135. // Methods
  136. public static int CreateMask ()
  137. {
  138. return 1;
  139. }
  140. public static int CreateMask (int prev)
  141. {
  142. if (prev == 0)
  143. return 1;
  144. if (prev == Int32.MinValue)
  145. throw new InvalidOperationException ("all bits set");
  146. return prev << 1;
  147. }
  148. public static Section CreateSection (short maxValue)
  149. {
  150. return CreateSection (maxValue, new Section (0, 0));
  151. }
  152. public static Section CreateSection (short maxValue, BitVector32.Section previous)
  153. {
  154. if (maxValue < 1)
  155. throw new ArgumentException ("maxValue");
  156. int bit = HighestSetBit(maxValue);
  157. int mask = (1 << bit) - 1;
  158. int offset = previous.Offset + HighestSetBit (previous.Mask);
  159. if (offset + bit > 32) {
  160. throw new ArgumentException ("Sections cannot exceed 32 bits in total");
  161. }
  162. return new Section ((short) mask, (short) offset);
  163. }
  164. public override bool Equals (object o)
  165. {
  166. return (o is BitVector32) && bits == ((BitVector32) o).bits;
  167. }
  168. public override int GetHashCode ()
  169. {
  170. return bits.GetHashCode ();
  171. }
  172. public override string ToString ()
  173. {
  174. return ToString (this);
  175. }
  176. public static string ToString (BitVector32 value)
  177. {
  178. StringBuilder b = new StringBuilder ();
  179. b.Append ("BitVector32{");
  180. long mask = (long) 0x80000000;
  181. while (mask > 0) {
  182. b.Append (((value.bits & mask) == 0) ? '0' : '1');
  183. mask >>= 1;
  184. }
  185. b.Append ('}');
  186. return b.ToString ();
  187. }
  188. // Private utilities
  189. private static int HighestSetBit (int i)
  190. {
  191. int count = 0;
  192. while(i >> count != 0)
  193. count++;
  194. return count;
  195. }
  196. }
  197. }