/// <summary> /// Removes all keys and values from the <see cref="T:ExitGames.Threading.SynchronizedDictionary`2"/>. /// </summary> public void Clear() { using (WriteLock.Enter(readerWriterLock)) { dictionary.Clear(); } }
/// <summary> /// Compares the existing value for the specified key with a specified value, /// and if they are equal, updates the key with a third value. /// </summary> /// <param name="key">The key whose value is compared with comparisonValue and possibly replaced.</param> /// <param name="newValue">The value that replaces the value of the element with key if the comparison results in equality</param> /// <param name="comparisonValue">The value that is compared to the value of the element with key</param> /// <returns>true if the value with key was equal to comparisonValue and replaced with newValue; otherwise, false.</returns> public bool TryUpdate(TKey key, TValue newValue, TValue comparisonValue) { bool flag; using (UpgradeableReadLock.Enter(readerWriterLock)) { TValue local; if (!dictionary.TryGetValue(key, out local)) { return(false); } if (!local.Equals(comparisonValue)) { flag = false; } else { using (WriteLock.Enter(readerWriterLock)) { dictionary[key] = newValue; flag = true; } } } return(flag); }
/// <summary> /// Removes the value with the specified key from the <see cref="T:ExitGames.Threading.SynchronizedDictionary`2"/>. /// </summary> /// <param name="key">The key of the element to remove.</param> /// <returns>true if the element is successfully found and removed; otherwise, false. /// This method returns false if key is not found in the <see cref="T:ExitGames.Threading.SynchronizedDictionary`2"/>.</returns> public bool Remove(TKey key) { bool flag; using (WriteLock.Enter(readerWriterLock)) { flag = dictionary.Remove(key); } return(flag); }
/// <summary> /// Calls <see cref="M:ExitGames.Threading.SimpleBaseFactory`2.DisposeItem(`0,`1)"/> for each item and clears the dictionary. /// </summary> public void Reset() { using (WriteLock.Enter(readerWriterLock)) { foreach (KeyValuePair <TKey, TValue> pair in items) { DisposeItem(pair.Key, pair.Value); } items.Clear(); } }
/// <summary> /// Adds a key/value pair to the dictionary if the key does not already exist, /// or updates a key/value pair in the dictionary if the key already exists. /// </summary> /// <param name="key">The key to be added or whose value should be updated.</param> /// <param name="addValueFactory">The function used to generate a value for an absent key.</param> /// <param name="updateValueFactory">The function used to generate a new value for an existing key based on the key's existing value.</param> /// <returns>The new value for the key. This will be either be the result of addValueFactory (if the key was absent) or the result of updateValueFactory (if the key was present).</returns> public TValue AddOrUpdate(TKey key, Func <TKey, TValue> addValueFactory, Func <TKey, TValue, TValue> updateValueFactory) { TValue local3; using (WriteLock.Enter(readerWriterLock)) { TValue local; TValue local2 = dictionary.TryGetValue(key, out local) ? updateValueFactory(key, local) : addValueFactory(key); dictionary[key] = local2; local3 = local2; } return(local3); }
/// <summary> /// Attempts to remove and return the value with the specified key from the <see cref="T:ExitGames.Threading.SynchronizedDictionary`2"/>. /// </summary> /// <param name="key">The key of the element to remove and return.</param> /// <param name="value">When this method returns true, value contains the object removed from the <see cref="T:ExitGames.Threading.SynchronizedDictionary`2"/> or the default value of if the operation failed.</param> /// <returns>true if an object was removed successfully; otherwise, false.</returns> public bool TryRemove(TKey key, out TValue value) { bool flag; using (WriteLock.Enter(readerWriterLock)) { if (dictionary.TryGetValue(key, out value)) { dictionary.Remove(key); return(true); } flag = false; } return(flag); }
/// <summary> /// Attempts to add the specified key and value to the <see cref="T:ExitGames.Threading.SynchronizedDictionary`2"/>. /// </summary> /// <param name="key">The key of the element to add.</param> /// <param name="value">The value of the element to add. The value can be a null reference (Nothing in Visual Basic) for reference types.</param> /// <returns>True if the key/value pair was added to the <see cref="T:ExitGames.Threading.SynchronizedDictionary`2"/> successfully. /// If the key already exists, this method returns false.</returns> public bool TryAdd(TKey key, TValue value) { bool flag; using (WriteLock.Enter(readerWriterLock)) { if (dictionary.ContainsKey(key)) { return(false); } dictionary.Add(key, value); flag = true; } return(flag); }
/// <summary> /// Adds a key/value pair to the dictionary if the key does not already exist, /// or updates a key/value pair in the dictionary if the key already exists. /// </summary> /// <param name="key">The key to be added or whose value should be updated.</param> /// <param name="addValue">The value to be added for an absent key.</param> /// <param name="updateValueFactory">The function used to generate a new value for an existing key based on the key's existing value.</param> /// <returns>The new value for the key. This will be either be addValue (if the key was absent) or the result of updateValueFactory (if the key was present).</returns> public TValue AddOrUpdate(TKey key, TValue addValue, Func <TKey, TValue, TValue> updateValueFactory) { using (WriteLock.Enter(readerWriterLock)) { TValue local; if (dictionary.TryGetValue(key, out local)) { TValue local2 = updateValueFactory(key, local); dictionary[key] = local2; return(local2); } dictionary.Add(key, addValue); } return(addValue); }
/// <summary> /// Adds a kay-value pair. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <returns>True on success, false if the key already exists.</returns> public bool Add(TKey key, TValue value) { bool flag; using (WriteLock.Enter(readerWriterLock)) { if (items.ContainsKey(key)) { return(false); } items.Add(key, value); flag = true; } return(flag); }
/// <summary> /// Removes the value with the specified key. /// </summary> /// <param name="key"> /// The key of the element to remove. /// </param> /// <returns> /// True if the element is successfully found and removed; otherwise, false. /// This method returns false if key is not found. /// </returns> public bool Remove(TKey key) { bool flag; using (WriteLock.Enter(readerWriterLock)) { TValue local; if (items.TryGetValue(key, out local)) { items.Remove(key); DisposeItem(key, local); return(true); } flag = false; } return(flag); }
/// <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 for the key.</returns> public TValue this[TKey key] { get { TValue local; using (ReadLock.Enter(readerWriterLock)) { local = dictionary[key]; } return(local); } set { using (WriteLock.Enter(readerWriterLock)) { dictionary[key] = value; } } }
/// <summary> /// Adds a key/value pair to the <see cref="T:ExitGames.Threading.SynchronizedDictionary`2"/> if the key /// does not already exist /// </summary> /// <param name="key">The key of the element to get or add.</param> /// <param name="valueFactory">The function used to generate a value for the key</param> /// <returns>The value for the key. This will be either the existing value for the key if the /// key is already in the dictionary, or the new value for the key as returned by valueFactory /// if the key was not in the dictionary.</returns> public TValue GetOrAdd(TKey key, Func <TKey, TValue> valueFactory) { TValue local2; using (UpgradeableReadLock.Enter(readerWriterLock)) { TValue local; if (dictionary.TryGetValue(key, out local)) { local2 = local; } else { using (WriteLock.Enter(readerWriterLock)) { local = valueFactory(key); dictionary.Add(key, local); local2 = local; } } } return(local2); }