2 files changed +9
-9
lines changed Original file line number Diff line number Diff line change 1
- namespace Akade . IndexedSet . DataStructures ;
1
+ using System . Runtime . InteropServices ;
2
+
3
+ namespace Akade . IndexedSet . DataStructures ;
2
4
3
5
/// <summary>
4
6
/// Modifiable lookup based on <see cref="Dictionary{TKey, TValue}"/> with <see cref="HashSet{T}"/> as value collection per key.
@@ -11,10 +13,8 @@ internal class Lookup<TKey, TValue>
11
13
12
14
public bool Add ( TKey key , TValue value )
13
15
{
14
- if ( ! _values . TryGetValue ( key , out HashSet < TValue > ? keySet ) )
15
- {
16
- keySet = _values [ key ] = [ ] ;
17
- }
16
+ ref HashSet < TValue > ? keySet = ref CollectionsMarshal . GetValueRefOrAddDefault ( _values , key , out _ ) ;
17
+ keySet ??= [ ] ;
18
18
19
19
if ( keySet . Add ( value ) )
20
20
{
Original file line number Diff line number Diff line change 1
1
using Akade . IndexedSet . Extensions ;
2
2
using Akade . IndexedSet . Utils ;
3
3
using System . Diagnostics . CodeAnalysis ;
4
+ using System . Runtime . InteropServices ;
4
5
5
6
namespace Akade . IndexedSet . DataStructures ;
6
7
@@ -177,11 +178,10 @@ internal bool Add(ReadOnlySpan<char> key, TElement element)
177
178
else
178
179
{
179
180
_children ??= [ ] ;
180
- if ( ! _children . TryGetValue ( key [ 0 ] , out TrieNode ? trieNode ) )
181
- {
182
- _children [ key [ 0 ] ] = trieNode = new ( ) ;
183
- }
184
181
182
+ ref TrieNode ? trieNode = ref CollectionsMarshal . GetValueRefOrAddDefault ( _children , key [ 0 ] , out _ ) ;
183
+
184
+ trieNode ??= new ( ) ;
185
185
return trieNode . Add ( key [ 1 ..] , element ) ;
186
186
}
187
187
}
0 commit comments