8

I need a data structure that acts like a SortedDictionary<int, double> but is sorted based on the values rather than the keys. I need it to take about 1-2 microseconds to add and remove items when we have about 3000 items in the dictionary.

My first thought was simply to switch the keys and values in my code. This very nearly works. I can add and remove elements in about 1.2 microseconds in my testing by doing this.

But the keys have to be unique in a SortedDictionary so that means that values in my inverse dictionary would have to be unique. And there are some cases where they may not be.

Any ideas of something in the .NET libraries already that would work for me?

Michael Covelli
  • 2,113
  • 4
  • 27
  • 41
  • I'm assuming SortedList<> doesn't meet that performance? – Simon Buchan Apr 11 '10 at 23:07
  • Can you explain what the keys and values represent in your domain? – John Feminella Apr 11 '10 at 23:09
  • @Simon I think that SortedList<> is also sorted by keys. http://msdn.microsoft.com/en-us/library/system.collections.sortedlist.aspx – Michael Covelli Apr 12 '10 at 00:38
  • @John The integers represent stock identifiers. The doubles represent the current amount of risk that we have in each stock in dollars. I need to be able to iterate through these names in order of descending risk or ascending risk at any time. And there is some time-critical code that will update this so it really needs to be able 1-2 microseconds for the operations. – Michael Covelli Apr 12 '10 at 00:40
  • You might want to see [how-do-you-sort-a-dictionary-by-value](http://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value?lq=1) – nawfal May 22 '14 at 05:29

2 Answers2

2

You can sort SortedDictionary by value like this:

yourList.Sort(
    delegate(KeyValuePair<int, double> val1,
    KeyValuePair<int, double> val2)
    {
        return val1.Value.CompareTo(val2.Value);
    }
);
Hun1Ahpu
  • 3,315
  • 3
  • 28
  • 34
0

The PowerCollections library has a class called OrderedMultiDictionary<TKey, TValue> that is basically like a SortedDictionary<TKey, TValue> but allows duplicates. When you lookup a key, you get an enumerable instead of a single value.

The library is free and you should be able to do exactly what you want with that class - store the values as the keys.

Aaronaught
  • 120,909
  • 25
  • 266
  • 342
  • Wow, that's great! Let me download and see if it does 1-2 us real quick. – Michael Covelli Apr 11 '10 at 23:14
  • Note: if you don't want to use that library, you could always implement this yourself with a `SortedDictionary`, and simply store a `List` as the value instead of a single `T`. – Aaronaught Apr 11 '10 at 23:14
  • Add/Remove is about 30 microseconds or so for the PowerCollections library. Pretty close, but I'm not sure if its enough for this app. Thanks for the pointer though. The SortedDictionary> is also a good idea, let me see if I can time that. Another thought I had was to just use the SortedDictionary add a tiny random term of order .0000001 or so that would force my values to be unique but not affect the outcome. – Michael Covelli Apr 11 '10 at 23:58
  • @Michael: How exactly are you profiling this? AFAIK, that class uses pretty much the same algorithms as the `SortedDictionary`, it just stores lists as values. – Aaronaught Apr 12 '10 at 00:05
  • @Aaronaught I'm just creating 3000 random ints and doubles and then timing 100k trials of getting values, removing, and re-inserting them using a System.Diagnostics.Stopwatch. – Michael Covelli Apr 12 '10 at 00:41
  • @Aaronaught I agree, his implementation in PowerCollections looks like it uses red-black trees which are, I think, the same thing that the SortedDictionary uses. I think that when you get down to the 1-2 microsecond level though, little differences in the implementation start to matter rather than the O(log n) performance of the algorithm. – Michael Covelli Apr 12 '10 at 00:44
  • @Michael: How are you guaranteeing unique values for insertion in your test? Just trying to understand the benchmark here. – Aaronaught Apr 12 '10 at 03:20
  • @Aaronaught Actually, his OrderedBag meets my needs just fine. If you have 3000 items in the bag, it only takes 1 microsecond to get the first or last element and 1 microsecond to add (on average). Which is perfect. Thanks! – Michael Covelli Apr 12 '10 at 23:35