Skip to content

[API Proposal]: Allow lookups using alternative key on SortedSet<T> and SortedDictionary<T> #101694

Closed

Description

@PaulusParssinen

Background and motivation

In #27229 APIs that allow lookups by "alternate" key which behavior is described by interface IAlternateEqualityComparer<in TAlternate, T> where TAlternate : allow ref struct were approved. This allows allocation-free lookups using ref structs such as ROS<T>! However, this API was only approved for the hash-based Dictionary/Set types that take an IEqualityComparer and as such the API is incompatible with the sorted Dictionary/Set types such as SortedSet<T> and SortedDictionary<T> which take an IComparer instead.

To allow allocation-free lookups on SortedSet<T> and SortedDictionary<T>, I propose adding IAlternateComparer<in TAlternate, T> interface and set of alternate lookup APIs that enable this.

API Proposal

namespace System.Collections.Generic
{
    public interface IAlternateComparer<in TAlternate, T> where TAlternate : allow ref struct
    {
        int Compare(TAlternate alternate, T other);
        T Create(TAlternate alternate);
    }

    public static partial class CollectionExtensions
    {
        public static SortedDictionary<TKey, TValue>.AlternateLookup<TAlternateKey> GetAlternateLookup<TKey, TValue, TAlternateKey>(
            this SortedDictionary<TKey, TValue> dictionary)
            where TKey : notnull where TAlternateKey : allow ref struct;
        public static bool TryGetAlternateLookup<TKey, TValue, TAlternateKey>(
            this SortedDictionary<TKey, TValue> dictionary,
            out SortedDictionary<TKey, TValue>.AlternateLookup<TAlternateKey> lookup)
            where TKey : notnull where TAlternateKey : allow ref struct;

        public static SortedSet<T>.AlternateLookup<TAlternate> GetAlternateLookup<TAlternate>(
            this SortedSet<T> set)
            where TAlternate : allow ref struct;
        public static bool TryGetAlternateLookup(
            this SortedSet<T> set, out SortedSet<T>.AlternateLookup<TAlternate> lookup)
            where TAlternate : allow ref struct;
    }

    public class SortedDictionary<TKey, TValue>
    {
        public readonly struct AlternateLookup<TAlternateKey> where TAlternateKey : allow ref struct
        {
            public bool ContainsKey(TAlternateKey key);
            public bool TryAdd(TAlternateKey key, TValue value);
            public bool TryGetValue(TAlternateKey key, [MaybeNullWhen(false)] out TValue value);
            public bool TryGetValue<TKey, TValue, TAlternateKey>(TAlternateKey key, [NotNullWhen(true)] out TKey? actualKey, [MaybeNullWhen(false)] out TValue value);
            public bool Remove(TAlternateKey key);
            public bool Remove(TAlternateKey  key, [MaybeNullWhen(false)] out TValue value));
        }
    }

    public class SortedSet<T>
    {
        public readonly struct AlternateLookup<TAlternate>
        {
            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 virtual SortedSet<T> GetViewBetween(TAlternate lowerValue, TAlternate upperValue);
        }
    }
}

namespace System.Runtime.InteropServices
{
    public static class CollectionsMarshal
    {
        public static ref TValue? GetValueRefOrAddDefault<TKey, TValue, TAlternateKey>(
            SortedDictionary<TKey, TValue>.AlternateLookup<TAlternateKey> dictionary,
            TAlternateKey key, out bool exists)
            where TKey : notnull where TAlternateKey : allow ref struct;
        public static ref TValue GetValueRefOrNullRef<TKey, TValue, TAlternateKey>(
            SortedDictionary<TKey, TValue>.AlternateLookup<TAlternateKey> dictionary,
            TAlternateKey key)
            where TKey : notnull where TAlternateKey : allow ref struct;
    }
}

API Usage

SortedSet<string>.AlternateLookup<ReadOnlySpan<char>> d = ...;
ReadOnlySpan<char> slice = ...;
d.TryAdd(slice, value);

Alternative Designs

No response

Risks

No response

Activity

  1. added
    api-suggestion
    on Apr 29, 2024
  2. added
    untriaged
    on Apr 29, 2024
  3. ghost added
    needs-area-label
    on Apr 29, 2024
  4. added and removed
    needs-area-label
    on Apr 29, 2024
  5. eiriktsarpalis commented on May 9, 2024

    @eiriktsarpalis
    Member

    What's the use case? These collections are slower when compared to hash table based implementations (both in terms of asymptotic complexity and the allocations incurred by populating the search tree), so any application requiring ROS based lookups would surely be performance sensitive enough to make use of Sorted* collections prohibitive.

  6. added
    needs-author-action
    on May 9, 2024
  7. PaulusParssinen commented on May 9, 2024

    @PaulusParssinen
    ContributorAuthor

    https://redis.io/glossary/redis-sorted-sets/ I guess.

    But we're giving up on BCL's implementation of sorted set in Garnet anyways and are looking to implement more specialized data structure for our needs anyways.

    So it was worth a try.

  8. removed
    needs-author-action
    on May 9, 2024
  9. eiriktsarpalis commented on May 9, 2024

    @eiriktsarpalis
    Member
  10. PaulusParssinen commented on May 9, 2024

    @PaulusParssinen
    ContributorAuthor

    Oh right.. I guess there is no ask for this then so I'll close it 👍

  11. removed
    untriaged
    on May 9, 2024
  12. locked and limited conversation to collaborators on Jun 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Collections

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions