Beispiel #1
0
        internal void IncrementHeight()
        {
            var newForwards = new SkipListNode <T> [Height + 1];

            for (int i = 0; i < Height; i++)
            {
                newForwards[i] = forwards[i];
            }

            forwards = newForwards;
        }
Beispiel #2
0
        public bool Remove(T value)
        {
            if (Count == 0)
            {
                return(false);
            }

            var nodesForUpdate = new SkipListNode <T> [Height];
            var curNode        = Head;

            for (int i = Height - 1; i >= 0; i--)
            {
                while (curNode[i] != null && value.CompareTo(curNode[i].Value) > 0)
                {
                    curNode = curNode[i];
                }

                nodesForUpdate[i] = curNode;
            }

            var nodeToDelete = nodesForUpdate[0][0];

            if (nodeToDelete != null && value.CompareTo(nodeToDelete.Value) == 0)
            {
                for (int i = 0; i < Head.Height; i++)
                {
                    if (nodesForUpdate[i][i] != nodeToDelete)
                    {
                        break;
                    }
                    else
                    {
                        nodesForUpdate[i][i] = nodeToDelete[i];
                    }
                }

                nodeToDelete.Invalidate();

                if (Head[Head.Height - 1] == null)
                {
                    Head.DecrementHeight();
                }

                Count--;
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #3
0
        public void Add(T value)
        {
            var nodesForUpdate = new SkipListNode <T> [Height];
            var curNode        = Head;

            for (int i = Height - 1; i >= 0; i--)
            {
                while (curNode[i] != null && value.CompareTo(curNode[i].Value) > 0)
                {
                    curNode = curNode[i];
                }

                nodesForUpdate[i] = curNode;
            }

            var nodeBeforeNodeForInsertion = nodesForUpdate[0];

            if (nodeBeforeNodeForInsertion[0] != null && value.CompareTo(nodeBeforeNodeForInsertion[0].Value) == 0)
            {
                throw new ArgumentException("Tried to insert duplicate value!");
            }

            int height = 1;

            while (randomizer.NextDouble() < probability && height < Head.Height + 1)
            {
                height++;
            }

            var newNode = new SkipListNode <T>(value, height);

            if (height > Head.Height)
            {
                Head.IncrementHeight();
                Head[Head.Height - 1] = newNode;
            }

            for (int i = 0; i < newNode.Height; i++)
            {
                if (i < nodesForUpdate.Length)
                {
                    newNode[i]           = nodesForUpdate[i][i];
                    nodesForUpdate[i][i] = newNode;
                }
            }

            Count++;
        }
Beispiel #4
0
        internal void DecrementHeight()
        {
            if (Height == 1)
            {
                return;
            }

            var newForwards = new SkipListNode <T> [Height - 1];

            for (int i = 0; i < Height - 1; i++)
            {
                newForwards[i] = forwards[i];
            }

            forwards = newForwards;
        }
Beispiel #5
0
        public void Clear()
        {
            var curNode  = Head;
            var lastNode = Head;

            while (curNode != null)
            {
                curNode = curNode[0];
                lastNode.Invalidate();
                lastNode = curNode;
            }

            Head       = new SkipListNode <T>(default(T), 1);
            Count      = 0;
            randomizer = new Random();
        }
Beispiel #6
0
 public SkipList()
 {
     Head       = new SkipListNode <T>(default(T), 1);
     Count      = 0;
     randomizer = new Random();
 }