在 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 个字符的文件。我可以将整个文件读入字符串还是需要高级数据结构?