-
Notifications
You must be signed in to change notification settings - Fork 931
/
Copy pathhashing-prime-test.js
67 lines (49 loc) · 2 KB
/
hashing-prime-test.js
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
const assert = require('assert');
/**
*
* @param {*} key
*/
function hashFunction(key, size = 10) {
const primeNumber = 1327144003n; // 2 ** 77232917 - 1
const hashCode = Array.from(key.toString()).reduce((hash, char) => {
return (hash * primeNumber + BigInt(char.codePointAt(0))) % BigInt(size);
}, 0n);
return parseInt(hashCode, 10);
}
// function hashCodeJava(key) {
// let h = 0;
// const value = key.toString();
// const length = value.length >> 1;
// for (let i = 0; i < length; i++) {
// h = 31 * h + value.codePointAt(i);
// }
// return h;
// }
function printHash(key) {
return { s: key, hashFn: hashFunction(key) };
}
// similar ending
// console.table(['00', '10', '20', '30', '40', '50', '60', '70', '80', '90'].map(printHash));
// similar start
// console.table(['10', '11', '12', '13', '14', '15', '16', '17', '18', '19'].map(printHash));
// console.table(['@', '#', '#!', 'stop', 'pots', 'Ca', 'DB', 'polygenelubricants', 'Pneumonoultramicroscopicsilicovolcanoconiosis'].map(printHash));
const size = 5100;
console.log(printHash(Array(size).fill('😁').join('')).hashFn);
console.log(printHash(Array(size).fill('1').join('')).hashFn);
console.log(printHash(Array(size).fill('A').join('')).hashFn);
// all different
// console.table(['cat', 'dog', 'rat', 'art', 10, '10', {a:1}, '😸', '🐶', '😸🐶', '🐶😸'].map(printHash));
// console.log(hashFunction(Array(1500).fill('😁').join('')));
// function test(){
// return 1n + 2n;
// }
// test();
// hashCode(10); //=> 97
// hashCode('10'); //=> 97
// assert.notEqual(hashCode(10), hashCode('10'), 'Hash code should be different with different types');
// assert.notEqual(hashCode('10string'), hashCode('10'), 'Hash code should be different with different types');
// hashCode(10) === hashCode('10'); //=> false
// hashCode('10') === hashCode('10string'); //=> false
// hashCode('art') === hashCode('rat'); //=> false
// hashCode('😄') === hashCode('😄'); //=> true
// hashCode('😄') === hashCode('😸'); //=> false