Open Ilchert opened 1 month ago
Tagging subscribers to this area: @dotnet/area-system-collections See info in area-owners.md if you want to be subscribed.
I'm super confused about the "Try" in the TryAdd
method. Does this have a "failure state" that the "Try" is avoiding? When would adding an element fail? A void
TryX
method that doesn't also avoid some sort of exception makes zero sense to me.
Maybe I completely misunderstood the intended semantics. Apologies if so.
Hello @julealgon , thanks for noticing. It was a missprint, it should return bool like original one.
Addings fails with duplicates https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Collections/src/System/Collections/Generic/SortedList.cs#L184-L185
it should return bool like original one.
Addings fails with duplicates
Ahh that makes a lot more sense! Thanks for clarifying @Ilchert .
Background and motivation
This proposal is similar to OrderedDictionary.TryAdd https://github.com/dotnet/runtime/issues/107947
The
CollectionsMarshal.GetValueRefOrAddDefault
method for dictionaries provides a powerful mechanism for adding or updating a dictionary entry using only one lookup. TheSortedList<TKey, TValue>
collection would similarly benefit from a such an API, however we don't need to make it as unsafe; simply knowing the int-based index of an entry makes it possible to very cheaply look up, look up nearest value and update its value using theGetKeyAtIndex/GetValueAtIndex
andSetValueAtIndex
methods, respectively.API Proposal
API Usage
Alternative Designs
Alternate solution is exposing keys array from SortedList and use binary search directly.
Usage
Risks
No response