没有“有效负载”的字典; .Net 中的价值

发布于 2024-08-19 07:38:10 字数 271 浏览 1 评论 0原文

有时,我需要检查一组值中是否有重复的 ID,通常我会使用字典来执行此操作 - 仅使用键并将值保留为空。

请注意,这是严格且高度优化的代码,因此请不要抱怨“过早优化”!假设 CPU 和 RAM 被挤压到极限的场景,我想收集关于更优化解决方案的意见;大概像 Lookup 类这样的东西会避免不必要的 RAM 分配,因此会稍微快一些。 BCL 中是否存在第三方此类或我忽略的某些类?

我知道谷歌已经发布了快速和紧凑字典类的代码 - 也许其中有一些东西可以移植到 C#/.Net?

谢谢。

Occassionally I need to check for duplicate IDs in a set of values and generally I use a Dictionary for this - using just the keys and leaving values empty.

Note that this is tight and highly optimized code so please no cries of 'premature optimization'! Assuming scenarios where CPU and RAM are being squeezed to the limit I was wanting to gather opinions on more optimal solutions; presumably something like a Lookup class would avoid unnecessary RAM allocations and would thus be slightly faster. Are there such classes either third party or perhaps some class I've overlooked in the BCL?

I understand google have released code for both fast and compact dictionary classes - perhaps there's something in there that could be ported to C#/.Net?

Thanks.

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

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

发布评论

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

评论(1

星星的軌跡 2024-08-26 07:38:10

使用 .NET 3.5 中的 HashSet 类

HashSet<int> set = new HashSet<int>() { 1, 2, 3 };
set.Add(5);
for (int index = 0; index < 10; index++)
{
    Console.WriteLine("{0} : {1}", index, set.Contains(index));
}

Use the HashSet class in .NET 3.5.

HashSet<int> set = new HashSet<int>() { 1, 2, 3 };
set.Add(5);
for (int index = 0; index < 10; index++)
{
    Console.WriteLine("{0} : {1}", index, set.Contains(index));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文