@@ -125,21 +125,18 @@ IEnumerator IEnumerable.GetEnumerator()
125125 /// <exception cref="KeyNotFoundException">The key {key} was not found in the lookup map.</exception>
126126 public TValue Get ( TKey key )
127127 {
128- var hash = GetHash ( key , _keys . Length ) ;
129- var originalHash = hash ;
128+ var capacity = _keys . Length ;
129+ var baseHash = GetHash ( key , capacity ) ;
130130
131- while ( _keyPresence [ hash ] )
131+ for ( int i = 0 ; i < capacity ; i ++ )
132132 {
133+ var hash = ( baseHash + i * i ) % capacity ;
134+
135+ if ( ! _keyPresence [ hash ] )
136+ break ; // Key not found
137+
133138 if ( _keys [ hash ] . Equals ( key ) )
134- {
135139 return _values [ hash ] ;
136- }
137-
138- hash = ( hash + 1 ) % _keys . Length ; // Linear probing
139- if ( hash == originalHash )
140- {
141- break ; // Full cycle, key not found
142- }
143140 }
144141
145142 throw new KeyNotFoundException ( ExtendedSystemObjectsResources . ErrorValueNotFound ) ;
@@ -153,22 +150,21 @@ public TValue Get(TKey key)
153150 /// <returns>The value amd bool check if it exists.</returns>
154151 public bool TryGetValue ( TKey key , out TValue value )
155152 {
156- var hash = GetHash ( key , _keys . Length ) ;
157- var originalHash = hash ;
153+ var capacity = _keys . Length ;
154+ var baseHash = GetHash ( key , capacity ) ;
158155
159- while ( _keyPresence [ hash ] )
156+ for ( int i = 0 ; i < capacity ; i ++ )
160157 {
158+ var hash = ( baseHash + i * i ) % capacity ;
159+
160+ if ( ! _keyPresence [ hash ] )
161+ break ; // Key not found
162+
161163 if ( _keys [ hash ] . Equals ( key ) )
162164 {
163165 value = _values [ hash ] ;
164166 return true ;
165167 }
166-
167- hash = ( hash + 1 ) % _keys . Length ; // Linear probing
168- if ( hash == originalHash )
169- {
170- break ; // Full cycle, key not found
171- }
172168 }
173169
174170 value = default ;
@@ -186,7 +182,8 @@ public bool TryGetValue(TKey key, out TValue value)
186182 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
187183 private static int GetHash ( TKey key , int capacity )
188184 {
189- return Math . Abs ( key . GetHashCode ( ) % capacity ) ;
185+ // Mask sign bit to avoid negative numbers
186+ return ( key . GetHashCode ( ) & 0x7FFFFFFF ) % capacity ;
190187 }
191188
192189 /// <summary>
0 commit comments