@@ -23,14 +23,13 @@ internal TValue Get(TKey key)
23
23
int hashCode = HashHelpers . GetHashCode ( key ) ;
24
24
int bucketIndex = hashCode & ( Buckets . Length - 1 ) ;
25
25
26
- SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > current = Buckets [ bucketIndex ] ;
26
+ SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > ? current = Buckets [ bucketIndex ] ;
27
27
28
- do
28
+ while ( current != null )
29
29
{
30
30
if ( ReferenceEquals ( current . Value . Key , key ) ) return current . Value . Value ;
31
31
current = current . Next ;
32
32
}
33
- while ( ! ReferenceEquals ( current , SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > . Empty ) ) ;
34
33
35
34
return default ! ;
36
35
}
@@ -42,8 +41,15 @@ private ImmutableHashTable(ImmutableHashTable<TKey, TValue> previous, in HashedK
42
41
if ( previous . Count >= previous . Buckets . Length )
43
42
{
44
43
this . Buckets = new SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > [ previous . Buckets . Length * 2 ] ;
45
- this . AddExistingValues ( previous ) ;
46
-
44
+ foreach ( SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > t in previous . Buckets )
45
+ {
46
+ SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > ? current = t ;
47
+ while ( current != null )
48
+ {
49
+ FillBucket ( in current . Value ) ;
50
+ current = current . Next ;
51
+ }
52
+ }
47
53
}
48
54
else
49
55
{
@@ -57,25 +63,6 @@ private ImmutableHashTable(ImmutableHashTable<TKey, TValue> previous, in HashedK
57
63
FillBucket ( in hashedKeyValue ) ;
58
64
}
59
65
60
- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
61
- private void AddExistingValues ( ImmutableHashTable < TKey , TValue > previous )
62
- {
63
- for ( var i = 0 ; i < Buckets . Length ; i ++ )
64
- {
65
- Buckets [ i ] = SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > . Empty ;
66
- }
67
-
68
- for ( var i = 0 ; i < previous . Buckets . Length ; i ++ )
69
- {
70
- SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > current = previous . Buckets [ i ] ;
71
- while ( ! ReferenceEquals ( current , SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > . Empty ) )
72
- {
73
- FillBucket ( in current . Value ) ;
74
- current = current . Next ;
75
- }
76
- }
77
- }
78
-
79
66
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
80
67
private void FillBucket ( in HashedKeyValue < TKey , TValue > hashedKeyValue )
81
68
{
@@ -86,16 +73,14 @@ private void FillBucket(in HashedKeyValue<TKey, TValue> hashedKeyValue)
86
73
private ImmutableHashTable ( )
87
74
{
88
75
this . Buckets = new SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > [ 2 ] ;
89
- this . Buckets [ 0 ] = SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > . Empty ;
90
- this . Buckets [ 1 ] = SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > . Empty ;
91
76
}
92
77
93
78
public IEnumerator < TValue > GetEnumerator ( )
94
79
{
95
80
foreach ( SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > bucket in Buckets )
96
81
{
97
- SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > current = bucket ;
98
- while ( ! ReferenceEquals ( current , SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > . Empty ) )
82
+ SinglyLinkedListNode < HashedKeyValue < TKey , TValue > > ? current = bucket ;
83
+ while ( current != null )
99
84
{
100
85
yield return current . Value . Value ;
101
86
current = current . Next ;
0 commit comments