I have a Dictionary<..., Statistics>
, where Statistics
is a custom structure with some fields. This struct implements the IComparer
interface.
public struct Statistics : IComparer<Statistics>
{
public UInt64 NumberOfOccurrences;
public UInt64 TotalNumberOfWords;
...
public int Compare(Statistics x, Statistics y)
{
// code for compare the statistics
...
}
}
I chose the dictionary to have better performance during updates of existing statistics.
However, I also want good performance in reading statistics in order from best to worst, so I should use a SortedDictionary
in place of Dictionary
.
Now I would like to limit the number of entries within the dictionary to a specified value, by removing the entries with worst statistics, in order to limit the memory usage.
I have seen that SortedDictionary
does not have constructors with a capacity
argument, so I thought I'd store the maximum number of entries in the integer variable elementsCountLimit
. Before adding a new entry in the SortedDictionary
, I could perform the following check:
if (dict.Count == elementsCountLimit)
{
// Remove the last entry, that is the
// entry with worst statistics.
}
- But, If I don't know the key, how can I remove the last entry?
- How can I limit the memory usage?