Skip to content

Commit

Permalink
feat: add remove range methods to collections
Browse files Browse the repository at this point in the history
  • Loading branch information
kalilistic committed Sep 23, 2023
1 parent 0d69b3b commit 6ef2402
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Dalamud.DrunkenToad/Collections/ThreadSafeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ public int AddRange(IEnumerable<KeyValuePair<TKey, TValue>> itemsToAdd)
/// <returns>True if the item was removed successfully; otherwise, false.</returns>
public bool Remove(TKey key) => this.items.TryRemove(key, out _);

/// <summary>
/// Removes a range of key-value pairs from the collection based on the keys.
/// </summary>
/// <param name="keysToRemove">The keys of the items to remove from the collection.</param>
/// <returns>The number of items successfully removed from the collection.</returns>
public int RemoveRange(IEnumerable<TKey> keysToRemove)
{
var removedCount = 0;
this.rwLock.EnterWriteLock();
try
{
foreach (var key in keysToRemove)
{
if (this.items.TryRemove(key, out _))
{
removedCount++;
}
}
}
finally
{
this.rwLock.ExitWriteLock();
}

return removedCount;
}

/// <summary>
/// Gets an item from the collection.
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions Dalamud.DrunkenToad/Collections/ThreadSafeSortedCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,33 @@ public bool Remove(T item)
return false;
}

/// <summary>
/// Removes a range of items from the collection.
/// </summary>
/// <param name="itemsToRemove">The items to be removed.</param>
/// <returns>The number of items successfully removed.</returns>
public int RemoveRange(IEnumerable<T> itemsToRemove)
{
this.setLock.EnterWriteLock();
var removedCount = 0;
try
{
foreach (var item in itemsToRemove)
{
if (this.Remove(item))
{
removedCount++;
}
}
}
finally
{
this.setLock.ExitWriteLock();
}

return removedCount;
}

/// <summary>
/// Updates an item in the collection by removing it, performing the specified action, and adding it back.
/// </summary>
Expand Down

0 comments on commit 6ef2402

Please sign in to comment.