@@ -8,107 +8,107 @@ namespace Open.Collections;
88/// Interface for implementing a Trie.
99/// </summary>
1010public interface ITrie < TKey , TValue >
11- where TKey : notnull
11+ where TKey : notnull
1212{
13- // NOTE: Path suffixed methods are provided to avoid ambiguity.
13+ // NOTE: Path suffixed methods are provided to avoid ambiguity.
1414
15- internal ITrieNode < TKey , TValue > EnsureNodes ( ReadOnlySpan < TKey > key ) ;
15+ internal ITrieNode < TKey , TValue > EnsureNodes ( ReadOnlySpan < TKey > key ) ;
1616
17- /// <summary>
18- /// Adds an entry to the Trie.
19- /// </summary>
20- /// <returns>Returns <see langword="true"/> if added; otherwise <see langword="false"/> if already exists.</returns>
21- bool Add ( ReadOnlySpan < TKey > key , in TValue value ) ;
17+ /// <summary>
18+ /// Adds an entry to the Trie.
19+ /// </summary>
20+ /// <returns>Returns <see langword="true"/> if added; otherwise <see langword="false"/> if already exists.</returns>
21+ bool Add ( ReadOnlySpan < TKey > key , in TValue value ) ;
2222
23- /// <inheritdoc cref="Add(ReadOnlySpan{TKey}, in TValue)"/>
24- bool AddPath ( IEnumerable < TKey > key , in TValue value ) ;
23+ /// <inheritdoc cref="Add(ReadOnlySpan{TKey}, in TValue)"/>
24+ bool AddPath ( IEnumerable < TKey > key , in TValue value ) ;
2525
26- /// <summary>
27- /// Resets the trie.
28- /// </summary>
29- void Clear ( ) ;
26+ /// <summary>
27+ /// Resets the trie.
28+ /// </summary>
29+ void Clear ( ) ;
3030
31- /// <summary>
32- /// Determines if a the specified <typeparamref name="TKey"/> exists.
33- /// </summary>
34- /// <returns><see langword="true"/> if found; otherwise <see langword="false"/>.</returns>
35- bool ContainsKey ( ReadOnlySpan < TKey > key ) ;
31+ /// <summary>
32+ /// Determines if a the specified <typeparamref name="TKey"/> exists.
33+ /// </summary>
34+ /// <returns><see langword="true"/> if found; otherwise <see langword="false"/>.</returns>
35+ bool ContainsKey ( ReadOnlySpan < TKey > key ) ;
3636
37- /// <inheritdoc cref="ContainsKey(ReadOnlySpan{TKey})"/>
38- bool ContainsKeyFromPath ( ICollection < TKey > key ) ;
37+ /// <inheritdoc cref="ContainsKey(ReadOnlySpan{TKey})"/>
38+ bool ContainsKeyFromPath ( ICollection < TKey > key ) ;
3939
40- /// <inheritdoc cref="ContainsKey(ReadOnlySpan{TKey})"/>
41- bool ContainsKeyFromPath ( IEnumerable < TKey > key ) ;
40+ /// <inheritdoc cref="ContainsKey(ReadOnlySpan{TKey})"/>
41+ bool ContainsKeyFromPath ( IEnumerable < TKey > key ) ;
4242
43- /// <summary>
44- /// Returns the value if found, otherwise adds using the provided factory function and then returns that value.
45- /// </summary>
46- TValue GetOrAdd ( ReadOnlySpan < TKey > key , Func < TValue > factory ) ;
43+ /// <summary>
44+ /// Returns the value if found, otherwise adds using the provided factory function and then returns that value.
45+ /// </summary>
46+ TValue GetOrAdd ( ReadOnlySpan < TKey > key , Func < TValue > factory ) ;
4747
48- /// <summary>
49- /// Returns the value if found, otherwise adds it and returns the value provided.
50- /// </summary>
51- TValue GetOrAdd ( ReadOnlySpan < TKey > key , in TValue value ) ;
48+ /// <summary>
49+ /// Returns the value if found, otherwise adds it and returns the value provided.
50+ /// </summary>
51+ TValue GetOrAdd ( ReadOnlySpan < TKey > key , in TValue value ) ;
5252
53- /// <inheritdoc cref="GetOrAdd(ReadOnlySpan{TKey}, Func{TValue})"/>
54- TValue GetOrAddFromPath ( ICollection < TKey > key , Func < TValue > factory ) ;
53+ /// <inheritdoc cref="GetOrAdd(ReadOnlySpan{TKey}, Func{TValue})"/>
54+ TValue GetOrAddFromPath ( ICollection < TKey > key , Func < TValue > factory ) ;
5555
56- /// <inheritdoc cref="GetOrAdd(ReadOnlySpan{TKey}, in TValue)"/>
57- TValue GetOrAddFromPath ( ICollection < TKey > key , in TValue value ) ;
56+ /// <inheritdoc cref="GetOrAdd(ReadOnlySpan{TKey}, in TValue)"/>
57+ TValue GetOrAddFromPath ( ICollection < TKey > key , in TValue value ) ;
5858
59- /// <inheritdoc cref="GetOrAdd(ReadOnlySpan{TKey}, Func{TValue})"/>
60- TValue GetOrAddFromPath ( IEnumerable < TKey > key , Func < TValue > factory ) ;
59+ /// <inheritdoc cref="GetOrAdd(ReadOnlySpan{TKey}, Func{TValue})"/>
60+ TValue GetOrAddFromPath ( IEnumerable < TKey > key , Func < TValue > factory ) ;
6161
62- /// <inheritdoc cref="GetOrAdd(ReadOnlySpan{TKey}, in TValue)"/>
63- TValue GetOrAddFromPath ( IEnumerable < TKey > key , in TValue value ) ;
62+ /// <inheritdoc cref="GetOrAdd(ReadOnlySpan{TKey}, in TValue)"/>
63+ TValue GetOrAddFromPath ( IEnumerable < TKey > key , in TValue value ) ;
6464
65- /// <summary>
66- /// Tries to get a value using the key and sets the <paramref name="value"/> with it if found.
67- /// </summary>
68- /// <returns><see langword="true"/> if found; otherwise <see langword="false"/>.</returns>
69- bool TryGetValue ( ReadOnlySpan < TKey > key , [ MaybeNullWhen ( false ) ] out TValue value ) ;
65+ /// <summary>
66+ /// Tries to get a value using the key and sets the <paramref name="value"/> with it if found.
67+ /// </summary>
68+ /// <returns><see langword="true"/> if found; otherwise <see langword="false"/>.</returns>
69+ bool TryGetValue ( ReadOnlySpan < TKey > key , [ MaybeNullWhen ( false ) ] out TValue value ) ;
7070
71- /// <inheritdoc cref="TryGetValue(ReadOnlySpan{TKey}, out TValue)"/>
72- bool TryGetValueFromPath ( ICollection < TKey > key , [ MaybeNullWhen ( false ) ] out TValue value ) ;
71+ /// <inheritdoc cref="TryGetValue(ReadOnlySpan{TKey}, out TValue)"/>
72+ bool TryGetValueFromPath ( ICollection < TKey > key , [ MaybeNullWhen ( false ) ] out TValue value ) ;
7373
74- /// <inheritdoc cref="TryGetValue(ReadOnlySpan{TKey}, out TValue)"/>
75- bool TryGetValueFromPath ( IEnumerable < TKey > key , [ MaybeNullWhen ( false ) ] out TValue value ) ;
74+ /// <inheritdoc cref="TryGetValue(ReadOnlySpan{TKey}, out TValue)"/>
75+ bool TryGetValueFromPath ( IEnumerable < TKey > key , [ MaybeNullWhen ( false ) ] out TValue value ) ;
7676}
7777
7878/// <summary>
7979/// Extensions for special cases for a Trie where some .NET versions may not have implicit span conversions for strings.
8080/// </summary>
8181public static class TrieExtensions
8282{
83- /// <inheritdoc cref="ITrie{TKey, TValue}.Add(ReadOnlySpan{TKey}, in TValue)"/>
84- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
85- [ ExcludeFromCodeCoverage ]
86- public static bool Add < T > ( this ITrie < char , T > target , string key , T value )
87- => target . Add ( key . AsSpan ( ) , value ) ;
88-
89- /// <inheritdoc cref="ITrie{TKey, TValue}.TryGetValue(ReadOnlySpan{TKey}, out TValue)"/>
90- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
91- [ ExcludeFromCodeCoverage ]
92- public static bool TryGetValue < T > ( this ITrie < char , T > target , string key , [ MaybeNullWhen ( false ) ] out T value )
93- => target . TryGetValue ( key . AsSpan ( ) , out value ) ;
94-
95- /// <inheritdoc cref="ITrie{TKey, TValue}.ContainsKey(ReadOnlySpan{TKey})"/>
96- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
97- [ ExcludeFromCodeCoverage ]
98- public static bool ContainsKey < T > ( this ITrie < char , T > target , string key )
99- => target . ContainsKey ( key . AsSpan ( ) ) ;
100-
101- /// <summary>
102- /// Gets the string instance representing the key.
103- /// </summary>
104- /// <remarks>Useful for using as a string pool.</remarks>
105- public static string Get ( this ITrie < char , string > target , ReadOnlySpan < char > key )
106- {
107- var node = target . EnsureNodes ( key ) ;
108- if ( node . TryGetValue ( out string ? v ) )
109- return v ;
110-
111- string value = key . ToString ( ) ;
112- return node . GetOrAdd ( in value ) ;
113- }
83+ /// <inheritdoc cref="ITrie{TKey, TValue}.Add(ReadOnlySpan{TKey}, in TValue)"/>
84+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
85+ [ ExcludeFromCodeCoverage ]
86+ public static bool Add < T > ( this ITrie < char , T > target , string key , T value )
87+ => target . Add ( key . AsSpan ( ) , value ) ;
88+
89+ /// <inheritdoc cref="ITrie{TKey, TValue}.TryGetValue(ReadOnlySpan{TKey}, out TValue)"/>
90+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
91+ [ ExcludeFromCodeCoverage ]
92+ public static bool TryGetValue < T > ( this ITrie < char , T > target , string key , [ MaybeNullWhen ( false ) ] out T value )
93+ => target . TryGetValue ( key . AsSpan ( ) , out value ) ;
94+
95+ /// <inheritdoc cref="ITrie{TKey, TValue}.ContainsKey(ReadOnlySpan{TKey})"/>
96+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
97+ [ ExcludeFromCodeCoverage ]
98+ public static bool ContainsKey < T > ( this ITrie < char , T > target , string key )
99+ => target . ContainsKey ( key . AsSpan ( ) ) ;
100+
101+ /// <summary>
102+ /// Gets the string instance representing the key.
103+ /// </summary>
104+ /// <remarks>Useful for using as a string pool.</remarks>
105+ public static string Get ( this ITrie < char , string > target , ReadOnlySpan < char > key )
106+ {
107+ var node = target . EnsureNodes ( key ) ;
108+ if ( node . TryGetValue ( out string ? v ) )
109+ return v ;
110+
111+ string value = key . ToString ( ) ;
112+ return node . GetOrAdd ( in value ) ;
113+ }
114114}
0 commit comments