Skip to content

Instantly share code, notes, and snippets.

@ridomin
Created November 18, 2021 15:45
Show Gist options
  • Save ridomin/2fec6b2d584c40ea364cd3250b03ff51 to your computer and use it in GitHub Desktop.
Save ridomin/2fec6b2d584c40ea364cd3250b03ff51 to your computer and use it in GitHub Desktop.
FixedSizeDictonary
public class FixedSizeDictonary<TKey, TValue> : Dictionary<TKey, TValue> where TKey : notnull
{
int size;
Queue<TKey> orderedKeys = new Queue<TKey>();
public FixedSizeDictonary(int maxSize) => size = maxSize;
public new void Add(TKey key, TValue value)
{
orderedKeys.Enqueue(key);
if (size != 0 && Count >= size) Remove(orderedKeys.Dequeue());
base.Add(key, value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment