Esempio n. 1
0
        private bool RemoveInternal(TKey key, Boolean matchValue, TValue value)
        {
            if (_count == 0)
            {
                return(false);
            }

            ConcurrentSkipListMapNode <TKey, TValue> traverseNode = _head;
            var     backlook  = GenerateInitialBacklook();
            var     nextIndex = TraverseNextStep(traverseNode.Forwards, key);
            Boolean found     = false;

            while (nextIndex != null)
            {
                for (var i = nextIndex.Value; i < traverseNode.Forwards.Length; i++)
                {
                    backlook[i] = traverseNode;
                }

                traverseNode = traverseNode.Forwards[nextIndex.Value];
                if (_comparer.Compare(traverseNode.Key, key) == 0)
                {
                    if (matchValue && EqualityComparer <TValue> .Default.Equals(traverseNode.Value, value) == false)
                    {
                        return(false);
                    }

                    found = true;
                    break;
                }
                else if (_comparer.Compare(key, traverseNode.Key) < 0)
                {
                    return(false);
                }

                nextIndex = TraverseNextStep(traverseNode.Forwards, key);
            }

            if (found == false)
            {
                return(false);
            }

            var foundNode = traverseNode;
            var prevNode  = backlook[nextIndex.Value];

            for (var i = 0; i < nextIndex.Value; i++)
            {
                backlook[i] = prevNode;
            }

            for (var i = 0; i < foundNode.Forwards.Length; i++)
            {
                backlook[i].Forwards[i] = foundNode.Forwards[i];
            }

            _count--;
            return(true);
        }
Esempio n. 2
0
        private ConcurrentSkipListMapNode <TKey, TValue>[] GenerateInitialBacklook()
        {
            var backlook = new ConcurrentSkipListMapNode <TKey, TValue> [_head.Forwards.Length];

            for (var i = 0; i < backlook.Length; i++)
            {
                backlook[i] = _head;
            }

            return(backlook);
        }
Esempio n. 3
0
            public bool MoveNext()
            {
                if (_currentNode.Forwards[0] != null)
                {
                    _currentNode = _currentNode.Forwards[0];
                    _current     = new KeyValuePair <TKey, TValue>(_currentNode.Key, _currentNode.Value);
                    return(true);
                }

                _currentNode = default;
                _current     = new KeyValuePair <TKey, TValue>();
                return(false);
            }
Esempio n. 4
0
        public void Add(TKey key, TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            ConcurrentSkipListMapNode <TKey, TValue> traverseNode = _head;
            var backlook  = new ConcurrentSkipListMapNode <TKey, TValue> [_head.Forwards.Length];
            var nextIndex = TraverseNextStep(_head.Forwards, key);

            while (nextIndex != null)
            {
                for (var i = nextIndex.Value; i < traverseNode.Forwards.Length; i++)
                {
                    backlook[i] = traverseNode;
                }

                traverseNode = traverseNode.Forwards[nextIndex.Value];

                if (_comparer.Compare(traverseNode.Key, key) == 0)
                {
                    throw new ArgumentException("the key already exists", nameof(key));
                }

                nextIndex = TraverseNextStep(traverseNode.Forwards, key);
            }

            for (var i = 0; i < traverseNode.Forwards.Length; i++)
            {
                backlook[i] = traverseNode;
            }

            var forwardLength = NewForwardLength();
            var newNode       = new ConcurrentSkipListMapNode <TKey, TValue>(forwardLength)
            {
                Key = key, Value = value
            };

            for (var i = 0; i < forwardLength; i++)
            {
                var prevNode = backlook[i];
                var nextNode = prevNode?.Forwards[i];

                newNode.Forwards[i]  = nextNode;
                prevNode.Forwards[i] = newNode;
            }

            _count++;
        }
Esempio n. 5
0
        public Boolean TryGetValue(TKey key, out TValue value)
        {
            ConcurrentSkipListMapNode <TKey, TValue> traverseNode = _head;
            var nextIndex = TraverseNextStep(traverseNode.Forwards, key);

            while (nextIndex != null)
            {
                traverseNode = traverseNode.Forwards[nextIndex.Value];

                if (_comparer.Compare(traverseNode.Key, key) == 0)
                {
                    value = traverseNode.Value;
                    return(true);
                }

                nextIndex = TraverseNextStep(traverseNode.Forwards, key);
            }

            value = default;
            return(false);
        }
Esempio n. 6
0
 public void Reset()
 {
     _currentNode = _skipListMap._head;
     _current     = new KeyValuePair <TKey, TValue>();
 }
Esempio n. 7
0
 internal Enumerator(ConcurrentSkipListMap <TKey, TValue> skipListMap)
 {
     _skipListMap = skipListMap;
     _currentNode = _skipListMap._head;
     _current     = new KeyValuePair <TKey, TValue>();
 }