I’m trying to use .NET 4’s SortedSet<T> collection. It seems to have everything I need minus a couple things.
Q: I want to be able to fetch all elements lower or higher in comparison to a given value. In Java’s TreeSet, there are methods named tailSet and headSet, which perform these operations. I’d like to be able to do the same with SortedSet<T>. The closest I can find is GetViewBetween. However, what if I wanted to use SortedSet with string? There is no max value of string that I know of, yet I need to give the method an upper and lower bounds.
How could I mimic the behavior of tailSet and headSet using SortedSet? Considering the implementation of SortedSet, I’d think that these would be very easy methods to implement.
Thanks!
I believe you can emulate them like this:
sortedSet.GetViewBetween(start, sortedSet.Max)sortedSet.GetViewBetween(sortedSet.Min, end)Alternately, you can use LINQ:
The primary difference is that
GetViewBetweengives you an object with a pointer to the original set, so any changes in the original set can be reflected in the copies. The LINQ version creates a new set based on the contents of the original, giving copies that don’t track each other. Of course, you could also do something likenew SortedSet<T>(set.GetViewBetween(set.Min, end))to get the cloning behavior.