-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsais.hxx
364 lines (346 loc) · 12.1 KB
/
sais.hxx
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
353
354
355
356
357
358
359
360
361
362
363
364
/*
* sais.hxx for sais-lite
* Copyright (c) 2008-2009 Yuta Mori All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _SAIS_HXX
#define _SAIS_HXX 1
#ifdef __cplusplus
#ifdef __INTEL_COMPILER
#pragma warning(disable : 383 981 1418)
// for icc 64-bit
//#define __builtin_vsnprintf(a, b, c, d) __builtin_vsnprintf(a, b, c, (char *)d)
#endif
#include <iterator>
#ifdef _OPENMP
# include <omp.h>
#endif
namespace saisxx_private {
/* find the start or end of each bucket */
template<typename string_type, typename bucket_type, typename index_type>
void
getCounts(const string_type T, bucket_type C, index_type n, index_type k) {
#ifdef _OPENMP
bucket_type D;
index_type i, j, p, sum, first, last;
int thnum, maxthreads = omp_get_max_threads();
#pragma omp parallel default(shared) private(D, i, thnum, first, last)
{
thnum = omp_get_thread_num();
D = C + thnum * k;
first = n / maxthreads * thnum;
last = (thnum < (maxthreads - 1)) ? n / maxthreads * (thnum + 1) : n;
for(i = 0; i < k; ++i) { D[i] = 0; }
for(i = first; i < last; ++i) { ++D[T[i]]; }
}
if(1 < maxthreads) {
#pragma omp parallel for default(shared) private(i, j, p, sum)
for(i = 0; i < k; ++i) {
for(j = 1, p = i + k, sum = C[i]; j < maxthreads; ++j, p += k) {
sum += C[p];
}
C[i] = sum;
}
}
#else
index_type i;
for(i = 0; i < k; ++i) { C[i] = 0; }
for(i = 0; i < n; ++i) { ++C[T[i]]; }
#endif
}
template<typename bucket_type, typename index_type>
void
getBuckets(const bucket_type C, bucket_type B, index_type k, bool end) {
index_type i, sum = 0;
if(end) { for(i = 0; i < k; ++i) { sum += C[i]; B[i] = sum; } }
else { for(i = 0; i < k; ++i) { sum += C[i]; B[i] = sum - C[i]; } }
}
/* compute SA and BWT */
template<typename string_type, typename sarray_type,
typename bucket_type, typename index_type>
void
induceSA(string_type T, sarray_type SA, bucket_type C, bucket_type B,
index_type n, index_type k) {
typedef typename std::iterator_traits<string_type>::value_type char_type;
sarray_type b;
index_type i, j;
char_type c0, c1;
/* compute SAl */
if(C == B) { getCounts(T, C, n, k); }
getBuckets(C, B, k, false); /* find starts of buckets */
b = SA + B[c1 = T[j = n - 1]];
*b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
for(i = 0; i < n; ++i) {
j = SA[i], SA[i] = ~j;
if(0 < j) {
if((c0 = T[--j]) != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
*b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
}
}
/* compute SAs */
if(C == B) { getCounts(T, C, n, k); }
getBuckets(C, B, k, true); /* find ends of buckets */
for(i = n - 1, b = SA + B[c1 = 0]; 0 <= i; --i) {
if(0 < (j = SA[i])) {
if((c0 = T[--j]) != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
*--b = ((j == 0) || (T[j - 1] > c1)) ? ~j : j;
} else {
SA[i] = ~j;
}
}
}
template<typename string_type, typename sarray_type,
typename bucket_type, typename index_type>
int
computeBWT(string_type T, sarray_type SA, bucket_type C, bucket_type B,
index_type n, index_type k) {
typedef typename std::iterator_traits<string_type>::value_type char_type;
sarray_type b;
index_type i, j, pidx = -1;
char_type c0, c1;
/* compute SAl */
if(C == B) { getCounts(T, C, n, k); }
getBuckets(C, B, k, false); /* find starts of buckets */
b = SA + B[c1 = T[j = n - 1]];
*b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
for(i = 0; i < n; ++i) {
if(0 < (j = SA[i])) {
SA[i] = ~(c0 = T[--j]);
if(c0 != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
*b++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
} else if(j != 0) {
SA[i] = ~j;
}
}
/* compute SAs */
if(C == B) { getCounts(T, C, n, k); }
getBuckets(C, B, k, true); /* find ends of buckets */
for(i = n - 1, b = SA + B[c1 = 0]; 0 <= i; --i) {
if(0 < (j = SA[i])) {
SA[i] = (c0 = T[--j]);
if(c0 != c1) { B[c1] = b - SA; b = SA + B[c1 = c0]; }
*--b = ((0 < j) && (T[j - 1] > c1)) ? ~((index_type)T[j - 1]) : j;
} else if(j != 0) {
SA[i] = ~j;
} else {
pidx = i;
}
}
return pidx;
}
/* find the suffix array SA of T[0..n-1] in {0..k}^n
use a working space (excluding s and SA) of at most 2n+O(1) for a constant alphabet */
template<typename string_type, typename sarray_type, typename index_type>
int
suffixsort(string_type T, sarray_type SA,
index_type fs, index_type n, index_type k,
bool isbwt) {
typedef typename std::iterator_traits<string_type>::value_type char_type;
sarray_type RA;
index_type i, j, m, p, q, plen, qlen, name, pidx = 0;
bool diff;
int c;
#ifdef _OPENMP
int maxthreads = omp_get_max_threads();
#else
# define maxthreads 1
#endif
char_type c0, c1;
/* stage 1: reduce the problem by at least 1/2
sort all the S-substrings */
if(fs < (maxthreads * k)) {
index_type *C, *B;
if((C = new index_type[maxthreads * k]) == 0) { return -2; }
B = (1 < maxthreads) ? C + k : C;
getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(i)
#endif
for(i = 0; i < n; ++i) { SA[i] = 0; }
for(i = n - 2, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
if((c0 = T[i]) < (c1 + c)) { c = 1; }
else if(c != 0) { SA[--B[c1]] = i + 1, c = 0; }
}
induceSA(T, SA, C, B, n, k);
delete [] C;
} else {
sarray_type C, B;
C = SA + n;
B = ((1 < maxthreads) || (k <= (fs - k))) ? C + k : C;
getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(i)
#endif
for(i = 0; i < n; ++i) { SA[i] = 0; }
for(i = n - 2, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
if((c0 = T[i]) < (c1 + c)) { c = 1; }
else if(c != 0) { SA[--B[c1]] = i + 1, c = 0; }
}
induceSA(T, SA, C, B, n, k);
}
/* compact all the sorted substrings into the first m items of SA
2*m must be not larger than n (proveable) */
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(i, j, p, c0, c1)
for(i = 0; i < n; ++i) {
p = SA[i];
if((0 < p) && (T[p - 1] > (c0 = T[p]))) {
for(j = p + 1; (j < n) && (c0 == (c1 = T[j])); ++j) { }
if((j < n) && (c0 < c1)) { SA[i] = ~p; }
}
}
for(i = 0, m = 0; i < n; ++i) { if((p = SA[i]) < 0) { SA[m++] = ~p; } }
#else
for(i = 0, m = 0; i < n; ++i) {
p = SA[i];
if((0 < p) && (T[p - 1] > (c0 = T[p]))) {
for(j = p + 1; (j < n) && (c0 == (c1 = T[j])); ++j) { }
if((j < n) && (c0 < c1)) { SA[m++] = p; }
}
}
#endif
j = m + (n >> 1);
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(i)
#endif
for(i = m; i < j; ++i) { SA[i] = 0; } /* init the name array buffer */
/* store the length of all substrings */
for(i = n - 2, j = n, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
if((c0 = T[i]) < (c1 + c)) { c = 1; }
else if(c != 0) { SA[m + ((i + 1) >> 1)] = j - i - 1; j = i + 1; c = 0; }
}
/* find the lexicographic names of all substrings */
for(i = 0, name = 0, q = n, qlen = 0; i < m; ++i) {
p = SA[i], plen = SA[m + (p >> 1)], diff = true;
if(plen == qlen) {
for(j = 0; (j < plen) && (T[p + j] == T[q + j]); ++j) { }
if(j == plen) { diff = false; }
}
if(diff != false) { ++name, q = p, qlen = plen; }
SA[m + (p >> 1)] = name;
}
/* stage 2: solve the reduced problem
recurse if names are not yet unique */
if(name < m) {
RA = SA + n + fs - m;
for(i = m + (n >> 1) - 1, j = m - 1; m <= i; --i) {
if(SA[i] != 0) { RA[j--] = SA[i] - 1; }
}
if(suffixsort(RA, SA, fs + n - m * 2, m, name, false) != 0) { return -2; }
for(i = n - 2, j = m - 1, c = 0, c1 = T[n - 1]; 0 <= i; --i, c1 = c0) {
if((c0 = T[i]) < (c1 + c)) { c = 1; }
else if(c != 0) { RA[j--] = i + 1, c = 0; } /* get p1 */
}
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(i)
#endif
for(i = 0; i < m; ++i) { SA[i] = RA[SA[i]]; } /* get index in s */
}
/* stage 3: induce the result for the original problem */
if(fs < (maxthreads * k)) {
index_type *B, *C;
if((C = new index_type[maxthreads * k]) == 0) { return -2; }
B = (1 < maxthreads) ? C + k : C;
/* put all left-most S characters into their buckets */
getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(i)
#endif
for(i = m; i < n; ++i) { SA[i] = 0; } /* init SA[m..n-1] */
for(i = m - 1; 0 <= i; --i) {
j = SA[i], SA[i] = 0;
SA[--B[T[j]]] = j;
}
if(isbwt == false) { induceSA(T, SA, C, B, n, k); }
else { pidx = computeBWT(T, SA, C, B, n, k); }
delete [] C;
} else {
sarray_type C, B;
C = SA + n;
B = ((1 < maxthreads) || (k <= (fs - k))) ? C + k : C;
/* put all left-most S characters into their buckets */
getCounts(T, C, n, k); getBuckets(C, B, k, true); /* find ends of buckets */
#ifdef _OPENMP
#pragma omp parallel for default(shared) private(i)
#endif
for(i = m; i < n; ++i) { SA[i] = 0; } /* init SA[m..n-1] */
for(i = m - 1; 0 <= i; --i) {
j = SA[i], SA[i] = 0;
SA[--B[T[j]]] = j;
}
if(isbwt == false) { induceSA(T, SA, C, B, n, k); }
else { pidx = computeBWT(T, SA, C, B, n, k); }
}
return pidx;
#ifndef _OPENMP
# undef maxthreads
#endif
}
} /* namespace saisxx_private */
/**
* @brief Constructs the suffix array of a given string in linear time.
* @param T[0..n-1] The input string. (random access iterator)
* @param SA[0..n-1] The output array of suffixes. (random access iterator)
* @param n The length of the given string.
* @param k The alphabet size.
* @return 0 if no error occurred, -1 or -2 otherwise.
*/
template<typename string_type, typename sarray_type, typename index_type>
int
saisxx(string_type T, sarray_type SA, index_type n, index_type k = 256) {
int err;
if((n < 0) || (k <= 0)) { return -1; }
if(n <= 1) { if(n == 1) { SA[0] = 0; } return 0; }
try { err = saisxx_private::suffixsort(T, SA, 0, n, k, false); }
catch(...) { err = -2; }
return err;
}
/**
* @brief Constructs the burrows-wheeler transformed string of a given string in linear time.
* @param T[0..n-1] The input string. (random access iterator)
* @param U[0..n-1] The output string. (random access iterator)
* @param A[0..n-1] The temporary array. (random access iterator)
* @param n The length of the given string.
* @param k The alphabet size.
* @return The primary index if no error occurred, -1 or -2 otherwise.
*/
template<typename string_type, typename sarray_type, typename index_type>
index_type
saisxx_bwt(string_type T, string_type U, sarray_type A, index_type n, index_type k = 256) {
typedef typename std::iterator_traits<string_type>::value_type char_type;
index_type i, pidx;
if((n < 0) || (k <= 0)) { return -1; }
if(n <= 1) { if(n == 1) { U[0] = T[0]; } return n; }
try {
pidx = saisxx_private::suffixsort(T, A, 0, n, k, true);
if(0 <= pidx) {
U[0] = T[n - 1];
for(i = 0; i < pidx; ++i) { U[i + 1] = (char_type)A[i]; }
for(i += 1; i < n; ++i) { U[i] = (char_type)A[i]; }
pidx += 1;
}
} catch(...) { pidx = -2; }
return pidx;
}
#endif /* __cplusplus */
#endif /* _SAIS_HXX */