Example #1
0
 /// <summary>
 /// Adds a node <paramref name="node"/> to <paramref name="header"/>
 /// </summary>
 /// <param name="node">The node to add.</param>
 /// <param name="header">The header.</param>
 private static void AddNode(FiboNode node, FiboNode header)
 {
     node.Right        = header;
     node.Left         = header.Left;
     header.Left.Right = node;
     header.Left       = node;
 }
Example #2
0
        private List <FiboNode> GetSiblingsOf(FiboNode node)
        {
            var siblings = new List <FiboNode>();

            foreach (var n in node.Siblings)
            {
                siblings.Add(n);
            }

            return(siblings);
        }
Example #3
0
        private void Consolidate()
        {
            var upperBound = Convert.ToInt32(Math.Floor(Math.Log(Count, FiboRatio)));
            var nodeArray  = new FiboNode[upperBound];
            var rootNodes  = GetSiblingsOf(TopNode);

            foreach (var node in rootNodes)
            {
                var degree = node.Degree;
                var x      = node;
                while (nodeArray[degree] != null)
                {
                    var another = nodeArray[degree];
                    if (comparer(x.Value, another.Value))
                    {
                        MakeChild(another, x);
                    }
                    else
                    {
                        MakeChild(x, another);
                        x = another;
                    }
                    nodeArray[degree] = null;
                    degree++;
                }
                nodeArray[degree] = x;
            }
            TopNode = null;
            foreach (var node in nodeArray)
            {
                if (node != null)
                {
                    if (TopNode == null)
                    {
                        node.Left = node.Right = node;
                        TopNode   = node;
                    }
                    else
                    {
                        AddNode(node, TopNode);
                        if (comparer(node.Value, TopNode.Value))
                        {
                            TopNode = node;
                        }
                    }
                }
            }
        }
Example #4
0
 /// <summary>
 /// Makes the <paramref name="child"/> to its new <param name="parent"/>
 /// </summary>
 /// <param name="child">The child.</param>
 /// <param name="parent">The parent.</param>
 private void MakeChild(FiboNode child, FiboNode parent)
 {
     RemoveNode(child);
     if (parent.Child == null)
     {
         parent.Child       = child;
         parent.Child.Right = parent.Child.Left = child;
     }
     else
     {
         AddNode(child, parent.Child);
     }
     child.Parent = parent;
     parent.Degree++;
     child.Mark = false;
 }
Example #5
0
        public void Add(T item)
        {
            item.ValidateNotNull("The item is null!");
            var node = new FiboNode {
                Value = item
            };

            if (TopNode == null)
            {
                node.Left = node.Right = node;
                TopNode   = node;
            }
            else
            {
                AddNode(node, TopNode);
                if (comparer(node.Value, TopNode.Value))
                {
                    TopNode = node;
                }
            }
            Count++;
        }
Example #6
0
 /// <summary>
 /// Removes the <paramref name="node"/> from its list.
 /// </summary>
 /// <param name="node">The node.</param>
 private static void RemoveNode(FiboNode node)
 {
     node.Left.Right = node.Right;
     node.Right.Left = node.Left;
 }