Exemplo n.º 1
0
        private void RemoveLast()
        {
            var item = Last;

            if (item.IsKey)
            {
                if (item.NextKey != null)
                {
                    item.NextKey.PreviousKey = null;
                }
                item.NextKey = null;
            }
            Last          = item.Next;
            item.Next     = null;
            Last.Previous = null;
            Count--;
        }
Exemplo n.º 2
0
 private void InsertHighSpeed(GWLinkedListNode <T> node, GWLinkedListNode <T> nodeToInsert)
 {
     while (node.NextKey != null && nodeToInsert.Data.Score > node.NextKey.Data.Score)
     {
         node = node.NextKey;
     }
     if (node.IsKey)
     {
         nodeToInsert.PreviousKey = node;
         if (node.NextKey != null)
         {
             node.NextKey.PreviousKey = nodeToInsert;
             nodeToInsert.NextKey     = node.NextKey;
         }
         node.NextKey = nodeToInsert;
     }
     InsertSmallSpeed(node, nodeToInsert, false);
 }
Exemplo n.º 3
0
        private void InsertSmallSpeed(GWLinkedListNode <T> node, GWLinkedListNode <T> nodeToInsert, bool canGoHighSpeed)
        {
            while (nodeToInsert.Data.Score > node.Data.Score)
            {
                if (node.IsKey && canGoHighSpeed)
                {
                    InsertHighSpeed(node, nodeToInsert);
                    return;
                }
                if (node.Next != null)
                {
                    node = node.Next;
                }
                else
                {
                    node.Next             = nodeToInsert;
                    nodeToInsert.Previous = node;
                    First = nodeToInsert;

                    if (Count > MaxSize)
                    {
                        RemoveLast();
                        return;
                    }
                    return;
                }
            }
            var oldPrevious = node.Previous;

            node.Previous         = nodeToInsert;
            nodeToInsert.Next     = node;
            nodeToInsert.Previous = oldPrevious;
            oldPrevious.Next      = nodeToInsert;

            if (Count > MaxSize)
            {
                RemoveLast();
                return;
            }
        }
Exemplo n.º 4
0
        public void SortedInsert(T data)
        {
            if (Last == null)
            {
                var newNode2 = new GWLinkedListNode <T>(data, internalCounter++ == 0);
                internalCounter = internalCounter % 4 == 0 ? 0 : internalCounter;
                First           = newNode2;
                Last            = newNode2;
                return;
            }

            if (Count == MaxSize && Last.Data.Score >= data.Score)
            {
                return;
            }

            var newNode = new GWLinkedListNode <T>(data, internalCounter++ == 0);

            internalCounter = internalCounter % 4 == 0 ? 0 : internalCounter;
            Count++;

            var node = Last;

            if (newNode.Data.Score > node.Data.Score)
            {
                InsertSmallSpeed(node, newNode, true);
            }
            else
            {
                if (Count <= MaxSize)
                {
                    node.Previous = newNode;
                    newNode.Next  = node;
                    Last          = newNode;
                }
            }
        }