Beispiel #1
0
        private void InsertRange(int index, IEnumerable <T> items, int count)
        {
            _array  = InternalList.InsertRangeHelper(index, count, _array, _count);
            _count += count;

            int stop = index + count;

            foreach (var item in items)
            {
                if (index >= stop)
                {
                    InsertRangeSizeMismatch();
                }
                _array[index++] = item;
            }
            if (index < stop)
            {
                InsertRangeSizeMismatch();
            }
        }
Beispiel #2
0
 public void InsertRangeHelper(int index, int spaceNeeded)
 {
     _array  = InternalList.InsertRangeHelper(index, spaceNeeded, _array, _count);
     _count += spaceNeeded;
 }
Beispiel #3
0
        private int DoInsert(ref AListSparseOperation <T> op, int index0, out AListNode <int, T> splitLeft, out AListNode <int, T> splitRight)
        {
            Debug.Assert(_totalCount + op.SourceCount >= _totalCount);             // caller ensures list size does not overflow

            if (op.WriteEmpty)
            {
                // SourceIndex will be 0 because inserting empty space always finishes on the first try.
                Debug.Assert(op.SourceIndex == 0);
                InsertSpace((uint)index0, op.SourceCount);
                op.SourceIndex = op.SourceCount;
                splitLeft      = splitRight = null;
                return(op.SourceCount);
            }

            uint index = (uint)(index0 + op.SourceIndex);
            int  i;

            BinarySearch(index, out i);

            int leftHere = _maxNodeSize - _list.Count;

            if (leftHere == 0)
            {
                SplitLeaf(out splitLeft, out splitRight, i == _list.Count);
                return(0);                // return without inserting anything
            }

            if (op.Source == null)
            {
                // Special case: insert a single item
                Debug.Assert(op.SourceCount == 1);
                _list.AutoRaiseCapacity(1, _maxNodeSize);

                _list.Insert(i, new Entry(index, op.Item));
                AdjustOffsetsStartingAt(i + 1, ref _list, 1);
                _totalCount++;
                op.SourceIndex++;
                splitLeft = splitRight = null;
                return(1);
            }

            splitLeft = splitRight = null;

            if (op.SparseSource != null)
            {
                var source   = op.SparseSource;
                var tempList = new InternalList <Entry>(leftHere);

                int?si;
                T   item;
                for (int prev = op.SourceIndex - 1; ; prev = si.Value)
                {
                    si   = prev;
                    item = source.NextHigherItem(ref si);
                    if (si == null)
                    {
                        break;
                    }
                    tempList.Add(new Entry {
                        Offset = (uint)(index0 + si.Value),
                        Item   = item
                    });
                    if (tempList.Count == leftHere)
                    {
                        break;
                    }
                }
                _list.InsertRangeHelper(i, tempList.Count);
                for (int j = 0; j < tempList.Count; j++)
                {
                    _list[i + j] = tempList[j];
                }

                int newSourceIndex       = si == null ? source.Count : si.Value + 1;
                int virtualItemsInserted = newSourceIndex - op.SourceIndex;
                op.SourceIndex = newSourceIndex;

                AdjustOffsetsStartingAt(i + tempList.Count, ref _list, virtualItemsInserted);
                _totalCount += (uint)virtualItemsInserted;
                return(virtualItemsInserted);
            }
            else
            {
                int c            = op.SourceCount;
                int leftToInsert = c - op.SourceIndex;
                Debug.Assert(leftToInsert > 0);
                int amtToIns = System.Math.Min(leftHere, leftToInsert);
                _list.AutoRaiseCapacity(amtToIns, _maxNodeSize);
                _list.InsertRangeHelper(i, amtToIns);
                for (int j = 0; j < amtToIns; j++)
                {
                    _list[i + j] = new Entry(index + (uint)j, op.Source[op.SourceIndex + j]);
                }
                AdjustOffsetsStartingAt(i + amtToIns, ref _list, amtToIns);
                _totalCount += (uint)amtToIns;

                op.SourceIndex += amtToIns;
                //if (tob != null) tob.AddingItems(slice, this, false);
                return(amtToIns);
            }
        }