Beispiel #1
0
        /// <summary>
        /// 插入元素到头部
        /// <para/>
        /// * 默认为节点没有添加到列表,并且待添加的节点 <see cref="LRUSpecializedLinkedListNode{TValue}.Previous"/> 和 <see cref="LRUSpecializedLinkedListNode{TValue}.Next"/> 都为 null
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public LRUSpecializedLinkedListNode <TValue>?InsertAtHead(LRUSpecializedLinkedListNode <TValue> node)
        {
            if (_count > 1)
            {
                _head !.Previous = node;
                node.Next        = _head;
                _head            = node;

                if (_count >= _capacity)
                {
                    return(RemoveTail());
                }
                _count++;
            }
            else if (_count == 1)
            {
                _count           = 2;
                _tail            = _head;
                _head            = node;
                _head.Next       = _tail;
                _tail !.Previous = _head;
            }
            else
            {
                _count = 1;
                _head  = node;
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// 移除节点
        /// <para/>
        /// * 默认为节点已经通过 <see cref="InsertAtHead(LRUSpecializedLinkedListNode{TValue})"/> 添加到列表
        /// </summary>
        /// <param name="node"></param>
        public void Remove(LRUSpecializedLinkedListNode <TValue> node)
        {
            if (node.Previous is not null) //移除的不是首节点
            {
                node.Previous.Next = node.Next;
                if (node.Next is not null)  //移除的不是尾节点
                {
                    node.Next.Previous = node.Previous;
                }
                else    //移除的是尾节点
                {
                    _tail      = node.Previous;
                    _tail.Next = null;
                }
            }
            else    //移除的是首节点
            {
                _head = node.Next;
                if (_head is not null)
                {
                    _head.Previous = null;
                }
            }

            if (--_count == 1)
            {
                _tail = null;
            }
        }
Beispiel #3
0
        /// <summary>
        /// 移动元素到头部
        /// <para/>
        /// * 默认为节点已经通过 <see cref="InsertAtHead(LRUSpecializedLinkedListNode{TValue})"/> 添加到列表
        /// </summary>
        /// <param name="node"></param>
        public void MoveToHead(LRUSpecializedLinkedListNode <TValue> node)
        {
            if (node == _head)
            {
                return;
            }

            if (node == _tail)
            {
                _tail            = _tail.Previous;
                _tail !.Next     = null;
                _head !.Previous = node;
                node.Next        = _head;
                node.Previous    = null;
                _head            = node;
                return;
            }

            node.Previous !.Next = node.Next;
            node.Next !.Previous = node.Previous;

            _head !.Previous = node;
            node.Next        = _head;
            node.Previous    = null;
            _head            = node;
        }
Beispiel #4
0
        private LRUSpecializedLinkedListNode <TValue>?RemoveTail()
        {
            var removedNode = _tail;

            _tail        = _tail !.Previous;
            _tail !.Next = null;

            return(removedNode);
        }