forked from gorhill/uMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliquid-dict.js
199 lines (162 loc) · 5.84 KB
/
liquid-dict.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
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
/*******************************************************************************
uMatrix - a Chromium browser extension to black/white list requests.
Copyright (C) 2014-2018 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uMatrix
*/
'use strict';
/******************************************************************************/
µMatrix.LiquidDict = (( ) => {
/******************************************************************************/
var LiquidDict = function() {
this.dict = new Map();
this.reset();
};
/******************************************************************************/
// Somewhat arbitrary: I need to come up with hard data to know at which
// point binary search is better than indexOf.
LiquidDict.prototype.cutoff = 500;
/******************************************************************************/
var meltBucket = function(ldict, len, bucket) {
ldict.frozenBucketCount -= 1;
if ( bucket.charCodeAt(0) === 0x20 /* ' ' */ ) {
return new Set(bucket.trim().split(' '));
}
let dict = new Set();
let offset = 0;
while ( offset < bucket.length ) {
dict.add(bucket.substring(offset, len));
offset += len;
}
return dict;
};
var freezeBucket = function(ldict, bucket) {
ldict.frozenBucketCount += 1;
let words = Array.from(bucket);
let wordLen = words[0].length;
if ( wordLen * words.length < ldict.cutoff ) {
return ' ' + words.join(' ') + ' ';
}
return words.sort().join('');
};
/******************************************************************************/
// How the key is derived dictates the number and size of buckets.
//
// http://jsperf.com/makekey-concat-vs-join/3
//
// Question: Why is using a prototyped function better than a standalone
// helper function?
LiquidDict.prototype.makeKey = function(word) {
let len = word.length;
if ( len > 255 ) { len = 255; }
let i = len >> 2;
return (word.charCodeAt( 0) & 0x03) << 14 |
(word.charCodeAt( i) & 0x03) << 12 |
(word.charCodeAt( i+i) & 0x03) << 10 |
(word.charCodeAt(i+i+i) & 0x03) << 8 |
len;
};
/******************************************************************************/
LiquidDict.prototype.test = function(word) {
let key = this.makeKey(word);
let bucket = this.dict.get(key);
if ( bucket === undefined ) {
return false;
}
if ( typeof bucket === 'object' ) {
return bucket.has(word);
}
if ( bucket.charCodeAt(0) === 0x20 /* ' ' */ ) {
return bucket.indexOf(' ' + word + ' ') !== -1;
}
// binary search
let len = word.length;
let left = 0;
// http://jsperf.com/or-vs-floor/3
let right = ~~(bucket.length / len + 0.5);
while ( left < right ) {
let i = left + right >> 1;
let needle = bucket.substr( len * i, len );
if ( word < needle ) {
right = i;
} else if ( word > needle ) {
left = i + 1;
} else {
return true;
}
}
return false;
};
/******************************************************************************/
LiquidDict.prototype.add = function(word) {
let key = this.makeKey(word);
let bucket = this.dict.get(key);
if ( bucket === undefined ) {
bucket = new Set();
this.dict.set(key, bucket);
bucket.add(word);
this.count += 1;
return true;
}
if ( typeof bucket === 'string' ) {
bucket = meltBucket(this, word.len, bucket);
this.dict.set(key, bucket);
}
if ( bucket.has(word) === false ) {
bucket.add(word);
this.count += 1;
return true;
}
this.duplicateCount += 1;
return false;
};
/******************************************************************************/
LiquidDict.prototype.freeze = function() {
for ( let entry of this.dict ) {
if ( typeof entry[1] === 'object' ) {
this.dict.set(entry[0], freezeBucket(this, entry[1]));
}
}
};
/******************************************************************************/
LiquidDict.prototype.reset = function() {
this.dict.clear();
this.count = 0;
this.duplicateCount = 0;
this.frozenBucketCount = 0;
};
/******************************************************************************/
const selfieVersion = 1;
LiquidDict.prototype.toSelfie = function() {
this.freeze();
return {
version: selfieVersion,
count: this.count,
duplicateCount: this.duplicateCount,
frozenBucketCount: this.frozenBucketCount,
dict: Array.from(this.dict)
};
};
LiquidDict.prototype.fromSelfie = function(selfie) {
if ( selfie.version !== selfieVersion ) { return false; }
this.count = selfie.count;
this.duplicateCount = selfie.duplicateCount;
this.frozenBucketCount = selfie.frozenBucketCount;
this.dict = new Map(selfie.dict);
return true;
};
/******************************************************************************/
return LiquidDict;
/******************************************************************************/
})();
/******************************************************************************/
µMatrix.ubiquitousBlacklist = new µMatrix.LiquidDict();