问题
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