主要改了 vendor/esaxx-rs/build.rs:只在 crt-static 目标下才启用 static_crt(true),用来修复 Windows 下常见的 MSVC 运行库冲突
This commit is contained in:
Vendored
+126
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* esa.hxx
|
||||
* Copyright (c) 2010 Daisuke Okanohara 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 _ESA_HXX
|
||||
#define _ESA_HXX
|
||||
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
#include "sais.hxx"
|
||||
|
||||
namespace esaxx_private {
|
||||
template<typename string_type, typename sarray_type, typename index_type>
|
||||
index_type suffixtree(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D, index_type n){
|
||||
if (n == 0){
|
||||
return 0;
|
||||
}
|
||||
sarray_type Psi = L;
|
||||
Psi[SA[0]] = SA[n-1];
|
||||
for (index_type i = 1; i < n; ++i){
|
||||
Psi[SA[i]] = SA[i-1];
|
||||
}
|
||||
|
||||
// Compare at most 2n log n charcters. Practically fastest
|
||||
// "Permuted Longest-Common-Prefix Array", Juha Karkkainen, CPM 09
|
||||
sarray_type PLCP = R;
|
||||
index_type h = 0;
|
||||
for (index_type i = 0; i < n; ++i){
|
||||
index_type j = Psi[i];
|
||||
while (i+h < n && j+h < n &&
|
||||
T[i+h] == T[j+h]){
|
||||
++h;
|
||||
}
|
||||
PLCP[i] = h;
|
||||
if (h > 0) --h;
|
||||
}
|
||||
|
||||
sarray_type H = L;
|
||||
for (index_type i = 0; i < n; ++i){
|
||||
H[i] = PLCP[SA[i]];
|
||||
}
|
||||
H[0] = -1;
|
||||
|
||||
std::vector<std::pair<index_type, index_type> > S;
|
||||
S.push_back(std::make_pair((index_type)-1, (index_type)-1));
|
||||
size_t nodeNum = 0;
|
||||
for (index_type i = 0; ; ++i){
|
||||
std::pair<index_type, index_type> cur (i, (i == n) ? -1 : H[i]);
|
||||
std::pair<index_type, index_type> cand(S.back());
|
||||
while (cand.second > cur.second){
|
||||
if (i - cand.first > 1){
|
||||
L[nodeNum] = cand.first;
|
||||
R[nodeNum] = i;
|
||||
D[nodeNum] = cand.second;
|
||||
++nodeNum;
|
||||
}
|
||||
cur.first = cand.first;
|
||||
S.pop_back();
|
||||
cand = S.back();
|
||||
}
|
||||
if (cand.second < cur.second){
|
||||
S.push_back(cur);
|
||||
}
|
||||
if (i == n) break;
|
||||
S.push_back(std::make_pair(i, n - SA[i] + 1));
|
||||
}
|
||||
return nodeNum;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Build an enhanced suffix array of a given string in linear time
|
||||
* For an input text T, esaxx() builds an enhancd suffix array in linear time.
|
||||
* i-th internal node is represented as a triple (L[i], R[i], D[i]);
|
||||
* L[i] and R[i] is the left/right boundary of the suffix array as SA[L[i]....R[i]-1]
|
||||
* D[i] is the depth of the internal node
|
||||
* The number of internal node is at most N-1 and return the actual number by
|
||||
* @param T[0...n-1] The input string. (random access iterator)
|
||||
* @param SA[0...n-1] The output suffix array (random access iterator)
|
||||
* @param L[0...n-1] The output left boundary of internal node (random access iterator)
|
||||
* @param R[0...n-1] The output right boundary of internal node (random access iterator)
|
||||
* @param D[0...n-1] The output depth of internal node (random access iterator)
|
||||
* @param n The length of the input string
|
||||
* @param k The alphabet size
|
||||
* @pram nodeNum The output the number of internal node
|
||||
* @return 0 if succeded, -1 or -2 otherwise
|
||||
*/
|
||||
|
||||
template<typename string_type, typename sarray_type, typename index_type>
|
||||
int esaxx(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D,
|
||||
index_type n, index_type k, index_type& nodeNum) {
|
||||
if ((n < 0) || (k <= 0)) return -1;
|
||||
int err = saisxx(T, SA, n, k);
|
||||
if (err != 0){
|
||||
return err;
|
||||
}
|
||||
nodeNum = esaxx_private::suffixtree(T, SA, L, R, D, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#endif // _ESA_HXX
|
||||
|
||||
Vendored
+89
@@ -0,0 +1,89 @@
|
||||
use crate::sais::saisxx;
|
||||
use crate::types::{SArray, StringT, SuffixError};
|
||||
use std::convert::TryInto;
|
||||
|
||||
fn suffixtree(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
left: &mut SArray,
|
||||
right: &mut SArray,
|
||||
depth: &mut SArray,
|
||||
n: usize,
|
||||
) -> usize {
|
||||
if n == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Psi = l
|
||||
left[suffix_array[0]] = suffix_array[n - 1];
|
||||
for i in 1..n {
|
||||
left[suffix_array[i]] = suffix_array[i - 1];
|
||||
}
|
||||
// Compare at most 2n log n charcters. Practically fastest
|
||||
// "Permuted Longest-Common-Prefix Array", Juha Karkkainen, CPM 09
|
||||
// PLCP = r
|
||||
let mut h = 0;
|
||||
for i in 0..n {
|
||||
let j = left[i];
|
||||
while i + h < n && j + h < n && string[i + h] == string[j + h] {
|
||||
h += 1;
|
||||
}
|
||||
right[i] = h;
|
||||
h = h.saturating_sub(1);
|
||||
}
|
||||
|
||||
// H = l
|
||||
for i in 0..n {
|
||||
left[i] = right[suffix_array[i]];
|
||||
}
|
||||
// TODO XXX: i32 necessary
|
||||
// l[0] = -1;
|
||||
|
||||
let mut s: Vec<(i32, i32)> = vec![(-1, -1)];
|
||||
let mut node_num = 0;
|
||||
let mut i: usize = 0;
|
||||
loop {
|
||||
let mut cur: (i32, i32) = (i as i32, if i == n { -1 } else { left[i] as i32 });
|
||||
let mut cand = s[s.len() - 1];
|
||||
while cand.1 > cur.1 {
|
||||
if (i as i32) - cand.0 > 1 {
|
||||
left[node_num] = cand.0.try_into().unwrap();
|
||||
right[node_num] = i;
|
||||
depth[node_num] = cand.1.try_into().unwrap();
|
||||
node_num += 1;
|
||||
if node_num >= n {
|
||||
break;
|
||||
}
|
||||
}
|
||||
cur.0 = cand.0;
|
||||
s.pop();
|
||||
cand = s[s.len() - 1];
|
||||
}
|
||||
if cand.1 < cur.1 {
|
||||
s.push(cur);
|
||||
}
|
||||
if i == n {
|
||||
break;
|
||||
}
|
||||
s.push((
|
||||
i.try_into().unwrap(),
|
||||
(n - suffix_array[i] + 1).try_into().unwrap(),
|
||||
));
|
||||
i += 1;
|
||||
}
|
||||
node_num
|
||||
}
|
||||
|
||||
pub(crate) fn esaxx_rs(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
left: &mut SArray,
|
||||
right: &mut SArray,
|
||||
depth: &mut SArray,
|
||||
k: usize,
|
||||
) -> Result<usize, SuffixError> {
|
||||
let n = string.len();
|
||||
saisxx(string, suffix_array, n, k)?;
|
||||
let node_num = suffixtree(string, suffix_array, left, right, depth, n);
|
||||
Ok(node_num)
|
||||
}
|
||||
Vendored
+633
@@ -0,0 +1,633 @@
|
||||
//
|
||||
// /*
|
||||
// * 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, 0U, 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 */
|
||||
// /*
|
||||
// * esa.hxx
|
||||
// * Copyright (c) 2010 Daisuke Okanohara 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 _ESA_HXX
|
||||
// #define _ESA_HXX
|
||||
//
|
||||
// #include <vector>
|
||||
// #include <utility>
|
||||
// #include <cassert>
|
||||
// #include "sais.hxx"
|
||||
//
|
||||
// namespace esaxx_private {
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// index_type suffixtree(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D, index_type n){
|
||||
// if (n == 0){
|
||||
// return 0;
|
||||
// }
|
||||
// sarray_type Psi = L;
|
||||
// Psi[SA[0]] = SA[n-1];
|
||||
// for (index_type i = 1; i < n; ++i){
|
||||
// Psi[SA[i]] = SA[i-1];
|
||||
// }
|
||||
//
|
||||
// // Compare at most 2n log n charcters. Practically fastest
|
||||
// // "Permuted Longest-Common-Prefix Array", Juha Karkkainen, CPM 09
|
||||
// sarray_type PLCP = R;
|
||||
// index_type h = 0;
|
||||
// for (index_type i = 0; i < n; ++i){
|
||||
// index_type j = Psi[i];
|
||||
// while (i+h < n && j+h < n &&
|
||||
// T[i+h] == T[j+h]){
|
||||
// ++h;
|
||||
// }
|
||||
// PLCP[i] = h;
|
||||
// if (h > 0) --h;
|
||||
// }
|
||||
//
|
||||
// sarray_type H = L;
|
||||
// for (index_type i = 0; i < n; ++i){
|
||||
// H[i] = PLCP[SA[i]];
|
||||
// }
|
||||
// H[0] = -1;
|
||||
//
|
||||
// std::vector<std::pair<index_type, index_type> > S;
|
||||
// S.push_back(std::make_pair((index_type)-1, (index_type)-1));
|
||||
// size_t nodeNum = 0;
|
||||
// for (index_type i = 0; ; ++i){
|
||||
// std::pair<index_type, index_type> cur (i, (i == n) ? -1 : H[i]);
|
||||
// std::pair<index_type, index_type> cand(S.back());
|
||||
// while (cand.second > cur.second){
|
||||
// if (i - cand.first > 1){
|
||||
// L[nodeNum] = cand.first;
|
||||
// R[nodeNum] = i;
|
||||
// D[nodeNum] = cand.second;
|
||||
// ++nodeNum;
|
||||
// }
|
||||
// cur.first = cand.first;
|
||||
// S.pop_back();
|
||||
// cand = S.back();
|
||||
// }
|
||||
// if (cand.second < cur.second){
|
||||
// S.push_back(cur);
|
||||
// }
|
||||
// if (i == n) break;
|
||||
// S.push_back(std::make_pair(i, n - SA[i] + 1));
|
||||
// }
|
||||
// return nodeNum;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @brief Build an enhanced suffix array of a given string in linear time
|
||||
// * For an input text T, esaxx() builds an enhancd suffix array in linear time.
|
||||
// * i-th internal node is represented as a triple (L[i], R[i], D[i]);
|
||||
// * L[i] and R[i] is the left/right boundary of the suffix array as SA[L[i]....R[i]-1]
|
||||
// * D[i] is the depth of the internal node
|
||||
// * The number of internal node is at most N-1 and return the actual number by
|
||||
// * @param T[0...n-1] The input string. (random access iterator)
|
||||
// * @param SA[0...n-1] The output suffix array (random access iterator)
|
||||
// * @param L[0...n-1] The output left boundary of internal node (random access iterator)
|
||||
// * @param R[0...n-1] The output right boundary of internal node (random access iterator)
|
||||
// * @param D[0...n-1] The output depth of internal node (random access iterator)
|
||||
// * @param n The length of the input string
|
||||
// * @param k The alphabet size
|
||||
// * @pram nodeNum The output the number of internal node
|
||||
// * @return 0 if succeded, -1 or -2 otherwise
|
||||
// */
|
||||
//
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// int esaxx(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D,
|
||||
// index_type n, index_type k, index_type& nodeNum) {
|
||||
// if ((n < 0) || (k <= 0)) return -1;
|
||||
// int err = saisxx(T, SA, n, k);
|
||||
// if (err != 0){
|
||||
// return err;
|
||||
// }
|
||||
// nodeNum = esaxx_private::suffixtree(T, SA, L, R, D, n);
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// #endif // _ESA_HXX
|
||||
// /*
|
||||
// * esa.hxx
|
||||
// * Copyright (c) 2010 Daisuke Okanohara 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 _ESA_HXX
|
||||
// #define _ESA_HXX
|
||||
//
|
||||
// #include <vector>
|
||||
// #include <utility>
|
||||
// #include <cassert>
|
||||
// #include "sais.hxx"
|
||||
//
|
||||
// namespace esaxx_private {
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// index_type suffixtree(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D, index_type n){
|
||||
// if (n == 0){
|
||||
// return 0;
|
||||
// }
|
||||
// sarray_type Psi = L;
|
||||
// Psi[SA[0]] = SA[n-1];
|
||||
// for (index_type i = 1; i < n; ++i){
|
||||
// Psi[SA[i]] = SA[i-1];
|
||||
// }
|
||||
//
|
||||
// // Compare at most 2n log n charcters. Practically fastest
|
||||
// // "Permuted Longest-Common-Prefix Array", Juha Karkkainen, CPM 09
|
||||
// sarray_type PLCP = R;
|
||||
// index_type h = 0;
|
||||
// for (index_type i = 0; i < n; ++i){
|
||||
// index_type j = Psi[i];
|
||||
// while (i+h < n && j+h < n &&
|
||||
// T[i+h] == T[j+h]){
|
||||
// ++h;
|
||||
// }
|
||||
// PLCP[i] = h;
|
||||
// if (h > 0) --h;
|
||||
// }
|
||||
//
|
||||
// sarray_type H = L;
|
||||
// for (index_type i = 0; i < n; ++i){
|
||||
// H[i] = PLCP[SA[i]];
|
||||
// }
|
||||
// H[0] = -1;
|
||||
//
|
||||
// std::vector<std::pair<index_type, index_type> > S;
|
||||
// S.push_back(std::make_pair((index_type)-1, (index_type)-1));
|
||||
// size_t nodeNum = 0;
|
||||
// for (index_type i = 0; ; ++i){
|
||||
// std::pair<index_type, index_type> cur (i, (i == n) ? -1 : H[i]);
|
||||
// std::pair<index_type, index_type> cand(S.back());
|
||||
// while (cand.second > cur.second){
|
||||
// if (i - cand.first > 1){
|
||||
// L[nodeNum] = cand.first;
|
||||
// R[nodeNum] = i;
|
||||
// D[nodeNum] = cand.second;
|
||||
// ++nodeNum;
|
||||
// }
|
||||
// cur.first = cand.first;
|
||||
// S.pop_back();
|
||||
// cand = S.back();
|
||||
// }
|
||||
// if (cand.second < cur.second){
|
||||
// S.push_back(cur);
|
||||
// }
|
||||
// if (i == n) break;
|
||||
// S.push_back(std::make_pair(i, n - SA[i] + 1));
|
||||
// }
|
||||
// return nodeNum;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @brief Build an enhanced suffix array of a given string in linear time
|
||||
// * For an input text T, esaxx() builds an enhancd suffix array in linear time.
|
||||
// * i-th internal node is represented as a triple (L[i], R[i], D[i]);
|
||||
// * L[i] and R[i] is the left/right boundary of the suffix array as SA[L[i]....R[i]-1]
|
||||
// * D[i] is the depth of the internal node
|
||||
// * The number of internal node is at most N-1 and return the actual number by
|
||||
// * @param T[0...n-1] The input string. (random access iterator)
|
||||
// * @param SA[0...n-1] The output suffix array (random access iterator)
|
||||
// * @param L[0...n-1] The output left boundary of internal node (random access iterator)
|
||||
// * @param R[0...n-1] The output right boundary of internal node (random access iterator)
|
||||
// * @param D[0...n-1] The output depth of internal node (random access iterator)
|
||||
// * @param n The length of the input string
|
||||
// * @param k The alphabet size
|
||||
// * @pram nodeNum The output the number of internal node
|
||||
// * @return 0 if succeded, -1 or -2 otherwise
|
||||
// */
|
||||
//
|
||||
// template<typename string_type, typename sarray_type, typename index_type>
|
||||
// int esaxx(string_type T, sarray_type SA, sarray_type L, sarray_type R, sarray_type D,
|
||||
// index_type n, index_type k, index_type& nodeNum) {
|
||||
// if ((n < 0) || (k <= 0)) return -1;
|
||||
// std::count<<"Here"<<std::endl;
|
||||
// int err = saisxx(T, SA, n, k);
|
||||
// if (err != 0){
|
||||
// return err;
|
||||
// }
|
||||
// std::cout<<"suffixtree"<<std::endl;
|
||||
// nodeNum = esaxx_private::suffixtree(T, SA, L, R, D, n);
|
||||
// std::count<<"ok"<<std::endl;
|
||||
// return 0;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// #endif // _ESA_HXX
|
||||
|
||||
#include <cstdint>
|
||||
#include "esa.hxx"
|
||||
|
||||
extern "C"{
|
||||
|
||||
int esaxx_int32(char32_t* T, int32_t* SA, int32_t* L, int32_t* R, int32_t* D,
|
||||
int32_t n, int32_t k, int32_t &nodeNum) {
|
||||
|
||||
return esaxx(T, SA, L, R, D, n, k, nodeNum);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Vendored
+383
@@ -0,0 +1,383 @@
|
||||
//! Small wrapper around sentencepiece's esaxx suffix array C++ library.
|
||||
//! Usage
|
||||
//!
|
||||
//! ```rust
|
||||
//! #[cfg(feature="cpp")]
|
||||
//! {
|
||||
//! let string = "abracadabra";
|
||||
//! let suffix = esaxx_rs::suffix(string).unwrap();
|
||||
//! let chars: Vec<_> = string.chars().collect();
|
||||
//! let mut iter = suffix.iter();
|
||||
//! assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
|
||||
//! assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
//! assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
//! assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
//! assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
//! assert_eq!(iter.next(), None);
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! The previous version uses unsafe optimized c++ code.
|
||||
//! There exists another implementation a bit slower (~2x slower) that uses
|
||||
//! safe rust. It's a bit slower because it uses usize (mostly 64bit) instead of i32 (32bit).
|
||||
//! But it does seems to fix a few OOB issues in the cpp version
|
||||
//! (which never seemed to cause real problems in tests but still.)
|
||||
//!
|
||||
//! ```rust
|
||||
//! let string = "abracadabra";
|
||||
//! let suffix = esaxx_rs::suffix_rs(string).unwrap();
|
||||
//! let chars: Vec<_> = string.chars().collect();
|
||||
//! let mut iter = suffix.iter();
|
||||
//! assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
|
||||
//! assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
//! assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
//! assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
//! assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
//! assert_eq!(iter.next(), None);
|
||||
//! ```
|
||||
|
||||
use std::convert::TryInto;
|
||||
mod esa;
|
||||
mod sais;
|
||||
mod types;
|
||||
|
||||
use esa::esaxx_rs;
|
||||
use types::SuffixError;
|
||||
|
||||
#[cfg(feature = "cc")]
|
||||
extern "C" {
|
||||
fn esaxx_int32(
|
||||
// This is char32
|
||||
T: *const u32,
|
||||
SA: *mut i32,
|
||||
L: *mut i32,
|
||||
R: *mut i32,
|
||||
D: *mut i32,
|
||||
n: u32,
|
||||
k: u32,
|
||||
nodeNum: &mut u32,
|
||||
) -> i32;
|
||||
}
|
||||
|
||||
#[cfg(feature = "cc")]
|
||||
fn esaxx(
|
||||
chars: &[char],
|
||||
sa: &mut [i32],
|
||||
l: &mut [i32],
|
||||
r: &mut [i32],
|
||||
d: &mut [i32],
|
||||
alphabet_size: u32,
|
||||
node_num: &mut u32,
|
||||
) -> Result<(), SuffixError> {
|
||||
let n = chars.len();
|
||||
if sa.len() != n || l.len() != n || r.len() != n || d.len() != n {
|
||||
return Err(SuffixError::InvalidLength);
|
||||
}
|
||||
unsafe {
|
||||
let err = esaxx_int32(
|
||||
chars.as_ptr() as *const u32,
|
||||
sa.as_mut_ptr(),
|
||||
l.as_mut_ptr(),
|
||||
r.as_mut_ptr(),
|
||||
d.as_mut_ptr(),
|
||||
n.try_into().unwrap(),
|
||||
alphabet_size,
|
||||
node_num,
|
||||
);
|
||||
if err != 0 {
|
||||
return Err(SuffixError::Internal);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub struct SuffixIterator<'a, T> {
|
||||
i: usize,
|
||||
suffix: &'a Suffix<T>,
|
||||
}
|
||||
|
||||
pub struct Suffix<T> {
|
||||
chars: Vec<char>,
|
||||
sa: Vec<T>,
|
||||
l: Vec<T>,
|
||||
r: Vec<T>,
|
||||
d: Vec<T>,
|
||||
node_num: usize,
|
||||
}
|
||||
|
||||
/// Creates the suffix array and provides an iterator over its items (Rust version)
|
||||
/// See [suffix](fn.suffix.html)
|
||||
pub fn suffix_rs(string: &str) -> Result<Suffix<usize>, SuffixError> {
|
||||
let chars: Vec<_> = string.chars().collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
let node_num = esaxx_rs(
|
||||
&chars.iter().map(|c| *c as u32).collect::<Vec<_>>(),
|
||||
&mut sa,
|
||||
&mut l,
|
||||
&mut r,
|
||||
&mut d,
|
||||
alphabet_size,
|
||||
)?;
|
||||
Ok(Suffix {
|
||||
chars,
|
||||
sa,
|
||||
l,
|
||||
r,
|
||||
d,
|
||||
node_num,
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates the suffix array and provides an iterator over its items (c++ unsafe version)
|
||||
///
|
||||
/// Gives you an iterator over the suffixes of the input array and their count within
|
||||
/// the input srtring.
|
||||
/// ```rust
|
||||
/// let string = "abracadabra";
|
||||
/// let suffix = esaxx_rs::suffix(string).unwrap();
|
||||
/// let chars: Vec<_> = string.chars().collect();
|
||||
/// let mut iter = suffix.iter();
|
||||
/// assert_eq!(iter.next().unwrap(), (&chars[..4], 2)); // abra
|
||||
/// assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
/// assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
/// assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
/// assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
/// assert_eq!(iter.next(), None);
|
||||
/// ```
|
||||
#[cfg(feature = "cpp")]
|
||||
pub fn suffix(string: &str) -> Result<Suffix<i32>, SuffixError> {
|
||||
let chars: Vec<_> = string.chars().collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let mut node_num = 0;
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
esaxx(
|
||||
&chars,
|
||||
&mut sa,
|
||||
&mut l,
|
||||
&mut r,
|
||||
&mut d,
|
||||
alphabet_size,
|
||||
&mut node_num,
|
||||
)?;
|
||||
Ok(Suffix {
|
||||
chars,
|
||||
sa,
|
||||
l,
|
||||
r,
|
||||
d,
|
||||
node_num: node_num.try_into()?,
|
||||
})
|
||||
}
|
||||
|
||||
impl<T> Suffix<T> {
|
||||
pub fn iter(&self) -> SuffixIterator<'_, T> {
|
||||
SuffixIterator { i: 0, suffix: self }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for SuffixIterator<'a, i32> {
|
||||
type Item = (&'a [char], u32);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let index = self.i;
|
||||
if index == self.suffix.node_num {
|
||||
None
|
||||
} else {
|
||||
let left: usize = self.suffix.l[index].try_into().ok()?;
|
||||
let offset: usize = self.suffix.sa[left].try_into().ok()?;
|
||||
let len: usize = self.suffix.d[index].try_into().ok()?;
|
||||
let freq: u32 = (self.suffix.r[index] - self.suffix.l[index])
|
||||
.try_into()
|
||||
.ok()?;
|
||||
self.i += 1;
|
||||
Some((&self.suffix.chars[offset..offset + len], freq))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for SuffixIterator<'a, usize> {
|
||||
type Item = (&'a [char], u32);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let index = self.i;
|
||||
if index == self.suffix.node_num {
|
||||
None
|
||||
} else {
|
||||
let left: usize = self.suffix.l[index];
|
||||
let offset: usize = self.suffix.sa[left];
|
||||
let len: usize = self.suffix.d[index];
|
||||
let freq: u32 = (self.suffix.r[index] - self.suffix.l[index])
|
||||
.try_into()
|
||||
.unwrap();
|
||||
self.i += 1;
|
||||
Some((&self.suffix.chars[offset..offset + len], freq))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "cpp")]
|
||||
mod cpp_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_esaxx() {
|
||||
let string = "abracadabra".to_string();
|
||||
let chars: Vec<_> = string.chars().collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let mut node_num = 0;
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
|
||||
esaxx(
|
||||
&chars,
|
||||
&mut sa,
|
||||
&mut l,
|
||||
&mut r,
|
||||
&mut d,
|
||||
alphabet_size,
|
||||
&mut node_num,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(node_num, 5);
|
||||
assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
assert_eq!(l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
assert_eq!(r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
assert_eq!(d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_esaxx_long() {
|
||||
let string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.".to_string();
|
||||
let chars: Vec<_> = string.chars().collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let mut node_num = 0;
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
|
||||
esaxx(
|
||||
&chars,
|
||||
&mut sa,
|
||||
&mut l,
|
||||
&mut r,
|
||||
&mut d,
|
||||
alphabet_size,
|
||||
&mut node_num,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(chars.len(), 574);
|
||||
assert_eq!(node_num, 260);
|
||||
// assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
// assert_eq!(l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
// assert_eq!(r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
// assert_eq!(d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_suffix() {
|
||||
let suffix = suffix("abracadabra").unwrap();
|
||||
assert_eq!(suffix.node_num, 5);
|
||||
assert_eq!(suffix.sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
assert_eq!(suffix.l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
assert_eq!(suffix.r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
assert_eq!(suffix.d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
|
||||
let mut iter = suffix.iter();
|
||||
let chars: Vec<_> = "abracadabra".chars().collect();
|
||||
assert_eq!(iter.next(), Some((&chars[..4], 2))); // abra
|
||||
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod rs_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_esaxx_rs() {
|
||||
let string = "abracadabra".to_string();
|
||||
let chars: Vec<_> = string.chars().map(|c| c as u32).collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
|
||||
let node_num = esaxx_rs(&chars, &mut sa, &mut l, &mut r, &mut d, alphabet_size).unwrap();
|
||||
println!("Node num {}", node_num);
|
||||
println!("sa {:?}", sa);
|
||||
println!("l {:?}", l);
|
||||
println!("r {:?}", r);
|
||||
println!("d {:?}", d);
|
||||
assert_eq!(node_num, 5);
|
||||
assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
assert_eq!(l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
assert_eq!(r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
assert_eq!(d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_esaxx_rs_long() {
|
||||
let string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.".to_string();
|
||||
let chars: Vec<_> = string.chars().map(|c| c as u32).collect();
|
||||
let n = chars.len();
|
||||
let mut sa = vec![0; n];
|
||||
let mut l = vec![0; n];
|
||||
let mut r = vec![0; n];
|
||||
let mut d = vec![0; n];
|
||||
let alphabet_size = 0x110000; // All UCS4 range.
|
||||
|
||||
let node_num = esaxx_rs(&chars, &mut sa, &mut l, &mut r, &mut d, alphabet_size).unwrap();
|
||||
assert_eq!(chars.len(), 574);
|
||||
assert_eq!(node_num, 260);
|
||||
// assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
// assert_eq!(l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
// assert_eq!(r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
// assert_eq!(d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_suffix_rs() {
|
||||
let suffix = suffix_rs("abracadabra").unwrap();
|
||||
assert_eq!(suffix.node_num, 5);
|
||||
assert_eq!(suffix.sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
assert_eq!(suffix.l, vec![1, 0, 5, 9, 0, 0, 3, 0, 0, 0, 2]);
|
||||
assert_eq!(suffix.r, vec![3, 5, 7, 11, 11, 1, 0, 1, 0, 0, 0]);
|
||||
assert_eq!(suffix.d, vec![4, 1, 3, 2, 0, 0, 0, 0, 0, 0, 0]);
|
||||
|
||||
let mut iter = suffix.iter();
|
||||
let chars: Vec<_> = "abracadabra".chars().collect();
|
||||
assert_eq!(iter.next(), Some((&chars[..4], 2))); // abra
|
||||
assert_eq!(iter.next(), Some((&chars[..1], 5))); // a
|
||||
assert_eq!(iter.next(), Some((&chars[1..4], 2))); // bra
|
||||
assert_eq!(iter.next(), Some((&chars[2..4], 2))); // ra
|
||||
assert_eq!(iter.next(), Some((&chars[..0], 11))); // ''
|
||||
assert_eq!(iter.next(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_out_of_bounds_bug() {
|
||||
let string = "banana$band$$";
|
||||
suffix_rs(string).unwrap();
|
||||
}
|
||||
}
|
||||
Vendored
+377
@@ -0,0 +1,377 @@
|
||||
/*
|
||||
* 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
|
||||
#include <iostream>
|
||||
|
||||
int32_t f(const int32_t s){
|
||||
int a = ((s < 0)?-~s:s);
|
||||
return a;
|
||||
}
|
||||
|
||||
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;
|
||||
int 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, index_type(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 */
|
||||
|
||||
Vendored
+485
@@ -0,0 +1,485 @@
|
||||
use crate::types::{Bucket, SArray, StringT, SuffixError};
|
||||
|
||||
fn has_high_bit(j: usize) -> bool {
|
||||
j > usize::MAX / 2
|
||||
}
|
||||
|
||||
fn get_counts(t: &StringT, c: &mut Bucket) {
|
||||
c.iter_mut().for_each(|c| *c = 0);
|
||||
t.iter().for_each(|character| c[*character as usize] += 1);
|
||||
}
|
||||
|
||||
fn get_buckets(c: &Bucket, b: &mut Bucket, _k: usize, end: bool) {
|
||||
let mut sum = 0;
|
||||
if end {
|
||||
b.iter_mut().enumerate().for_each(|(i, b_el)| {
|
||||
sum += c[i];
|
||||
*b_el = sum;
|
||||
});
|
||||
} else {
|
||||
b.iter_mut().enumerate().for_each(|(i, b_el)| {
|
||||
*b_el = sum;
|
||||
sum += c[i];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn induce_sa(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
counts: &mut Bucket,
|
||||
buckets: &mut Bucket,
|
||||
n: usize,
|
||||
k: usize,
|
||||
) {
|
||||
assert!(n <= suffix_array.len());
|
||||
get_counts(string, counts);
|
||||
get_buckets(counts, buckets, k, false);
|
||||
|
||||
let mut c0;
|
||||
let mut j = n - 1;
|
||||
let mut c1 = string[j] as usize;
|
||||
let mut index = buckets[c1];
|
||||
suffix_array[index] = if j > 0 && (string[j - 1] as usize) < c1 {
|
||||
!j
|
||||
} else {
|
||||
j
|
||||
};
|
||||
index += 1;
|
||||
for i in 0..n {
|
||||
j = suffix_array[i];
|
||||
suffix_array[i] = !j;
|
||||
if !has_high_bit(j) && j > 0 {
|
||||
j -= 1;
|
||||
c0 = string[j] as usize;
|
||||
if c0 != c1 {
|
||||
buckets[c1] = index;
|
||||
c1 = c0;
|
||||
index = buckets[c1];
|
||||
}
|
||||
suffix_array[index] = if j > 0 && !has_high_bit(j) && (string[j - 1] as usize) < c1 {
|
||||
!j
|
||||
} else {
|
||||
j
|
||||
};
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Compute SA
|
||||
// XXX: true here.
|
||||
get_counts(string, counts);
|
||||
get_buckets(counts, buckets, k, true);
|
||||
c1 = 0;
|
||||
index = buckets[c1];
|
||||
for i in (0..n).rev() {
|
||||
j = suffix_array[i];
|
||||
if j > 0 && !has_high_bit(j) {
|
||||
j -= 1;
|
||||
c0 = string[j] as usize;
|
||||
if c0 != c1 {
|
||||
buckets[c1] = index;
|
||||
c1 = c0;
|
||||
index = buckets[c1];
|
||||
}
|
||||
index -= 1;
|
||||
suffix_array[index] = if j == 0 || (string[j - 1] as usize) > c1 {
|
||||
!j
|
||||
} else {
|
||||
j
|
||||
};
|
||||
} else {
|
||||
suffix_array[i] = !j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_bwt(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
counts: &mut Bucket,
|
||||
buckets: &mut Bucket,
|
||||
n: usize,
|
||||
k: usize,
|
||||
) -> usize {
|
||||
// TODO
|
||||
let mut pidx = 0;
|
||||
get_counts(string, counts);
|
||||
get_buckets(counts, buckets, k, false);
|
||||
let mut j = n - 1;
|
||||
let mut c1 = string[j] as usize;
|
||||
let mut c0;
|
||||
let mut index = buckets[c1];
|
||||
// bb = SA + B[c1 = T[j = n - 1]];
|
||||
// *bb++ = ((0 < j) && (T[j - 1] < c1)) ? ~j : j;
|
||||
suffix_array[index] = if j > 0 && (string[j - 1] as usize) < c1 {
|
||||
!j
|
||||
} else {
|
||||
j
|
||||
};
|
||||
index += 1;
|
||||
for i in 0..n {
|
||||
j = suffix_array[i];
|
||||
if j > 0 {
|
||||
j -= 1;
|
||||
c0 = string[j] as usize;
|
||||
suffix_array[i] = !c0;
|
||||
if c0 != c1 {
|
||||
buckets[c1] = index;
|
||||
c1 = c0;
|
||||
index = buckets[c1];
|
||||
}
|
||||
suffix_array[index] = if j > 0 && (string[j - 1] as usize) < c1 {
|
||||
!j
|
||||
} else {
|
||||
j
|
||||
};
|
||||
index += 1;
|
||||
} else if j != 0 {
|
||||
suffix_array[i] = !j;
|
||||
}
|
||||
}
|
||||
|
||||
// Compute SA
|
||||
get_counts(string, counts);
|
||||
get_buckets(counts, buckets, k, true);
|
||||
c1 = 0;
|
||||
index = buckets[c1];
|
||||
for i in (0..n).rev() {
|
||||
j = suffix_array[i];
|
||||
if j > 0 {
|
||||
j -= 1;
|
||||
c0 = string[j] as usize;
|
||||
suffix_array[i] = c0;
|
||||
if c0 != c1 {
|
||||
buckets[c1] = index;
|
||||
c1 = c0;
|
||||
index = buckets[c1];
|
||||
}
|
||||
index -= 1;
|
||||
suffix_array[index] = if j > 0 && (string[j - 1] as usize) > c1 {
|
||||
!(string[j - 1] as usize)
|
||||
} else {
|
||||
j
|
||||
};
|
||||
} else if j != 0 {
|
||||
suffix_array[i] = !j;
|
||||
} else {
|
||||
pidx = i
|
||||
}
|
||||
}
|
||||
pidx
|
||||
}
|
||||
|
||||
#[allow(clippy::many_single_char_names)]
|
||||
fn suffixsort(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
fs: usize,
|
||||
n: usize,
|
||||
k: usize,
|
||||
is_bwt: bool,
|
||||
) -> Result<usize, SuffixError> {
|
||||
let mut pidx = 0;
|
||||
let mut c0;
|
||||
|
||||
let mut counts = vec![0; k];
|
||||
let mut buckets = vec![0; k];
|
||||
get_counts(string, &mut counts);
|
||||
get_buckets(&counts, &mut buckets, k, true);
|
||||
// stage 1:
|
||||
// reduce the problem by at least 1/2
|
||||
// sort all the S-substrings
|
||||
for item in suffix_array.iter_mut() {
|
||||
*item = 0;
|
||||
}
|
||||
let mut c_index = 0;
|
||||
let mut c1 = string[n - 1] as usize;
|
||||
for i in (0..n - 1).rev() {
|
||||
c0 = string[i] as usize;
|
||||
if c0 < c1 + c_index {
|
||||
c_index = 1;
|
||||
} else if c_index != 0 {
|
||||
buckets[c1] -= 1;
|
||||
suffix_array[buckets[c1]] = i + 1;
|
||||
c_index = 0;
|
||||
}
|
||||
c1 = c0;
|
||||
}
|
||||
induce_sa(string, suffix_array, &mut counts, &mut buckets, n, k);
|
||||
|
||||
// compact all the sorted substrings into the first m items of SA
|
||||
// 2*m must be not larger than n (proveable)
|
||||
|
||||
// TODO: This was in the parallel loop.
|
||||
let mut p;
|
||||
let mut j;
|
||||
let mut m = 0;
|
||||
for i in 0..n {
|
||||
p = suffix_array[i];
|
||||
c0 = string[p] as usize;
|
||||
if p > 0 && (string[p - 1] as usize) > c0 {
|
||||
// TODO overly complex. But fricking hard to get right.
|
||||
j = p + 1;
|
||||
if j < n {
|
||||
c1 = string[j] as usize;
|
||||
}
|
||||
while j < n && c0 == c1 {
|
||||
c1 = string[j] as usize;
|
||||
j += 1;
|
||||
}
|
||||
if j < n && c0 < c1 {
|
||||
suffix_array[m] = p;
|
||||
m += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
j = m + (n >> 1);
|
||||
for item in suffix_array.iter_mut().take(j).skip(m) {
|
||||
*item = 0;
|
||||
}
|
||||
|
||||
/* store the length of all substrings */
|
||||
j = n;
|
||||
let mut c_index = 0;
|
||||
c1 = string[n - 1] as usize;
|
||||
for i in (0..n - 1).rev() {
|
||||
c0 = string[i] as usize;
|
||||
if c0 < c1 + c_index {
|
||||
c_index = 1;
|
||||
} else if c_index != 0 {
|
||||
suffix_array[m + ((i + 1) >> 1)] = j - i - 1;
|
||||
j = i + 1;
|
||||
c_index = 0;
|
||||
}
|
||||
c1 = c0;
|
||||
}
|
||||
|
||||
/* find the lexicographic names of all substrings */
|
||||
let mut name = 0;
|
||||
let mut q = n;
|
||||
let mut qlen = 0;
|
||||
let mut plen;
|
||||
let mut diff;
|
||||
for i in 0..m {
|
||||
p = suffix_array[i];
|
||||
plen = suffix_array[m + (p >> 1)];
|
||||
diff = true;
|
||||
if plen == qlen {
|
||||
j = 0;
|
||||
while j < plen && string[p + j] == string[q + j] {
|
||||
j += 1;
|
||||
}
|
||||
if j == plen {
|
||||
diff = false;
|
||||
}
|
||||
}
|
||||
if diff {
|
||||
name += 1;
|
||||
q = p;
|
||||
qlen = plen;
|
||||
}
|
||||
suffix_array[m + (p >> 1)] = name;
|
||||
}
|
||||
/* stage 2: solve the reduced problem
|
||||
recurse if names are not yet unique */
|
||||
if name < m {
|
||||
let ra_index = n + fs - m;
|
||||
j = m - 1;
|
||||
let a = m + (n >> 1);
|
||||
for i in (m..a).rev() {
|
||||
if suffix_array[i] != 0 {
|
||||
suffix_array[ra_index + j] = suffix_array[i] - 1;
|
||||
// XXX: Bug underflow caught by Rust yeah (well cpp used i32)
|
||||
j = j.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
// XXX: Could call transmute on SA to avoid allocation.
|
||||
// but it requires unsafe.
|
||||
let ra: Vec<u32> = suffix_array
|
||||
.iter()
|
||||
.skip(ra_index)
|
||||
.take(m)
|
||||
.map(|n| *n as u32)
|
||||
.collect();
|
||||
suffixsort(&ra, suffix_array, fs + n - m * 2, m, name, false)?;
|
||||
// let ra: &[char] =
|
||||
// unsafe { std::mem::transmute::<&[usize], &[char]>(&sa[ra_index..ra_index + m]) };
|
||||
// suffixsort(ra, sa, fs + n - m * 2, m, name, false)?;
|
||||
j = m - 1;
|
||||
c_index = 0;
|
||||
c1 = string[n - 1] as usize;
|
||||
for i in (0..n - 1).rev() {
|
||||
c0 = string[i] as usize;
|
||||
if c0 < c1 + c_index {
|
||||
c_index = 1;
|
||||
} else if c_index != 0 {
|
||||
suffix_array[ra_index + j] = i + 1;
|
||||
c_index = 0;
|
||||
j = j.saturating_sub(1);
|
||||
}
|
||||
c1 = c0;
|
||||
}
|
||||
// get index in s
|
||||
for i in 0..m {
|
||||
suffix_array[i] = suffix_array[ra_index + suffix_array[i]];
|
||||
}
|
||||
}
|
||||
|
||||
/* stage 3: induce the result for the original problem */
|
||||
/* put all left-most S characters into their buckets */
|
||||
get_counts(string, &mut counts);
|
||||
get_buckets(&counts, &mut buckets, k, true);
|
||||
for item in suffix_array.iter_mut().take(n).skip(m) {
|
||||
*item = 0;
|
||||
}
|
||||
for i in (0..m).rev() {
|
||||
j = suffix_array[i];
|
||||
suffix_array[i] = 0;
|
||||
if buckets[string[j] as usize] > 0 {
|
||||
buckets[string[j] as usize] -= 1;
|
||||
suffix_array[buckets[string[j] as usize]] = j;
|
||||
}
|
||||
}
|
||||
if is_bwt {
|
||||
pidx = compute_bwt(string, suffix_array, &mut counts, &mut buckets, n, k);
|
||||
} else {
|
||||
induce_sa(string, suffix_array, &mut counts, &mut buckets, n, k);
|
||||
}
|
||||
|
||||
Ok(pidx)
|
||||
}
|
||||
|
||||
pub fn saisxx(
|
||||
string: &StringT,
|
||||
suffix_array: &mut SArray,
|
||||
n: usize,
|
||||
k: usize,
|
||||
) -> Result<(), SuffixError> {
|
||||
if n == 1 {
|
||||
suffix_array[0] = 0;
|
||||
return Ok(());
|
||||
}
|
||||
let fs = 0;
|
||||
suffixsort(string, suffix_array, fs, n, k, false)?;
|
||||
Ok(())
|
||||
}
|
||||
fn _saisxx_bwt(
|
||||
t: &StringT,
|
||||
u: &mut StringT,
|
||||
sa: &mut SArray,
|
||||
n: usize,
|
||||
k: usize,
|
||||
) -> Result<usize, SuffixError> {
|
||||
if n <= 1 {
|
||||
if n == 1 {
|
||||
u[0] = t[0];
|
||||
}
|
||||
return Ok(n);
|
||||
}
|
||||
let mut pidx = suffixsort(t, sa, 0, n, k, true)?;
|
||||
u[0] = t[n - 1];
|
||||
for i in 0..pidx {
|
||||
u[i + 1] = sa[i] as u32;
|
||||
}
|
||||
for i in pidx + 1..n {
|
||||
u[i] = sa[i] as u32
|
||||
}
|
||||
pidx += 1;
|
||||
Ok(pidx)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_induce_sa() {
|
||||
let chars: Vec<_> = "abracadabra".chars().map(|c| c as u32).collect();
|
||||
let mut c = vec![0; 256];
|
||||
let mut b = vec![0; 256];
|
||||
|
||||
let mut sa = vec![0, 0, 3, 5, 7, 0, 0, 0, 0, 0, 0];
|
||||
induce_sa(&chars, &mut sa, &mut b, &mut c, chars.len(), 256);
|
||||
assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
|
||||
let mut sa = vec![0, 0, 7, 3, 5, 0, 0, 0, 0, 0, 0];
|
||||
induce_sa(&chars, &mut sa, &mut b, &mut c, chars.len(), 256);
|
||||
assert_eq!(sa, vec![10, 7, 0, 3, 5, 8, 1, 4, 6, 9, 2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_induce_sa_long() {
|
||||
let string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.".to_string();
|
||||
let chars: Vec<_> = string.chars().map(|c| c as u32).collect();
|
||||
let mut c = vec![0; 256];
|
||||
let mut b = vec![0; 256];
|
||||
let mut sa = vec![
|
||||
5, 11, 14, 21, 27, 32, 35, 39, 48, 52, 64, 74, 80, 86, 90, 95, 99, 110, 119, 125, 130,
|
||||
135, 141, 145, 152, 157, 160, 168, 176, 181, 183, 190, 193, 198, 202, 212, 215, 218,
|
||||
223, 225, 230, 239, 245, 248, 252, 261, 265, 270, 275, 286, 290, 295, 299, 304, 309,
|
||||
320, 333, 343, 355, 366, 369, 373, 385, 388, 392, 398, 403, 407, 415, 418, 427, 434,
|
||||
445, 451, 457, 467, 471, 476, 485, 490, 498, 509, 518, 523, 529, 539, 549, 558, 561,
|
||||
567, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 148, 396, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 534, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 88, 113, 116, 185, 206, 220, 250, 302,
|
||||
337, 351, 360, 371, 379, 412, 423, 439, 459, 462, 515, 0, 0, 0, 208, 501, 0, 0, 0, 139,
|
||||
204, 234, 313, 358, 479, 542, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 102, 526, 545, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 3, 29, 56, 58, 78, 127, 133, 155, 174, 188, 237, 283, 324, 326, 335,
|
||||
347, 409, 425, 430, 449, 464, 537, 551, 565, 0, 0, 0, 0, 0, 512, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 505, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 16, 42, 45, 61, 137, 171, 257, 329, 340, 381, 400, 442, 487, 503,
|
||||
520, 554, 0, 0, 0, 0, 0, 163, 494, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 268,
|
||||
483, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 122, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
437, 556, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 375,
|
||||
496, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 106, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69, 104, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
assert_eq!(sa.len(), chars.len());
|
||||
induce_sa(&chars, &mut sa, &mut b, &mut c, chars.len(), 256);
|
||||
assert_eq!(
|
||||
sa,
|
||||
vec![
|
||||
145, 392, 523, 5, 80, 451, 567, 245, 366, 418, 74, 445, 561, 529, 181, 223, 290,
|
||||
157, 48, 198, 467, 90, 239, 286, 275, 434, 490, 21, 119, 309, 343, 130, 270, 183,
|
||||
86, 248, 385, 539, 64, 99, 304, 11, 212, 299, 518, 218, 471, 261, 32, 190, 415,
|
||||
558, 265, 457, 373, 39, 168, 498, 476, 333, 407, 202, 427, 14, 135, 509, 230, 110,
|
||||
252, 27, 125, 35, 95, 141, 295, 388, 403, 215, 176, 193, 225, 52, 320, 355, 160,
|
||||
549, 369, 152, 398, 485, 108, 151, 285, 332, 466, 573, 73, 244, 365, 148, 396, 149,
|
||||
146, 393, 147, 395, 394, 524, 6, 81, 452, 568, 246, 367, 419, 0, 75, 446, 562, 534,
|
||||
530, 182, 224, 531, 462, 337, 439, 220, 535, 185, 351, 291, 206, 158, 49, 199, 468,
|
||||
113, 360, 302, 116, 515, 379, 88, 250, 371, 412, 423, 459, 91, 208, 501, 240, 287,
|
||||
319, 139, 479, 276, 358, 234, 542, 435, 204, 313, 51, 118, 201, 211, 260, 384, 470,
|
||||
364, 115, 491, 545, 22, 120, 526, 67, 102, 38, 98, 140, 144, 197, 222, 229, 274,
|
||||
298, 391, 406, 414, 475, 517, 522, 533, 301, 411, 233, 312, 478, 210, 259, 383,
|
||||
363, 92, 430, 409, 310, 3, 78, 449, 565, 335, 93, 155, 237, 347, 480, 277, 133,
|
||||
174, 537, 551, 283, 464, 56, 324, 492, 344, 425, 420, 431, 58, 326, 131, 29, 127,
|
||||
188, 34, 192, 417, 560, 271, 512, 47, 63, 342, 444, 508, 548, 331, 184, 532, 362,
|
||||
463, 402, 489, 87, 249, 359, 37, 97, 143, 297, 390, 405, 154, 429, 505, 350, 318,
|
||||
282, 520, 235, 16, 386, 137, 540, 65, 100, 45, 61, 340, 442, 506, 546, 329, 338,
|
||||
440, 171, 42, 305, 554, 12, 381, 503, 213, 400, 487, 272, 257, 180, 243, 221, 521,
|
||||
536, 163, 494, 378, 525, 300, 410, 311, 209, 187, 502, 519, 186, 352, 292, 543, 19,
|
||||
268, 353, 483, 4, 10, 79, 85, 450, 456, 566, 572, 219, 336, 207, 236, 24, 122, 472,
|
||||
17, 25, 123, 94, 156, 159, 167, 238, 387, 138, 357, 541, 50, 200, 469, 114, 66,
|
||||
101, 46, 62, 341, 443, 507, 547, 330, 361, 317, 339, 441, 162, 267, 262, 164, 556,
|
||||
437, 172, 348, 43, 481, 306, 278, 217, 294, 308, 33, 191, 416, 559, 511, 179, 242,
|
||||
316, 266, 436, 555, 178, 241, 496, 375, 473, 1, 76, 447, 563, 263, 165, 303, 497,
|
||||
458, 196, 228, 232, 55, 323, 18, 374, 40, 169, 7, 82, 453, 569, 499, 376, 134, 175,
|
||||
538, 205, 422, 117, 474, 516, 477, 2, 77, 334, 408, 448, 564, 281, 41, 170, 380,
|
||||
315, 552, 255, 106, 71, 13, 89, 109, 251, 372, 397, 433, 528, 557, 150, 284, 465,
|
||||
461, 203, 413, 382, 57, 325, 346, 424, 428, 504, 15, 136, 553, 493, 293, 510, 231,
|
||||
460, 345, 111, 69, 104, 8, 83, 454, 570, 253, 31, 129, 214, 247, 264, 289, 368,
|
||||
426, 112, 438, 28, 126, 173, 401, 488, 36, 96, 142, 296, 389, 404, 349, 44, 60,
|
||||
328, 482, 216, 307, 177, 495, 421, 314, 70, 105, 432, 59, 327, 279, 513, 194, 226,
|
||||
53, 321, 500, 544, 377, 9, 84, 455, 571, 23, 121, 356, 161, 280, 254, 527, 68, 103,
|
||||
288, 273, 258, 132, 550, 256, 370, 514, 153, 399, 486, 166, 30, 128, 20, 26, 124,
|
||||
189, 269, 354, 484, 107, 72, 195, 227, 54, 322
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
pub type Bucket = [usize];
|
||||
/// We need to use u32 instead of char, because when we recurse
|
||||
/// we use suffix array elements as ways to replace our original
|
||||
/// string. Using chars can fail. Look for ra variable.
|
||||
pub type StringT = [u32];
|
||||
pub type SArray = [usize];
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SuffixError {
|
||||
InvalidLength,
|
||||
Internal,
|
||||
IntConversion(std::num::TryFromIntError),
|
||||
}
|
||||
|
||||
impl From<std::num::TryFromIntError> for SuffixError {
|
||||
fn from(err: std::num::TryFromIntError) -> Self {
|
||||
Self::IntConversion(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user