|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | + |
| 9 | +namespace System.Collections.Frozen |
| 10 | +{ |
| 11 | + public abstract partial class FrozenDictionary<TKey, TValue> |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Gets an instance of a type that may be used to perform operations on a <see cref="FrozenDictionary{TKey, TValue}"/> |
| 15 | + /// using a <typeparamref name="TAlternateKey"/> as a key instead of a <typeparamref name="TKey"/>. |
| 16 | + /// </summary> |
| 17 | + /// <typeparam name="TAlternateKey">The alternate type of a key for performing lookups.</typeparam> |
| 18 | + /// <returns>The created lookup instance.</returns> |
| 19 | + /// <exception cref="InvalidOperationException">This instance's comparer is not compatible with <typeparamref name="TAlternateKey"/>.</exception> |
| 20 | + /// <remarks> |
| 21 | + /// This instance must be using a comparer that implements <see cref="IAlternateEqualityComparer{TAlternateKey, TKey}"/> with |
| 22 | + /// <typeparamref name="TAlternateKey"/> and <typeparamref name="TKey"/>. If it doesn't, an exception will be thrown. |
| 23 | + /// </remarks> |
| 24 | + public AlternateLookup<TAlternateKey> GetAlternateLookup<TAlternateKey>() where TAlternateKey : notnull, allows ref struct |
| 25 | + { |
| 26 | + if (!TryGetAlternateLookup(out AlternateLookup<TAlternateKey> lookup)) |
| 27 | + { |
| 28 | + ThrowHelper.ThrowIncompatibleComparer(); |
| 29 | + } |
| 30 | + |
| 31 | + return lookup; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets an instance of a type that may be used to perform operations on a <see cref="FrozenDictionary{TKey, TValue}"/> |
| 36 | + /// using a <typeparamref name="TAlternateKey"/> as a key instead of a <typeparamref name="TKey"/>. |
| 37 | + /// </summary> |
| 38 | + /// <typeparam name="TAlternateKey">The alternate type of a key for performing lookups.</typeparam> |
| 39 | + /// <param name="lookup">The created lookup instance when the method returns true, or a default instance that should not be used if the method returns false.</param> |
| 40 | + /// <returns>true if a lookup could be created; otherwise, false.</returns> |
| 41 | + /// <remarks> |
| 42 | + /// This instance must be using a comparer that implements <see cref="IAlternateEqualityComparer{TAlternateKey, TKey}"/> with |
| 43 | + /// <typeparamref name="TAlternateKey"/> and <typeparamref name="TKey"/>. If it doesn't, the method will return false. |
| 44 | + /// </remarks> |
| 45 | + public bool TryGetAlternateLookup<TAlternateKey>(out AlternateLookup<TAlternateKey> lookup) where TAlternateKey : notnull, allows ref struct |
| 46 | + { |
| 47 | + // The comparer must support the specified TAlternateKey. If it doesn't we can't create a lookup. |
| 48 | + // Some implementations where TKey is string rely on the length of the input and use it as part of the storage scheme. |
| 49 | + // That means we can only support TAlternateKeys that have a length we can check, which means we have to special-case |
| 50 | + // it. Since which implementation we pick is based on a heuristic and can't be predicted by the consumer, we don't |
| 51 | + // just have this requirement in that one implementation but for all implementations that might be picked for string. |
| 52 | + // As such, if the key is a string, we only support ReadOnlySpan<char> as the alternate key. |
| 53 | + if (Comparer is IAlternateEqualityComparer<TAlternateKey, TKey> && |
| 54 | + (typeof(TKey) != typeof(string) || typeof(TAlternateKey) == typeof(ReadOnlySpan<char>))) |
| 55 | + { |
| 56 | + lookup = new AlternateLookup<TAlternateKey>(this); |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + lookup = default; |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary>Gets the <see cref="Comparer"/> as an <see cref="IAlternateEqualityComparer{TAlternate, T}"/>.</summary> |
| 65 | + /// <remarks>This must only be used when it's already been proven that the comparer implements the target interface.</remarks> |
| 66 | + private protected IAlternateEqualityComparer<TAlternateKey, TKey> GetAlternateEqualityComparer<TAlternateKey>() where TAlternateKey : notnull, allows ref struct |
| 67 | + { |
| 68 | + Debug.Assert(Comparer is IAlternateEqualityComparer<TAlternateKey, TKey>, "Must have already been verified"); |
| 69 | + return Unsafe.As<IAlternateEqualityComparer<TAlternateKey, TKey>>(Comparer); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Provides a type that may be used to perform operations on a <see cref="FrozenDictionary{TKey, TValue}"/> |
| 74 | + /// using a <typeparamref name="TAlternateKey"/> as a key instead of a <typeparamref name="TKey"/>. |
| 75 | + /// </summary> |
| 76 | + /// <typeparam name="TAlternateKey">The alternate type of a key for performing lookups.</typeparam> |
| 77 | + public readonly struct AlternateLookup<TAlternateKey> where TAlternateKey : notnull, allows ref struct |
| 78 | + { |
| 79 | + /// <summary>Initialize the instance. The dictionary must have already been verified to have a compatible comparer.</summary> |
| 80 | + internal AlternateLookup(FrozenDictionary<TKey, TValue> dictionary) |
| 81 | + { |
| 82 | + Debug.Assert(dictionary is not null); |
| 83 | + Debug.Assert(dictionary.Comparer is IAlternateEqualityComparer<TAlternateKey, TKey>); |
| 84 | + Dictionary = dictionary; |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary>Gets the <see cref="FrozenDictionary{TKey, TValue}"/> against which this instance performs operations.</summary> |
| 88 | + public FrozenDictionary<TKey, TValue> Dictionary { get; } |
| 89 | + |
| 90 | + /// <summary>Gets or sets the value associated with the specified alternate key.</summary> |
| 91 | + /// <param name="key">The alternate key of the value to get or set.</param> |
| 92 | + /// <value> |
| 93 | + /// The value associated with the specified alternate key. If the specified alternate key is not found, a get operation throws |
| 94 | + /// a <see cref="KeyNotFoundException"/>, and a set operation creates a new element with the specified key. |
| 95 | + /// </value> |
| 96 | + /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> |
| 97 | + /// <exception cref="KeyNotFoundException">The alternate key does not exist in the collection.</exception> |
| 98 | + public TValue this[TAlternateKey key] |
| 99 | + { |
| 100 | + get |
| 101 | + { |
| 102 | + ref readonly TValue valueRef = ref Dictionary.GetValueRefOrNullRefCore(key); |
| 103 | + if (Unsafe.IsNullRef(in valueRef)) |
| 104 | + { |
| 105 | + ThrowHelper.ThrowKeyNotFoundException(); |
| 106 | + } |
| 107 | + |
| 108 | + return valueRef; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary>Determines whether the <see cref="FrozenDictionary{TKey, TValue}"/> contains the specified alternate key.</summary> |
| 113 | + /// <param name="key">The alternate key to check.</param> |
| 114 | + /// <returns><see langword="true"/> if the key is in the dictionary; otherwise, <see langword="false"/>.</returns> |
| 115 | + /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> |
| 116 | + public bool ContainsKey(TAlternateKey key) => |
| 117 | + !Unsafe.IsNullRef(in Dictionary.GetValueRefOrNullRefCore(key)); |
| 118 | + |
| 119 | + /// <summary>Gets the value associated with the specified alternate key.</summary> |
| 120 | + /// <param name="key">The alternate key of the value to get.</param> |
| 121 | + /// <param name="value"> |
| 122 | + /// When this method returns, contains the value associated with the specified key, if the key is found; |
| 123 | + /// otherwise, the default value for the type of the value parameter. |
| 124 | + /// </param> |
| 125 | + /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception> |
| 126 | + public bool TryGetValue(TAlternateKey key, [MaybeNullWhen(false)] out TValue value) |
| 127 | + { |
| 128 | + ref readonly TValue valueRef = ref Dictionary.GetValueRefOrNullRefCore(key); |
| 129 | + |
| 130 | + if (!Unsafe.IsNullRef(in valueRef)) |
| 131 | + { |
| 132 | + value = valueRef; |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + value = default; |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments