-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Neo.IO.Caching; | ||
|
||
abstract class KeyedCollectionSlim<TKey, TItem> where TKey : notnull | ||
{ | ||
private readonly LinkedList<TItem> _items = new(); | ||
private readonly Dictionary<TKey, LinkedListNode<TItem>> dict = new(); | ||
|
||
public int Count => dict.Count; | ||
public TItem First => _items.First.Value; | ||
|
||
protected abstract TKey GetKeyForItem(TItem item); | ||
|
||
public void Add(TItem item) | ||
{ | ||
var key = GetKeyForItem(item); | ||
var node = _items.AddLast(item); | ||
if (!dict.TryAdd(key, node)) | ||
{ | ||
_items.RemoveLast(); | ||
throw new ArgumentException("An element with the same key already exists in the collection."); | ||
} | ||
} | ||
|
||
public bool Contains(TKey key) => dict.ContainsKey(key); | ||
|
||
public void Remove(TKey key) | ||
{ | ||
if (dict.Remove(key, out var node)) | ||
_items.Remove(node); | ||
} | ||
|
||
public void RemoveFirst() | ||
{ | ||
var key = GetKeyForItem(_items.First.Value); | ||
dict.Remove(key); | ||
_items.RemoveFirst(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters