Beispiel #1
0
        private void MoveToFront(LRUPrefixTreeMapNode newNode)
        {
            if (_head == newNode) // If it's already the head, stop
            {
                return;
            }

            if (_tail == newNode && newNode.Prev != null) // If it's the tail and it has a previous node, aka is not also the head
            {
                _tail      = newNode.Prev;
                _tail.Next = null;
            }

            if (_head != null)
            {
                _head.Prev = newNode;
                if (_head.Next == newNode)
                {
                    _head.Next        = newNode.Next;
                    newNode.Next.Prev = _head;
                }
            }

            newNode.Prev = null;
            newNode.Next = _head;

            _head = newNode;
        }
Beispiel #2
0
        private void RemoveLRU()
        {
            if (_map.Count == _capacity)
            {
                RemoveWordFromTree(_tail.Value);
                _map.Remove(_tail.Value);

                _tail.Prev.Next = null;
                _tail           = _tail.Prev;
            }
        }
Beispiel #3
0
        private void AddToMap(LRUPrefixTreeNode node)
        {
            if (_map.ContainsKey(node))
            {
                MoveToFront(_map[node]);
            }
            else
            {
                var mapNode = new LRUPrefixTreeMapNode(node);

                if (_tail != null)
                {
                    RemoveLRU();
                }
                else
                {
                    _tail = mapNode;
                }

                MoveToFront(mapNode);

                _map.Add(node, mapNode);
            }
        }