简体   繁体   中英

Data structure in .NET that stores items as sorted and unique and queryable by range (and is not SortedSet)

The scenario is a timeline of events, that I want to be able to query for all items within a specific date range.

I am looking for a data structure in .NET (up to v4.0) that stores items as sorted and unique (for example, by using a comparer or a unique key). It should support adding/deleting at no more than logarithmic complexity, and performing binary search at that complexity as well.

System.Collections.Generic.SortedSet seemed like what I wanted, but its GetViewBetween() method returns an inclusive list of items, as a SortedSet.

I am missing two things in it:

  1. Calling ToList() or enumerating the SortedSet is too expensive, as the list is long. I need the method return a List<T> , not a SortedSet<T> .
  2. Calling it with an inclusive/exclusive date range, to my choice - not possible and I need that too.

If you know a good library that contains such a data structure, that is tested and familiar, I would sure like to try it.

Thanks.

After a bit of reading, it would seem that SortedList or SortedDictionary is what you need. SortedList appears to use less memory and is technically a binary tree, where SortedDictionary is faster with unsorted data. Beyond that, they are very close cousins.

Here's a good question/answer on the differences: SortedList vs. SortedDictionary vs. Sort()

The problem as I see it is that you want a data structure organized on two axes - your unique key and by time. If you can trade space for time, I'd suggest different data structures (wrapped in your own class to ensure consistency) for each. You might want to use a SortedList to keep track of your key-oriented data. I believe it's based on a Red-Black Tree and should have the characteristics you desire for key-search. Alternatively, if you don't need them ordered by key, you could use a simple Dictionary.

To support date-based search, you might want to have a B-tree (one implementation, note I haven't tested it: http://blog.daisley-harrison.com/blog/post/NET-Generic-BTree-Library-and-Source-Code.aspx ) keyed by date. Make sure that it supports duplicate keys, however, since these might not be unique. It can contain either a copy of the data or simply the key associated with that timestamp.

All of these structures have log(n), or better, complexity for search. Listing of items between two dates should be pretty efficient, with the best performance coming with the B-Tree/Dictionary combination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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