Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
VariantIterator: remove unnecessary move construction
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauricio Carneiro committed Aug 19, 2014
1 parent 448320a commit adf2514
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
20 changes: 7 additions & 13 deletions gamgee/variant_iterator.cpp
Expand Up @@ -16,22 +16,17 @@ VariantIterator::VariantIterator(vcfFile* variant_file_ptr, const std::shared_pt
m_variant_file_ptr {variant_file_ptr},
m_variant_header_ptr {variant_header_ptr},
m_variant_record_ptr {utils::make_shared_variant(bcf_init1())}, ///< important to initialize the record buffer in the constructor so we can reuse it across the iterator
m_variant_record {fetch_next_record()}
{}

VariantIterator::VariantIterator(VariantIterator&& original) :
m_variant_file_ptr {move(original.m_variant_file_ptr)},
m_variant_header_ptr {move(original.m_variant_header_ptr)},
m_variant_record_ptr {move(original.m_variant_record_ptr)},
m_variant_record {move(original.m_variant_record)}
{}
m_variant_record {m_variant_header_ptr, m_variant_record_ptr}
{
fetch_next_record();
}

Variant& VariantIterator::operator*() {
return m_variant_record;
}

Variant& VariantIterator::operator++() {
m_variant_record = fetch_next_record();
fetch_next_record();
return m_variant_record;
}

Expand All @@ -47,12 +42,11 @@ bool VariantIterator::empty() {
* @brief pre-fetches the next variant record
* @warning we're reusing the existing htslib memory, so users should be aware that all objects from the previous iteration are now stale unless a deep copy has been performed
*/
Variant VariantIterator::fetch_next_record() {
void VariantIterator::fetch_next_record() {
if (bcf_read1(m_variant_file_ptr, m_variant_header_ptr.get(), m_variant_record_ptr.get()) < 0) {
m_variant_file_ptr = nullptr;
return Variant{};
m_variant_record = Variant{};
}
return Variant{m_variant_header_ptr, m_variant_record_ptr};
}

}
Expand Down
9 changes: 7 additions & 2 deletions gamgee/variant_iterator.h
Expand Up @@ -31,7 +31,12 @@ class VariantIterator {
/**
* @brief a VariantIterator move constructor guarantees all objects will have the same state.
*/
VariantIterator(VariantIterator&&);
VariantIterator(VariantIterator&&) = default;

/**
* @brief a VariantIterator move assignment operator guarantees all objects will have the same state.
*/
VariantIterator& operator= (VariantIterator&&) = default;

/**
* @brief inequality operator (needed by for-each loop)
Expand Down Expand Up @@ -71,7 +76,7 @@ class VariantIterator {
std::shared_ptr<bcf1_t> m_variant_record_ptr; ///< pointer to the internal structure of the variant record. Useful to only allocate it once.
Variant m_variant_record; ///< temporary record to hold between fetch (operator++) and serve (operator*)

Variant fetch_next_record(); ///< fetches next Variant record into existing htslib memory without making a copy
void fetch_next_record(); ///< fetches next Variant record into existing htslib memory without making a copy
};

} // end namespace gamgee
Expand Down

0 comments on commit adf2514

Please sign in to comment.