18#include <seqan3/contrib/sdsl-lite.hpp>
49template <
typename index_t>
68 index_type::text_layout_mode,
69 typename index_type::sdsl_index_type>>;
74 using sdsl_char_type =
typename index_type::sdsl_char_type;
76 using sdsl_index_type =
typename index_t::sdsl_index_type;
78 using sdsl_sigma_type =
typename index_type::sdsl_sigma_type;
80 using index_alphabet_type =
typename index_t::alphabet_type;
103 sdsl_sigma_type sigma{};
120 sdsl_char_type _last_char{};
131 bool fwd_cursor_last_used =
false;
142 template <
typename csa_t>
143 requires (std::same_as<csa_t, typename index_type::sdsl_index_type>
144 || std::same_as<csa_t, typename index_type::rev_sdsl_index_type>)
145 bool bidirectional_search(csa_t
const & csa,
146 sdsl_char_type
const c,
152 assert((l_fwd <= r_fwd) && (r_fwd < csa.size()));
153 assert(r_fwd + 1 >= l_fwd);
154 assert(r_bwd + 1 - l_bwd == r_fwd + 1 - l_fwd);
156 size_type _l_fwd, _r_fwd, _l_bwd, _r_bwd;
159 if constexpr (!std::same_as<index_alphabet_type, seqan3::contrib::sdsl::plain_byte_alphabet>)
161 cc = csa.char2comp[c];
162 if (cc == 0 && c > 0)
167 if (r_fwd + 1 - l_fwd == csa.size())
171 _r_fwd = csa.C[cc + 1] - 1;
177 auto const r_s_b = csa.wavelet_tree.lex_count(l_fwd, r_fwd + 1, c);
178 size_type const rank_l = std::get<0>(r_s_b);
179 size_type const s = std::get<1>(r_s_b), b = std::get<2>(r_s_b);
180 size_type const rank_r = r_fwd - l_fwd - s - b + rank_l;
181 _l_fwd = c_begin + rank_l;
182 _r_fwd = c_begin + rank_r;
187 if (_r_fwd >= _l_fwd)
193 assert(r_fwd + 1 >= l_fwd);
194 assert(r_bwd + 1 - l_bwd == r_fwd + 1 - l_fwd);
201 template <
typename csa_t>
202 requires (std::same_as<csa_t, typename index_type::sdsl_index_type>
203 || std::same_as<csa_t, typename index_type::rev_sdsl_index_type>)
204 bool bidirectional_search_cycle(csa_t
const & csa,
205 sdsl_char_type
const c,
213 assert((l_parent <= r_parent) && (r_parent < csa.size()));
216 if constexpr (std::same_as<index_alphabet_type, seqan3::contrib::sdsl::plain_byte_alphabet>)
219 c_begin = csa.C[csa.char2comp[c]];
221 auto const r_s_b = csa.wavelet_tree.lex_count(l_parent, r_parent + 1, c);
222 size_type const s = std::get<1>(r_s_b), b = std::get<2>(r_s_b), rank_l = std::get<0>(r_s_b),
223 rank_r = r_parent - l_parent - s - b + rank_l;
225 size_type const _l_fwd = c_begin + rank_l;
226 size_type const _r_fwd = c_begin + rank_r;
228 size_type const _r_bwd = r_bwd + 1 + rank_r - rank_l;
230 if (_r_fwd >= _l_fwd)
236 assert(r_fwd + 1 >= l_fwd);
237 assert(r_bwd + 1 - l_bwd == r_fwd + 1 - l_fwd);
261 fwd_rb(
_index.size() - 1),
263 rev_rb(
_index.size() - 1),
264 sigma(
_index.fwd_fm.index.sigma - index_t::text_layout_mode),
285 assert(!(fwd_lb ==
rhs.fwd_lb && fwd_rb ==
rhs.fwd_rb && depth ==
rhs.depth) || (depth == 0)
286 || (parent_lb ==
rhs.parent_lb && parent_rb ==
rhs.parent_rb && _last_char ==
rhs._last_char));
307 return !(*
this ==
rhs);
330 fwd_cursor_last_used =
true;
337 sdsl_sigma_type c = 1;
339 && !bidirectional_search(index->fwd_fm.index,
340 index->fwd_fm.index.comp2char[c],
355 _last_char =
static_cast<sdsl_char_type
>(c);
383 fwd_cursor_last_used =
false;
390 sdsl_sigma_type c = 1;
392 && !bidirectional_search(index->rev_fm.index,
393 index->rev_fm.index.comp2char[c],
408 _last_char =
static_cast<sdsl_char_type
>(c);
429 template <
typename char_t>
430 requires std::convertible_to<char_t, index_alphabet_type>
434 fwd_cursor_last_used =
true;
446 if (bidirectional_search(index->fwd_fm.index,
c_char, fwd_lb, fwd_rb, rev_lb, rev_rb))
460 template <
typename char_type>
461 requires seqan3::detail::is_char_adaptation_v<char_type>
480 template <
typename char_t>
481 requires std::convertible_to<char_t, index_alphabet_type>
485 fwd_cursor_last_used =
false;
497 if (bidirectional_search(index->rev_fm.index,
c_char, rev_lb, rev_rb, fwd_lb, fwd_rb))
511 template <
typename char_type>
512 requires seqan3::detail::is_char_adaptation_v<char_type>
534 template <std::ranges::range seq_t>
537 static_assert(std::ranges::forward_range<seq_t>,
"The query must model forward_range.");
538 static_assert(std::convertible_to<range_innermost_value_t<seq_t>, index_alphabet_type>,
539 "The alphabet of the sequence must be convertible to the alphabet of the index.");
544 auto last = std::ranges::end(
seq);
547 fwd_cursor_last_used = (first != last);
552 sdsl_char_type c = _last_char;
555 for (
auto it = first;
it != last; ++
len, ++
it)
604 template <std::ranges::range seq_t>
607 static_assert(std::ranges::bidirectional_range<seq_t>,
"The query must model bidirectional_range.");
608 static_assert(std::convertible_to<range_innermost_value_t<seq_t>, index_alphabet_type>,
609 "The alphabet of the sequence must be convertible to the alphabet of the index.");
614 auto last = std::ranges::end(
rev_seq);
618 fwd_cursor_last_used =
false;
623 sdsl_char_type c = _last_char;
626 for (
auto it = first;
it != last; ++
len, ++
it)
684 assert(fwd_cursor_last_used);
689 sdsl_sigma_type c = _last_char + 1;
692 && !bidirectional_search_cycle(index->fwd_fm.index,
693 index->fwd_fm.index.comp2char[c],
707 _last_char =
static_cast<sdsl_char_type
>(c);
744 assert(!fwd_cursor_last_used);
749 sdsl_sigma_type c = _last_char + 1;
751 && !bidirectional_search_cycle(index->rev_fm.index,
752 index->rev_fm.index.comp2char[c],
766 _last_char =
static_cast<sdsl_char_type
>(c);
793 return index->fwd_fm.index.comp2char[_last_char] - 1;
812 assert(depth != 0 || (fwd_lb == rev_lb && fwd_rb == rev_rb && fwd_lb == 0 && fwd_rb == index->size() - 1));
841 cur.parent_lb = parent_lb;
842 cur.parent_rb = parent_rb;
843 cur.node = {fwd_lb, fwd_rb, depth, _last_char};
846 if (!fwd_cursor_last_used)
873 template <std::ranges::range text_t>
877 static_assert(std::ranges::input_range<text_t>,
"The text must model input_range.");
879 static_assert(std::same_as<range_innermost_value_t<text_t>, index_alphabet_type>,
880 "The alphabet types of the given text and index differ.");
888 template <std::ranges::range text_t>
892 static_assert(std::ranges::input_range<text_t>,
"The text collection must model input_range.");
894 static_assert(std::same_as<range_innermost_value_t<text_t>, index_alphabet_type>,
895 "The alphabet types of the given text and index differ.");
929 assert(index !=
nullptr && (1 + fwd_rb - fwd_lb == 1 + rev_rb - rev_lb));
931 return 1 + fwd_rb - fwd_lb;
954 occ.emplace_back(0, offset() - index->fwd_fm.index[fwd_lb +
i]);
994 return std::views::iota(fwd_lb, fwd_lb +
count())
995 | std::views::transform(
1006 assert(index !=
nullptr);
1008 return std::views::iota(fwd_lb, fwd_lb +
count())
1009 | std::views::transform(
1026 template <cereal_archive archive_t>
Core alphabet concept and free function/type trait wrappers.
Provides alphabet adaptations for standard char types.
The SeqAn Bidirectional FM Index Cursor.
Definition bi_fm_index_cursor.hpp:51
bool extend_right() noexcept
Tries to extend the query by the smallest possible character to the right such that the query is foun...
Definition bi_fm_index_cursor.hpp:327
size_type count() const noexcept
Counts the number of occurrences of the searched query in the text.
Definition bi_fm_index_cursor.hpp:927
bool extend_left(seq_t &&seq) noexcept
Tries to extend the query by seq to the left.
Definition bi_fm_index_cursor.hpp:605
bool operator==(bi_fm_index_cursor const &rhs) const noexcept
Compares two cursors.
Definition bi_fm_index_cursor.hpp:281
bool extend_left(char_type const *cstring) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition bi_fm_index_cursor.hpp:513
auto path_label(text_t &&text) const noexcept
Returns the searched query.
Definition bi_fm_index_cursor.hpp:874
bool extend_left() noexcept
Tries to extend the query by the smallest possible character to the left such that the query is found...
Definition bi_fm_index_cursor.hpp:380
auto path_label(text_t &&text) const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition bi_fm_index_cursor.hpp:889
locate_result_type locate() const
Locates the occurrences of the searched query in the text.
Definition bi_fm_index_cursor.hpp:945
bool cycle_back() noexcept
Tries to replace the rightmost character of the query by the next lexicographically larger character ...
Definition bi_fm_index_cursor.hpp:680
bool cycle_front() noexcept
Tries to replace the leftmost character of the query by the next lexicographically larger character s...
Definition bi_fm_index_cursor.hpp:740
auto lazy_locate() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition bi_fm_index_cursor.hpp:1003
fwd_cursor to_fwd_cursor() const noexcept
Returns a unidirectional seqan3::fm_index_cursor on the original text. path_label() on the returned u...
Definition bi_fm_index_cursor.hpp:836
size_type query_length() const noexcept
Returns the depth of the cursor node in the implicit suffix tree, i.e. the length of the sequence sea...
Definition bi_fm_index_cursor.hpp:808
bool extend_right(char_type const *cstring) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition bi_fm_index_cursor.hpp:462
size_type last_rank() noexcept
Outputs the rightmost respectively leftmost rank depending on whether extend_right() or extend_left()...
Definition bi_fm_index_cursor.hpp:789
index_t index_type
Type of the index.
Definition bi_fm_index_cursor.hpp:54
typename index_type::size_type size_type
Type for representing positions in the indexed text.
Definition bi_fm_index_cursor.hpp:60
bool operator!=(bi_fm_index_cursor const &rhs) const noexcept
Compares two cursors.
Definition bi_fm_index_cursor.hpp:303
std::vector< std::pair< size_type, size_type > > locate() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition bi_fm_index_cursor.hpp:960
bi_fm_index_cursor() noexcept=default
Default constructor. Accessing member functions on a default constructed object is undefined behavior...
auto lazy_locate() const
Locates the occurrences of the searched query in the text on demand, i.e. a std::ranges::view is retu...
Definition bi_fm_index_cursor.hpp:989
bool extend_right(seq_t &&seq) noexcept
Tries to extend the query by seq to the right.
Definition bi_fm_index_cursor.hpp:535
bool extend_right(char_t const c) noexcept
Tries to extend the query by the character c to the right.
Definition bi_fm_index_cursor.hpp:431
bool extend_left(char_t const c) noexcept
Tries to extend the query by the character c to the left.
Definition bi_fm_index_cursor.hpp:482
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
The SeqAn FM Index Cursor.
Definition fm_index_cursor.hpp:83
The SeqAn FM Index.
Definition fm_index.hpp:185
Provides various transformation traits used by the range module.
Provides the unidirectional seqan3::fm_index.
Provides the seqan3::fm_index_cursor for searching in the unidirectional seqan3::fm_index.
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition alphabet/concept.hpp:152
@ seq
The "sequence", usually a range of nucleotides or amino acids.
text_layout
The possible text layouts (single, collection) the seqan3::fm_index and seqan3::bi_fm_index can suppo...
Definition search/fm_index/concept.hpp:69
@ single
The text is a single range.
Definition search/fm_index/concept.hpp:71
@ collection
The text is a range of ranges.
Definition search/fm_index/concept.hpp:73
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition slice.hpp:137
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
Provides seqan3::views::slice.
Provides alphabet adaptations for standard uint types.