BitArray.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //
  2. // Bit Array.cs
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. using System;
  10. namespace System.Collections {
  11. [Serializable]
  12. public sealed class BitArray : ICollection, ICloneable {
  13. int [] _array;
  14. int _length;
  15. int _version = 0;
  16. #region Constructors
  17. public BitArray (BitArray bits)
  18. {
  19. if (bits == null)
  20. throw new ArgumentNullException ("bits");
  21. _length = bits._length;
  22. _array = new int [(_length + 31) / 32];
  23. Array.Copy(bits._array, _array, _array.Length);
  24. }
  25. public BitArray (bool [] values)
  26. {
  27. if (values == null)
  28. throw new ArgumentNullException ("values");
  29. _length = values.Length;
  30. _array = new int [(_length + 31) / 32];
  31. for (int i = 0; i < values.Length; i++)
  32. this [i] = values [i];
  33. }
  34. public BitArray (byte [] bytes)
  35. {
  36. if (bytes == null)
  37. throw new ArgumentNullException ("bytes");
  38. _length = bytes.Length * 8;
  39. _array = new int [(_length + 31) / 32];
  40. for (int i = 0; i < bytes.Length; i++)
  41. setByte (i, bytes [i]);
  42. }
  43. public BitArray (int [] values)
  44. {
  45. if (values == null)
  46. throw new ArgumentNullException ("values");
  47. int arrlen = values.Length;
  48. _length = arrlen*32;
  49. _array = new int [arrlen];
  50. Array.Copy (values, _array, arrlen);
  51. }
  52. public BitArray (int length)
  53. {
  54. if (length < 0)
  55. throw new ArgumentOutOfRangeException ("length");
  56. _length = length;
  57. _array = new int [(_length + 31) / 32];
  58. }
  59. public BitArray (int length, bool defaultValue) : this (length)
  60. {
  61. if (defaultValue) {
  62. for (int i = 0; i < _array.Length; i++)
  63. _array[i] = ~0;
  64. }
  65. }
  66. private BitArray (int [] array, int length)
  67. {
  68. _array = array;
  69. _length = length;
  70. }
  71. #endregion
  72. #region Utility Methods
  73. byte getByte (int byteIndex)
  74. {
  75. int index = byteIndex / 4;
  76. int shift = (byteIndex % 4) * 8;
  77. int theByte = _array [index] & (0xff << shift);
  78. return (byte)((theByte >> shift) & 0xff);
  79. }
  80. void setByte (int byteIndex, byte value)
  81. {
  82. int index = byteIndex / 4;
  83. int shift = (byteIndex % 4) * 8;
  84. // clear the byte
  85. _array [index] &= ~(0xff << shift);
  86. // or in the new byte
  87. _array [index] |= value << shift;
  88. _version++;
  89. }
  90. void checkOperand (BitArray operand)
  91. {
  92. if (operand == null)
  93. throw new ArgumentNullException ();
  94. if (operand._length != _length)
  95. throw new ArgumentException ();
  96. }
  97. #endregion
  98. public int Count {
  99. get { return _length; }
  100. }
  101. public bool IsReadOnly {
  102. get { return false; }
  103. }
  104. public bool IsSynchronized {
  105. get { return false; }
  106. }
  107. public bool this [int index] {
  108. get { return Get (index); }
  109. set { Set (index, value); }
  110. }
  111. public int Length {
  112. get { return _length; }
  113. set {
  114. if (value < 0)
  115. throw new ArgumentOutOfRangeException ();
  116. int newLen = value;
  117. if (_length != newLen) {
  118. int numints = (newLen + 31) / 32;
  119. int [] newArr = new int [numints];
  120. int copylen = (numints > _array.Length) ? _array.Length : numints;
  121. Array.Copy (_array, newArr, copylen);
  122. // set the internal state
  123. _array = newArr;
  124. _length = newLen;
  125. _version++;
  126. }
  127. }
  128. }
  129. public object SyncRoot {
  130. get { return this; }
  131. }
  132. public object Clone ()
  133. {
  134. // LAMESPEC: docs say shallow, MS makes deep.
  135. return new BitArray (this);
  136. }
  137. public void CopyTo (Array array, int index)
  138. {
  139. if (array == null)
  140. throw new ArgumentNullException ("array");
  141. if (index < 0)
  142. throw new ArgumentOutOfRangeException ("index");
  143. if (array.Rank != 1)
  144. throw new ArgumentException ("array", "Array rank must be 1");
  145. if (index >= array.Length)
  146. throw new ArgumentException ("index", "index is greater than array.Length");
  147. // in each case, check to make sure enough space in array
  148. if (array is bool []) {
  149. if (array.Length - index < _length)
  150. throw new ArgumentException ();
  151. bool [] barray = (bool []) array;
  152. // Copy the bits into the array
  153. for (int i = 0; i < _length; i++)
  154. barray[index + i] = this [i];
  155. } else if (array is byte []) {
  156. int numbytes = (_length + 7) / 8;
  157. if ((array.Length - index) < numbytes)
  158. throw new ArgumentException ();
  159. byte [] barray = (byte []) array;
  160. // Copy the bytes into the array
  161. for (int i = 0; i < numbytes; i++)
  162. barray [index + i] = getByte (i);
  163. } else if (array is int []) {
  164. Array.Copy (_array, 0, array, index, (_length + 31) / 32);
  165. } else {
  166. throw new ArgumentException ("array", "Unsupported type");
  167. }
  168. }
  169. public BitArray Not ()
  170. {
  171. int ints = (_length + 31) / 32;
  172. for (int i = 0; i < ints; i++)
  173. _array [i] = ~_array [i];
  174. _version++;
  175. return this;
  176. }
  177. public BitArray And (BitArray value)
  178. {
  179. checkOperand (value);
  180. int ints = (_length + 31) / 32;
  181. for (int i = 0; i < ints; i++)
  182. _array [i] &= value._array [i];
  183. _version++;
  184. return this;
  185. }
  186. public BitArray Or (BitArray value)
  187. {
  188. checkOperand (value);
  189. int ints = (_length + 31) / 32;
  190. for (int i = 0; i < ints; i++)
  191. _array [i] |= value._array [i];
  192. _version++;
  193. return this;
  194. }
  195. public BitArray Xor (BitArray value)
  196. {
  197. checkOperand (value);
  198. int ints = (_length + 31) / 32;
  199. for (int i = 0; i < ints; i++)
  200. _array [i] ^= value._array [i];
  201. _version++;
  202. return this;
  203. }
  204. public bool Get (int index)
  205. {
  206. if (index < 0 || index >= _length)
  207. throw new ArgumentOutOfRangeException ();
  208. return (_array [index / 32] & (1 << (index % 32))) != 0;
  209. }
  210. public void Set (int index, bool value)
  211. {
  212. if (index < 0 || index >= _length)
  213. throw new ArgumentOutOfRangeException ();
  214. if (value)
  215. _array [index / 32] |= (1 << (index % 32));
  216. else
  217. _array [index / 32] &= ~(1 << (index % 32));
  218. _version++;
  219. }
  220. public void SetAll (bool value)
  221. {
  222. if (value) {
  223. for (int i = 0; i < _array.Length; i++)
  224. _array[i] = ~0;
  225. }
  226. else
  227. Array.Clear (_array, 0, _array.Length);
  228. _version++;
  229. }
  230. public IEnumerator GetEnumerator ()
  231. {
  232. return new BitArrayEnumerator (this);
  233. }
  234. [Serializable]
  235. class BitArrayEnumerator : IEnumerator, ICloneable {
  236. BitArray _bitArray;
  237. bool _current;
  238. int _index, _max, _version;
  239. public object Clone () {
  240. return MemberwiseClone ();
  241. }
  242. public BitArrayEnumerator (BitArray ba)
  243. {
  244. _index = -1;
  245. _bitArray = ba;
  246. _max = ba._length;
  247. _version = ba._version;
  248. }
  249. public object Current {
  250. get {
  251. if (_index == -1)
  252. throw new InvalidOperationException ("Enum not started");
  253. if (_index >= _bitArray.Count)
  254. throw new InvalidOperationException ("Enum Ended");
  255. return _current;
  256. }
  257. }
  258. public bool MoveNext ()
  259. {
  260. checkVersion ();
  261. if (_index < (_bitArray.Count - 1)) {
  262. _current = _bitArray [++_index];
  263. return true;
  264. }
  265. else
  266. _index = _bitArray.Count;
  267. return false;
  268. }
  269. public void Reset ()
  270. {
  271. checkVersion ();
  272. _index = -1;
  273. }
  274. void checkVersion ()
  275. {
  276. if (_version != _bitArray._version)
  277. throw new InvalidOperationException ();
  278. }
  279. }
  280. }
  281. }