Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search outparam #1991

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 72 additions & 5 deletions include/seqan3/search/fm_index/bi_fm_index_cursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,18 +964,85 @@ class bi_fm_index_cursor
{
assert(index != nullptr);

std::vector<std::pair<size_type, size_type>> occ;
std::vector<std::pair<size_type, size_type>> occ{};
occ.reserve(count());

if (!count())
return occ; // empty result vector

// fill occ with locations in order to not allocate another vector
for (size_type i = 0; i < count(); ++i)
occ.emplace_back(offset() - index->fwd_fm.index[fwd_lb + i], 0);
std::sort(occ.begin(), occ.end());

size_type seq_idx = index->fwd_fm.text_begin_rs.rank(occ[0].first + 1);
size_type seq_offset = index->fwd_fm.text_begin_ss.select(seq_idx);
size_type next_seq_offset = (seq_idx == index->fwd_fm.text_begin_rs.rank(index->size() - 1))
? index->size() - 1
: index->fwd_fm.text_begin_ss.select(seq_idx + 1);

// replace first occurrence value with correct entries
occ[0].second = occ[0].first - seq_offset; // position in seq i
occ[0].first = seq_idx - 1; // position in seq i

for (size_type i = 1; i < count(); ++i)
{
size_type loc = offset() - index->fwd_fm.index[fwd_lb + i];
size_type sequence_rank = index->fwd_fm.text_begin_rs.rank(loc + 1);
size_type sequence_position = loc - index->fwd_fm.text_begin_ss.select(sequence_rank);
occ.emplace_back(sequence_rank - 1, sequence_position);
if (occ[i].first >= next_seq_offset)
{
seq_idx = index->fwd_fm.text_begin_rs.rank(occ[i].first + 1);
seq_offset = index->fwd_fm.text_begin_ss.select(seq_idx);
}

// replace occurrence values with correct entries
occ[i].second = occ[i].first - seq_offset; // position in seq i
occ[i].first = seq_idx - 1; // position in seq i
}

return occ;
}

//!\overload
void locate(std::vector<std::pair<size_type, size_type>> & occ) const
//!\cond
requires index_t::text_layout_mode == text_layout::collection
//!\endcond
{
assert(index != nullptr);
auto const start = occ.size();
occ.reserve(start + count());

if (!count())
return; // empty result vector

// fill occ with locations in order to not allocate another vector
for (size_type i = 0; i < count(); ++i)
occ.emplace_back(offset() - index->fwd_fm.index[fwd_lb + i], 0);
std::sort(occ.begin(), occ.end());

size_type seq_idx = index->fwd_fm.text_begin_rs.rank(occ[0].first + 1);
size_type seq_offset = index->fwd_fm.text_begin_ss.select(seq_idx);
size_type next_seq_offset = (seq_idx == index->fwd_fm.text_begin_rs.rank(index->fwd_fm.size() - 1))
? index->fwd_fm.size() - 1
: index->fwd_fm.text_begin_ss.select(seq_idx + 1);

// replace first occurrence value with correct entries
occ[start].second = occ[start].first - seq_offset; // position in seq i
occ[start].first = seq_idx - 1; // position in seq i

for (size_type i = 1; i < count(); ++i)
{
if (occ[start + i].first >= next_seq_offset)
{
seq_idx = index->fwd_fm.text_begin_rs.rank(occ[start + i].first + 1);
seq_offset = index->fwd_fm.text_begin_ss.select(seq_idx);
}

// replace occurrence values with correct entries
occ[start + i].second = occ[start + i].first - seq_offset; // position in seq i
occ[start + i].first = seq_idx - 1; // position in seq i
}
}

/*!\brief Locates the occurrences of the searched query in the text on demand, i.e. a ranges::view is returned
* and every position is located once it is accessed.
* \returns Positions in the text.
Expand Down
58 changes: 44 additions & 14 deletions include/seqan3/search/fm_index/fm_index_cursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,39 +516,69 @@ class fm_index_cursor
*
* Strong exception guarantee (no data is modified in case an exception is thrown).
*/
std::vector<size_type> locate() const
void locate(std::vector<size_type> & occ) const
//!\cond
requires index_t::text_layout_mode == text_layout::single
//!\endcond
{
assert(index != nullptr);

std::vector<size_type> occ(count());
for (size_type i = 0; i < occ.size(); ++i)
occ.reserve(occ.size() + count());
for (size_type i = 0; i < count(); ++i)
{
occ[i] = offset() - index->index[node.lb + i];
occ.push_back(offset() - index->index[node.lb + i]);
}
return occ;
}

//!\overload
std::vector<std::pair<size_type, size_type>> locate() const
auto locate() const
{
using hit_t = std::conditional_t<index_t::text_layout_mode == text_layout::collection,
std::pair<size_type, size_type>,
size_type>;
std::vector<hit_t> occ{};
locate(occ);
return occ;
}

void locate(std::vector<std::pair<size_type, size_type>> & occ) const
//!\cond
requires index_t::text_layout_mode == text_layout::collection
//!\endcond
{
assert(index != nullptr);
auto const start = occ.size();
occ.reserve(start + count());

if (!count())
return; // empty result vector

std::vector<std::pair<size_type, size_type>> occ;
occ.reserve(count());
// fill occ with locations in order to not allocate another vector
for (size_type i = 0; i < count(); ++i)
occ.emplace_back(offset() - index->index[node.lb + i], 0);
std::sort(occ.begin(), occ.end());

size_type seq_idx = index->text_begin_rs.rank(occ[0].first + 1);
size_type seq_offset = index->text_begin_ss.select(seq_idx);
size_type next_seq_offset = (seq_idx == index->text_begin_rs.rank(index->size() - 1))
? index->size() - 1
: index->text_begin_ss.select(seq_idx + 1);

// replace first occurrence value with correct entries
occ[start].second = occ[start].first - seq_offset; // position in seq i
occ[start].first = seq_idx - 1; // position in seq i

for (size_type i = 1; i < count(); ++i)
{
size_type loc = offset() - index->index[node.lb + i];
size_type sequence_rank = index->text_begin_rs.rank(loc + 1);
size_type sequence_position = loc - index->text_begin_ss.select(sequence_rank);
occ.emplace_back(sequence_rank - 1, sequence_position);
if (occ[start + i].first >= next_seq_offset)
{
seq_idx = index->text_begin_rs.rank(occ[start + i].first + 1);
seq_offset = index->text_begin_ss.select(seq_idx);
}

// replace occurrence values with correct entries
occ[start + i].second = occ[start + i].first - seq_offset; // position in seq i
occ[start + i].first = seq_idx - 1; // position in seq i
}
return occ;
}

/*!\brief Locates the occurrences of the searched query in the text on demand, i.e. a ranges::view is returned and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ TYPED_TEST_P(fm_index_cursor_collection_test, lazy_locate)
TypeParam it = TypeParam(fm);
it.extend_right("ACG"_dna4);

EXPECT_TRUE(std::ranges::equal(it.locate(), it.lazy_locate()));
EXPECT_TRUE(std::ranges::equal(seqan3::uniquify(it.locate()), seqan3::uniquify(it.lazy_locate())));
}

TYPED_TEST_P(fm_index_cursor_collection_test, concept_check)
Expand Down