BitArray.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace cyh.game
  5. {
  6. public class BitArray : IList<bool>
  7. {
  8. private byte[] array;
  9. private ulong count;
  10. public int Count { get { return (int)this.count; } }
  11. bool ICollection<bool>.IsReadOnly => false;
  12. public BitArray()
  13. {
  14. this.array = Array.Empty<byte>();
  15. this.count = 0;
  16. }
  17. public BitArray(int capacity)
  18. {
  19. this.array = new byte[capacity];
  20. this.count = 0;
  21. }
  22. private void resize(int minsize)
  23. {
  24. if (minsize == 0)
  25. {
  26. if (this.array.Length == 0)
  27. this.array = new byte[4];
  28. else
  29. Array.Resize<byte>(ref this.array, this.array.Length << 1);
  30. }
  31. else if (minsize > this.array.Length)
  32. {
  33. Array.Resize<byte>(ref this.array, minsize);
  34. }
  35. }
  36. public bool this[int index]
  37. {
  38. get
  39. {
  40. if (index < 0)
  41. throw new ArgumentOutOfRangeException();
  42. int offset = index & 7;
  43. index >>= 3;
  44. return (this.array[index] & (1 << offset)) != 0;
  45. }
  46. set
  47. {
  48. int offset = index & 7;
  49. if ((ulong)index >= this.count)
  50. {
  51. this.count = (ulong)index + 1;
  52. index >>= 3;
  53. if (index >= this.array.Length)
  54. this.resize(index + 1);
  55. }
  56. else
  57. index >>= 3;
  58. if (value)
  59. this.array[index] |= (byte)(1 << offset);
  60. else
  61. this.array[index] &= (byte)~(1 << offset);
  62. }
  63. }
  64. public bool this[ulong index]
  65. {
  66. get
  67. {
  68. int offset = (int)(index & 7);
  69. index >>= 3;
  70. return (this.array[index] & (1 << offset)) != 0;
  71. }
  72. set
  73. {
  74. int offset = (int)(index & 7);
  75. if ((ulong)index >= this.count)
  76. {
  77. this.count = (ulong)index + 1;
  78. index >>= 3;
  79. if (index >= (ulong)this.array.Length)
  80. this.resize((int)index + 1);
  81. }
  82. else
  83. index >>= 3;
  84. if (value)
  85. this.array[index] |= (byte)(1 << offset);
  86. else
  87. this.array[index] &= (byte)~(1 << offset);
  88. }
  89. }
  90. public void Add(bool bit)
  91. {
  92. this.count++;
  93. byte offset = (byte)(this.count & 7);
  94. int index = (int)(this.count >> 3);
  95. if (index >= this.array.Length)
  96. this.resize(0);
  97. if (bit)
  98. this.array[index] |= (byte)(1 << offset);
  99. else
  100. this.array[index] &= (byte)~(1 << offset);
  101. }
  102. public override string ToString()
  103. {
  104. System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder((int)this.count);
  105. for (ulong i = 0; i < this.count; i++)
  106. {
  107. stringBuilder.Append(this[i] ? '1' : '0');
  108. }
  109. return stringBuilder.ToString();
  110. }
  111. int IList<bool>.IndexOf(bool item)
  112. {
  113. throw new NotSupportedException();
  114. }
  115. void IList<bool>.Insert(int index, bool item)
  116. {
  117. throw new NotSupportedException();
  118. }
  119. void IList<bool>.RemoveAt(int index)
  120. {
  121. throw new NotSupportedException();
  122. }
  123. void ICollection<bool>.Clear()
  124. {
  125. this.array = Array.Empty<byte>();
  126. this.count = 0;
  127. }
  128. bool ICollection<bool>.Contains(bool item)
  129. {
  130. throw new NotSupportedException();
  131. }
  132. void ICollection<bool>.CopyTo(bool[] array, int arrayIndex)
  133. {
  134. for (ulong i = 0; i < this.count; i++)
  135. {
  136. array[arrayIndex++] = this[i];
  137. }
  138. }
  139. bool ICollection<bool>.Remove(bool item)
  140. {
  141. throw new NotSupportedException();
  142. }
  143. public IEnumerator<bool> GetEnumerator()
  144. {
  145. return new Enumerator(this);
  146. }
  147. IEnumerator IEnumerable.GetEnumerator()
  148. {
  149. return this.GetEnumerator();
  150. }
  151. class Enumerator : IEnumerator<bool>
  152. {
  153. private BitArray Array;
  154. private long index = -1;
  155. public Enumerator(BitArray array)
  156. {
  157. this.Array = array;
  158. }
  159. public bool Current { get { return this.Array[(ulong)this.index]; } }
  160. object IEnumerator.Current { get { return this.Array[(ulong)this.index]; } }
  161. public void Dispose()
  162. {
  163. this.index = -1;
  164. }
  165. public bool MoveNext()
  166. {
  167. this.index++;
  168. return this.index < (long)this.Array.count;
  169. }
  170. public void Reset()
  171. {
  172. this.index = -1;
  173. }
  174. }
  175. public static void Set(byte[] data, int index, bool value)
  176. {
  177. if (index < 0)
  178. throw new ArgumentOutOfRangeException();
  179. int offset = index & 7;
  180. index >>= 3;
  181. if (index >= data.Length)
  182. throw new ArgumentOutOfRangeException();
  183. if (value)
  184. data[index] |= (byte)(1 << offset);
  185. else
  186. data[index] &= (byte)~(1 << offset);
  187. }
  188. public static bool Get(byte[] data, int index)
  189. {
  190. int offset = index & 7;
  191. index >>= 3;
  192. if (index >= data.Length)
  193. throw new ArgumentOutOfRangeException();
  194. return (data[index] & (1 << offset)) != 0;
  195. }
  196. }
  197. }