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

[FIX] Incorrect copy/move of fm_index #1144

Merged
merged 1 commit into from
Jul 3, 2019
Merged

Conversation

eseiler
Copy link
Member

@eseiler eseiler commented Jun 28, 2019

Resolves #1121

@codecov
Copy link

codecov bot commented Jun 28, 2019

Codecov Report

Merging #1144 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1144      +/-   ##
==========================================
+ Coverage   96.42%   96.42%   +<.01%     
==========================================
  Files         196      196              
  Lines        7740     7756      +16     
==========================================
+ Hits         7463     7479      +16     
  Misses        277      277
Impacted Files Coverage Δ
include/seqan3/search/fm_index/fm_index.hpp 100% <100%> (ø) ⬆️
include/seqan3/search/fm_index/bi_fm_index.hpp 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6725719...ec11ae8. Read the comment docs.

@eseiler eseiler requested a review from smehringer June 28, 2019 13:50
Copy link
Member

@smehringer smehringer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are missing!

include/seqan3/search/fm_index/fm_index.hpp Outdated Show resolved Hide resolved
include/seqan3/search/fm_index/fm_index.hpp Outdated Show resolved Hide resolved
@eseiler eseiler requested a review from h-2 July 1, 2019 13:43
rhs.text_begin_ss.set_vector(&rhs.text_begin);
rhs.text_begin_rs.set_vector(&rhs.text_begin);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since C++11 Copy'n'swap is typically implemented like this:

  • copy constructor with copy implementation
  • move constructor with move implementation
  • assignment operator that takes by value and std::swaps
  • You very rarely need to define a .swap() member since C++11. Always use std::swap which uses the move constructor automatically.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write it like this:

fm_index(fm_index const & rhs) :
    index{rhs.index}, sigma{rhs.sigma}, text_begin{rhs.text_begin}, text_begin_ss{rhs.text_begin_ss}
{
    text_begin_ss.set_vector(&text_begin);
    text_begin_rs.set_vector(&text_begin);
}

fm_index(fm_index && rhs) :
    index{std::move(rhs.index)}, sigma{std::move(rhs.sigma)}, text_begin{std::move(rhs.text_begin)}, text_begin_ss{std::move(rhs.text_begin_ss)}
{
    text_begin_ss.set_vector(&text_begin);
    text_begin_rs.set_vector(&text_begin);
}

fm_index & operator=(fm_index rhs) //!< Delegate to swap().
{
    std::swap(*this, rhs);
    return *this;
}

This clearly highlights that only thing non-standard that happens is calling set_vector for two members.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhm, using std::swap segfaults. It doesn't seem to set the pointers correctly.

Copy link
Member

@h-2 h-2 Jul 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that sounds weird. Then again, maybe std::swap uses assignment which uses std::swap ... 🤔

In any case, if it works now and std::swap works now as well, we could also just put move assignment of the members (no swap required) into the assignment operator and get rid of the swap member, or not? I really don't like having a swap member, it looks weird 😇

Copy link
Member Author

@eseiler eseiler Jul 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that sounds weird. Then again, maybe std::swap uses assignment which uses std::swap ... 🤔

It kinda looked like it in the debugger.

In any case, if it works now and std::swap works now as well, we could also just put move assignment of the members (no swap required) into the assignment operator and get rid of the swap member, or not? I really don't like having a swap member, it looks weird 😇

Ok, done!

fm_index & operator=(fm_index &&) = default; //!< Move assignment.
~fm_index() = default; //!< Destructor.
fm_index() = default; //!< Defaulted.
fm_index(fm_index const & rhs) //!< When copy constructing, also set the pointers of rank and select support.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do users of the fm_index (need to) know what rank and select support are?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When copy constructing, also update internal data structures. ?

@eseiler eseiler requested a review from h-2 July 2, 2019 14:42
@eseiler eseiler force-pushed the fix/fm_index_cm branch 2 times, most recently from 66c1f38 to 7aa94b0 Compare July 2, 2019 15:32
@h-2 h-2 merged commit f4e8fd4 into seqan:master Jul 3, 2019
@eseiler eseiler deleted the fix/fm_index_cm branch July 4, 2019 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Processing returned ranges from search segfaults
3 participants