GetArrayLength() private static method

Used for conversion between different representations of bit array. Returns (n+(div-1))/div, rearranged to avoid arithmetic overflow. For example, in the bit to int case, the straightforward calc would be (n+31)/32, but that would cause overflow. So instead it's rearranged to ((n-1)/32) + 1, with special casing for 0. Usage: GetArrayLength(77, BitsPerInt32): returns how many ints must be allocated to store 77 bits.
private static GetArrayLength ( int n, int div ) : int
n int
div int use a conversion constant, e.g. BytesPerInt32 to get /// how many ints are required to store n bytes
return int
Ejemplo n.º 1
0
        public BitArray Not()
        {
            int arrayLength = BitArray.GetArrayLength(this.m_length, 32);

            for (int i = 0; i < arrayLength; i++)
            {
                this.m_array[i] = ~this.m_array[i];
            }
            this._version++;
            return(this);
        }
Ejemplo n.º 2
0
        public void SetAll(bool value)
        {
            int num         = value ? -1 : 0;
            int arrayLength = BitArray.GetArrayLength(this.m_length, 32);

            for (int i = 0; i < arrayLength; i++)
            {
                this.m_array[i] = num;
            }
            this._version++;
        }
Ejemplo n.º 3
0
        public BitArray Not()
        {
            int arrayLength = BitArray.GetArrayLength(this.m_length, 32);

            for (int index = 0; index < arrayLength; ++index)
            {
                this.m_array[index] = ~this.m_array[index];
            }
            this._version = this._version + 1;
            return(this);
        }
Ejemplo n.º 4
0
        public void SetAll(bool value)
        {
            int num         = value ? -1 : 0;
            int arrayLength = BitArray.GetArrayLength(this.m_length, 32);

            for (int index = 0; index < arrayLength; ++index)
            {
                this.m_array[index] = num;
            }
            this._version = this._version + 1;
        }
Ejemplo n.º 5
0
        public BitArray(BitArray bits)
        {
            if (bits == null)
            {
                throw new ArgumentNullException("bits");
            }
            int arrayLength = BitArray.GetArrayLength(bits.m_length, 32);

            this.m_array  = new int[arrayLength];
            this.m_length = bits.m_length;
            Array.Copy(bits.m_array, this.m_array, arrayLength);
            this._version = bits._version;
        }
Ejemplo n.º 6
0
 /// <summary>Copies the entire <see cref="T:System.Collections.BitArray" /> to a compatible one-dimensional <see cref="T:System.Array" />, starting at the specified index of the target array.</summary>
 /// <param name="array">The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.BitArray" />. The <see cref="T:System.Array" /> must have zero-based indexing. </param>
 /// <param name="index">The zero-based index in <paramref name="array" /> at which copying begins. </param>
 /// <exception cref="T:System.ArgumentNullException">
 ///         <paramref name="array" /> is <see langword="null" />. </exception>
 /// <exception cref="T:System.ArgumentOutOfRangeException">
 ///         <paramref name="index" /> is less than zero. </exception>
 /// <exception cref="T:System.ArgumentException">
 ///         <paramref name="array" /> is multidimensional.-or- The number of elements in the source <see cref="T:System.Collections.BitArray" /> is greater than the available space from <paramref name="index" /> to the end of the destination <paramref name="array" />. </exception>
 /// <exception cref="T:System.InvalidCastException">The type of the source <see cref="T:System.Collections.BitArray" /> cannot be cast automatically to the type of the destination <paramref name="array" />. </exception>
 // Token: 0x060036FA RID: 14074 RVA: 0x000D2C90 File Offset: 0x000D0E90
 public void CopyTo(Array array, int index)
 {
     if (array == null)
     {
         throw new ArgumentNullException("array");
     }
     if (index < 0)
     {
         throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
     }
     if (array.Rank != 1)
     {
         throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
     }
     if (array is int[])
     {
         Array.Copy(this.m_array, 0, array, index, BitArray.GetArrayLength(this.m_length, 32));
         return;
     }
     if (array is byte[])
     {
         int arrayLength = BitArray.GetArrayLength(this.m_length, 8);
         if (array.Length - index < arrayLength)
         {
             throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
         }
         byte[] array2 = (byte[])array;
         for (int i = 0; i < arrayLength; i++)
         {
             array2[index + i] = (byte)(this.m_array[i / 4] >> i % 4 * 8 & 255);
         }
         return;
     }
     else
     {
         if (!(array is bool[]))
         {
             throw new ArgumentException(Environment.GetResourceString("Arg_BitArrayTypeUnsupported"));
         }
         if (array.Length - index < this.m_length)
         {
             throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
         }
         bool[] array3 = (bool[])array;
         for (int j = 0; j < this.m_length; j++)
         {
             array3[index + j] = ((this.m_array[j / 32] >> j % 32 & 1) != 0);
         }
         return;
     }
 }
