Пример #1
0
        /// <summary>
        /// IComparable method implementation
        /// </summary>
        public int CompareTo(SkipListNode <T> other)
        {
            if (other == null)
            {
                return(-1);
            }

            return(this.Value.CompareTo(other.Value));
        }
Пример #2
0
        /// <summary>
        /// CONSTRUCTORS
        /// </summary>
        public SkipListNode(T value, int level)
        {
            if (level < 0)
            {
                throw new ArgumentOutOfRangeException("Invalid value for level.");
            }

            Value    = value;
            Forwards = new SkipListNode <T> [level];
        }
Пример #3
0
        /// <summary>
        /// CONSTRUCTOR
        /// </summary>
        public SkipList()
        {
            _count           = 0;
            _currentMaxLevel = 1;
            _randomizer      = new Random();
            _firstNode       = new SkipListNode <T>(default(T), MaxLevel);

            for (int i = 0; i < MaxLevel; ++i)
            {
                _firstNode.Forwards[i] = _firstNode;
            }
        }
Пример #4
0
        /// <summary>
        /// Remove an element from list and then return it
        /// </summary>
        public bool Remove(T item, out T deleted)
        {
            // Find the node in each of the levels
            var current     = _firstNode;
            var toBeUpdated = new SkipListNode <T> [MaxLevel];

            // Walk after all the nodes that have values less than the node we are looking for.
            // Mark all nodes as toBeUpdated.
            for (int i = _currentMaxLevel - 1; i >= 0; --i)
            {
                while (current.Forwards[i] != _firstNode && current.Forwards[i].Value.IsLessThan(item))
                {
                    current = current.Forwards[i];
                }

                toBeUpdated[i] = current;
            }

            current = current.Forwards[0];

            // Return default value of T if the item was not found
            if (current.Value.IsEqualTo(item) == false)
            {
                deleted = default(T);
                return(false);
            }

            // We know that the node is in the list.
            // Unlink it from the levels where it exists.
            for (int i = 0; i < _currentMaxLevel; ++i)
            {
                if (toBeUpdated[i].Forwards[i] == current)
                {
                    toBeUpdated[i].Forwards[i] = current.Forwards[i];
                }
            }

            // Decrement the count
            --_count;

            // Check to see if we've deleted the highest-level node
            // Decrement level
            while (_currentMaxLevel > 1 && _firstNode.Forwards[_currentMaxLevel - 1] == _firstNode)
            {
                --_currentMaxLevel;
            }

            // Assign the deleted output parameter to the node.Value
            deleted = current.Value;
            return(true);
        }
Пример #5
0
        /// <summary>
        /// Adds item to the list
        /// </summary>
        public void Add(T item)
        {
            var current     = _firstNode;
            var toBeUpdated = new SkipListNode <T> [MaxLevel];

            for (int i = _currentMaxLevel - 1; i >= 0; --i)
            {
                while (current.Forwards[i] != _firstNode && current.Forwards[i].Value.IsLessThan(item))
                {
                    current = current.Forwards[i];
                }

                toBeUpdated[i] = current;
            }

            current = current.Forwards[0];

            // Get the next node level, and update list level if required.
            int lvl = _getNextLevel();

            if (lvl > _currentMaxLevel)
            {
                for (int i = _currentMaxLevel; i < lvl; ++i)
                {
                    toBeUpdated[i] = _firstNode;
                }

                _currentMaxLevel = lvl;
            }

            // New node
            var newNode = new SkipListNode <T>(item, lvl);

            // Insert the new node into the skip list
            for (int i = 0; i < lvl; ++i)
            {
                newNode.Forwards[i]        = toBeUpdated[i].Forwards[i];
                toBeUpdated[i].Forwards[i] = newNode;
            }

            // Increment the count
            ++_count;
        }