-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathhasher.mbt
220 lines (186 loc) · 4.75 KB
/
hasher.mbt
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///|
let gPRIME1 = 0x9E3779B1
///|
let gPRIME2 = 0x85EBCA77
///|
let gPRIME3 = 0xC2B2AE3D
///|
let gPRIME4 = 0x27D4EB2F
///|
let gPRIME5 = 0x165667B1
///|
/// xxhash32 Hasher
struct Hasher {
mut acc : Int
}
///|
pub fn Hasher::new(seed~ : Int = 0) -> Hasher {
{ acc: seed + gPRIME5 }
}
///|
pub fn Hasher::combine[T : Hash](self : Hasher, value : T) -> Unit {
value.hash_combine(self)
}
///|
pub fn Hasher::combine_unit(self : Hasher) -> Unit {
self.combine_int(0)
}
///|
pub fn Hasher::combine_bool(self : Hasher, value : Bool) -> Unit {
self.combine_int(if value { 1 } else { 0 })
}
///|
pub fn Hasher::combine_int(self : Hasher, value : Int) -> Unit {
self.acc += 4
self.consume4(value)
}
///|
pub fn Hasher::combine_int64(self : Hasher, value : Int64) -> Unit {
self.acc += 8
self.consume4(value.to_int())
self.consume4((value.reinterpret_as_uint64() >> 32).to_int())
}
///|
pub fn Hasher::combine_uint(self : Hasher, value : UInt) -> Unit {
self.combine_int(value.reinterpret_as_int())
}
///|
pub fn Hasher::combine_uint64(self : Hasher, value : UInt64) -> Unit {
self.combine_int64(value.reinterpret_as_int64())
}
///|
pub fn Hasher::combine_double(self : Hasher, value : Double) -> Unit {
self.combine_int64(value.reinterpret_as_int64())
}
///|
pub fn Hasher::combine_float(self : Hasher, value : Float) -> Unit {
self.combine_int(value.reinterpret_as_int())
}
///|
pub fn Hasher::combine_byte(self : Hasher, value : Byte) -> Unit {
self.consume1(value)
}
///|
pub fn Hasher::combine_bytes(self : Hasher, value : Bytes) -> Unit {
let mut remain = value.length()
let mut cur = 0
while remain >= 4 {
self.consume4(endian32(value, cur))
cur += 4
remain -= 4
}
while remain >= 1 {
self.consume1(value[cur])
cur += 1
remain -= 1
}
}
///|
pub fn Hasher::combine_string(self : Hasher, value : String) -> Unit {
for i = 0; i < value.length(); i = i + 1 {
self.combine_int(value[i].to_int())
}
}
///|
pub fn Hasher::combine_char(self : Hasher, value : Char) -> Unit {
self.combine_int(value.to_int())
}
///|
pub fn Hasher::finalize(self : Hasher) -> Int {
self.avalanche()
}
///|
fn Hasher::avalanche(self : Hasher) -> Int {
let mut acc = self.acc.reinterpret_as_uint()
acc = acc ^ (acc >> 15)
acc *= gPRIME2.reinterpret_as_uint()
acc = acc ^ (acc >> 13)
acc *= gPRIME3.reinterpret_as_uint()
acc = acc ^ (acc >> 16)
acc.reinterpret_as_int()
}
///|
fn Hasher::consume4(self : Hasher, input : Int) -> Unit {
self.acc = rotl(self.acc + input * gPRIME3, 17) * gPRIME4
}
///|
fn Hasher::consume1(self : Hasher, input : Byte) -> Unit {
self.acc = rotl(self.acc + input.to_int() * gPRIME5, 11) * gPRIME1
}
///|
fn rotl(x : Int, r : Int) -> Int {
(x << r) | (x.reinterpret_as_uint() >> (32 - r)).reinterpret_as_int()
}
///|
fn endian32(input : Bytes, cur : Int) -> Int {
input[cur + 0].to_int() |
(
(input[cur + 1].to_int() << 8) |
(input[cur + 2].to_int() << 16) |
(input[cur + 3].to_int() << 24)
)
}
// pub impl Hash for String with hash(self) {
// let hasher = Hasher::new()
// hasher.combine(self)
// hasher.finalize()
// }
///|
pub impl Hash for String with hash_combine(self, hasher) {
hasher.combine_string(self)
}
///|
// https://github.com/skeeto/hash-prospector
pub impl Hash for Int with hash(self) {
let self = self.reinterpret_as_uint()
let mut x = self ^ (self >> 17)
x = x * 0xed5ad4bb
x = x ^ (x >> 11)
x = x * 0xac4c1b51
x = x ^ (x >> 15)
x = x * 0x31848bab
x = x ^ (x >> 14)
x.reinterpret_as_int()
}
///|
pub impl Hash for Int with hash_combine(self, hasher) {
hasher.combine_int(self)
}
///|
pub impl Hash for UInt with hash_combine(self, hasher) {
hasher.combine_uint(self)
}
///|
pub impl Hash for UInt64 with hash_combine(self, hasher) {
hasher.combine_uint64(self)
}
///|
pub impl[X : Hash] Hash for X? with hash_combine(self, hasher) {
match self {
None => hasher.combine_int(0)
Some(x) => hasher..combine_int(1).combine(x)
}
}
///|
pub impl[T : Hash, E : Hash] Hash for Result[T, E] with hash_combine(
self,
hasher
) {
match self {
Ok(x) => hasher..combine_int(0).combine(x)
Err(x) => hasher..combine_int(1).combine(x)
}
}