示例#1
0
    public void Insert(int index, T item)
    {
        if (index < 0 || index > Count)
        {
            throw new ArgumentOutOfRangeException();
        }
        if (this.IsReadOnly)
        {
            throw new NotSupportedException("Deque is readonly.");
        }

        if (index == 0)
        {
            AddBack(item);
            return;
        }

        if (index == Count)
        {
            AddFront(item);
            return;
        }

        int          totalIndex   = GetTotalIndex(index);
        HeadPosition headPosition = new HeadPosition()
        {
            MapPosition     = totalIndex / DATABLOCK_LENGTH,
            DataBlockOffset = (totalIndex % DATABLOCK_LENGTH)
        };

        if (headPosition - RearHead < FrontHead - headPosition)
        {
            T last = QueueMap[headPosition.MapPosition][headPosition.DataBlockOffset];
            QueueMap[headPosition.MapPosition][headPosition.DataBlockOffset] = item;
            for (var head = headPosition.IncNew();
                 head != FrontHead;
                 head.Inc())
            {
                T temp = QueueMap[head.MapPosition][head.DataBlockOffset];
                QueueMap[head.MapPosition][head.DataBlockOffset] = last;
                last = temp;
            }
            AddFront(last);
        }
        else
        {
            headPosition.Dec();
            T last = QueueMap[headPosition.MapPosition][headPosition.DataBlockOffset];
            QueueMap[headPosition.MapPosition][headPosition.DataBlockOffset] = item;
            for (var head = headPosition.DecNew();
                 head != RearHead;
                 head.Dec())
            {
                T temp = QueueMap[head.MapPosition][head.DataBlockOffset];
                QueueMap[head.MapPosition][head.DataBlockOffset] = last;
                last = temp;
            }
            AddBack(last);
        }
    }
示例#2
0
    public void RemoveAt(int index)
    {
        if (index < 0 || index >= Count)
        {
            throw new ArgumentOutOfRangeException();
        }

        if (index == 0)
        {
            GetBack();
            return;
        }

        if (index == Count - 1)
        {
            GetFront();
            return;
        }

        int          totalIndex   = GetTotalIndex(index);
        HeadPosition headPosition = new HeadPosition()
        {
            MapPosition     = totalIndex / DATABLOCK_LENGTH,
            DataBlockOffset = (totalIndex % DATABLOCK_LENGTH)
        };

        if (headPosition - RearHead < FrontHead - headPosition)
        {
            for (var head = headPosition.IncNew();
                 head != FrontHead;
                 head.Inc())
            {
                QueueMap[headPosition.MapPosition][headPosition.DataBlockOffset] = QueueMap[head.MapPosition][head.DataBlockOffset];
                headPosition.Inc();
            }
            GetFront();
        }
        else
        {
            for (var head = headPosition.DecNew();
                 head != RearHead;
                 head.Dec())
            {
                QueueMap[headPosition.MapPosition][headPosition.DataBlockOffset] = QueueMap[head.MapPosition][head.DataBlockOffset];
                headPosition.Dec();
            }
            GetBack();
        }
    }