internal _ThreadSafeDictionaryNode(int hashCode, TKey key, TValue value, _ThreadSafeDictionaryNode <TKey, TValue> nextNode)
 {
     _hashCode = hashCode;
     _key      = key;
     _value    = value;
     _nextNode = nextNode;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a value for a given key or creates a new one using the given
        /// createValue delegate.
        /// </summary>
        public TValue GetOrCreateValue(TKey key, Func <TKey, TValue> createValue)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (createValue == null)
            {
                createValue = ValueConstructorDelegate <TKey, TValue> .GetDefault();
            }

            var owner       = _GetOwner();
            var comparer    = owner._comparer;
            int hashCode    = owner._comparer.GetHashCode(key) & int.MaxValue;
            int bucketIndex = hashCode % owner._buckets.Length;

            TValue result;

            if (owner._TryGetValue(hashCode, key, out result))
            {
                return(result);
            }

            result = createValue(key);

            if (owner._count >= owner._buckets.Length * 2)
            {
                owner._Resize();
                bucketIndex = hashCode % owner._buckets.Length;
            }

            var newNode = new _ThreadSafeDictionaryNode <TKey, TValue>(hashCode, key, result, owner._buckets[bucketIndex]);

            owner._buckets[bucketIndex] = newNode;
            owner._count++;

            return(result);
        }