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?
Write a simple array implementation for
Heap<T>. Then, use aHeap<TKey>, and aDictionary<TKey, Statistics>together. Heap will serve as a priority queue. Careful: The top element of the heap should be the item to be deleted next. i.e. If you want to keep the “maximum” items, the heap should keep the minimum item at the root. When you will insert an item and the heap is full, also delete the root in Log(n) time. Whenever you delete an item from the heap delete it from the dictionary as well. Pseudocode: