コード例 #1
0
        /// <summary>
        /// Inserts an element into the <see cref="BigArray{T}"/> at the specified index.
        /// </summary>
        /// <param name="index">Index of <see cref="BigArray{T}"/> where the value will be.</param>
        /// <param name="item">The data to be placed.</param>
        public void Insert(int index, T item)
        {
            if (index == Count)
            {
                Add(item);
                return;
            }

            var blockInfo = _arrayMap.BlockInfo(index);

            int blockSubindex = index - blockInfo.CommonStartIndex;
            int indexOfBlock  = blockInfo.IndexOfBlock;
            var block         = _blockCollection[blockInfo.IndexOfBlock];

            bool isMaxSize = (block.Count == _balancer.GetMaxBlockSize(indexOfBlock));
            bool isNeedToAddPreviosBlock = (blockSubindex == 0 && blockInfo.Count >= _balancer.GetDefaultBlockSize(indexOfBlock));

            if (isMaxSize)
            {
                _blockCollection.TryToDivideBlock(blockInfo.IndexOfBlock);
                _arrayMap.DataChanged(blockInfo.IndexOfBlock);
                Insert(index, item);
                return;
            }

            //Insertion
            if (!isNeedToAddPreviosBlock)
            {
                _blockCollection[blockInfo.IndexOfBlock].Insert(blockSubindex, item);
                _blockCollection.TryToDivideBlock(blockInfo.IndexOfBlock);
                _arrayMap.DataChanged(blockInfo.IndexOfBlock);
            }
            //Try to add to the previous block
            else
            {
                //If there is need - add new block
                bool isStartBlock    = (blockInfo.IndexOfBlock == 0);
                bool isPrevBlockFull = false;

                if (!isStartBlock)
                {
                    isPrevBlockFull = (_blockCollection[blockInfo.IndexOfBlock].Count == _balancer.GetMaxBlockSize(indexOfBlock));
                }

                if (isStartBlock || isPrevBlockFull)
                {
                    _blockCollection.InsertNewBlock(blockInfo.IndexOfBlock);

                    blockInfo.IndexOfBlock++;
                }

                _blockCollection[blockInfo.IndexOfBlock - 1].Add(item);
                _arrayMap.DataChanged(blockInfo.IndexOfBlock - 1);
            }

            Count++;
        }
コード例 #2
0
        public static void AddNewBlockAndInsertNewBlock()
        {
            var blockCollection = new BlockCollection <int>();

            //Add
            blockCollection.AddNewBlock();
            Assert.AreEqual(blockCollection.Count, 1);
            //Add
            blockCollection.AddNewBlock();
            Assert.AreEqual(blockCollection.Count, 2);
            //Insert
            blockCollection.InsertNewBlock(1);
            Assert.AreEqual(blockCollection.Count, 3);
        }
コード例 #3
0
        public static void AddNewBlockAndInsertNewBlock()
        {
            var blockCollection = new BlockCollection<int>();

            //Add
            blockCollection.AddNewBlock();
            Assert.AreEqual(blockCollection.Count, 1);
            //Add
            blockCollection.AddNewBlock();
            Assert.AreEqual(blockCollection.Count, 2);
            //Insert
            blockCollection.InsertNewBlock(1);
            Assert.AreEqual(blockCollection.Count, 3);
        }