/// <summary> /// Uses the specified functions to remove a key/value pair from the dictionary if the key exists and passes a condition, or to update a key/value pair in the dictionary if the key exists but does not. /// </summary> /// <param name="key">The key to be removed or whose value should be updated</param> /// <param name="removalConditionFunc">The function used to check if a key should be removed or updated.</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>Whether the key was found and successfully removed or not.</returns> public bool RemoveOrUpdate(TKey key, Func <TKey, TValue, bool> removalConditionFunc, Func <TKey, TValue, TValue> updateValueFactory) { bool blnReturn; using (EnterReadLock.Enter(LockObject)) { if (_dicData.TryGetValue(key, out TValue objExistingValue)) { blnReturn = removalConditionFunc(key, objExistingValue); if (blnReturn) { using (LockObject.EnterWriteLock()) blnReturn = _dicData.Remove(key); } if (!blnReturn) // Not an else in case we somehow fail at removing a key that still passes the condition { using (LockObject.EnterWriteLock()) _dicData[key] = updateValueFactory(key, objExistingValue); } } else { blnReturn = false; } } return(blnReturn); }
// ReSharper disable once InheritdocInvalidUsage /// <inheritdoc /> public T this[int index] { get { using (EnterReadLock.Enter(LockObject)) return(_lstOrderedData[index]); } set { using (EnterReadLock.Enter(LockObject)) { T objOldItem = _lstOrderedData[index]; if (objOldItem.Equals(value)) { return; } using (LockObject.EnterWriteLock()) { _setData.Remove(objOldItem); _setData.Add(value); _lstOrderedData[index] = value; } } } }
/// <inheritdoc /> public bool TryTake(out KeyValuePair <TKey, TValue> item) { bool blnTakeSuccessful = false; TKey objKeyToTake = default; TValue objValue = default; // Immediately enter a write lock to prevent attempted reads until we have either taken the item we want to take or failed to do so using (LockObject.EnterWriteLock()) { if (_lstIndexes.Count > 0) { // FIFO to be compliant with how the default for BlockingCollection<T> is ConcurrentQueue objKeyToTake = _lstIndexes[0]; if (_dicUnorderedData.TryGetValue(objKeyToTake, out objValue)) { blnTakeSuccessful = _dicUnorderedData.Remove(objKeyToTake); if (blnTakeSuccessful) { _lstIndexes.RemoveAt(0); } } } } if (blnTakeSuccessful) { item = new KeyValuePair <TKey, TValue>(objKeyToTake, objValue); return(true); } item = default; return(false); }
public void CopyFrom(CharacterCache objExistingCache) { using (LockObject.EnterWriteLock()) using (EnterReadLock.Enter(objExistingCache.LockObject)) { _strBackground = objExistingCache.Background; _strBuildMethod = objExistingCache.BuildMethod; _strCharacterAlias = objExistingCache.CharacterAlias; _strCharacterName = objExistingCache.CharacterName; _strCharacterNotes = objExistingCache.CharacterNotes; _strConcept = objExistingCache.Concept; _blnCreated = objExistingCache.Created; _strDescription = objExistingCache.Description; _strEssence = objExistingCache.Essence; _strGameNotes = objExistingCache.GameNotes; _strKarma = objExistingCache.Karma; _strFileName = objExistingCache.FileName; _strMetatype = objExistingCache.Metatype; _strMetavariant = objExistingCache.Metavariant; _strPlayerName = objExistingCache.PlayerName; _strSettingsFile = objExistingCache.SettingsFile; _imgMugshot?.Dispose(); _imgMugshot = objExistingCache.Mugshot.Clone() as Image; } }
/// <inheritdoc /> protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { using (EnterReadLock.Enter(LockObject)) { if (_blnSkipCollectionChanged) { return; } if (e.Action == NotifyCollectionChangedAction.Reset) { _blnSkipCollectionChanged = true; using (LockObject.EnterWriteLock()) { // Remove all duplicate entries for (int intLastIndex = Count - 1; intLastIndex >= 0; --intLastIndex) { T objItem = this[intLastIndex]; for (int intIndex = IndexOf(objItem); intIndex != intLastIndex; intIndex = IndexOf(objItem)) { RemoveAt(intIndex); --intLastIndex; } } } _blnSkipCollectionChanged = false; } } base.OnCollectionChanged(e); }
/// <inheritdoc /> public void Add(TKey key, TValue value) { using (LockObject.EnterWriteLock()) { _dicUnorderedData.Add(key, value); _lstIndexes.Add(key); } }
/// <inheritdoc cref="Dictionary{TKey, TValue}.Clear" /> public void Clear() { using (LockObject.EnterWriteLock()) { _dicUnorderedData.Clear(); _lstIndexes.Clear(); } }
private void SetDefaultEventHandlers() { using (LockObject.EnterWriteLock()) { _onMyDoubleClick += OnDefaultDoubleClick; _onMyKeyDown += OnDefaultKeyDown; _onMyContextMenuDeleteClick += OnDefaultContextMenuDeleteClick; } }
/// <inheritdoc /> public void RemoveAt(int index) { using (LockObject.EnterWriteLock()) { T objToRemove = _lstOrderedData[index]; if (_setData.Remove(objToRemove)) { _lstOrderedData.RemoveAt(index); } } }
/// <inheritdoc /> public void Insert(int index, T item) { using (LockObject.EnterWriteLock()) { if (!_setData.Add(item)) { return; } _lstOrderedData.Insert(index, item); } }
public KeyValuePair <TKey, TValue> this[int index] { get { using (EnterReadLock.Enter(LockObject)) return(new KeyValuePair <TKey, TValue>(_lstIndexes[index], _dicUnorderedData[_lstIndexes[index]])); } set { using (EnterReadLock.Enter(LockObject)) { if (_dicUnorderedData.TryGetValue(value.Key, out TValue objOldValue)) { int intOriginalIndex = _lstIndexes.IndexOf(value.Key); if (index == intOriginalIndex) { return; } TKey objKeyToRemove = _lstIndexes[index]; using (LockObject.EnterWriteLock()) { _lstIndexes[index] = value.Key; for (int i = intOriginalIndex; i < _lstIndexes.Count - 2; ++i) { if (i != index) { _lstIndexes[i] = _lstIndexes[i + 1]; } } _lstIndexes.RemoveAt(_lstIndexes.Count - 1); if (objKeyToRemove != null) { _dicUnorderedData.Remove(objKeyToRemove); } if (!objOldValue.Equals(value.Value)) { _dicUnorderedData[value.Key] = value.Value; } } } else { using (LockObject.EnterWriteLock()) { TKey objKeyToRemove = _lstIndexes[index]; _dicUnorderedData.Remove(objKeyToRemove); _dicUnorderedData.Add(value.Key, value.Value); _lstIndexes[index] = value.Key; } } } } }
public bool Remove(TKey key) { using (LockObject.EnterWriteLock()) { if (!_dicUnorderedData.Remove(key)) { return(false); } _lstIndexes.Remove(key); return(true); } }
public bool TryAdd(TKey key, TValue value) { // Immediately enter a write lock to prevent attempted reads until we have either added the item we want to add or failed to do so using (LockObject.EnterWriteLock()) { if (_dicData.ContainsKey(key)) { return(false); } _dicData.Add(key, value); } return(true); }
/// <inheritdoc /> public override bool TryAdd(T item) { using (LockObject.EnterWriteLock()) { int intExistingIndex = IndexOf(item); if (intExistingIndex == -1) { return(base.TryAdd(item)); } Move(intExistingIndex, Count - 1); return(true); } }
public override int Add(object value) { using (LockObject.EnterWriteLock()) { int intExistingIndex = IndexOf(value); if (intExistingIndex == -1) { return(base.Add(value)); } int intNewIndex = Count - 1; Move(intExistingIndex, intNewIndex); return(intNewIndex); } }
/// <inheritdoc cref="List{T}.Insert" /> public override void Insert(int index, T item) { using (LockObject.EnterWriteLock()) { int intExistingIndex = IndexOf(item); if (intExistingIndex == -1) { base.Insert(index, item); } else { Move(intExistingIndex, Math.Min(index, Count - 1)); } } }
public void Insert(int index, KeyValuePair <TKey, TValue> item) { using (EnterReadLock.Enter(LockObject)) { if (_dicUnorderedData.ContainsKey(item.Key)) { throw new ArgumentException(null, nameof(item)); } using (LockObject.EnterWriteLock()) { _dicUnorderedData.Add(item.Key, item.Value); _lstIndexes.Insert(index, item.Key); } } }
/// <inheritdoc /> public void Dispose() { if (_blnIsDisposed) { return; } using (LockObject.EnterWriteLock()) { _blnIsDisposed = true; _imgMugshot?.Dispose(); _tskRunningDownloadTask?.Dispose(); _dicMyPluginData.Dispose(); } LockObject.Dispose(); }
/// <inheritdoc cref="List{T}.Insert" /> public override void Insert(int index, T item) { using (LockObject.EnterWriteLock()) { if (index >= _intMaxSize) { return; } while (Count >= _intMaxSize) { RemoveAt(Count - 1); } base.Insert(index, item); } }
/// <inheritdoc /> public override void Add(T item) { using (LockObject.EnterWriteLock()) { int intExistingIndex = IndexOf(item); if (intExistingIndex == -1) { base.Add(item); } else { Move(intExistingIndex, Count - 1); } } }
/// <inheritdoc /> protected override void InsertItem(int index, T item) { // Immediately enter a write lock to prevent attempted reads until we have either inserted the item we want to insert or failed to do so using (LockObject.EnterWriteLock()) { int intExistingIndex = IndexOf(item); if (intExistingIndex == -1) { base.InsertItem(index, item); } else { MoveItem(intExistingIndex, Math.Min(index, Count - 1)); } } }
/// <summary> /// Adds a key/value pair to the dictionary if the key does not already exist, or to update 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 (EnterReadLock.Enter(LockObject)) { if (_dicData.TryGetValue(key, out TValue objExistingValue)) { TValue objNewValue = updateValueFactory(key, objExistingValue); using (LockObject.EnterWriteLock()) _dicData[key] = objNewValue; return(objNewValue); } using (LockObject.EnterWriteLock()) _dicData.Add(key, addValue); return(addValue); } }
/// <inheritdoc cref="List{T}.Sort()" /> public void Sort() { using (EnterReadLock.Enter(LockObject)) { if (_setData.Comparer is IComparer <T> comparer) { using (LockObject.EnterWriteLock()) _lstOrderedData.Sort(comparer); } else { using (LockObject.EnterWriteLock()) _lstOrderedData.Sort(); } } }
/// <inheritdoc /> public bool TryTake(out T item) { // Immediately enter a write lock to prevent attempted reads until we have either taken the item we want to take or failed to do so using (LockObject.EnterWriteLock()) { if (_lstData.Count > 0) { // FIFO to be compliant with how the default for BlockingCollection<T> is ConcurrentQueue item = _lstData[0]; _lstData.RemoveAt(0); return(true); } } item = default; return(false); }
public void Insert(int index, Tuple <TKey, TValue> item) { using (EnterReadLock.Enter(LockObject)) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (_dicUnorderedData.ContainsKey(item.Item1)) { throw new ArgumentException(null, nameof(item)); } using (LockObject.EnterWriteLock()) { _dicUnorderedData.Add(item.Item1, item.Item2); _lstIndexes.Insert(index, item.Item1); } } }
// ReSharper disable once InheritdocInvalidUsage /// <inheritdoc /> public TValue this[TKey key] { get { using (EnterReadLock.Enter(LockObject)) return(_dicData[key]); } set { using (EnterReadLock.Enter(LockObject)) { if (_dicData.TryGetValue(key, out TValue objValue) && objValue.Equals(value)) { return; } using (LockObject.EnterWriteLock()) _dicData[key] = value; } } }
/// <inheritdoc cref="List{T}" /> public new T this[int index] { get { using (EnterReadLock.Enter(LockObject)) return(base[index]); } set { using (EnterReadLock.Enter(LockObject)) { if (base[index].Equals(value)) { return; } using (LockObject.EnterWriteLock()) base[index] = value; } } }
object IList.this[int index] { get { using (EnterReadLock.Enter(LockObject)) return(_lstData[index]); } set { using (EnterReadLock.Enter(LockObject)) { if (_lstData[index].Equals(value)) { return; } using (LockObject.EnterWriteLock()) _lstData[index] = (T)value; } } }
public TValue this[TKey key] { get { using (EnterReadLock.Enter(LockObject)) return(_dicUnorderedData[key]); } set { using (EnterReadLock.Enter(LockObject)) { TValue objOldValue = _dicUnorderedData[key]; if (objOldValue.Equals(value)) { return; } using (LockObject.EnterWriteLock()) _dicUnorderedData[key] = value; } } }
/// <summary> /// Uses the specified functions to add a key/value pair to the dictionary if the key does not already exist, or to update 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 objReturn; using (EnterReadLock.Enter(LockObject)) { if (_dicData.TryGetValue(key, out TValue objExistingValue)) { objReturn = updateValueFactory(key, objExistingValue); using (LockObject.EnterWriteLock()) _dicData[key] = objReturn; } else { objReturn = addValueFactory(key); using (LockObject.EnterWriteLock()) _dicData.Add(key, objReturn); } } return(objReturn); }