Multi Indexed in Collection with O(1) searching time

不羁岁月 提交于 2020-01-17 07:40:50

问题


Suppose I want to make one Collection of Class which have 10 properties, This collection holds about 10 million items.

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 i.e ID or Name)

If I use List than it by LINQ query it will require O(n) time complexity, So it can't be used.

C# have dictionary which can be indexed by only one key type. 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. So it would be not feasible.

P.S. 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).

So how can I implement this kind of data structure??


回答1:


You cannot. You need to make a trade of between memory or time. Either create your 10 different indexes (i.e. 10 internal dictionaries) to get O(1) lookup time or do linear searches to prevent any increase in the memory footprint.

Those are your choices. There is no magic bullet that gives you the best of both here.



来源:https://stackoverflow.com/questions/19389097/multi-indexed-in-collection-with-o1-searching-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!