mscorlib(4.0.0.0) API with additions
BitArray.cs
2 using System.Threading;
3 
4 namespace System.Collections
5 {
8  [ComVisible(true)]
9  [__DynamicallyInvokable]
10  public sealed class BitArray : ICollection, IEnumerable, ICloneable
11  {
12  [Serializable]
13  private class BitArrayEnumeratorSimple : IEnumerator, ICloneable
14  {
15  private BitArray bitarray;
16 
17  private int index;
18 
19  private int version;
20 
21  private bool currentElement;
22 
23  public virtual object Current
24  {
25  get
26  {
27  if (index == -1)
28  {
29  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted"));
30  }
31  if (index >= bitarray.Count)
32  {
33  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded"));
34  }
35  return currentElement;
36  }
37  }
38 
39  internal BitArrayEnumeratorSimple(BitArray bitarray)
40  {
41  this.bitarray = bitarray;
42  index = -1;
43  version = bitarray._version;
44  }
45 
46  public object Clone()
47  {
48  return MemberwiseClone();
49  }
50 
51  public virtual bool MoveNext()
52  {
53  if (version != bitarray._version)
54  {
55  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion"));
56  }
57  if (index < bitarray.Count - 1)
58  {
59  index++;
60  currentElement = bitarray.Get(index);
61  return true;
62  }
63  index = bitarray.Count;
64  return false;
65  }
66 
67  public void Reset()
68  {
69  if (version != bitarray._version)
70  {
71  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion"));
72  }
73  index = -1;
74  }
75  }
76 
77  private const int BitsPerInt32 = 32;
78 
79  private const int BytesPerInt32 = 4;
80 
81  private const int BitsPerByte = 8;
82 
83  private int[] m_array;
84 
85  private int m_length;
86 
87  private int _version;
88 
89  [NonSerialized]
90  private object _syncRoot;
91 
92  private const int _ShrinkThreshold = 256;
93 
100  [__DynamicallyInvokable]
101  public bool this[int index]
102  {
103  [__DynamicallyInvokable]
104  get
105  {
106  return Get(index);
107  }
108  [__DynamicallyInvokable]
109  set
110  {
111  Set(index, value);
112  }
113  }
114 
118  [__DynamicallyInvokable]
119  public int Length
120  {
121  [__DynamicallyInvokable]
122  get
123  {
124  return m_length;
125  }
126  [__DynamicallyInvokable]
127  set
128  {
129  if (value < 0)
130  {
131  throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
132  }
133  int arrayLength = GetArrayLength(value, 32);
134  if (arrayLength > m_array.Length || arrayLength + 256 < m_array.Length)
135  {
136  int[] array = new int[arrayLength];
137  Array.Copy(m_array, array, (arrayLength > m_array.Length) ? m_array.Length : arrayLength);
138  m_array = array;
139  }
140  if (value > m_length)
141  {
142  int num = GetArrayLength(m_length, 32) - 1;
143  int num2 = m_length % 32;
144  if (num2 > 0)
145  {
146  m_array[num] &= (1 << num2) - 1;
147  }
148  Array.Clear(m_array, num + 1, arrayLength - num - 1);
149  }
150  m_length = value;
151  _version++;
152  }
153  }
154 
157  public int Count => m_length;
158 
161  public object SyncRoot
162  {
163  get
164  {
165  if (_syncRoot == null)
166  {
167  Interlocked.CompareExchange<object>(ref _syncRoot, new object(), (object)null);
168  }
169  return _syncRoot;
170  }
171  }
172 
175  public bool IsReadOnly => false;
176 
179  public bool IsSynchronized => false;
180 
181  private BitArray()
182  {
183  }
184 
189  [__DynamicallyInvokable]
190  public BitArray(int length)
191  : this(length, defaultValue: false)
192  {
193  }
194 
200  [__DynamicallyInvokable]
201  public BitArray(int length, bool defaultValue)
202  {
203  if (length < 0)
204  {
205  throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
206  }
207  m_array = new int[GetArrayLength(length, 32)];
208  m_length = length;
209  int num = defaultValue ? (-1) : 0;
210  for (int i = 0; i < m_array.Length; i++)
211  {
212  m_array[i] = num;
213  }
214  _version = 0;
215  }
216 
222  [__DynamicallyInvokable]
223  public BitArray(byte[] bytes)
224  {
225  if (bytes == null)
226  {
227  throw new ArgumentNullException("bytes");
228  }
229  if (bytes.Length > 268435455)
230  {
231  throw new ArgumentException(Environment.GetResourceString("Argument_ArrayTooLarge", 8), "bytes");
232  }
233  m_array = new int[GetArrayLength(bytes.Length, 4)];
234  m_length = bytes.Length * 8;
235  int num = 0;
236  int i;
237  for (i = 0; bytes.Length - i >= 4; i += 4)
238  {
239  m_array[num++] = ((bytes[i] & 0xFF) | ((bytes[i + 1] & 0xFF) << 8) | ((bytes[i + 2] & 0xFF) << 16) | ((bytes[i + 3] & 0xFF) << 24));
240  }
241  switch (bytes.Length - i)
242  {
243  case 3:
244  m_array[num] = (bytes[i + 2] & 0xFF) << 16;
245  goto case 2;
246  case 2:
247  m_array[num] |= (bytes[i + 1] & 0xFF) << 8;
248  goto case 1;
249  case 1:
250  m_array[num] |= (bytes[i] & 0xFF);
251  break;
252  }
253  _version = 0;
254  }
255 
260  [__DynamicallyInvokable]
261  public BitArray(bool[] values)
262  {
263  if (values == null)
264  {
265  throw new ArgumentNullException("values");
266  }
267  m_array = new int[GetArrayLength(values.Length, 32)];
268  m_length = values.Length;
269  for (int i = 0; i < values.Length; i++)
270  {
271  if (values[i])
272  {
273  m_array[i / 32] |= 1 << i % 32;
274  }
275  }
276  _version = 0;
277  }
278 
284  [__DynamicallyInvokable]
285  public BitArray(int[] values)
286  {
287  if (values == null)
288  {
289  throw new ArgumentNullException("values");
290  }
291  if (values.Length > 67108863)
292  {
293  throw new ArgumentException(Environment.GetResourceString("Argument_ArrayTooLarge", 32), "values");
294  }
295  m_array = new int[values.Length];
296  m_length = values.Length * 32;
297  Array.Copy(values, m_array, values.Length);
298  _version = 0;
299  }
300 
305  [__DynamicallyInvokable]
306  public BitArray(BitArray bits)
307  {
308  if (bits == null)
309  {
310  throw new ArgumentNullException("bits");
311  }
312  int arrayLength = GetArrayLength(bits.m_length, 32);
313  m_array = new int[arrayLength];
314  m_length = bits.m_length;
315  Array.Copy(bits.m_array, m_array, arrayLength);
316  _version = bits._version;
317  }
318 
325  [__DynamicallyInvokable]
326  public bool Get(int index)
327  {
328  if (index < 0 || index >= Length)
329  {
330  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
331  }
332  return (m_array[index / 32] & (1 << index % 32)) != 0;
333  }
334 
341  [__DynamicallyInvokable]
342  public void Set(int index, bool value)
343  {
344  if (index < 0 || index >= Length)
345  {
346  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
347  }
348  if (value)
349  {
350  m_array[index / 32] |= 1 << index % 32;
351  }
352  else
353  {
354  m_array[index / 32] &= ~(1 << index % 32);
355  }
356  _version++;
357  }
358 
361  [__DynamicallyInvokable]
362  public void SetAll(bool value)
363  {
364  int num = value ? (-1) : 0;
365  int arrayLength = GetArrayLength(m_length, 32);
366  for (int i = 0; i < arrayLength; i++)
367  {
368  m_array[i] = num;
369  }
370  _version++;
371  }
372 
381  [__DynamicallyInvokable]
382  public BitArray And(BitArray value)
383  {
384  if (value == null)
385  {
386  throw new ArgumentNullException("value");
387  }
388  if (Length != value.Length)
389  {
390  throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer"));
391  }
392  int arrayLength = GetArrayLength(m_length, 32);
393  for (int i = 0; i < arrayLength; i++)
394  {
395  m_array[i] &= value.m_array[i];
396  }
397  _version++;
398  return this;
399  }
400 
409  [__DynamicallyInvokable]
410  public BitArray Or(BitArray value)
411  {
412  if (value == null)
413  {
414  throw new ArgumentNullException("value");
415  }
416  if (Length != value.Length)
417  {
418  throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer"));
419  }
420  int arrayLength = GetArrayLength(m_length, 32);
421  for (int i = 0; i < arrayLength; i++)
422  {
423  m_array[i] |= value.m_array[i];
424  }
425  _version++;
426  return this;
427  }
428 
437  [__DynamicallyInvokable]
438  public BitArray Xor(BitArray value)
439  {
440  if (value == null)
441  {
442  throw new ArgumentNullException("value");
443  }
444  if (Length != value.Length)
445  {
446  throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer"));
447  }
448  int arrayLength = GetArrayLength(m_length, 32);
449  for (int i = 0; i < arrayLength; i++)
450  {
451  m_array[i] ^= value.m_array[i];
452  }
453  _version++;
454  return this;
455  }
456 
459  [__DynamicallyInvokable]
460  public BitArray Not()
461  {
462  int arrayLength = GetArrayLength(m_length, 32);
463  for (int i = 0; i < arrayLength; i++)
464  {
465  m_array[i] = ~m_array[i];
466  }
467  _version++;
468  return this;
469  }
470 
481  public void CopyTo(Array array, int index)
482  {
483  if (array == null)
484  {
485  throw new ArgumentNullException("array");
486  }
487  if (index < 0)
488  {
489  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
490  }
491  if (array.Rank != 1)
492  {
493  throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
494  }
495  if (array is int[])
496  {
497  Array.Copy(m_array, 0, array, index, GetArrayLength(m_length, 32));
498  return;
499  }
500  if (array is byte[])
501  {
502  int arrayLength = GetArrayLength(m_length, 8);
503  if (array.Length - index < arrayLength)
504  {
505  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
506  }
507  byte[] array2 = (byte[])array;
508  for (int i = 0; i < arrayLength; i++)
509  {
510  array2[index + i] = (byte)((m_array[i / 4] >> i % 4 * 8) & 0xFF);
511  }
512  return;
513  }
514  if (array is bool[])
515  {
516  if (array.Length - index < m_length)
517  {
518  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
519  }
520  bool[] array3 = (bool[])array;
521  for (int j = 0; j < m_length; j++)
522  {
523  array3[index + j] = (((m_array[j / 32] >> j % 32) & 1) != 0);
524  }
525  return;
526  }
527  throw new ArgumentException(Environment.GetResourceString("Arg_BitArrayTypeUnsupported"));
528  }
529 
532  public object Clone()
533  {
534  BitArray bitArray = new BitArray(m_array);
535  bitArray._version = _version;
536  bitArray.m_length = m_length;
537  return bitArray;
538  }
539 
542  [__DynamicallyInvokable]
544  {
545  return new BitArrayEnumeratorSimple(this);
546  }
547 
548  private static int GetArrayLength(int n, int div)
549  {
550  if (n <= 0)
551  {
552  return 0;
553  }
554  return (n - 1) / div + 1;
555  }
556  }
557 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
BitArray(int[] values)
Initializes a new instance of the T:System.Collections.BitArray class that contains bit values copied...
Definition: BitArray.cs:285
bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.BitArray is synchronized (thread s...
Definition: BitArray.cs:179
static void Clear(Array array, int index, int length)
Sets a range of elements in an array to the default value of each element type.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
void CopyTo(Array array, int index)
Copies the entire T:System.Collections.BitArray to a compatible one-dimensional T:System....
Definition: BitArray.cs:481
int? Length
Gets or sets the number of elements in the T:System.Collections.BitArray.
Definition: BitArray.cs:120
int Count
Gets the number of elements contained in the T:System.Collections.BitArray.
Definition: BitArray.cs:157
Manages a compact array of bit values, which are represented as Booleans, where true indicates that t...
Definition: BitArray.cs:10
bool IsReadOnly
Gets a value indicating whether the T:System.Collections.BitArray is read-only.
Definition: BitArray.cs:175
object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: BitArray.cs:162
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
BitArray Not()
Inverts all the bit values in the current T:System.Collections.BitArray, so that elements set to true...
Definition: BitArray.cs:460
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
bool Get(int index)
Gets the value of the bit at a specific position in the T:System.Collections.BitArray.
Definition: BitArray.cs:326
IEnumerator GetEnumerator()
Returns an enumerator that iterates through the T:System.Collections.BitArray.
Definition: BitArray.cs:543
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
BitArray And(BitArray value)
Performs the bitwise AND operation between the elements of the current T:System.Collections....
Definition: BitArray.cs:382
BitArray(int length, bool defaultValue)
Initializes a new instance of the T:System.Collections.BitArray class that can hold the specified num...
Definition: BitArray.cs:201
Supports cloning, which creates a new instance of a class with the same value as an existing instance...
Definition: ICloneable.cs:7
void Set(int index, bool value)
Sets the bit at a specific position in the T:System.Collections.BitArray to the specified value.
Definition: BitArray.cs:342
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
BitArray Xor(BitArray value)
Performs the bitwise exclusive OR operation between the elements of the current T:System....
Definition: BitArray.cs:438
BitArray Or(BitArray value)
Performs the bitwise OR operation between the elements of the current T:System.Collections....
Definition: BitArray.cs:410
void SetAll(bool value)
Sets all bits in the T:System.Collections.BitArray to the specified value.
Definition: BitArray.cs:362
The exception that is thrown when one of the arguments provided to a method is not valid.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
BitArray(byte[] bytes)
Initializes a new instance of the T:System.Collections.BitArray class that contains bit values copied...
Definition: BitArray.cs:223
BitArray(BitArray bits)
Initializes a new instance of the T:System.Collections.BitArray class that contains bit values copied...
Definition: BitArray.cs:306
Specifies that the class can be serialized.
The exception that is thrown when a method call is invalid for the object's current state.
BitArray(int length)
Initializes a new instance of the T:System.Collections.BitArray class that can hold the specified num...
Definition: BitArray.cs:190
object Clone()
Creates a shallow copy of the T:System.Collections.BitArray.
Definition: BitArray.cs:532
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
BitArray(bool[] values)
Initializes a new instance of the T:System.Collections.BitArray class that contains bit values copied...
Definition: BitArray.cs:261