Beispiel #1
0
        /// <summary>
        /// This methods allocates a mutable array and copies all the colletion elements to it
        /// </summary>
        /// <param name="collection">Collection to copy elements from</param>
        /// <returns>Array</returns>
        private static T[] AllocInternalArray(ICollection <T> collection)
        {
            T[] ret = ImmutableArrayInternal <T> .AllocInternalArray(collection.Count);

            collection.CopyTo(ret, 0);
            return(ret);
        }
Beispiel #2
0
        protected bool EqualsTo(ImmutableArrayInternal <T> array)
        {
            if (this.Length != array.Length)
            {
                return(false);
            }

            //fast compare
            if (this.GetHashCode() != array.GetHashCode())
            {
                return(false);
            }

            //slow compare
            int sourceIndex = this._offset, destIndex = array._offset;

            for (int i = 0; i < this.Length; i++)
            {
                if (!this._data[sourceIndex++].Equals(array._data[destIndex++]))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #3
0
        /// <summary>
        /// Gets a portion of the array starting from the specified index for the specified length
        /// </summary>
        /// <param name="sourceIndex">Index to start from</param>
        /// <param name="length">Length of the portion</param>
        /// <returns>An immutable array that is a portion of this array</returns>
        public ImmutableArray <T> SubArray(int sourceIndex, int length)
        {
            if ((this.Count == sourceIndex) && (length == 0))
            {
                return(Empty);
            }

            List <ImmutableArrayInternal <T> > ret = new List <ImmutableArrayInternal <T> >();
            int i = this.GetArrayIndexAt(ref sourceIndex);

            if ((sourceIndex + length) > this.Count)
            {
                throw new ArgumentOutOfRangeException("The sourceIndex and length specified overcome the array length");
            }

            ImmutableArrayInternal <T> array = _arrays[i];

            array   = new ImmutableArrayInternal <T>(sourceIndex, array, array.Length < length ? array.Length : length);
            length -= array.Length;

            ret.Add(array);
            for (i += 1; (length > 0) && (i < _arrays.Count); i++)
            {
                array = _arrays[i];
                if (array.Length > length)                 //if only partial create a subarray
                {
                    array = array.SubArrayInternal(0, length);
                }

                ret.Add(array);
                length -= array.Length;
            }

            return(new ImmutableArray <T>(ret.ToArray()));
        }
Beispiel #4
0
        private void Add(ImmutableArrayInternal <T> array)
        {
            if (array.Length <= 0)
            {
                return;
            }

            _arrays.Add(array);
            _offsets.Add(this.Count + array.Length);
            _length += array.Length;
        }
Beispiel #5
0
        /// <summary>
        /// This method allocates a copy of the specified array
        /// </summary>
        /// <param name="sourceIndex">Index to start the copy</param>
        /// <param name="data">Array of data to be copied</param>
        /// <param name="length">Length od data to topy</param>
        /// <returns>A partial/complete clone of the specified array</returns>
        private static T[] AllocInternalArray(int sourceIndex, T[] data, int length)
        {
            if (length == 0)
            {
                return(EmptyArray);
            }

            T[] ret = ImmutableArrayInternal <T> .AllocInternalArray(length);

            Array.Copy(data, sourceIndex, ret, sourceIndex, length);
            return(ret);
        }
Beispiel #6
0
        public ImmutableArrayInternal(int sourceIndex, ImmutableArrayInternal <T> data, int length)
        {
            if ((sourceIndex < 0) || (sourceIndex >= data.Length))
            {
                throw new IndexOutOfRangeException("Index must be between 0 and upper array bound");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data", "Array cannot be null");
            }

            if ((sourceIndex + length) > data.Length)
            {
                throw new ArgumentOutOfRangeException("The sourceIndex and length specified overcome the source ByteArray length");
            }

            _offset = data.Offset + sourceIndex;
            _data   = data.Data;
            _length = length;
        }
Beispiel #7
0
        public ImmutableArrayInternal(int sourceIndex, T[] data, int length)
        {
            if (sourceIndex < 0)
            {
                throw new IndexOutOfRangeException("Index cannot be less than zero");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data", "Array cannot be null");
            }

            if ((sourceIndex + length) > data.Length)
            {
                throw new ArgumentOutOfRangeException("The sourceIndex and length specified overcome the source ByteArray length");
            }

            _offset = 0;
            _data   = ImmutableArrayInternal <T> .AllocInternalArray(sourceIndex, data, length);

            _length = length;
        }
Beispiel #8
0
 public ImmutableArrayInternal(ImmutableArrayInternal <T> data, int length)
     : this(0, data, length)
 {
 }
Beispiel #9
0
 public ImmutableArrayInternal(ImmutableArrayInternal <T> data)
     : this(data, data.Length)
 {
 }