Ejemplo n.º 7
0
 /// <summary>从目标数组的指定索引处开始将整个 <see cref="T:System.Collections.BitArray" /> 复制到兼容的一维 <see cref="T:System.Array" />。</summary>
 /// <param name="array">一维 <see cref="T:System.Array" />,它是从 <see cref="T:System.Collections.BitArray" /> 复制的元素的目标。<see cref="T:System.Array" /> 必须具有从零开始的索引。</param>
 /// <param name="index">
 /// <paramref name="array" /> 中从零开始的索引,从此索引处开始进行复制。</param>
 /// <exception cref="T:System.ArgumentNullException">
 /// <paramref name="array" /> is null. </exception>
 /// <exception cref="T:System.ArgumentOutOfRangeException">
 /// <paramref name="index" /> is less than zero. </exception>
 /// <exception cref="T:System.ArgumentException">
 /// <paramref name="array" /> is multidimensional.-or- The number of elements in the source <see cref="T:System.Collections.BitArray" /> is greater than the available space from <paramref name="index" /> to the end of the destination <paramref name="array" />. </exception>
 /// <exception cref="T:System.InvalidCastException">The type of the source <see cref="T:System.Collections.BitArray" /> cannot be cast automatically to the type of the destination <paramref name="array" />. </exception>
 /// <filterpriority>2</filterpriority>
 public void CopyTo(Array array, int index)
 {
     if (array == null)
     {
         throw new ArgumentNullException("array");
     }
     if (index < 0)
     {
         throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
     }
     if (array.Rank != 1)
     {
         throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
     }
     if (array is int[])
     {
         Array.Copy((Array)this.m_array, 0, array, index, BitArray.GetArrayLength(this.m_length, 32));
     }
     else if (array is byte[])
     {
         int arrayLength = BitArray.GetArrayLength(this.m_length, 8);
         if (array.Length - index < arrayLength)
         {
             throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
         }
         byte[] numArray = (byte[])array;
         for (int index1 = 0; index1 < arrayLength; ++index1)
         {
             numArray[index + index1] = (byte)(this.m_array[index1 / 4] >> index1 % 4 * 8 & (int)byte.MaxValue);
         }
     }
     else
     {
         if (!(array is bool[]))
         {
             throw new ArgumentException(Environment.GetResourceString("Arg_BitArrayTypeUnsupported"));
         }
         if (array.Length - index < this.m_length)
         {
             throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
         }
         bool[] flagArray = (bool[])array;
         for (int index1 = 0; index1 < this.m_length; ++index1)
         {
             flagArray[index + index1] = (uint)(this.m_array[index1 / 32] >> index1 % 32 & 1) > 0U;
         }
     }
 }
Ejemplo n.º 8
0
        public BitArray(int length, bool defaultValue)
        {
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            this.m_array  = new int[BitArray.GetArrayLength(length, 32)];
            this.m_length = length;
            int num = defaultValue ? -1 : 0;

            for (int i = 0; i < this.m_array.Length; i++)
            {
                this.m_array[i] = num;
            }
            this._version = 0;
        }
