BitArray.cs 8.3 KB

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