.NET SortedDictionary但排序方式价值观价值观、方式、NET、SortedDictionary

2023-09-02 21:37:30 作者:依赖

我需要一个数据结构,它就像一个 SortedDictionary< INT,双> 但基于价值而非键进行排序。我需要它需要大约1-2微秒添加和删除的项目,当我们在字典中大约3000个项目。

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.

我的第一个想法是简单地在我的code键和值切换。这非常接近的作品。我可以添加和做这个删除在我的测试中大约1.2微秒的元素。

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.

但按键都必须在一个SortedDictionary唯一的,这样意味着我的逆字典值必须是唯一的。并有一些情况下,他们可能不

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.

的东西在.NET库任何想法已经将工作适合我?

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

推荐答案

在 PowerCollections 库有一类叫做 OrderedMultiDictionary&LT; TKEY的,TValue&GT; ,基本上是像 SortedDictionary&LT; TKEY的,TValue&GT; ,但允许重复。当你查找的关键,你得到的不是一个值的枚举。

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.