-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathHashCode.js
More file actions
68 lines (63 loc) · 2.12 KB
/
HashCode.js
File metadata and controls
68 lines (63 loc) · 2.12 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
56
57
58
59
60
61
62
63
64
65
66
67
68
/* Copyright (c) 2012-2022 The ANTLR Project Contributors. All rights reserved.
* Use is of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
import { stringHashCode } from "../utils/stringHashCode.js";
export default class HashCode {
constructor() {
this.count = 0;
this.hash = 0;
}
update() {
for(let i=0;i<arguments.length;i++) {
let value = arguments[i];
if (value == null)
continue;
if(Array.isArray(value))
this.update.apply(this, value);
else {
let k = 0;
switch (typeof(value)) {
case 'undefined':
case 'function':
continue;
case 'number':
case 'boolean':
k = value;
break;
case 'string':
k = stringHashCode(value);
break;
default:
if(value.updateHashCode)
value.updateHashCode(this);
else
console.log("No updateHashCode for " + value.toString())
continue;
}
k = k * 0xCC9E2D51;
k = (k << 15) | (k >>> (32 - 15));
k = k * 0x1B873593;
this.count = this.count + 1;
let hash = this.hash ^ k;
hash = (hash << 13) | (hash >>> (32 - 13));
hash = hash * 5 + 0xE6546B64;
this.hash = hash;
}
}
}
finish() {
let hash = this.hash ^ (this.count * 4);
hash = hash ^ (hash >>> 16);
hash = hash * 0x85EBCA6B;
hash = hash ^ (hash >>> 13);
hash = hash * 0xC2B2AE35;
hash = hash ^ (hash >>> 16);
return hash;
}
static hashStuff() {
let hash = new HashCode();
hash.update.apply(hash, arguments);
return hash.finish();
}
}