-
Notifications
You must be signed in to change notification settings - Fork 0
/
PerfectMinimalHashGenerator.cs
155 lines (133 loc) · 3.23 KB
/
PerfectMinimalHashGenerator.cs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.CodeDom;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis;
namespace StaticTypeDictionaryBenchmark
{
public static class PerfectMinimalHashGenerator
{
public readonly struct HashDictionary<T>
{
private readonly int[] _seeds;
private readonly T?[] _values;
public HashDictionary(int[] seeds, T?[] values)
{
_seeds = seeds;
_values = values;
}
public T? Get(in ReadOnlySpan<char> key)
{
var offset = Hash(key, 0) % _seeds.Length;
var d = _seeds[offset];
if (d < 0)
{
return _values[0 - d - 1];
}
return _values[Hash(key, d) % _values.Length];
}
}
public static HashDictionary<T> Build<T>(IDictionary<string, T> input) where T : class
{
var size = input.Count;
List<string>?[] buckets = new List<string>[size];
var result = new int[size];
foreach (var key in input.Keys)
{
var hashedKey = Hash(key, 0) % size;
buckets[hashedKey] ??= new List<string>(1);
buckets[hashedKey]!.Add(key);
}
Array.Sort(buckets, Comparison);
bool[] used = new bool[size];
List<int> slots = new List<int>();
T?[] values = new T[size];
int b = 0;
for (; b < size; b++)
{
var bucket = buckets[b];
if (bucket is null || bucket!.Count <= 1)
{
break;
}
int counter = 0;
int d = 1;
Array.Fill(used, false);
slots.Clear();
while (counter < bucket.Count)
{
var slot = Hash(bucket[counter], d) % size;
if (values[slot] is not null || used[slot])
{
d++;
counter = 0;
slots.Clear();
Array.Fill(used, false);
}
else
{
used[slot] = true;
slots.Add(slot);
counter++;
}
}
result[Hash(bucket[0], 0) % size] = d;
for (int i = 0; i < bucket.Count; i++)
{
var slot = slots[i];
Debug.Assert(values[slot] is null);
values[slot] = input[bucket[i]];
}
}
Queue<int> freelist = new Queue<int>();
for (int i = 0; i < size; i++)
{
if (values[i] is null)
{
freelist.Enqueue(i);
}
}
for (; b < size; b++)
{
var bucket = buckets[b];
if (bucket is null)
{
break;
}
Debug.Assert(bucket.Count == 1);
var slot = freelist.Dequeue();
result[Hash(bucket[0], 0) % size] = 0 - slot - 1;
Debug.Assert(values[slot] is null);
values[slot] = input[bucket[0]];
}
var bytes = MemoryMarshal.AsBytes(result.AsSpan());
var text =
$"public ReadOnlySpan<byte> Data => new[] {{{string.Join(',', bytes.ToArray().Select(a => "0x"+a.ToString("X2")))}}};";
return new HashDictionary<T>(result, values);
static int Comparison(List<string>? x, List<string>? y)
{
if (ReferenceEquals(x, y))
{
return 0;
}
if (x is null)
{
return 1;
}
if (y is null)
{
return -1;
}
return -x.Count.CompareTo(y.Count);
}
}
private static int Hash(in ReadOnlySpan<char> key, int seed)
{
var bytes = MemoryMarshal.AsBytes(key);
uint output = 0;
var outputBytes = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref output, 1));
System.IO.Hashing.XxHash32.Hash(bytes, outputBytes, seed);
return (int) (output & 0x7FFFFFF);
}
}
}