-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDeterministicHashCode.cs
More file actions
55 lines (45 loc) · 1.26 KB
/
DeterministicHashCode.cs
File metadata and controls
55 lines (45 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Text;
using YellowCounter.FileSystemState.HashCodes;
namespace PathReduxTests.HashCodes
{
// Want a deterministic hash function so our tests are repeatable.
// https://andrewlock.net/why-is-string-gethashcode-different-each-time-i-run-my-program-in-net-core/
public class DeterministicHashCode : IHashCode
{
private bool dead = false;
private bool odd = false;
private int hash1 = 352654597; //(5381 << 16) + 5381;
private int hash2 = 352654597;
public void Add(char value)
{
unchecked
{
if(!odd)
{
hash1 = ((hash1 << 5) + hash1) ^ value;
}
else
{
hash2 = ((hash2 << 5) + hash2) ^ value;
}
}
odd = !odd;
}
public int ToHashCode()
{
deadCheck();
unchecked
{
return hash1 + (hash2 * 1566083941);
}
}
private void deadCheck()
{
if(dead)
throw new Exception("Cannot call ToHashCode() twice");
dead = true;
}
}
}