18#include <sdsl/suffix_trees.hpp>
53template <
typename index_t>
72 index_type::text_layout_mode,
73 typename index_type::sdsl_index_type>>;
78 using sdsl_char_type =
typename index_type::sdsl_char_type;
80 using sdsl_index_type =
typename index_t::sdsl_index_type;
82 using sdsl_sigma_type =
typename index_type::sdsl_sigma_type;
84 using index_alphabet_type =
typename index_t::alphabet_type;
107 sdsl_sigma_type sigma{};
124 sdsl_char_type _last_char{};
135 bool fwd_cursor_last_used =
false;
146 template <
typename csa_t>
147 requires (std::same_as<csa_t, typename index_type::sdsl_index_type>
148 || std::same_as<csa_t, typename index_type::rev_sdsl_index_type>)
bool
149 bidirectional_search(csa_t
const & csa,
150 sdsl_char_type
const c,
156 assert((l_fwd <= r_fwd) && (r_fwd < csa.size()));
157 assert(r_fwd + 1 >= l_fwd);
158 assert(r_bwd + 1 - l_bwd == r_fwd + 1 - l_fwd);
160 size_type _l_fwd, _r_fwd, _l_bwd, _r_bwd;
163 if constexpr (!std::same_as<index_alphabet_type, sdsl::plain_byte_alphabet>)
165 cc = csa.char2comp[c];
166 if (cc == 0 && c > 0)
171 if (r_fwd + 1 - l_fwd == csa.size())
175 _r_fwd = csa.C[cc + 1] - 1;
181 auto const r_s_b = csa.wavelet_tree.lex_count(l_fwd, r_fwd + 1, c);
182 size_type const rank_l = std::get<0>(r_s_b);
183 size_type const s = std::get<1>(r_s_b), b = std::get<2>(r_s_b);
184 size_type const rank_r = r_fwd - l_fwd - s - b + rank_l;
185 _l_fwd = c_begin + rank_l;
186 _r_fwd = c_begin + rank_r;
191 if (_r_fwd >= _l_fwd)
197 assert(r_fwd + 1 >= l_fwd);
198 assert(r_bwd + 1 - l_bwd == r_fwd + 1 - l_fwd);
205 template <
typename csa_t>
206 requires (std::same_as<csa_t, typename index_type::sdsl_index_type>
207 || std::same_as<csa_t, typename index_type::rev_sdsl_index_type>)
bool
208 bidirectional_search_cycle(csa_t
const & csa,
209 sdsl_char_type
const c,
217 assert((l_parent <= r_parent) && (r_parent < csa.size()));
220 if constexpr (std::same_as<index_alphabet_type, sdsl::plain_byte_alphabet>)
223 c_begin = csa.C[csa.char2comp[c]];
225 auto const r_s_b = csa.wavelet_tree.lex_count(l_parent, r_parent + 1, c);
226 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),
227 rank_r = r_parent - l_parent - s - b + rank_l;
229 size_type const _l_fwd = c_begin + rank_l;
230 size_type const _r_fwd = c_begin + rank_r;
232 size_type const _r_bwd = r_bwd + 1 + rank_r - rank_l;
234 if (_r_fwd >= _l_fwd)
240 assert(r_fwd + 1 >= l_fwd);
241 assert(r_bwd + 1 - l_bwd == r_fwd + 1 - l_fwd);
265 fwd_rb(_index.
size() - 1),
267 rev_rb(_index.
size() - 1),
268 sigma(_index.fwd_fm.index.sigma - index_t::text_layout_mode),
287 assert(index !=
nullptr);
289 assert(!(fwd_lb == rhs.fwd_lb && fwd_rb == rhs.fwd_rb && depth == rhs.depth) || (depth == 0)
290 || (parent_lb == rhs.parent_lb && parent_rb == rhs.parent_rb && _last_char == rhs._last_char));
292 return std::tie(fwd_lb, fwd_rb, depth) ==
std::tie(rhs.fwd_lb, rhs.fwd_rb, rhs.depth);
309 assert(index !=
nullptr);
311 return !(*
this == rhs);
334 fwd_cursor_last_used =
true;
337 assert(index !=
nullptr);
339 size_type new_parent_lb = fwd_lb, new_parent_rb = fwd_rb;
341 sdsl_char_type c = 1;
343 && !bidirectional_search(index->fwd_fm.index,
344 index->fwd_fm.index.comp2char[c],
355 parent_lb = new_parent_lb;
356 parent_rb = new_parent_rb;
386 fwd_cursor_last_used =
false;
389 assert(index !=
nullptr);
391 size_type new_parent_lb = rev_lb, new_parent_rb = rev_rb;
393 sdsl_char_type c = 1;
395 && !bidirectional_search(index->rev_fm.index,
396 index->rev_fm.index.comp2char[c],
407 parent_lb = new_parent_lb;
408 parent_rb = new_parent_rb;
431 template <
typename char_t>
432 requires std::convertible_to<char_t, index_alphabet_type>
bool
436 fwd_cursor_last_used =
true;
439 assert(index !=
nullptr);
445 size_type new_parent_lb = fwd_lb, new_parent_rb = fwd_rb;
447 auto c_char =
seqan3::to_rank(
static_cast<index_alphabet_type
>(c)) + 1;
448 if (bidirectional_search(index->fwd_fm.index, c_char, fwd_lb, fwd_rb, rev_lb, rev_rb))
450 parent_lb = new_parent_lb;
451 parent_rb = new_parent_rb;
462 template <
typename char_type>
463 requires seqan3::detail::is_char_adaptation_v<char_type>
bool
482 template <
typename char_t>
483 requires std::convertible_to<char_t, index_alphabet_type>
bool
487 fwd_cursor_last_used =
false;
490 assert(index !=
nullptr);
496 size_type new_parent_lb = rev_lb, new_parent_rb = rev_rb;
498 auto c_char =
seqan3::to_rank(
static_cast<index_alphabet_type
>(c)) + 1;
499 if (bidirectional_search(index->rev_fm.index, c_char, rev_lb, rev_rb, fwd_lb, fwd_rb))
501 parent_lb = new_parent_lb;
502 parent_rb = new_parent_rb;
513 template <
typename char_type>
514 requires seqan3::detail::is_char_adaptation_v<char_type>
bool
536 template <std::ranges::range seq_t>
539 static_assert(std::ranges::forward_range<seq_t>,
"The query must model forward_range.");
540 static_assert(std::convertible_to<range_innermost_value_t<seq_t>, index_alphabet_type>,
541 "The alphabet of the sequence must be convertible to the alphabet of the index.");
543 assert(index !=
nullptr);
546 auto last = std::ranges::end(
seq);
549 fwd_cursor_last_used = (first != last);
552 size_type _fwd_lb = fwd_lb, _fwd_rb = fwd_rb, _rev_lb = rev_lb, _rev_rb = rev_rb;
553 size_type new_parent_lb = parent_lb, new_parent_rb = parent_rb;
554 sdsl_char_type c = _last_char;
557 for (
auto it = first; it != last; ++len, ++it)
566 new_parent_lb = _fwd_lb;
567 new_parent_rb = _fwd_rb;
568 if (!bidirectional_search(index->fwd_fm.index, c, _fwd_lb, _fwd_rb, _rev_lb, _rev_rb))
577 parent_lb = new_parent_lb;
578 parent_rb = new_parent_rb;
606 template <std::ranges::range seq_t>
609 static_assert(std::ranges::bidirectional_range<seq_t>,
"The query must model bidirectional_range.");
610 static_assert(std::convertible_to<range_innermost_value_t<seq_t>, index_alphabet_type>,
611 "The alphabet of the sequence must be convertible to the alphabet of the index.");
612 assert(index !=
nullptr);
614 auto rev_seq = std::views::reverse(
seq);
616 auto last = std::ranges::end(rev_seq);
620 fwd_cursor_last_used =
false;
623 size_type _fwd_lb = fwd_lb, _fwd_rb = fwd_rb, _rev_lb = rev_lb, _rev_rb = rev_rb;
624 size_type new_parent_lb = parent_lb, new_parent_rb = parent_rb;
625 sdsl_char_type c = _last_char;
628 for (
auto it = first; it != last; ++len, ++it)
637 new_parent_lb = _rev_lb;
638 new_parent_rb = _rev_rb;
639 if (!bidirectional_search(index->rev_fm.index, c, _rev_lb, _rev_rb, _fwd_lb, _fwd_rb))
648 parent_lb = new_parent_lb;
649 parent_rb = new_parent_rb;
686 assert(fwd_cursor_last_used);
691 sdsl_char_type c = _last_char + 1;
694 && !bidirectional_search_cycle(index->fwd_fm.index,
695 index->fwd_fm.index.comp2char[c],
745 assert(!fwd_cursor_last_used);
750 sdsl_char_type c = _last_char + 1;
752 && !bidirectional_search_cycle(index->rev_fm.index,
753 index->rev_fm.index.comp2char[c],
793 return index->fwd_fm.index.comp2char[_last_char] - 1;
810 assert(index !=
nullptr);
812 assert(depth != 0 || (fwd_lb == rev_lb && fwd_rb == rev_rb && fwd_lb == 0 && fwd_rb == index->size() - 1));
838 assert(index !=
nullptr);
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.");
878 static_assert(range_dimension_v<text_t> == 1,
"The input cannot be a text collection.");
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.");
881 assert(index !=
nullptr);
883 size_type const query_begin = offset() - index->fwd_fm.index[fwd_lb];
888 template <std::ranges::range text_t>
892 static_assert(std::ranges::input_range<text_t>,
"The text collection must model input_range.");
893 static_assert(range_dimension_v<text_t> == 2,
"The input must be a text collection.");
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.");
896 assert(index !=
nullptr);
899 size_type const location = offset() - index->fwd_fm.index[fwd_lb];
903 size_type const rank = index->fwd_fm.text_begin_rs.rank(location + 1);
908 size_type const start_location = index->fwd_fm.text_begin_ss.select(rank);
910 size_type const query_begin = location - start_location;
929 assert(index !=
nullptr && (1 + fwd_rb - fwd_lb == 1 + rev_rb - rev_lb));
931 return 1 + fwd_rb - fwd_lb;
948 assert(index !=
nullptr);
954 occ.emplace_back(0, offset() - index->fwd_fm.index[fwd_lb + i]);
963 assert(index !=
nullptr);
969 size_type loc = offset() - index->fwd_fm.index[fwd_lb + i];
970 size_type sequence_rank = index->fwd_fm.text_begin_rs.rank(loc + 1);
971 size_type sequence_position = loc - index->fwd_fm.text_begin_ss.select(sequence_rank);
992 assert(index !=
nullptr);
994 return std::views::iota(fwd_lb, fwd_lb +
count())
996 [*
this, _offset = offset()](
auto sa_pos)
1006 assert(index !=
nullptr);
1008 return std::views::iota(fwd_lb, fwd_lb +
count())
1010 [*
this, _offset = offset()](
auto sa_pos)
1012 auto loc = _offset - index->fwd_fm.index[sa_pos];
1013 size_type sequence_rank = index->fwd_fm.text_begin_rs.rank(loc + 1);
1014 size_type sequence_position = loc - index->fwd_fm.text_begin_ss.select(sequence_rank);
1026 template <cereal_archive archive_t>
1027 void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
1036 archive(_last_char);
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:55
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:331
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:607
bool operator==(bi_fm_index_cursor const &rhs) const noexcept
Compares two cursors.
Definition: bi_fm_index_cursor.hpp:285
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:515
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:383
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:682
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:741
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:464
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:58
typename index_type::size_type size_type
Type for representing positions in the indexed text.
Definition: bi_fm_index_cursor.hpp:64
bool operator!=(bi_fm_index_cursor const &rhs) const noexcept
Compares two cursors.
Definition: bi_fm_index_cursor.hpp:307
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:537
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:433
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:484
The SeqAn FM Index Cursor.
Definition: fm_index_cursor.hpp:87
The SeqAn FM Index.
Definition: fm_index.hpp:189
Provides various transformation traits used by the range module.
T emplace_back(T... args)
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: concept.hpp:155
@ 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: concept.hpp:91
@ single
The text is a single range.
Definition: concept.hpp:93
@ collection
The text is a range of ranges.
Definition: concept.hpp:95
decltype(detail::transform< trait_t >(list_t{})) transform
Apply a transformation trait to every type in the list and return a seqan3::type_list of the results.
Definition: traits.hpp:470
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:146
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition: slice.hpp:178
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
Provides seqan3::views::slice.
Provides alphabet adaptations for standard uint types.