コード例 #1
0
        /// <summary>
        /// Inserts the given node into the skip list.
        /// </summary>
        internal void Insert(SkipListNode <T> node)
        {
            SkipListNode <T>[] previousNodes = new SkipListNode <T> [this.MaxLevels];
            SkipListNode <T>   previous      = this.Head;

            // find the insertion point, making use of the highest / most sparse levels first
            for (int level = this.Levels - 1; level >= 0; --level)
            {
                while (previous.Next[level] != null && previous.Next[level].Value.CompareTo(node.Value) < 0)
                {
                    previous = previous.Next[level];
                }

                if (level <= node.Levels)
                {
                    previousNodes[level] = previous;
                }
            }

            // Increase the skip list's level if necessary
            while (this.Levels < node.Levels)
            {
                ++this.Levels;
                previousNodes[this.Levels - 1] = this.Head;
            }

            // Update all of the levels in the skip list to point to the new node
            for (int curLevel = 0; curLevel < node.Levels; ++curLevel)
            {
                node.Next[curLevel] = previousNodes[curLevel].Next[curLevel];
                previousNodes[curLevel].Next[curLevel] = node;
            }

            ++this.Count;
        }
コード例 #2
0
        public IEnumerator <T> GetEnumerator()
        {
            SkipListNode <T> current = this.Head.Next[0];

            while (current != null)
            {
                yield return(current.Value);

                current = current.Next[0];
            }
        }
コード例 #3
0
        /// <summary>
        /// Finds the node with the given value within the skip list.
        /// </summary>
        /// <returns>The node with the given value, or null if no node exists.</returns>
        internal SkipListNode <T> Find(T value)
        {
            SkipListNode <T> current = this.Head;

            for (int level = this.Levels - 1; level >= 0; --level)
            {
                while (current.Next[level] != null && current.Next[level].Value.CompareTo(value) <= 0)
                {
                    current = current.Next[level];
                }
            }

            if (current != null && current.Value.CompareTo(value) == 0)
            {
                return(current);
            }
            else
            {
                return(null);
            }
        }
コード例 #4
0
        /// <summary>
        /// Removes the node with the given value from the list.
        /// </summary>
        /// <returns>Returns true if a node was deleted.</returns>
        public bool Delete(T value)
        {
            bool deleted = false;

            SkipListNode <T>[] previousNodes = new SkipListNode <T> [this.Levels];
            SkipListNode <T>   node          = this.Head;

            for (int level = this.Levels - 1; level >= 0; --level)
            {
                while (node.Next[level] != null && node.Next[level].Value.CompareTo(value) < 0)
                {
                    node = node.Next[level];
                }

                if (node.Next[level] != null)
                {
                    previousNodes[level] = node;
                }
            }

            node = node.Next[0];

            if (node.Value.CompareTo(value) == 0)
            {
                for (int level = 0; level < node.Levels; ++level)
                {
                    deleted = true;
                    SkipListNode <T> toDelete = previousNodes[level].Next[level];
                    previousNodes[level].Next[level] = toDelete.Next[level];
                    toDelete.Next[level]             = null;
                }
            }

            --this.Count;
            return(deleted);
        }