8#ifndef INCLUDED_SDSL_SUFFIX_ARRAY_ALGORITHM
9#define INCLUDED_SDSL_SUFFIX_ARRAY_ALGORITHM
43template <
class t_csa,
class t_pat_iter>
46 typename t_csa::size_type l,
47 typename t_csa::size_type r,
50 typename t_csa::size_type & l_res,
51 typename t_csa::size_type & r_res,
53 typename std::enable_if<std::is_same<csa_tag, typename t_csa::index_category>::value,
csa_tag>::type x =
csa_tag())
56 assert(r < csa.size());
62 auto l_res_upper = r + 1;
63 auto r_res_upper = r + 1;
66 if ((
typename t_csa::size_type)(end - begin) >=
size)
70 auto compare = [&](
typename t_csa::size_type i) ->
int
72 for (
auto current = begin; current != end; current++)
74 auto index = csa.char2comp[*current];
77 if (csa.C[index + 1] - 1 < i)
87 while (l_res < l_res_upper)
89 typename t_csa::size_type sample = l_res + (l_res_upper - l_res) / 2;
90 int result = compare(sample);
93 else if (result == -1)
100 while (r_res + 1 < r_res_upper)
102 typename t_csa::size_type sample = r_res + (r_res_upper - r_res) / 2;
103 int result = compare(sample);
106 else if (result == -1)
107 r_res_upper = sample;
112 return r_res - l_res + 1;
129template <
class t_csa>
132 typename t_csa::size_type l,
133 typename t_csa::size_type r,
134 typename t_csa::char_type c,
135 typename t_csa::size_type & l_res,
136 typename t_csa::size_type & r_res,
138 typename std::enable_if<std::is_same<csa_tag, typename t_csa::index_category>::value,
csa_tag>::type x =
csa_tag())
166template <
class t_csa>
169 typename t_csa::size_type l,
170 typename t_csa::size_type r,
171 typename t_csa::char_type c,
172 typename t_csa::size_type & l_res,
173 typename t_csa::size_type & r_res,
175 typename std::enable_if<std::is_same<csa_tag, typename t_csa::index_category>::value,
csa_tag>::type x =
csa_tag())
178 assert(r < csa.size());
179 typename t_csa::size_type cc = csa.char2comp[c];
180 if (cc == 0 and c > 0)
187 typename t_csa::size_type c_begin = csa.C[cc];
188 if (l == 0 and r + 1 == csa.size())
191 r_res = csa.C[cc + 1] - 1;
195 l_res = c_begin + csa.bwt.rank(l, c);
196 r_res = c_begin + csa.bwt.rank(r + 1, c) - 1;
199 assert(r_res + 1 - l_res >= 0);
200 return r_res + 1 - l_res;
227template <
class t_csa,
class t_pat_iter>
230 typename t_csa::size_type l,
231 typename t_csa::size_type r,
234 typename t_csa::size_type & l_res,
235 typename t_csa::size_type & r_res,
237 typename std::enable_if<std::is_same<csa_tag, typename t_csa::index_category>::value,
csa_tag>::type x =
csa_tag())
240 while (begin < it and r + 1 - l > 0)
269 class t_sa_sample_strat,
271 class t_alphabet_strat>
285 assert(l_fwd <= r_fwd);
286 assert(r_fwd < csa_fwd.
size());
288 size_type c_begin = csa_fwd.
C[csa_fwd.
char2comp[c]];
289 auto r_s_b = csa_fwd.
wavelet_tree.lex_count(l_fwd, r_fwd + 1, c);
290 size_type rank_l = std::get<0>(r_s_b);
291 size_type s = std::get<1>(r_s_b), b = std::get<2>(r_s_b);
292 size_type rank_r = r_fwd - l_fwd - s - b + rank_l;
293 l_fwd_res = c_begin + rank_l;
294 r_fwd_res = c_begin + rank_r;
295 assert(r_fwd_res + 1 >= l_fwd_res);
296 l_bwd_res = l_bwd + s;
297 r_bwd_res = r_bwd - b;
298 assert(r_bwd_res - l_bwd_res == r_fwd_res - l_fwd_res);
299 return r_fwd_res + 1 - l_fwd_res;
332template <
class t_pat_iter,
336 class t_sa_sample_strat,
338 class t_alphabet_strat>
355 while (begin < it and r_fwd + 1 - l_fwd > 0)
373 return r_fwd + 1 - l_fwd;
406template <
class t_pat_iter,
410 class t_sa_sample_strat,
412 class t_alphabet_strat>
428 t_pat_iter it = begin;
429 while (it < end and r_fwd + 1 - l_fwd > 0)
447 return r_fwd + 1 - l_fwd;
463template <
class t_csa,
class t_pat_iter>
464typename t_csa::size_type
count(t_csa
const & csa, t_pat_iter begin, t_pat_iter end,
csa_tag)
466 if (end - begin > (
typename std::iterator_traits<t_pat_iter>::difference_type)csa.size())
468 typename t_csa::size_type t = 0;
469 typename t_csa::size_type result =
backward_search(csa, 0, csa.size() - 1, begin, end, t, t);
473template <
class t_csx,
class t_pat_iter>
474typename t_csx::size_type
count(t_csx
const & csx, t_pat_iter begin, t_pat_iter end)
476 typename t_csx::index_category tag;
477 return count(csx, begin, end, tag);
492template <
class t_csx>
493typename t_csx::size_type
count(t_csx
const & csx,
const typename t_csx::string_type & pat)
495 typename t_csx::index_category tag;
496 return count(csx, pat.begin(), pat.end(), tag);
511template <
typename t_csx,
typename t_pat_iter>
512auto lex_interval(t_csx
const & csx, t_pat_iter begin, t_pat_iter end) -> std::array<typename t_csx::size_type, 2>
514 std::array<typename t_csx::size_type, 2> res;
534template <
class t_csa,
class t_pat_iter,
class t_rac =
int_vector<64>>
539 typename std::enable_if<std::is_same<csa_tag, typename t_csa::index_category>::value,
csa_tag>::type x =
542 typename t_csa::size_type occ_begin, occ_end, occs;
543 occs =
backward_search(csa, 0, csa.size() - 1, begin, end, occ_begin, occ_end);
545 for (
typename t_csa::size_type i = 0; i < occs; ++i)
547 occ[i] = csa[occ_begin + i];
565template <
class t_csx,
class t_rac =
int_vector<64>>
566t_rac
locate(t_csx
const & csx,
const typename t_csx::string_type & pat)
568 typename t_csx::index_category tag;
569 return locate<t_csx,
decltype(pat.begin()), t_rac>(csx, pat.begin(), pat.end(), tag);
584template <
class t_csa,
class t_text_iter>
587 typename t_csa::size_type begin,
588 typename t_csa::size_type end,
591 typename std::enable_if<std::is_same<csa_tag, typename t_csa::index_category>::value,
csa_tag>::type x =
csa_tag())
593 typename t_csa::extract_category extract_tag;
594 return extract(csa, begin, end, text, extract_tag);
598template <
class t_csa,
class t_text_iter>
599typename t_csa::size_type
600extract(t_csa
const & csa,
typename t_csa::size_type begin,
typename t_csa::size_type end, t_text_iter text,
lf_tag)
602 assert(end < csa.size());
603 assert(begin <= end);
604 auto steps = end - begin + 1;
607 auto order = csa.isa[end];
611 auto rc = csa.wavelet_tree.inverse_select(order);
614 order = csa.
C[csa.char2comp[c]] + j;
618 return end - begin + 1;
622template <
class t_csa,
class t_text_iter>
623typename t_csa::size_type
624extract(t_csa
const & csa,
typename t_csa::size_type begin,
typename t_csa::size_type end, t_text_iter text,
psi_tag)
626 assert(end < csa.size());
627 assert(begin <= end);
628 typename t_csa::size_type steps = end - begin + 1;
629 for (
typename t_csa::size_type i = 0, order = csa.isa[begin]; steps != 0; --steps, ++i)
633 order = csa.psi[order];
635 return end - begin + 1;
651template <
class t_csa>
654 typename t_csa::size_type begin,
655 typename t_csa::size_type end,
657 typename std::enable_if<std::is_same<csa_tag, typename t_csa::index_category>::value,
csa_tag>::type x =
csa_tag())
659 assert(end <= csa.size());
660 assert(begin <= end);
661 typedef typename t_csa::string_type string_type;
662 string_type result(end - begin + 1, (
typename string_type::value_type)0);
663 extract(csa, begin, end, result.begin());
A class for the Compressed Suffix Array (CSA) based on a Wavelet Tree (WT) of the Burrow Wheeler Tran...
wavelet_tree_type const & wavelet_tree
const alphabet_type::char2comp_type & char2comp
alphabet_type::char_type char_type
int_vector ::size_type size_type
const alphabet_type::C_type & C
size_type size() const
Number of elements in the .
size_type size() const noexcept
The number of elements in the int_vector.
csa_wt.hpp contains an implementation of the compressed suffix array based on a wavelet tree.
int_vector.hpp contains the sdsl::int_vector class.
Namespace for the succinct data structure library.
auto lex_interval(t_csx const &csx, t_pat_iter begin, t_pat_iter end) -> std::array< typename t_csx::size_type, 2 >
Returns the lexicographic interval of a pattern in the SA.
t_csa::size_type forward_search(t_csa const &csa, typename t_csa::size_type l, typename t_csa::size_type r, t_pat_iter begin, t_pat_iter end, typename t_csa::size_type &l_res, typename t_csa::size_type &r_res, SDSL_UNUSED typename std::enable_if< std::is_same< csa_tag, typename t_csa::index_category >::value, csa_tag >::type x=csa_tag())
Forward search for a pattern in an -interval in the CSA.
t_csa::size_type extract(t_csa const &csa, typename t_csa::size_type begin, typename t_csa::size_type end, t_text_iter text, SDSL_UNUSED typename std::enable_if< std::is_same< csa_tag, typename t_csa::index_category >::value, csa_tag >::type x=csa_tag())
Writes the substring T[begin..end] of the original text T to text[0..end-begin+1].
uint64_t count(t_k2_treap const &treap, k2_treap_ns::point_type p1, k2_treap_ns::point_type p2)
Count how many points are in the rectangle (p1,p2)
csa_wt ::size_type bidirectional_search_backward(csa_wt< t_wt, t_dens, t_inv_dens, t_sa_sample_strat, t_isa, t_alphabet_strat > const &csa_fwd, SDSL_UNUSED csa_wt< t_wt, t_dens, t_inv_dens, t_sa_sample_strat, t_isa, t_alphabet_strat > const &csa_bwd, typename csa_wt<>::size_type l_fwd, typename csa_wt<>::size_type r_fwd, typename csa_wt<>::size_type l_bwd, typename csa_wt<>::size_type r_bwd, t_pat_iter begin, t_pat_iter end, typename csa_wt<>::size_type &l_fwd_res, typename csa_wt<>::size_type &r_fwd_res, typename csa_wt<>::size_type &l_bwd_res, typename csa_wt<>::size_type &r_bwd_res, SDSL_UNUSED typename std::enable_if< t_wt::lex_ordered, csa_tag >::type x=csa_tag())
Bidirectional search in backward direction.
t_csa::char_type first_row_symbol(const typename t_csa::size_type i, t_csa const &csa)
Get the symbol at position i in the first row of the sorted suffixes of CSA.
csa_wt< t_wt >::size_type bidirectional_search_forward(SDSL_UNUSED csa_wt< t_wt, t_dens, t_inv_dens, t_sa_sample_strat, t_isa, t_alphabet_strat > const &csa_fwd, csa_wt< t_wt, t_dens, t_inv_dens, t_sa_sample_strat, t_isa, t_alphabet_strat > const &csa_bwd, typename csa_wt<>::size_type l_fwd, typename csa_wt<>::size_type r_fwd, typename csa_wt<>::size_type l_bwd, typename csa_wt<>::size_type r_bwd, t_pat_iter begin, t_pat_iter end, typename csa_wt<>::size_type &l_fwd_res, typename csa_wt<>::size_type &r_fwd_res, typename csa_wt<>::size_type &l_bwd_res, typename csa_wt<>::size_type &r_bwd_res, SDSL_UNUSED typename std::enable_if< t_wt::lex_ordered, csa_tag >::type x=csa_tag())
Bidirectional search in forward direction.
t_csa::size_type backward_search(t_csa const &csa, typename t_csa::size_type l, typename t_csa::size_type r, typename t_csa::char_type c, typename t_csa::size_type &l_res, typename t_csa::size_type &r_res, SDSL_UNUSED typename std::enable_if< std::is_same< csa_tag, typename t_csa::index_category >::value, csa_tag >::type x=csa_tag())
Backward search for a character c in an -interval in the CSA.
int_vector ::size_type size(range_type const &r)
Size of a range.
csa_wt< t_wt >::size_type bidirectional_search(csa_wt< t_wt, t_dens, t_inv_dens, t_sa_sample_strat, t_isa, t_alphabet_strat > const &csa_fwd, typename csa_wt<>::size_type l_fwd, typename csa_wt<>::size_type r_fwd, typename csa_wt<>::size_type l_bwd, typename csa_wt<>::size_type r_bwd, typename csa_wt<>::char_type c, typename csa_wt<>::size_type &l_fwd_res, typename csa_wt<>::size_type &r_fwd_res, typename csa_wt<>::size_type &l_bwd_res, typename csa_wt<>::size_type &r_bwd_res, SDSL_UNUSED typename std::enable_if< t_wt::lex_ordered, csa_tag >::type x=csa_tag())
Bidirectional search for a character c on an interval of the suffix array.
t_rac locate(t_csa const &csa, t_pat_iter begin, t_pat_iter end, SDSL_UNUSED typename std::enable_if< std::is_same< csa_tag, typename t_csa::index_category >::value, csa_tag >::type x=csa_tag())
Calculates all occurrences of a pattern pat in a CSA.
Contains declarations and definitions of data structure concepts.
suffix_array_helper.hpp contains some helper classes for CSTs
wt_pc.hpp contains a class for the wavelet tree of byte sequences.