-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: System.Collections.Generic.SortedList<TKey, TValue> should have SetByIndex(Int32,TValue) #58962
Comments
Tagging subscribers to this area: @eiriktsarpalis Issue DetailsBackground and motivationThere is no way to set the value of an entry by index in the generic I'm rather surprised nobody has mentioned this here even though there is interest over on StackOverflow. API Proposal
API Usage
RisksNo response
|
Updating the value of a dictionary without specifying the key feels a bit error-prone to me. That being said, I understand the motivation and there is precedent in the non-generic collection. @stephentoub should we be considering this? |
It might be the practical use of this is only applicable to a class override to further extend the functionality, which is my use case. So maybe Also, this class differs from a traditional dictionary in that it provides an indexed order as well. This is important functionality to the design of the class. If I didn't care about the index, then I would just use a dictionary. |
We can investigate potentially marking as |
@rhaokiel ping |
My apologies for the delay. @eiriktsarpalis, I've updated the proposal to use |
I think this is ok, we can put it up for review. |
namespace System.Collections.Generic
{
public partial class SortedList<TKey, TValue>
{
public TKey GetKeyAtIndex(int index);
public TValue GetValueAtIndex(int index);
public void SetValueAtIndex(int index, TValue value);
}
} |
@rhaokiel would you be interested in contributing an implementation for the approved API? |
@eiriktsarpalis sure. I'm not very sure on the etiquette though. Do I submit a PR at this point? I believe the following changes would satisfying the approved API: namespace System.Collections.Generic
{
public class SortedList<TKey, TValue>
{
+ public TKey GetKeyAtIndex(int index)
- private TKey GetKey(int index)
{ ... }
+ public TValue GetValueAtIndex(int index)
- private TValue GetByIndex(int index)
{ ... }
+ public void SetValueAtIndex(int index, TValue value)
+ {
+ if (index < 0 || index >= _size)
+ throw new ArgumentOutOfRangeException(nameof(index), index, SR.ArgumentOutOfRange_Index);
+ values[index] = value;
+ version++;
+ }
public sealed class KeyList : IList<TKey>, ICollection
{
public TKey this[int index]
{
get
{
+ return _dict.GetKeyAtIndex(index);
- return _dict.GetKey(index);
public sealed class ValueList : IList<TValue>, ICollection
{
public TValue this[int index]
{
get
{
+ return _dict.GetValueAtIndex(index);
- return _dict.GetByIndex(index); |
Yes feel free to submit a PR, public API diff should match what was approved in #58962 (comment) |
@eiriktsarpalis, can this be closed now? |
Background and motivation
There is no way to set the value of an entry by index in the generic
SortedList<TKey, TValue>
like there is in the non-generic version. This causes code to incur a BinarySearch cost (log n) to set the value when the index is already known.I'm rather surprised nobody has mentioned this here even though there is interest over on StackOverflow (4 years old).
API Proposal
(Updated to reflect API Review's conclusions)
Usage Examples
Allows an overriding class to handle complicated scenarios
Such as this example in which a binary search must be performed before deciding between a set or remove operation.
Risks
The index of a particular key could change via another thread between acquisition and use. However, none of the other currently implemented methods in
SortedList<TKey, TValue>
take precautions to make the class thread-safe.The text was updated successfully, but these errors were encountered: