【发布时间】:2012-03-03 10:38:18
【问题描述】:
在 C# 上下文中最好的想法是什么,
-
在 C# 中,我使用的是字典。我希望它使用更少的内存空间。什么会更好?
键类型为
Uint64或键类型为string的字典?在这两种情况下,值都是一个自定义类,每个字典都相同。我已将字典声明如下,
private static readonly Dictionary<string, List<Node>> HashTable = new Dictionary<string, List<Node>>();类节点定义如下,
public class Node { public UInt64 CurrentIndex { get; set; } public string NextHashedString { get; set; } public int NextHashPos { get; set; } }字符串的键实际上是一个字符串的哈希值,计算如下, 字符串的长度可以是 1 到 20 个字符。
static UInt64 CalculateHash(string read, bool lowTolerance) { UInt64 hashedValue = 0; int i = 0; while (i < read.Length) { hashedValue += read.ElementAt(i) * (UInt64)Math.Pow(31, i); if (lowTolerance) i += 2; else i++; } return hashedValue; }现在,我想将此哈希值存储为字典的键。什么是最好的主意。我用作 Uint64 或将其转换为字符串并将字符串用作字典键。 我的主要目标是字典使用最少的空间并且搜索键的时间更快。
我有一个包含 3571079 个字符的文件。我可以将整个文件读入字符串还是需要高级数据结构?
【问题讨论】:
-
您没有提供关于第一种情况的足够数据。能够在 UInt64 键或字符串键之间进行选择是不寻常的......
-
这个问题我不清楚...你能提供一些你想要达到的目标的例子吗?
-
@JonSkeet 我用 sn-ps 修改了问题。
-
@digEmAll 请查看修改后的问题。
-
好吧,你也可以更新问题标题,这对这种情况不是很有帮助。
标签: c# string data-structures dictionary uint64