BitVector32.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // System.Collections.Specialized.BitVector32.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Lawrence Pit ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. //
  10. using System;
  11. using System.Text;
  12. namespace System.Collections.Specialized {
  13. public struct BitVector32 {
  14. int value;
  15. public struct Section {
  16. private short mask;
  17. private short offset;
  18. internal Section (short mask, short offset) {
  19. this.mask = mask;
  20. this.offset = offset;
  21. }
  22. public short Mask {
  23. get { return mask; }
  24. }
  25. public short Offset {
  26. get { return offset; }
  27. }
  28. public override bool Equals (object o)
  29. {
  30. if (! (o is Section))
  31. return false;
  32. Section section = (Section) o;
  33. return this.mask == section.mask &&
  34. this.offset == section.offset;
  35. }
  36. public override int GetHashCode ()
  37. {
  38. return (((Int16) mask).GetHashCode () << 16) +
  39. ((Int16) offset).GetHashCode ();
  40. }
  41. public override string ToString ()
  42. {
  43. return "Section{0x" + Convert.ToString(mask, 16) +
  44. ", 0x" + Convert.ToString(offset, 16) + "}";
  45. }
  46. }
  47. // Constructors
  48. public BitVector32 (BitVector32 source)
  49. {
  50. value = source.value;
  51. }
  52. public BitVector32 (int init)
  53. {
  54. value = init;
  55. }
  56. // Properties
  57. public int Data {
  58. get { return value; }
  59. }
  60. [MonoTODO]
  61. public int this [BitVector32.Section section] {
  62. get { return ((this.value >> section.Offset) & section.Mask); }
  63. set {
  64. throw new NotImplementedException ();
  65. }
  66. }
  67. public bool this [int bit] {
  68. get { return (value & bit) == bit; }
  69. set {
  70. if (value)
  71. this.value |= bit;
  72. else
  73. this.value &= ~bit;
  74. }
  75. }
  76. // Methods
  77. public static int CreateMask ()
  78. {
  79. return 1;
  80. }
  81. public static int CreateMask (int prev)
  82. {
  83. if (prev == 0)
  84. return 1;
  85. if (prev == Int32.MinValue)
  86. throw new InvalidOperationException ("all bits set");
  87. return prev << 1;
  88. }
  89. public static Section CreateSection (short maxValue)
  90. {
  91. return CreateSection (maxValue, new Section (0, 0));
  92. }
  93. public static Section CreateSection (short maxValue, BitVector32.Section previous)
  94. {
  95. if (maxValue < 1)
  96. throw new ArgumentException ("maxValue");
  97. int newmask = (int) maxValue;
  98. int mask = 0x8000;
  99. while ((newmask & mask) == 0)
  100. mask >>= 1;
  101. while (mask > 0) {
  102. newmask |= mask;
  103. mask >>= 1;
  104. }
  105. short count = 0;
  106. int prev = previous.Mask;
  107. mask = 0x8000;
  108. while (mask > 0) {
  109. if ((prev & mask) != 0)
  110. count++;
  111. mask >>= 1;
  112. }
  113. return new Section ((short) newmask, (short) (previous.Offset + count));
  114. }
  115. public override bool Equals (object o)
  116. {
  117. if (!(o is BitVector32))
  118. return false;
  119. return value == ((BitVector32) o).value;
  120. }
  121. public override int GetHashCode ()
  122. {
  123. return value.GetHashCode ();
  124. }
  125. public override string ToString ()
  126. {
  127. return ToString (this);
  128. }
  129. public static string ToString (BitVector32 value)
  130. {
  131. long val = (long) value.value;
  132. StringBuilder b = new StringBuilder ();
  133. b.Append ("BitVector32{");
  134. long mask = (long) 0x80000000;
  135. while (mask > 0) {
  136. b.Append (((val & mask) == 0) ? '0' : '1');
  137. mask >>= 1;
  138. }
  139. b.Append ('}');
  140. return b.ToString ();
  141. }
  142. }
  143. }