高效的 Int32/Uint32 排序映射/稀疏数组

发布于 2024-10-27 01:37:04 字数 145 浏览 11 评论 0原文

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

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

I'm looking for a specialized (and fast) Int32/UInt32 sorted map (that preferably is faster then System.Collections.Generic.SortedDictionary where K is either Int32 or UInt32).

It's going to be used as a sparse array, are there any implementations for .NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个少女永远怀春 2024-11-03 01:37:04

正如评论中提到的,我会编写一个自定义集合,它使用 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.
}

As was mentioned in the comments, I'd write a custom collection that uses both a SortedDictionary and a regular Dictionary as its backing store. It doubles your memory usage, but it's the best performance for lookups and iteration. Modifications will be slower, but it sounds like you're mostly interested in fast accesses.

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文