I need a CircularBuffer IDictionary. Can anyone point me to a good open source implementation.
So a IDictionary that has a maximum capacity, say configured to 100 items, which when item 101 is added the original first item is popped/removed from the dictionary thus ensuring the item count never exceeds 100.
Thanks
To keep O(1) insertion (with removal of the oldest item past 100) and O(1) lookups, you’ll need a class that implements IDictionary and keeps an internal ordered list. If memory is more a concern, a BST implementation like
SortedListcould be more appropriate. Anyway, your class will contain both aT[]and aDictionary<T,K>(orSortedList<T,K>). Do your own circular buffer indexing (easy), and keep both collections current in the add, remove, etc. methods. You’ll have:Make it generic and implement
IDictionary<T,K>andIDictionarysince there’s no reason not to and you’ll get an edge in performance.One major consideration: what do you do about duplicate keys? I’m assuming you can’t actually keep the duplicates, so:
Countof the dictionary, then insert the key using thethis[key]indexer. if the size increases, then check if the list already has the maximum capacity, remove the front item from the list and dictionary and add the new item to the back. If the dictionary did not increase in size, move the item from its existing place in the list to the back of the list.Finally, note that the internal list keeps keys, not values. This is to ensure O(1) dequeue when the list capacity is exceeded.