Created
November 18, 2021 15:45
-
-
Save ridomin/2fec6b2d584c40ea364cd3250b03ff51 to your computer and use it in GitHub Desktop.
FixedSizeDictonary
This file contains hidden or 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
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