Exemple #1
0
        /// <summary>
        /// Removes the specified association from the <see cref="ConcurrentDictionaryKey{TKey,TValue}"/>, comparing both key and value.
        /// </summary>
        /// <param name="item">A <see cref="KeyValuePair{TKey,TValue}"/> representing the association to remove.</param>
        /// <returns>true if the association was successfully removed from the <see cref="ConcurrentDictionaryKey{TKey,TValue}"/>;
        /// otherwise, false. This method also returns false if the association is not found in
        /// the original <see cref="ConcurrentDictionaryKey{TKey,TValue}"/>.
        ///</returns>
        bool ICollection <KeyValuePair <TKey, TValue> > .Remove(KeyValuePair <TKey, TValue> item)
        {
            KeyValuePair <TKey, TValue>?           oldItem;
            ConcurrentDictionaryKey <TKey, TValue> searchKey = new ConcurrentDictionaryKey <TKey, TValue>(item.Key, item.Value);

            return(base.RemoveItem(ref searchKey, out oldItem));
        }
Exemple #2
0
        public TValue AddOrUpdate(TKey key, Func <TKey, TValue> addValueFactory, Func <TKey, TValue, TValue> updateValueFactory)
        {
            if (null == addValueFactory)
            {
                throw new ArgumentNullException("addValueFactory");
            }

            if (null == updateValueFactory)
            {
                throw new ArgumentNullException("updateValueFactory");
            }

            var searchKey = new ConcurrentDictionaryKey <TKey, TValue>(key);
            KeyValuePair <TKey, TValue>?latestItem;

            while (true)
            {
                if (this.FindItem(ref searchKey, out latestItem))
                {
                    TValue storedValue = latestItem.Value.Value;
                    TValue newValue    = updateValueFactory(key, storedValue);

                    if (TryUpdate(key, newValue, storedValue))
                    {
                        return(newValue);
                    }
                }
                else
                {
                    return(AddOrUpdate(key, addValueFactory(key), updateValueFactory));
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Removes the element with the specified key from the dictionary.
        /// </summary>
        /// <param name="key">The key of the element to remove.</param>
        /// <returns>true if the element is successfully removed; otherwise, false. This method
        /// also returns false if key was not found in the original dictionary.</returns>
        bool IDictionary <TKey, TValue> .Remove(TKey key)
        {
            KeyValuePair <TKey, TValue>?           oldItem;
            ConcurrentDictionaryKey <TKey, TValue> searchKey = new ConcurrentDictionaryKey <TKey, TValue>(key);

            return(base.RemoveItem(ref searchKey, out oldItem));
        }
Exemple #4
0
        /// <summary>
        /// Determines whether the dictionary
        /// contains an element with the specified key.
        /// </summary>
        /// <param name="key">The key to locate in the dictionary.</param>
        /// <returns>true if the dictionary contains
        /// an element with the key; otherwise, false.</returns>
        public bool ContainsKey(TKey key)
        {
            KeyValuePair <TKey, TValue>?           presentItem;
            ConcurrentDictionaryKey <TKey, TValue> searchKey = new ConcurrentDictionaryKey <TKey, TValue>(key);

            return(FindItem(ref searchKey, out presentItem));
        }
Exemple #5
0
        /// <summary>
        /// Determines whether the specified association exists in the dictionary.
        /// </summary>
        /// <param name="item">The key-value association to search fo in the dicionary.</param>
        /// <returns>True if item is found in the dictionary; otherwise, false.</returns>
        /// <remarks>
        /// This method compares both key and value. It uses the default equality comparer to compare values.
        /// </remarks>
        bool ICollection <KeyValuePair <TKey, TValue> > .Contains(KeyValuePair <TKey, TValue> item)
        {
            KeyValuePair <TKey, TValue>?           presentItem;
            ConcurrentDictionaryKey <TKey, TValue> searchKey = new ConcurrentDictionaryKey <TKey, TValue>(item.Key, item.Value);

            return
                (FindItem(ref searchKey, out presentItem));
        }
Exemple #6
0
        public bool TryRemove(TKey key, out TValue value)
        {
            var searchKey = new ConcurrentDictionaryKey <TKey, TValue>(key);
            KeyValuePair <TKey, TValue>?oldItem;

            var res = base.RemoveItem(ref searchKey, out oldItem);

            value = res ? oldItem.Value.Value : default(TValue);

            return(res);
        }
Exemple #7
0
        public bool TryUpdate(
            TKey key,
            TValue newValue,
            TValue comparisonValue
            )
        {
            var searchKey = new ConcurrentDictionaryKey <TKey, TValue>(key);
            KeyValuePair <TKey, TValue>?newItem = new KeyValuePair <TKey, TValue>(key, newValue);
            KeyValuePair <TKey, TValue>?dummy;

            return(base.ReplaceItem(ref searchKey, ref newItem, out dummy, item => EqualityComparer <TValue> .Default.Equals(item.Value.Value, comparisonValue)));
        }
Exemple #8
0
        /// <summary>
        /// Gets the value associated with the specified key.
        /// </summary>
        /// <param name="key">The key whose value to get.</param>
        /// <param name="value">
        /// When this method returns, the value associated with the specified key, if
        /// the key is found; otherwise, the default value for the type of the value
        /// parameter. This parameter is passed uninitialized.
        ///</param>
        /// <returns>
        /// true if the dictionary contains an element with the specified key; otherwise, false.
        /// </returns>
        public bool TryGetValue(TKey key, out TValue value)
        {
            KeyValuePair <TKey, TValue>?           presentItem;
            ConcurrentDictionaryKey <TKey, TValue> searchKey = new ConcurrentDictionaryKey <TKey, TValue>(key);

            var res = FindItem(ref searchKey, out presentItem);

            if (res)
            {
                value = presentItem.Value.Value;
                return(true);
            }
            else
            {
                value = default(TValue);
                return(false);
            }
        }
Exemple #9
0
        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <param name="key">The key of the value to get or set.</param>
        /// <returns>The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element with the specified key.</returns>
        /// <remarks>
        /// When working with multiple threads, that can each potentialy remove the searched for item, a <see cref="KeyNotFoundException"/> can always be expected.
        /// </remarks>
        public TValue this[TKey key]
        {
            get
            {
                KeyValuePair <TKey, TValue>?           presentItem;
                ConcurrentDictionaryKey <TKey, TValue> searchKey = new ConcurrentDictionaryKey <TKey, TValue>(key);

                if (!FindItem(ref searchKey, out presentItem))
                {
                    throw new KeyNotFoundException("The property is retrieved and key is not found.");
                }
                return(presentItem.Value.Value);
            }
            set
            {
                KeyValuePair <TKey, TValue>?newItem = new KeyValuePair <TKey, TValue>(key, value);
                KeyValuePair <TKey, TValue>?presentItem;
                InsertItem(ref newItem, out presentItem);
            }
        }
Exemple #10
0
        public TValue GetOrAdd(
            TKey key,
            Func <TKey, TValue> valueFactory
            )
        {
            if (null == valueFactory)
            {
                throw new ArgumentNullException("valueFactory");
            }

            var searchKey = new ConcurrentDictionaryKey <TKey, TValue>(key);
            KeyValuePair <TKey, TValue>?oldItem;

            if (base.FindItem(ref searchKey, out oldItem))
            {
                return(oldItem.Value.Value);
            }

            KeyValuePair <TKey, TValue>?newItem = new KeyValuePair <TKey, TValue>(key, valueFactory(key));

            base.GetOldestItem(ref newItem, out oldItem);

            return(oldItem.Value.Value);
        }
Exemple #11
0
 /// <summary>
 /// Compares a storeable item to a search key. Should return true if they match.
 /// </summary>
 /// <param name="item">Reference to the storeable item to compare.</param>
 /// <param name="key">Reference to the search key to compare.</param>
 /// <returns>True if the storeable item and search key match; false otherwise.</returns>
 internal protected override bool ItemEqualsKey(ref KeyValuePair <TKey, TValue>?item, ref ConcurrentDictionaryKey <TKey, TValue> key)
 {
     return(item.HasValue && _Comparer.Equals(item.Value.Key, key._Key) && (key._IgnoreValue || EqualityComparer <TValue> .Default.Equals(item.Value.Value, key._Value)));
 }
Exemple #12
0
 /// <summary>
 /// Get a hashcode for given search key.
 /// </summary>
 /// <param name="key">Reference to the key to get a hash value for.</param>
 /// <returns>The hash value as an <see cref="UInt32"/>.</returns>
 /// <remarks>
 /// The hash returned should be properly randomized hash. The standard GetItemHashCode methods are usually not good enough.
 /// A storeable item and a matching search key should return the same hash code.
 /// So the statement <code>ItemEqualsItem(storeableItem, searchKey) ? GetItemHashCode(storeableItem) == GetItemHashCode(searchKey) : true </code> should always be true;
 /// </remarks>
 internal protected override UInt32 GetKeyHashCode(ref ConcurrentDictionaryKey <TKey, TValue> key)
 {
     return(Hasher.Rehash(_Comparer.GetHashCode(key._Key)));
 }