not_a_commie said:
The new SortedSet<T> class in .NET 4 rejects inserts when
IComparible.CompareTo(..) == 0. That's lame. The two Ordered
collections in PowerCollections behave the same way. None of those
three are actually a HashSet. Is anyone aware of an ordered HashSet
implementation?
I'm unaware that such a thing even could exist, at least without major
hoop-jumping under the covers. The underlying structure (buckets) of a hash
table (and presumably of a hash set) is optimized for random access but is not
ordered by key. If you want both random AND key-ordered sequential access you
need a SortedList, SortedSet, Dictionary, etc. Their underlying structure (best
case a tree) permits sequential access as well as random. A HashSet is
IEnumerable but I would think that you could not expect a key-sorted order to
result if it is truly based on a hashing algorithm.
It's unclear how any of that relates to your IComparable complaint, though. If
you mean that duplicate keys are not allowed, that is also true for hash tables
and dictionaries of pretty much all kinds, AFAIK. You can always force a unique
compound key by appending, e.g., a timestamp or sequence number, to your
"natural" key. Of course you would have to re-implement IComparable so as to
account for that.
HTH,
-rick-