BitArray.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. //
  2. // Bit Array.cs
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. //
  7. // (C) 2003 Ben Maurer
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. namespace System.Collections {
  33. [Serializable]
  34. public sealed class BitArray : ICollection, ICloneable {
  35. int [] _array;
  36. int _length;
  37. int _version = 0;
  38. #region Constructors
  39. public BitArray (BitArray bits)
  40. {
  41. if (bits == null)
  42. throw new ArgumentNullException ("bits");
  43. _length = bits._length;
  44. _array = new int [(_length + 31) / 32];
  45. Array.Copy(bits._array, _array, _array.Length);
  46. }
  47. public BitArray (bool [] values)
  48. {
  49. if (values == null)
  50. throw new ArgumentNullException ("values");
  51. _length = values.Length;
  52. _array = new int [(_length + 31) / 32];
  53. for (int i = 0; i < values.Length; i++)
  54. this [i] = values [i];
  55. }
  56. public BitArray (byte [] bytes)
  57. {
  58. if (bytes == null)
  59. throw new ArgumentNullException ("bytes");
  60. _length = bytes.Length * 8;
  61. _array = new int [(_length + 31) / 32];
  62. for (int i = 0; i < bytes.Length; i++)
  63. setByte (i, bytes [i]);
  64. }
  65. public BitArray (int [] values)
  66. {
  67. if (values == null)
  68. throw new ArgumentNullException ("values");
  69. int arrlen = values.Length;
  70. _length = arrlen*32;
  71. _array = new int [arrlen];
  72. Array.Copy (values, _array, arrlen);
  73. }
  74. public BitArray (int length)
  75. {
  76. if (length < 0)
  77. throw new ArgumentOutOfRangeException ("length");
  78. _length = length;
  79. _array = new int [(_length + 31) / 32];
  80. }
  81. public BitArray (int length, bool defaultValue) : this (length)
  82. {
  83. if (defaultValue) {
  84. for (int i = 0; i < _array.Length; i++)
  85. _array[i] = ~0;
  86. }
  87. }
  88. private BitArray (int [] array, int length)
  89. {
  90. _array = array;
  91. _length = length;
  92. }
  93. #endregion
  94. #region Utility Methods
  95. byte getByte (int byteIndex)
  96. {
  97. int index = byteIndex / 4;
  98. int shift = (byteIndex % 4) * 8;
  99. int theByte = _array [index] & (0xff << shift);
  100. return (byte)((theByte >> shift) & 0xff);
  101. }
  102. void setByte (int byteIndex, byte value)
  103. {
  104. int index = byteIndex / 4;
  105. int shift = (byteIndex % 4) * 8;
  106. // clear the byte
  107. _array [index] &= ~(0xff << shift);
  108. // or in the new byte
  109. _array [index] |= value << shift;
  110. _version++;
  111. }
  112. void checkOperand (BitArray operand)
  113. {
  114. if (operand == null)
  115. throw new ArgumentNullException ();
  116. if (operand._length != _length)
  117. throw new ArgumentException ();
  118. }
  119. #endregion
  120. public int Count {
  121. get { return _length; }
  122. }
  123. public bool IsReadOnly {
  124. get { return false; }
  125. }
  126. public bool IsSynchronized {
  127. get { return false; }
  128. }
  129. public bool this [int index] {
  130. get { return Get (index); }
  131. set { Set (index, value); }
  132. }
  133. public int Length {
  134. get { return _length; }
  135. set {
  136. if (value < 0)
  137. throw new ArgumentOutOfRangeException ();
  138. int newLen = value;
  139. if (_length != newLen) {
  140. int numints = (newLen + 31) / 32;
  141. int [] newArr = new int [numints];
  142. int copylen = (numints > _array.Length) ? _array.Length : numints;
  143. Array.Copy (_array, newArr, copylen);
  144. // set the internal state
  145. _array = newArr;
  146. _length = newLen;
  147. _version++;
  148. }
  149. }
  150. }
  151. public object SyncRoot {
  152. get { return this; }
  153. }
  154. public object Clone ()
  155. {
  156. // LAMESPEC: docs say shallow, MS makes deep.
  157. return new BitArray (this);
  158. }
  159. public void CopyTo (Array array, int index)
  160. {
  161. if (array == null)
  162. throw new ArgumentNullException ("array");
  163. if (index < 0)
  164. throw new ArgumentOutOfRangeException ("index");
  165. if (array.Rank != 1)
  166. throw new ArgumentException ("array", "Array rank must be 1");
  167. if (index >= array.Length)
  168. throw new ArgumentException ("index", "index is greater than array.Length");
  169. // in each case, check to make sure enough space in array
  170. if (array is bool []) {
  171. if (array.Length - index < _length)
  172. throw new ArgumentException ();
  173. bool [] barray = (bool []) array;
  174. // Copy the bits into the array
  175. for (int i = 0; i < _length; i++)
  176. barray[index + i] = this [i];
  177. } else if (array is byte []) {
  178. int numbytes = (_length + 7) / 8;
  179. if ((array.Length - index) < numbytes)
  180. throw new ArgumentException ();
  181. byte [] barray = (byte []) array;
  182. // Copy the bytes into the array
  183. for (int i = 0; i < numbytes; i++)
  184. barray [index + i] = getByte (i);
  185. } else if (array is int []) {
  186. Array.Copy (_array, 0, array, index, (_length + 31) / 32);
  187. } else {
  188. throw new ArgumentException ("array", "Unsupported type");
  189. }
  190. }
  191. public BitArray Not ()
  192. {
  193. int ints = (_length + 31) / 32;
  194. for (int i = 0; i < ints; i++)
  195. _array [i] = ~_array [i];
  196. _version++;
  197. return this;
  198. }
  199. public BitArray And (BitArray value)
  200. {
  201. checkOperand (value);
  202. int ints = (_length + 31) / 32;
  203. for (int i = 0; i < ints; i++)
  204. _array [i] &= value._array [i];
  205. _version++;
  206. return this;
  207. }
  208. public BitArray Or (BitArray value)
  209. {
  210. checkOperand (value);
  211. int ints = (_length + 31) / 32;
  212. for (int i = 0; i < ints; i++)
  213. _array [i] |= value._array [i];
  214. _version++;
  215. return this;
  216. }
  217. public BitArray Xor (BitArray value)
  218. {
  219. checkOperand (value);
  220. int ints = (_length + 31) / 32;
  221. for (int i = 0; i < ints; i++)
  222. _array [i] ^= value._array [i];
  223. _version++;
  224. return this;
  225. }
  226. public bool Get (int index)
  227. {
  228. if (index < 0 || index >= _length)
  229. throw new ArgumentOutOfRangeException ();
  230. return (_array [index / 32] & (1 << (index % 32))) != 0;
  231. }
  232. public void Set (int index, bool value)
  233. {
  234. if (index < 0 || index >= _length)
  235. throw new ArgumentOutOfRangeException ();
  236. if (value)
  237. _array [index / 32] |= (1 << (index % 32));
  238. else
  239. _array [index / 32] &= ~(1 << (index % 32));
  240. _version++;
  241. }
  242. public void SetAll (bool value)
  243. {
  244. if (value) {
  245. for (int i = 0; i < _array.Length; i++)
  246. _array[i] = ~0;
  247. }
  248. else
  249. Array.Clear (_array, 0, _array.Length);
  250. _version++;
  251. }
  252. public IEnumerator GetEnumerator ()
  253. {
  254. return new BitArrayEnumerator (this);
  255. }
  256. [Serializable]
  257. class BitArrayEnumerator : IEnumerator, ICloneable {
  258. BitArray _bitArray;
  259. bool _current;
  260. int _index, _max, _version;
  261. public object Clone () {
  262. return MemberwiseClone ();
  263. }
  264. public BitArrayEnumerator (BitArray ba)
  265. {
  266. _index = -1;
  267. _bitArray = ba;
  268. _max = ba._length;
  269. _version = ba._version;
  270. }
  271. public object Current {
  272. get {
  273. if (_index == -1)
  274. throw new InvalidOperationException ("Enum not started");
  275. if (_index >= _bitArray.Count)
  276. throw new InvalidOperationException ("Enum Ended");
  277. return _current;
  278. }
  279. }
  280. public bool MoveNext ()
  281. {
  282. checkVersion ();
  283. if (_index < (_bitArray.Count - 1)) {
  284. _current = _bitArray [++_index];
  285. return true;
  286. }
  287. else
  288. _index = _bitArray.Count;
  289. return false;
  290. }
  291. public void Reset ()
  292. {
  293. checkVersion ();
  294. _index = -1;
  295. }
  296. void checkVersion ()
  297. {
  298. if (_version != _bitArray._version)
  299. throw new InvalidOperationException ();
  300. }
  301. }
  302. }
  303. }