Missing indexing methods for SortedDictionary?

I have a fairly standard data structure need: a sorted collection with fast insert, delete that can be accessed efficiently by key and by index.

The SortedList has O(n) insertion so the closest is the SortedDictionary but it has no means of indexing. Why not This should be the difference over a standard Dictionary that you can easily perform operations like head and tail. I would expect the method RemoveAt, GetAt etc like the SortedList.

The SortedDictioary.ValueCollection seems next to useless. You can foreach it but not index it.

Am I missing something

thanks in advance,
Duncan


Answer this question

Missing indexing methods for SortedDictionary?

  • Ant999

    No, you're not missing anything, there isn't an index on a SortedDictionary, nor is there one on the value collection. You can of course keep both a SortedList and a SortedDictionary, but then you have the added onus of having to update two collections. I recently ran a lot of benchmarks on many of the collection classes, and found that SortedList really is unusable unless you are inserting data in sequential order, though, (I was surprised to find that the Dictionary<> was by far the fastest)

     


  • sinankoylu

    Its pretty ***. I don't think it deserves to be called 'Sorted' if you can't perform basic operations on the order in constant time. Seems there is a missing hole in the collections that .net provides.

    If you still have them, post your benchmarks, they would be interesting to see.

    Thanks,
    Duncan

  • Zigzag

    I suspect that indexing is not available because of concurrency. What good is knowing an index position if, before you execute even a single byte of code after obtaining it, items can have been inserted, possibly invalidating it Sychronization for this case requires the calling code's involvement/participation, which starts to complicate the interface.

    I agree that it's kind of lame to penalize those who would be able to provide their own outer synchronization code, in order to protect others from burning themselves.

    Glenn


  • Missing indexing methods for SortedDictionary?