在 C# 中,是否有开箱即用的方法来构建 3 路查找表?
我有一个内存中的“表”,可能看起来像这样:
Favorite# Name Profession
--------- ---------- ------------------
3 Names.Adam Profession.Baker
9 Names.Bob Profession.Teacher
7 Names.Carl Profession.Coder
7 Names.Dave Profession.Miner
5 Names.Fred Profession.Teacher
我想要做的是使用 3 个字段中的任何一个进行快速有效的查找。 换句话说,我想要:
myTable[3]
和myTable[Names.Adam]
和myTable[Professions.Baker]
全部返回 < code>{3,Names.Adam,Profession.Baker}myTable[Profession.Teacher]
返回两个{9,Names.Bob,Profession.Teacher}
和{5,Names.Fred,Profession.Teacher}
。
该表是在运行时根据用户的操作构建的,并且不能存储在数据库中,因为它用在无法保证数据库连接的部分中。
现在,我“简单地”(哈!)使用 3 个 uber-Dictionaries 存储它,每个 uber-Dictionaries 使用其中一列(FavoriteNumber、Name、Profession)进行键控,并且 uber-Dictionaries 中的每个值都包含 2 个字典,这些字典本身是键控的其余各列(因此“Name”超级词典中的值的类型为 Dictionary
和 Dictionary
这需要在 2 个字典中进行 2 次查找,并再次遍历一个数组(通常包含 1 或 2 个元素)。
任何人都可以提出更好的方法来执行此操作,因为该表我不介意花费额外的内存?可能很小(不超过 20 个条目),但我愿意牺牲一点 CPU 来使其代码更容易维护......
I have an in-memory "table" that might looks something like this:
Favorite# Name Profession
--------- ---------- ------------------
3 Names.Adam Profession.Baker
9 Names.Bob Profession.Teacher
7 Names.Carl Profession.Coder
7 Names.Dave Profession.Miner
5 Names.Fred Profession.Teacher
And what I want to do, is do quick and efficient lookups, using any of the 3 fields.
In other words, I want:
myTable[3]
andmyTable[Names.Adam]
andmyTable[Professions.Baker]
to all return{3,Names.Adam,Profession.Baker}
myTable[Profession.Teacher]
to return both{9,Names.Bob,Profession.Teacher}
and{5,Names.Fred,Profession.Teacher}
.
The table is built during runtime, according to the actions of the user, and cannot be stored in a database since it is used in sections in which database connectivity cannot be guaranteed.
Right now, I "simply" (hah!) store this using 3 uber-Dictionaries, each keyed using one of the columns (FavoriteNumber, Name, Profession), and each value in the uber-Dictionaries holding 2 Dictionaries which are themselves keyed with each of the remaining columns (so the values in the "Name" uber-dictionary are of the type Dictionary<FavoriteNumber,Profession[]>
and Dictionary<Profession, FavoriteNumber[]>
This requires 2 lookups in 2 Dictionaries, and another traverse of an array (which usually holds 1 or 2 elements.)
Can anyone suggest a better way to do this? I don't mind spending extra memory, since the table is likely to be small (no more than 20 entries) but I'm willing to sacrifice a little CPU to make it more readily maintainable code...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
但实际上并不是使用字典,但如果您创建这样的类集合,
则可以使用 LINQ 来搜索该集合。
或者,如果您更喜欢普通的 LINQ 语法,
这些
selectedPeople
变量中的每一个都将被键入为IEnumerable
,您可以使用循环来搜索它们。Not really however using a dictionary, but if you create a collection of classes like this
you can use LINQ to search the collections.
or if you prefer the normal LINQ syntax
Each of these
selectedPeople
variables will be typed asIEnumerable<Person>
and you can use a loop to search through them.对于 20 行,只需使用 线性扫描 - 它将在各个方面都是最有效的。
对于较大的套装; hzere 是一种使用 LINQ 的
ToLookup
和延迟索引的方法:For 20 rows, just use linear scanning - it will be the most efficient in every way.
For larger sets; hzere's an approach using LINQ's
ToLookup
and delayed indexing:我认为执行此操作的方法是编写自己的对象,其中
record 是保存元素的类。
在内部,您保留一个列表,每个索引器执行 List.FindAll() 来获取您需要的内容。
I think the way to do this is to write your own object that has
where record is a class that holds your elements.
Internally, you keep a List and each indexer does List.FindAll() to get what you need.
没有任何开箱即用的东西(可能除了数据表)。 不过,它可以通过比您所拥有的更简单的方式来完成:
创建一个类来保存数据:
然后保留 3 个指向同一引用的字典:
我建议将所有这些封装到一个外观类中隐藏实现细节。
Nothing out-of-the-box (except perhaps a DataTable). Nevertheless, it can be accomplished in a more simple way that what you've got:
Create a class to hold the data:
Then keep 3 dictionaries that point to the same reference:
I'd recommend encapsulating all of this into a facade class that hides the implementation details.
您可以使用 sqlite 数据库作为支持吗? 使用 sqlite,您甚至可以选择构建内存数据库。
Could you use an sqlite database as the backing? With sqlite you even have the option of building an in-memory db.