views:

248

answers:

2

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?

+1  A: 

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
Wow, that's great! Let me download and see if it does 1-2 us real quick.
Michael Covelli
Note: if you don't want to use that library, you could always implement this yourself with a `SortedDictionary`, and simply store a `List<T>` as the value instead of a single `T`.
Aaronaught
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<double, List<int>> is also a good idea, let me see if I can time that. Another thought I had was to just use the SortedDictionary<double, int> 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
@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
@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
@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
@Michael: How are you guaranteeing unique values for insertion in your test? Just trying to understand the benchmark here.
Aaronaught
@Aaronaught Actually, his OrderedBag<T> 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
+1  A: 

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
Innovative. I like it.
Atømix