19 #include <sdsl/suffix_trees.hpp>
28 namespace seqan3::detail
33 struct fm_index_validator
49 template <semialphabet alphabet_t, text_layout text_layout_mode_, std::ranges::range text_t>
50 static void validate(text_t && text)
54 static_assert(std::ranges::bidirectional_range<text_t>,
"The text must model bidirectional_range.");
55 static_assert(std::convertible_to<range_innermost_value_t<text_t>, alphabet_t>,
56 "The alphabet of the text collection must be convertible to the alphabet of the index.");
57 static_assert(range_dimension_v<text_t> == 1,
"The input cannot be a text collection.");
59 if (std::ranges::empty(text))
64 static_assert(std::ranges::bidirectional_range<text_t>,
65 "The text collection must model bidirectional_range.");
66 static_assert(std::ranges::bidirectional_range<std::ranges::range_reference_t<text_t>>,
67 "The elements of the text collection must model bidirectional_range.");
68 static_assert(std::convertible_to<range_innermost_value_t<text_t>, alphabet_t>,
69 "The alphabet of the text collection must be convertible to the alphabet of the index.");
70 static_assert(range_dimension_v<text_t> == 2,
"The input must be a text collection.");
72 if (std::ranges::empty(text))
75 static_assert(
alphabet_size<range_innermost_value_t<text_t>> <= 256,
"The alphabet is too big.");
85 template <
typename index_t>
86 class fm_index_cursor;
88 template <
typename index_t>
89 class bi_fm_index_cursor;
95 detail::sdsl_index sdsl_index_type_>
96 class reverse_fm_index;
136 sdsl::csa_wt<sdsl::wt_blcd<sdsl::bit_vector,
137 sdsl::rank_support_v<>,
138 sdsl::select_support_scan<>,
139 sdsl::select_support_scan<0>>,
142 sdsl::sa_order_sa_sampling<>,
143 sdsl::isa_sampling<>,
144 sdsl::plain_byte_alphabet>;
199 using sdsl_index_type = sdsl_index_type_;
203 using sdsl_char_type =
typename sdsl_index_type::alphabet_type::char_type;
205 using sdsl_sigma_type =
typename sdsl_index_type::alphabet_type::sigma_type;
208 friend class detail::reverse_fm_index<alphabet_t, text_layout_mode_, sdsl_index_type_>;
211 sdsl_index_type index;
214 sdsl::sd_vector<> text_begin;
216 sdsl::select_support_sd<1> text_begin_ss;
218 sdsl::rank_support_sd<1> text_begin_rs;
239 template <std::ranges::range text_t>
243 void construct(text_t && text)
245 detail::fm_index_validator::validate<alphabet_t, text_layout_mode_>(text);
247 constexpr
auto sigma = alphabet_size<alphabet_t>;
254 sdsl::int_vector<8> tmp_text(std::ranges::distance(text));
260 if constexpr (sigma == 256)
264 "character alphabets the last one/two values are reserved"
265 "(single sequence/collection).");
269 | std::views::reverse,
270 std::ranges::begin(tmp_text));
272 sdsl::construct_im(index, tmp_text, 0);
281 template <std::ranges::range text_t>
285 void construct(text_t && text,
bool reverse =
false)
287 detail::fm_index_validator::validate<alphabet_t, text_layout_mode_>(text);
291 for (
auto && t : text)
292 text_sizes.
push_back(std::ranges::distance(t));
294 size_t const number_of_texts{text_sizes.
size()};
299 if (number_of_texts == text_size)
302 constexpr
auto sigma = alphabet_size<alphabet_t>;
307 sdsl::sd_vector_builder builder(text_size, number_of_texts);
308 size_t prefix_sum{0};
310 for (
auto &&
size : text_sizes)
312 builder.set(prefix_sum);
313 prefix_sum +=
size + 1;
316 text_begin = sdsl::sd_vector<>(builder);
317 text_begin_ss = sdsl::select_support_sd<1>(&text_begin);
318 text_begin_rs = sdsl::rank_support_sd<1>(&text_begin);
321 sdsl::int_vector<8> tmp_text(text_size - (number_of_texts > 1));
323 constexpr uint8_t delimiter = sigma >= 255 ? 255 : sigma + 1;
331 if constexpr (sigma >= 255)
335 " for full character alphabets the last one/"
336 "two values are reserved (single sequence/"
343 std::ranges::begin(tmp_text));
348 if (number_of_texts == 1)
349 tmp_text.back() = delimiter;
351 std::ranges::reverse(tmp_text);
358 if (number_of_texts == 1)
360 std::ranges::reverse(tmp_text.begin(), tmp_text.end() - 1);
361 tmp_text.back() = delimiter;
365 std::ranges::reverse(tmp_text);
369 sdsl::construct_im(index, tmp_text, 0);
387 template <
typename bi_fm_index_t>
390 template <
typename fm_index_t>
393 template <
typename fm_index_t>
394 friend class detail::fm_index_cursor_node;
403 index{rhs.index}, text_begin{rhs.text_begin}, text_begin_ss{rhs.text_begin_ss}, text_begin_rs{rhs.text_begin_rs}
405 text_begin_ss.set_vector(&text_begin);
406 text_begin_rs.set_vector(&text_begin);
411 index{
std::
move(rhs.index)}, text_begin{
std::
move(rhs.text_begin)},text_begin_ss{
std::
move(rhs.text_begin_ss)},
412 text_begin_rs{
std::
move(rhs.text_begin_rs)}
414 text_begin_ss.set_vector(&text_begin);
415 text_begin_rs.set_vector(&text_begin);
423 text_begin_ss =
std::move(rhs.text_begin_ss);
424 text_begin_rs =
std::move(rhs.text_begin_rs);
426 text_begin_ss.set_vector(&text_begin);
427 text_begin_rs.set_vector(&text_begin);
442 template <std::ranges::b
idirectional_range text_t>
445 construct(std::forward<text_t>(text));
495 return (index == rhs.index) && (text_begin == rhs.text_begin);
511 return !(*
this == rhs);
540 template <cereal_archive archive_t>
541 void CEREAL_SERIALIZE_FUNCTION_NAME(archive_t & archive)
545 archive(text_begin_ss);
546 text_begin_ss.set_vector(&text_begin);
547 archive(text_begin_rs);
548 text_begin_rs.set_vector(&text_begin);
550 auto sigma = alphabet_size<alphabet_t>;
552 if (sigma != alphabet_size<alphabet_t>)
555 " but it is being read into an fm_index with an alphabet of size " +
564 (tmp ?
"text collection" :
"single text") +
565 " but it is being read into an fm_index expecting a " +
577 template <std::ranges::range text_t>
584 namespace seqan3::detail
602 class reverse_fm_index :
public fm_index<alphabet_t, text_layout_mode, sdsl_index_type>
606 template <std::ranges::range text_t>
607 void construct_(text_t && text)
611 auto reverse_text = text | std::views::reverse;
612 this->construct(reverse_text);
616 auto reverse_text = text | views::deep{std::views::reverse} | std::views::reverse;
617 this->construct(reverse_text,
true);
625 template <std::ranges::b
idirectional_range text_t>
626 explicit reverse_fm_index(text_t && text)
628 construct_(std::forward<text_t>(text));
Adaptations of algorithms from the Ranges TS.
Provides seqan3::views::to_rank.
The SeqAn Bidirectional FM Index Cursor.
Definition: bi_fm_index_cursor.hpp:58
The SeqAn FM Index Cursor.
Definition: fm_index_cursor.hpp:90
The SeqAn FM Index.
Definition: fm_index.hpp:193
fm_index()=default
Defaulted.
static constexpr text_layout text_layout_mode
Indicates whether index is built over a collection.
Definition: fm_index.hpp:374
cursor_type cursor() const noexcept
Returns a seqan3::fm_index_cursor on the index that can be used for searching.
Definition: fm_index.hpp:528
size_type size() const noexcept
Returns the length of the indexed text including sentinel characters.
Definition: fm_index.hpp:460
bool operator!=(fm_index const &rhs) const noexcept
Compares two indices.
Definition: fm_index.hpp:509
bool empty() const noexcept
Checks whether the index is empty.
Definition: fm_index.hpp:476
bool operator==(fm_index const &rhs) const noexcept
Compares two indices.
Definition: fm_index.hpp:492
fm_index & operator=(fm_index rhs)
When copy/move assigning, also update internal data structures.
Definition: fm_index.hpp:419
~fm_index()=default
Defaulted.
alphabet_t alphabet_type
The type of the underlying character of the indexed text.
Definition: fm_index.hpp:380
fm_index(fm_index &&rhs)
When move constructing, also update internal data structures.
Definition: fm_index.hpp:410
typename sdsl_index_type::size_type size_type
Type for representing positions in the indexed text.
Definition: fm_index.hpp:382
fm_index(text_t &&text)
Constructor that immediately constructs the index given a range. The range cannot be empty.
Definition: fm_index.hpp:443
fm_index(fm_index const &rhs)
When copy constructing, also update internal data structures.
Definition: fm_index.hpp:402
Provides various transformation traits used by the range module.
Provides the internal representation of a node of the seqan3::fm_index_cursor.
This header includes C++17 filesystem support and imports it into namespace std::filesystem (independ...
Provides the seqan3::fm_index_cursor for searching in the unidirectional seqan3::fm_index.
constexpr auto alphabet_size
A type trait that holds the size of a (semi-)alphabet.
Definition: concept.hpp:858
text_layout
The possible text layouts (single, collection) the seqan3::fm_index and seqan3::bi_fm_index can suppo...
Definition: concept.hpp:81
sdsl_wt_index_type default_sdsl_index_type
The default FM Index Configuration.
Definition: fm_index.hpp:150
sdsl::csa_wt< sdsl::wt_blcd< sdsl::bit_vector, sdsl::rank_support_v<>, sdsl::select_support_scan<>, sdsl::select_support_scan< 0 > >, 16, 10 '000 '000, sdsl::sa_order_sa_sampling<>, sdsl::isa_sampling<>, sdsl::plain_byte_alphabet > sdsl_wt_index_type
The FM Index Configuration using a Wavelet Tree.
Definition: fm_index.hpp:144
fm_index(text_t &&) -> fm_index< range_innermost_value_t< text_t >, text_layout
Deduces the alphabet and dimensions of the text.
Definition: fm_index.hpp:578
@ single
The text is a single range.
Definition: concept.hpp:83
@ collection
The text is a range of ranges.
Definition: concept.hpp:85
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:471
constexpr auto join_with
A join view, please use std::views::join if you don't need a separator.
Definition: join_with.hpp:37
auto const to_rank
A view that calls seqan3::to_rank() on each element in the input range.
Definition: to_rank.hpp:71
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:74
The basis for seqan3::alphabet, but requires only rank interface (not char).
Provides seqan3::views::join_with.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Adaptations of concepts from the Ranges TS.
Provides the concept for seqan3::detail::sdsl_index.