Skip to content

[API Proposal]: SortedSet<T>.GetAlternateLookup() #108642

@terrajobst

Description

@terrajobst

Background and motivation

We added GetAlternateLookup() to many collection types, including Dictionary<TKey, TValue> and HashSet<T>. We should add it to SortedSet<T> as well, for symmetry. In my case, I often use SortedSet<string> over HashSet<T> because I'm persisting data to disk and I'd like to have a stable order to make diffs less jarring.

API Proposal

namespace System.Collections.Generic;

public partial class SortedSet<T>
{
    public readonly struct AlternateLookup<TAlternate>
    {
        public SortedSet<T> Set { get; }
        public bool Add(TAlternate item);
        public bool Contains(TAlternate item);
        public bool Remove(TAlternate item);
        public bool TryGetValue(TAlternate equalValue, [MaybeNullWhen(false)] out T actualValue);
    }

    public AlternateLookup<TAlternate> GetAlternateLookup<TAlternate>();
    public bool TryGetAlternateLookup<TAlternate>(out AlternateLookup<TAlternate> lookup);
}

API Usage

var set = new SortedSet<string>() { "Immo", "Landwerth" };
var lookup = set.GetAlternateLookup<ReadOnlySpan<char>>();

var span = "Immo".AsSpan();
lookup.Contains(span);

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions