forked from hola/challenge_word_classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
69 lines (63 loc) · 1.09 KB
/
solution.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
68
69
'use strict';
const N = 364442;
function hash(v) {
const h = string_hash(v);
return h % N;
}
function string_hash(str) {
let hash = 5381;
let i = str.length;
while(i) {
hash = (hash * 33) ^ str.charCodeAt(--i);
}
return hash >>> 0;
}
function bitreader(buf) {
let offset = 0;
let acc = 0;
let n = 0;
return function() {
n -= 1;
if (n < 0) {
if (offset >= buf.length) throw new Error("UEB");
acc = (acc << 8) | buf[offset++];
n += 8;
}
const v = acc >>> n;
acc &= (1 << n) - 1;
return v;
}
}
function decoder(buf) {
const reader = bitreader(buf);
return function() {
while(1) {
let v = 0;
while (reader()) {
v += 1;
}
return v;
}
}
}
function decodeAll(buf) {
const d = decoder(buf);
let n = 0;
let arr = [];
while(1) {
try {
n += d();
arr.push(n);
} catch(err) {
break;
}
}
return arr;
}
let HASHS;
module.exports = {
init: function(buf) {
HASHS = decodeAll(buf);
},
test: w => HASHS.indexOf(hash(w.toLowerCase())) !== -1
}