Ejemplo n.º 9
0
 public BitArray(bool[] values)
 {
     if (values == null)
     {
         throw new ArgumentNullException("values");
     }
     this.m_array  = new int[BitArray.GetArrayLength(values.Length, 32)];
     this.m_length = values.Length;
     for (int i = 0; i < values.Length; i++)
     {
         if (values[i])
         {
             this.m_array[i / 32] |= 1 << i % 32;
         }
     }
     this._version = 0;
 }
Ejemplo n.º 10
0
        public BitArray(byte[] bytes)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }
            if (bytes.Length > 268435455)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_ArrayTooLarge", new object[]
                {
                    8
                }), "bytes");
            }
            this.m_array  = new int[BitArray.GetArrayLength(bytes.Length, 4)];
            this.m_length = bytes.Length * 8;
            int num  = 0;
            int num2 = 0;

            while (bytes.Length - num2 >= 4)
            {
                this.m_array[num++] = ((int)(bytes[num2] & byte.MaxValue) | (int)(bytes[num2 + 1] & byte.MaxValue) << 8 | (int)(bytes[num2 + 2] & byte.MaxValue) << 16 | (int)(bytes[num2 + 3] & byte.MaxValue) << 24);
                num2 += 4;
            }
            switch (bytes.Length - num2)
            {
            case 1:
                goto IL_103;

            case 2:
                break;

            case 3:
                this.m_array[num] = (int)(bytes[num2 + 2] & byte.MaxValue) << 16;
                break;

            default:
                goto IL_11C;
            }
            this.m_array[num] |= (int)(bytes[num2 + 1] & byte.MaxValue) << 8;
IL_103:
            this.m_array[num] |= (int)(bytes[num2] & byte.MaxValue);
IL_11C:
            this._version = 0;
        }
Ejemplo n.º 11
0
        public BitArray Xor(BitArray value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (this.Length != value.Length)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer"));
            }
            int arrayLength = BitArray.GetArrayLength(this.m_length, 32);

            for (int i = 0; i < arrayLength; i++)
            {
                this.m_array[i] ^= value.m_array[i];
            }
            this._version++;
            return(this);
        }
Ejemplo n.º 12
0
        public BitArray Or(BitArray value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (this.Length != value.Length)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_ArrayLengthsDiffer"));
            }
            int arrayLength = BitArray.GetArrayLength(this.m_length, 32);

            for (int index = 0; index < arrayLength; ++index)
            {
                this.m_array[index] |= value.m_array[index];
            }
            this._version = this._version + 1;
            return(this);
        }
Ejemplo n.º 13
0
        public BitArray(byte[] bytes)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }
            if (bytes.Length > 268435455)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_ArrayTooLarge", (object)8), "bytes");
            }
            this.m_array  = new int[BitArray.GetArrayLength(bytes.Length, 4)];
            this.m_length = bytes.Length * 8;
            int index1 = 0;
            int index2 = 0;

            while (bytes.Length - index2 >= 4)
            {
                this.m_array[index1++] = (int)bytes[index2] & (int)byte.MaxValue | ((int)bytes[index2 + 1] & (int)byte.MaxValue) << 8 | ((int)bytes[index2 + 2] & (int)byte.MaxValue) << 16 | ((int)bytes[index2 + 3] & (int)byte.MaxValue) << 24;
                index2 += 4;
            }
            switch (bytes.Length - index2)
            {
            case 1:
                this.m_array[index1] |= (int)bytes[index2] & (int)byte.MaxValue;
                break;

            case 2:
                this.m_array[index1] |= ((int)bytes[index2 + 1] & (int)byte.MaxValue) << 8;
                goto case 1;

            case 3:
                this.m_array[index1] = ((int)bytes[index2 + 2] & (int)byte.MaxValue) << 16;
                goto case 2;
            }
            this._version = 0;
        }