简体   繁体   English

使用O(1)搜索时间在Collection中进行多重索引

[英]Multi Indexed in Collection with O(1) searching time

Suppose I want to make one Collection of Class which have 10 properties, This collection holds about 10 million items. 假设我想制作一个具有10个属性的Class Collection,这个集合包含大约1000万个项目。

Now I want to search this collection with O(1) time complexity or near to O(1) by any property of Class.(Not by only one property ie ID or Name) 现在我想用O(1)时间复杂度搜索这个集合,或者通过Class的任何属性来搜索O(1)。(不仅仅是一个属性,即ID或Name)

If I use List than it by LINQ query it will require O(n) time complexity, So it can't be used. 如果我使用List而不是LINQ查询它将需要O(n)时间复杂度,所以它不能被使用。

C# have dictionary which can be indexed by only one key type. C#有字典,只能用一种密钥类型索引。 So it also can't be used. 所以它也不能使用。

As one solution I can make 10 Dictionaries indexed with every property, but this solution will require large amount of memory as it has 10 million items. 作为一个解决方案,我可以使用每个属性索引10个词典,但是这个解决方案需要大量内存,因为它有1000万个项目。 So it would be not feasible. 所以这是不可行的。

PS I want only in memory solution (No database) and the collection can be searched by any single property of class (eg. MyCollection[2] or MyCollection["John"] or MyCollection["12/12/2013"] etc) and the searching time must be near to O(1). PS我只想在内存解决方案(没有数据库)和集合可以被类的任何单个属性搜索(例如,MyCollection [2]或MyCollection [“John”]或MyCollection [“12/12/2013”​​]等)搜索时间必须接近O(1)。

So how can I implement this kind of data structure?? 那么我该如何实现这种数据结构呢?

You cannot. 你不能。 You need to make a trade of between memory or time. 你需要在记忆或时间之间进行交易。 Either create your 10 different indexes (ie 10 internal dictionaries) to get O(1) lookup time or do linear searches to prevent any increase in the memory footprint. 创建10个不同的索引(即10个内部词典)以获得O(1)查找时间或进行线性搜索以防止内存占用增加。

Those are your choices. 这些是你的选择。 There is no magic bullet that gives you the best of both here. 没有任何神奇的子弹可以让你在这里得到最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM