5

I want to build a Dictionary (key, value), but I want that this Dictionary have limited size, for example 1000 entries, so when I rich this limit size, I want to remove the first element and add a new element(FIFO).

I want to use dictionary because I am always searching keys in the dictionary (i need that it will be fast)

How to do this?

anomepani
  • 1,796
  • 2
  • 25
  • 34
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • You say you want to remove the first element when size limit is reached. Do you mean the first item that was entered in the dictionary? In that case, you have FIFO behavior, not LIFO. Or do you want to remove the item at index position 0? Removing the item at position 0 is neither LIFO nor FIFO. – Joel Lee Apr 28 '11 at 06:34
  • In that case, please see my answer. I don't think any of the other answers handle FIFO. – Joel Lee Apr 28 '11 at 07:36

3 Answers3

8

To get both a dictionary and either LIFO/FIFO behavior (for deleting newest/oldest entry), you can use an OrderedDictionary. See http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx.

To make this convenient to use, you could derive your own class from OrderedDictionary, along the lines suggested by @ArsenMkrt.

Note, however, that OrderedDictionary does not use Generics, so there will be some inefficency due to boxing (items in the dictionary will be inserted as object). The only way to overcome this is to create a dual data structure which has all items in the dictionary mirrored in a Queue (for FIFO), or a Stack (for LIFO). For details see the answer by "Qua" to the following SO question, which deals with precisely the situation where you need an efficient way keep track of the order in which dictionary items were inserted.

Fastest and most efficient collection type in C#

Community
  • 1
  • 1
Joel Lee
  • 3,656
  • 1
  • 18
  • 21
3

Derive from dictionary and ovverride add method like this

if(myDic.Count == MAXCOUNT - 1)
{
    myDic.Remove(myDic[0]);
}
myDic.Add(key, item);
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • What if the newly added item gets index 0? or the previously added item, since you remove before adding? – Lasse V. Karlsen Apr 28 '11 at 06:18
  • That is the reqirement, isn't it? – Arsen Mkrtchyan Apr 28 '11 at 06:59
  • @ArsenMrkt I am not sure what the requirement is. OP says he wants to delete the "first" item. But that is not the first thing that was entered in the dictionary. Deleting the item in position zero is neither LIFO nor FIFO. I have asked OP to clarify. – Joel Lee Apr 28 '11 at 07:05
  • 3
    A simple dictionary has no concept of "ordering" as such, so if I add 1, 2, 3, 4 and then 5, I would expect 1 to be removed if the limit is 4. However, if the dictionary ends up with the 4 in position 0, that will be removed instead. Provided, of course, that you can actually index into the dictionary contents with a simple indexer (which you can't ordinarily.) – Lasse V. Karlsen Apr 28 '11 at 07:58
0

I dont know whether we can limit the dictionary size explicitly. Sometime back I wanted to do the same.

I solved the problem by writting one method which keeps checking the dictionary count manually and when its size exceeded beyond certain limit, it will remove some entries from dictionary.

Shekhar
  • 11,438
  • 36
  • 130
  • 186