Changeset 3174


Ignore:
Timestamp:
Mar 8, 2014, 3:51:20 PM (9 years ago)
Author:
Peter
Message:

#refs 784

Speed up copy and assignment by keeping pointers of maps rather than
hard copies. Incrementing input iterators is expected to [possibly]
modify underlying resource. Take istream_iterator as an example, which
obviously modifies the istream it points to when it's incremented and
thus effects other iterators that point to the same istream. This is
described in concept description as that input iterators are only
guaranteed to be a single pass iterator. Given that users do not
expect input iterators to be independent, we can use that to speed up
copy constructor by only copiding a [smart] pointer rather than the
whole map, which might get quite heavy if there are a lot of pairs
mapped far away from each other.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/omic/BamPairIterator.h

    r3173 r3174  
    2929#include <boost/concept/assert.hpp>
    3030#include <boost/iterator/iterator_facade.hpp>
     31#include <boost/shared_ptr.hpp>
    3132
    3233#include <iterator>
     
    5152     stepping until it finds a read whose mate it has already seen, and
    5253     operator* return the pair of BamReads.
     54
     55     Note that BamPairIterator is a single-pass iterator, i.e., once it
     56     is incremented the behaviour of its copies is undefined.
    5357   */
    5458  template<typename Base>
     
    7882    Base end_;
    7983    std::pair<BamRead, BamRead> x_;
    80     std::map<std::string, BamRead> siam_reads_;
     84    boost::shared_ptr<std::map<std::string, BamRead> > siam_reads_;
    8185    typedef std::pair<int32_t, int32_t> Position;
    82     std::multimap<Position, BamRead> reads_;
     86    boost::shared_ptr<std::multimap<Position, BamRead> > reads_;
    8387    friend class boost::iterator_core_access;
    8488
     
    116120  template<typename Base>
    117121  BamPairIterator<Base>::BamPairIterator(Base base, Base end)
    118     : iter_(base), end_(end)
     122    : iter_(base), end_(end),
     123      siam_reads_(new std::map<std::string, BamRead>),
     124      reads_(new std::multimap<Position, BamRead>)
    119125  {
    120126    BOOST_CONCEPT_ASSERT((boost::InputIterator<Base>));
     
    167173      Position mate_position(iter_->mtid(), iter_->mpos());
    168174      // clear siam reads if iter is more advanced than siam reads
    169       if (siam_reads_.size() && less(siam_reads_.begin()->second, *iter_))
    170         siam_reads_.clear();
     175      if (siam_reads_->size() && less(siam_reads_->begin()->second, *iter_))
     176        siam_reads_->clear();
    171177
    172178      // have not seen mate yet
    173179      if (mate_position > position)
    174         reads_.insert(std::make_pair(mate_position, *iter_));
     180        reads_->insert(std::make_pair(mate_position, *iter_));
    175181      else if (position > mate_position) {
    176182        std::multimap<Position, BamRead>::iterator
    177           lower = reads_.lower_bound(position);
     183          lower = reads_->lower_bound(position);
    178184        // erase all reads with mate position less than current
    179185        // position (assuming input range is sorted)
    180         reads_.erase(reads_.begin(), lower);
     186        reads_->erase(reads_->begin(), lower);
    181187
    182188        // find mate and assign pair
    183         for (; lower!=reads_.end() && lower->first == position; ++lower)
     189        for (; lower!=reads_->end() && lower->first == position; ++lower)
    184190          if (same_query_name(lower->second, *iter_)) {
    185191            x_.first = lower->second;
     
    191197        // check if we have already seen mate and stored it in map
    192198        std::map<std::string, BamRead>::iterator
    193           mate = siam_reads_.lower_bound(iter_->name());
    194         if (mate!=siam_reads_.end() && same_query_name(mate->second, *iter_)) {
     199          mate = siam_reads_->lower_bound(iter_->name());
     200        if (mate!=siam_reads_->end() && same_query_name(mate->second, *iter_)) {
    195201          x_.first = mate->second;
    196202          x_.second = *iter_;
     
    199205        else
    200206          // insert with hint
    201           siam_reads_.insert(mate, std::make_pair(iter_->name(), *iter_));
     207          siam_reads_->insert(mate, std::make_pair(iter_->name(), *iter_));
    202208      }
    203209    }
Note: See TracChangeset for help on using the changeset viewer.