forked from Yomguithereal/mnemonist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suffix-array.js
352 lines (287 loc) · 7.8 KB
/
suffix-array.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
* Mnemonist Suffix Array
* =======================
*
* Linear time implementation of a suffix array using the recursive
* method by Karkkainen and Sanders.
*
* [References]:
* https://www.cs.helsinki.fi/u/tpkarkka/publications/jacm05-revised.pdf
* http://people.mpi-inf.mpg.de/~sanders/programs/suffix/
* http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.184.442&rep=rep1&type=pdf
*
* [Article]:
* "Simple Linear Work Suffix Array Construction", Karkkainen and Sanders.
*
* [Note]:
* A paper by Simon J. Puglisi, William F. Smyth & Andrew Turpin named
* "The Performance of Linear Time Suffix Sorting Algorithms" seems to
* prove that supralinear algorithm are in fact better faring for
* "real" world use cases. It would be nice to check this out in JavaScript
* because the high level of the language could change a lot to the fact.
*
* The current code is largely inspired by the following:
* https://github.com/tixxit/suffixarray/blob/master/suffixarray.js
*/
/**
* Constants.
*/
var SEPARATOR = '\u0001';
/**
* Function used to sort the triples.
*
* @param {string|array} string - Padded sequence.
* @param {array} array - Array to sort (will be mutated).
* @param {number} offset - Index offset.
*/
function sort(string, array, offset) {
var l = array.length,
buckets = [],
i = l,
j = -1,
b,
d = 0,
bits;
while (i--)
j = Math.max(string[array[i] + offset], j);
bits = j >> 24 && 32 || j >> 16 && 24 || j >> 8 && 16 || 8;
for (; d < bits; d += 4) {
for (i = 16; i--;)
buckets[i] = [];
for (i = l; i--;)
buckets[((string[array[i] + offset]) >> d) & 15].push(array[i]);
for (b = 0; b < 16; b++) {
for (j = buckets[b].length; j--;)
array[++i] = buckets[b][j];
}
}
}
/**
* Comparison helper.
*/
function compare(string, lookup, m, n) {
return (
(string[m] - string[n]) ||
(m % 3 === 2 ?
(string[m + 1] - string[n + 1]) || (lookup[m + 2] - lookup[n + 2]) :
(lookup[m + 1] - lookup[n + 1]))
);
}
/**
* Recursive function used to build the suffix tree in linear time.
*
* @param {string|array} string - Padded sequence.
* @param {number} l - True length of sequence (unpadded).
* @return {array}
*/
function build(string, l) {
var a = [],
b = [],
al = (2 * l / 3) | 0,
bl = l - al,
r = (al + 1) >> 1,
i = al,
j = 0,
k,
lookup = [],
result = [];
if (l === 1)
return [0];
while (i--)
a[i] = ((i * 3) >> 1) + 1;
for (i = 3; i--;)
sort(string, a, i);
j = b[((a[0] / 3) | 0) + (a[0] % 3 === 1 ? 0 : r)] = 1;
for (i = 1; i < al; i++) {
if (string[a[i]] !== string[a[i - 1]] ||
string[a[i] + 1] !== string[a[i - 1] + 1] ||
string[a[i] + 2] !== string[a[i - 1] + 2])
j++;
b[((a[i] / 3) | 0) + (a[i] % 3 === 1 ? 0 : r)] = j;
}
if (j < al) {
b = build(b, al);
for (i = al; i--;)
a[i] = b[i] < r ? b[i] * 3 + 1 : ((b[i] - r) * 3 + 2);
}
for (i = al; i--;)
lookup[a[i]] = i;
lookup[l] = -1;
lookup[l + 1] = -2;
b = l % 3 === 1 ? [l - 1] : [];
for (i = 0; i < al; i++) {
if (a[i] % 3 === 1)
b.push(a[i] - 1);
}
sort(string, b, 0);
for (i = 0, j = 0, k = 0; i < al && j < bl;)
result[k++] = (
compare(string, lookup, a[i], b[j]) < 0 ?
a[i++] :
b[j++]
);
while (i < al)
result[k++] = a[i++];
while (j < bl)
result[k++] = b[j++];
return result;
}
/**
* Function used to create the array we are going to work on.
*
* @param {string|array} target - Target sequence.
* @return {array}
*/
function convert(target) {
// Creating the alphabet array
var length = target.length,
paddingOffset = length % 3,
array = new Array(length + paddingOffset),
l,
i;
// If we have an arbitrary sequence, we need to transform it
if (typeof target !== 'string') {
var uniqueTokens = Object.create(null);
for (i = 0; i < length; i++) {
if (!uniqueTokens[target[i]])
uniqueTokens[target[i]] = true;
}
var alphabet = Object.create(null),
sortedUniqueTokens = Object.keys(uniqueTokens).sort();
for (i = 0, l = sortedUniqueTokens.length; i < l; i++)
alphabet[sortedUniqueTokens[i]] = i + 1;
for (i = 0; i < length; i++) {
array[i] = alphabet[target[i]];
}
}
else {
for (i = 0; i < length; i++)
array[i] = target.charCodeAt(i);
}
// Padding the array
for (; i < paddingOffset; i++)
array[i] = 0;
return array;
}
/**
* Suffix Array.
*
* @constructor
* @param {string|array} string - Sequence for which to build the suffix array.
*/
function SuffixArray(string) {
// Properties
this.hasArbitrarySequence = typeof string !== 'string';
this.string = string;
this.length = string.length;
// Building the array
this.array = build(convert(string), this.length);
}
/**
* Convenience known methods.
*/
SuffixArray.prototype.toString = function() {
return this.array.join(',');
};
SuffixArray.prototype.toJSON = function() {
return this.array;
};
SuffixArray.prototype.inspect = function() {
var array = new Array(this.length);
for (var i = 0; i < this.length; i++)
array[i] = this.string.slice(this.array[i]);
// Trick so that node displays the name of the constructor
Object.defineProperty(array, 'constructor', {
value: SuffixArray,
enumerable: false
});
return array;
};
if (typeof Symbol !== 'undefined')
SuffixArray.prototype[Symbol.for('nodejs.util.inspect.custom')] = SuffixArray.prototype.inspect;
/**
* Generalized Suffix Array.
*
* @constructor
*/
function GeneralizedSuffixArray(strings) {
// Properties
this.hasArbitrarySequence = typeof strings[0] !== 'string';
this.size = strings.length;
if (this.hasArbitrarySequence) {
this.text = [];
for (var i = 0, l = this.size; i < l; i++) {
this.text.push.apply(this.text, strings[i]);
if (i < l - 1)
this.text.push(SEPARATOR);
}
}
else {
this.text = strings.join(SEPARATOR);
}
this.firstLength = strings[0].length;
this.length = this.text.length;
// Building the array
this.array = build(convert(this.text), this.length);
}
/**
* Method used to retrieve the longest common subsequence of the generalized
* suffix array.
*
* @return {string|array}
*/
GeneralizedSuffixArray.prototype.longestCommonSubsequence = function() {
var lcs = this.hasArbitrarySequence ? [] : '',
lcp,
i,
j,
s,
t;
for (i = 1; i < this.length; i++) {
s = this.array[i];
t = this.array[i - 1];
if (s < this.firstLength &&
t < this.firstLength)
continue;
if (s > this.firstLength &&
t > this.firstLength)
continue;
lcp = Math.min(this.length - s, this.length - t);
for (j = 0; j < lcp; j++) {
if (this.text[s + j] !== this.text[t + j]) {
lcp = j;
break;
}
}
if (lcp > lcs.length)
lcs = this.text.slice(s, s + lcp);
}
return lcs;
};
/**
* Convenience known methods.
*/
GeneralizedSuffixArray.prototype.toString = function() {
return this.array.join(',');
};
GeneralizedSuffixArray.prototype.toJSON = function() {
return this.array;
};
GeneralizedSuffixArray.prototype.inspect = function() {
var array = new Array(this.length);
for (var i = 0; i < this.length; i++)
array[i] = this.text.slice(this.array[i]);
// Trick so that node displays the name of the constructor
Object.defineProperty(array, 'constructor', {
value: GeneralizedSuffixArray,
enumerable: false
});
return array;
};
if (typeof Symbol !== 'undefined')
GeneralizedSuffixArray.prototype[Symbol.for('nodejs.util.inspect.custom')] = GeneralizedSuffixArray.prototype.inspect;
/**
* Exporting.
*/
SuffixArray.GeneralizedSuffixArray = GeneralizedSuffixArray;
module.exports = SuffixArray;