Esempio n. 1
0
        /// <summary>
        /// Retrieves an existing value associated with the specified key or, if it can't be found, associates a specified value with the key and returns that later value.
        /// </summary>
        /// <param name="key">The key to find an existing value with or, if it can't be found, insert a new value with.</param>
        /// <param name="newValue">The new value to insert if an existing associated value can not be found in the dictionary.</param>
        /// <returns>The existing value if it can be found; otherwise the newly inserted value.</returns>
        /// <exception cref="ArgumentNullException">Gets raised when the <paramref name="key"/> parameter is null.</exception>
        public TValue GetOldest(TKey key, TValue newValue)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            var item = new ConcurrentWeakDictionaryItem(GetHashCode(key), new WeakReference(key), newValue == null ? null : new WeakReference(newValue));
            ConcurrentWeakDictionaryItem oldItem;
            TValue res;

            do
            {
                if (!base.GetOldestItem(ref item, out oldItem))
                {
                    return(newValue);
                }

                if (oldItem._Value == null)
                {
                    return(null);
                }

                res = (TValue)oldItem._Value.Target;
            }while (res == null);

            return(res);
        }
Esempio n. 2
0
        /// <summary>
        /// Compares two storeable items for equality.
        /// </summary>
        /// <param name="item1">Reference to the first storeable item to compare.</param>
        /// <param name="item2">Reference to the second storeable item to compare.</param>
        /// <returns>True if the two soreable items should be regarded as equal.</returns>
        internal protected override bool ItemEqualsItem(ref ConcurrentWeakDictionaryItem item1, ref ConcurrentWeakDictionaryItem item2)
        {
            var key1 = (TKey)item1._Key.Target;
            var key2 = (TKey)item2._Key.Target;

            return(key1 == null && key2 == null ? item1._Key == item2._Key : _Comparer.Equals(key1, key2));
        }
Esempio n. 3
0
        protected internal override Type GetKeyType(ref ConcurrentWeakDictionaryItem item)
        {
            if (item._Key == null)
            {
                return(null);
            }

            object key = item._Key.Target;

            return(key == null ? null : key.GetType());
        }
Esempio n. 4
0
        /// <summary>
        /// Inserts a key value association into the dictionary.
        /// </summary>
        /// <param name="key">The key to identify the value with.</param>
        /// <param name="value">The value to associate the key with.</param>
        /// <exception cref="ArgumentNullException">Gets raised when the <paramref name="key"/> parameter is null.</exception>
        public void Insert(TKey key, TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            var item = new ConcurrentWeakDictionaryItem(GetHashCode(key), new WeakReference(key), value == null ? null : new WeakReference(value));
            ConcurrentWeakDictionaryItem oldItem;

            base.InsertItem(ref item, out oldItem);
        }
Esempio n. 5
0
 /// <summary>
 /// Indicates if a specific content item should be treated as garbage and removed.
 /// </summary>
 /// <param name="item">The item to judge.</param>
 /// <returns>A boolean value that is true if the item is not empty and should be treated as garbage; false otherwise.</returns>
 internal protected override bool IsGarbage(ref ConcurrentWeakDictionaryItem item)
 {
     return(item._Key != null && (item._Key.Target == null || (item._Value != null && item._Value.Target == null)));
 }
Esempio n. 6
0
 /// <summary>
 /// Indicates if a specific item reference contains a valid item.
 /// </summary>
 /// <param name="item">The storeable item reference to check.</param>
 /// <returns>True if the reference doesn't refer to a valid item; false otherwise.</returns>
 /// <remarks>The statement <code>IsEmpty(default(TStoredI))</code> should always be true.</remarks>
 internal protected override bool IsEmpty(ref ConcurrentWeakDictionaryItem item)
 {
     return(item._Key == null);
 }
Esempio n. 7
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 ConcurrentWeakDictionaryItem item, ref ConcurrentWeakDictionaryKey <TKey> key)
        {
            var key1 = (TKey)item._Key.Target;

            return(_Comparer.Equals(key1, key._Key));
        }
Esempio n. 8
0
 /// <summary>
 /// Get a hashcode for given storeable item.
 /// </summary>
 /// <param name="item">Reference to the item 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 GetItemHashCode(ref ConcurrentWeakDictionaryItem item)
 {
     return(item._Hash);
 }