2

我正在寻找一个专门的(和快速的)Int32/UInt32 排序映射(最好比 System.Collections.Generic.SortedDictionary 更快,其中 K 是 Int32 或 UInt32)。

它将被用作稀疏数组,.NET 是否有任何实现?

4

1 回答 1

1

正如评论中提到的,我会编写一个自定义集合,它同时使用 SortedDictionary 和常规 Dictionary 作为其后备存储。它使您的内存使用量加倍,但它是查找和迭代的最佳性能。修改会更慢,但听起来你最感兴趣的是快速访问。

public class DoubleDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
    private Dictionary<TKey, TValue> backingHash = new Dictionary<TKey, TValue>();
    private SortedDictionary<TKey, TValue> backingTree = new SortedDictionary<TKey, TValue>();

    // For all the modify methods, do it in both.
    // For all retrieval methods, pick one of the backing dictionaries, and just use that one.
    // For example, contains and get on the Hash, iteration on the Tree.
}
于 2011-03-25T19:59:13.187 回答