Esempio n. 1
0
        //O(log(n)
        public void Insert(T value)
        {
            //find the random level up to which we link the new node
            var level = 0;
            for (int i = 0; i < MaxHeight
                && coinFlipper.Next(0, 2) == 1; i++)
            {
                level++;
            }

            var newNode = new SkipListNode<T>()
            {
                value = value,
                //only level + 1 number of links (level is zero index based)
                Next = new SkipListNode<T>[level + 1]
            };

            //init current to head before insertion
            var current = Head;

            //go down from top level
            for (int i = MaxHeight - 1; i >= 0; i--)
            {
                //move on current level of linked list
                //until next element is less than new value to insert
                while (true)
                {
                    if (current.Next[i] == null
                        || current.Next[i].value.CompareTo(value) > 0)
                    {

                        break;
                    }

                    current = current.Next[i];

                }

                //if this level is greater than
                //maximum levels of link for new node
                //then jump to next level
                if (i > level)
                {
                    continue;
                }

                //insert and update pointers
                newNode.Next[i] = current.Next[i];
                current.Next[i] = newNode;

                //for base level set previous node
                if (i == 0)
                {
                    newNode.Prev = current;
                }
            }
        }
Esempio n. 2
0
 public SkipList(int maxHeight = 32)
 {
     MaxHeight = maxHeight;
     Head = new SkipListNode<T>()
     {
         Prev = null,
         Next = new SkipListNode<T>[maxHeight],
         value = default(T)
     };
 }
        public bool MoveNext()
        {
            if (current.Next[0] != null)
            {
                current = current.Next[0];
                return(true);
            }

            return(false);
        }
 public void Dispose()
 {
     head    = null;
     current = null;
 }
 public void Reset()
 {
     current = head;
 }
 internal SkipListEnumerator(SkipListNode <T> head)
 {
     this.head    = head;
     this.current = head;
 }