예제 #1
0
        /// <summary>
        /// Adds the elements of the specified collection to the end of <see cref="BigArray{T}"/>.
        /// </summary>
        /// <param name="collection">The collection whose elements should be added to the end of the <see cref="BigArray{T}"/>.
        ///  The collection it self can't benull, but it can contain elements that are null, if type T is a reference type. </param>
        public void AddRange(ICollection <T> collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection");
            }

            if (collection.Count == 0)
            {
                return;
            }

            _blockCollection.AddFirstBlockIfThereIsNeeded();
            int lastBlockIndex = _blockCollection.Count - 1;
            var lastBlock      = _blockCollection[lastBlockIndex];

            //Transfer data to the last block while it is possible
            var sizeOfTransferToLastBlock = 0;
            var emptySize = _balancer.GetMaxBlockSize(lastBlockIndex) - lastBlock.Count;

            if (emptySize != 0)
            {
                sizeOfTransferToLastBlock = Math.Min(emptySize, collection.Count);
                var enumerator = collection.GetEnumerator();

                for (int i = 0; i < sizeOfTransferToLastBlock; i++)
                {
                    enumerator.MoveNext();
                    lastBlock.Add(enumerator.Current);
                }
            }

            //Transfer other data as new blocks
            if (sizeOfTransferToLastBlock != collection.Count)
            {
                _blockCollection.Add(collection, sizeOfTransferToLastBlock);
            }

            Count += collection.Count;
            _arrayMap.DataChanged(lastBlockIndex);
        }