4

I have a requirement of using a dictionary in the project but as we know that they are only accessible using the keys and not using the indexes, and I want to access the items in dictionary using indexes. So I fiddle over the web and found out about OrderedDictionary as they are accessible using both the indexes and keys but they have some performance issue and as I am reading/writing the dictionary every minute of the day so it's not a good idea to use OrderedDictionary.

So lastly my question in here is that is there any alternative available which gives me functionality of Dictionary and I can also access it using indexes and doesn't cause a performance issue.

Community
  • 1
  • 1
Safran Ali
  • 4,477
  • 9
  • 40
  • 57
  • 12
    You know that you can call ElementAt(index) on the Dictionary? – Dominik Sep 02 '11 at 10:59
  • 8
    How is reading/writing every minute to the dictionary a performance issue? – tzup Sep 02 '11 at 10:59
  • 1
    Do you want to access them by index or just iterate over all items? Otherwise you could use the [Values](http://msdn.microsoft.com/en-us/library/ekcfxy3x.aspx) property. – riezebosch Sep 02 '11 at 11:03
  • If you want to access the data using index that means you want it to return the data in same order as it was inserted. is that the case? – Ankur Sep 02 '11 at 11:06
  • Why do you need to access by index? – Jodrell Sep 02 '11 at 11:14
  • @SAli, you´re welcome just have in mind, that even if you can access a certain position in the dictionary, I´m not sure that the dictionary implementation will hold added items in the order they were added. – Dominik Sep 02 '11 at 11:15
  • 1
    @Dominik Not sure how useful ElementAt(index) is here. It's a Linq extension that works best with stable sequences, and also requires iterating over a sequence from the beginning until "index" is reached (perhaps there are optimizations for underlying sequences that are in fact indexable). Enumerating over a dictionary will not give the insert order, plus dictionaries will get re-organized to maintain load factor, meaning the ordering is not stable. – Tim Lloyd Sep 02 '11 at 11:19
  • @tzup I have more than 100 items updated in the dictionary every minute and as it says here in this post http://stackoverflow.com/questions/2565455/what-is-the-complexity-of-ordereddictionary that OrderedDictionary should be used when read/writes are minimal ... – Safran Ali Sep 02 '11 at 11:20
  • 1
    @S Ali A hundred updates a minute is practically nothing. – Tim Lloyd Sep 02 '11 at 11:21
  • @chibacity: I agree, I commented since I was under the impression the OP explicitly stated that he wanted to be able to access the index, possible he has a valid reason to do so. – Dominik Sep 02 '11 at 11:27
  • 1
    @Dominik Sure, I'm adding the fact that the ordering can also change, so `ElementAt(index)` is also unreliable. I don't think it can be used at all. – Tim Lloyd Sep 02 '11 at 11:30
  • Off topic: Are you sure you will have a performance issue with OrderedDictionary? Just because its not the fastest implementation does not mean it wouldn't be *good enough* ? – Neil Fenwick Sep 02 '11 at 11:40
  • @chibacity: Agreed agin, it may be unreliable, yet the OP didn´t state what he is actually trying to achive, so I think mentioning it is valid. – Dominik Sep 02 '11 at 12:02
  • @Dominik Mentioning it is valid, I'm just not so sure about the 11 upvotes and counting given the issues! :) – Tim Lloyd Sep 02 '11 at 12:13

3 Answers3

6

SortedList<TKey, TValue> has a property, Values, that is an IList<TValue>. Is it enough? It's fast only for small "sets" of elements. The difference with SortedDictionary is here http://msdn.microsoft.com/en-us/library/5z658b67(v=vs.80).aspx

Can I ask you why you want to access it "by index"? You can still enumerate it with foreach, you know?

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • 1
    The index isn't "stable". If you insert B and then insert A, the index of A will be 0, the index of B will be 1. The only collection that maintain insertion order is `OrderedDictionary` (http://stackoverflow.com/questions/2722767/c-order-preserving-data-structures) – xanatos Sep 02 '11 at 11:39
  • Old question, but if you always want to remove the first item then you may want System.Collections.Generic.Queue. – amonroejj Nov 14 '19 at 16:56
3

Try SortedDictionary http://msdn.microsoft.com/en-us/library/f7fta44c.aspx

Ankit Dass
  • 521
  • 5
  • 7
3

in response to your comment that you are only expecting a hundred updates a minute, this is very little work - practically nothing. You can still use an OrderedDictionary, performance will not be an issue for you.

Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
  • I agree with your answer that 100 items is pratically nothing ... but 100 itemas inlcude signup, login, error, pagevisits etc counts that can be any number of read and write in the dictionary ... and I was not arguing that OrderedDictionary will cause a performance issue ... I wrote it in my question but I also mentioned the post where I read it ... I hope I clearify my concerns – Safran Ali Sep 03 '11 at 08:42