Skip to content

Commit 6dd9fd9

Browse files
committed
remove unwarranted salt
1 parent 00ffe76 commit 6dd9fd9

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

Advanced.Algorithms/DataStructures/Dictionary/OpenAddressDictionary.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public V this[K key]
3737
//O(1) time complexity; worst case O(n)
3838
public bool ContainsKey(K key)
3939
{
40-
var hashCode = SaltHash(Math.Abs(key.GetHashCode()));
40+
var hashCode = getHash(key);
4141
var index = hashCode % bucketSize;
4242

4343
if (hashArray[index] == null)
@@ -84,7 +84,7 @@ public void Add(K key, V value)
8484

8585
Grow();
8686

87-
var hashCode = SaltHash(Math.Abs(key.GetHashCode()));
87+
var hashCode = getHash(key);
8888

8989
var index = hashCode % bucketSize;
9090

@@ -130,7 +130,7 @@ public void Add(K key, V value)
130130
//O(1) time complexity; worst case O(n)
131131
public void Remove(K key)
132132
{
133-
var hashCode = SaltHash(Math.Abs(key.GetHashCode()));
133+
var hashCode = getHash(key);
134134
var curIndex = hashCode % bucketSize;
135135

136136
if (hashArray[curIndex] == null)
@@ -228,7 +228,7 @@ public void Clear()
228228

229229
private void SetValue(K key, V value)
230230
{
231-
var index = (SaltHash(Math.Abs(key.GetHashCode()))) % bucketSize;
231+
var index = getHash(key) % bucketSize;
232232

233233
if (hashArray[index] == null)
234234
{
@@ -269,7 +269,7 @@ private void SetValue(K key, V value)
269269

270270
private V GetValue(K key)
271271
{
272-
var index = (SaltHash(Math.Abs(key.GetHashCode()))) % bucketSize;
272+
var index = getHash(key) % bucketSize;
273273

274274
if (hashArray[index] == null)
275275
{
@@ -363,13 +363,13 @@ private void Shrink()
363363
}
364364

365365
/// <summary>
366-
/// salt the hash with a random number
366+
/// get hash
367367
/// </summary>
368368
/// <param name="v"></param>
369369
/// <returns></returns>
370-
private int SaltHash(int key)
370+
private int getHash(K key)
371371
{
372-
return key * 3;
372+
return Math.Abs(key.GetHashCode());
373373
}
374374

375375
//Implementation for the GetEnumerator method.

Advanced.Algorithms/DataStructures/HashSet/OpenAddressHashSet.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public OpenAddressHashSet(int initialBucketSize = 2)
3131
//O(1) time complexity; worst case O(n)
3232
public bool Contains(V value)
3333
{
34-
var hashCode = SaltHash(Math.Abs(value.GetHashCode()));
34+
var hashCode = getHash(value);
3535
var index = hashCode % bucketSize;
3636

3737
if (hashArray[index] == null)
@@ -78,7 +78,7 @@ public void Add(V value)
7878

7979
Grow();
8080

81-
var hashCode = SaltHash(Math.Abs(value.GetHashCode()));
81+
var hashCode = getHash(value);
8282

8383
var index = hashCode % bucketSize;
8484

@@ -124,7 +124,7 @@ public void Add(V value)
124124
//O(1) time complexity; worst case O(n)
125125
public void Remove(V value)
126126
{
127-
var hashCode = SaltHash(Math.Abs(value.GetHashCode()));
127+
var hashCode = getHash(value);
128128
var curIndex = hashCode % bucketSize;
129129

130130
if (hashArray[curIndex] == null)
@@ -222,7 +222,7 @@ public void Clear()
222222

223223
private void SetValue(V value)
224224
{
225-
var index = (SaltHash(Math.Abs(value.GetHashCode()))) % bucketSize;
225+
var index = getHash(value) % bucketSize;
226226

227227
if (hashArray[index] == null)
228228
{
@@ -263,7 +263,7 @@ private void SetValue(V value)
263263

264264
private V GetValue(V value)
265265
{
266-
var index = (SaltHash(Math.Abs(value.GetHashCode()))) % bucketSize;
266+
var index = getHash(value) % bucketSize;
267267

268268
if (hashArray[index] == null)
269269
{
@@ -357,13 +357,13 @@ private void Shrink()
357357
}
358358

359359
/// <summary>
360-
/// salt the hash with a random number
360+
/// get hash
361361
/// </summary>
362362
/// <param name="v"></param>
363363
/// <returns></returns>
364-
private int SaltHash(int value)
364+
private int getHash(V value)
365365
{
366-
return value * 3;
366+
return Math.Abs(value.GetHashCode());
367367
}
368368

369369
//Implementation for the GetEnumerator method.

0 commit comments

Comments
 (0)