Changeset 4026


Ignore:
Timestamp:
Jan 16, 2021, 8:43:53 AM (8 months ago)
Author:
Peter
Message:

add rvalue insert to SegmentSet?; closes #968.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/SegmentSet.h

    r3550 r4026  
    77  Copyright (C) 2010 Peter Johansson
    88  Copyright (C) 2012 Jari Häkkinen
    9   Copyright (C) 2014, 2016 Peter Johansson
     9  Copyright (C) 2014, 2016, 2021 Peter Johansson
    1010
    1111  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    4545   */
    4646  template<typename T, class Compare = std::less<T> >
    47   class SegmentSet 
    48     : public SegmentTree<std::set<Segment<T, Compare>, 
     47  class SegmentSet
     48    : public SegmentTree<std::set<Segment<T, Compare>,
    4949                                  SegmentCompare<T, Compare> >,
    5050                         Compare,
     
    7171
    7272    /**
     73       insert \a segment into set. If there is no gap between \a
     74       segment and neighboring segments the segments are merged.
     75
     76       \since New in yat 0.19
     77     */
     78    typename me::const_iterator
     79    insert_merge(typename me::value_type&& segment)
     80    {
     81      std::pair<typename me::iterator, typename me::iterator> p =
     82        this->overlap_range(segment);
     83      return move_insert_merge(p, std::move(segment));
     84    }
     85
     86    /**
    7387       \brief insert with a hint
    7488
     
    8397                                       const typename me::value_type& segment)
    8498    {
     99      typename me::value_type tmp(segment);
     100      return insert_merge(hint, std::move(tmp));
     101    }
     102
     103
     104    /**
     105       \since New in yat 0.19
     106     */
     107    typename me::iterator
     108    insert_merge(typename me::iterator hint,
     109                 typename me::value_type&& segment)
     110    {
    85111      std::pair<typename me::iterator, typename me::iterator> p(hint, hint);
    86112      if (this->container_.empty())
    87         return insert_merge(p, segment);
     113        return move_insert_merge(p, std::move(segment));
    88114
    89115      // If hint points to an element that is less than segment, hint
    90116      // is no good and we ignore the hint.
    91117      if (hint!=this->end() && compare(*hint, segment))
    92         return insert_merge(segment);
     118        return insert_merge(std::move(segment));
    93119
    94120      if (p.first!=this->begin()) {
     
    97123        // no good.
    98124        if (compare(segment, *p.first))
    99           return insert_merge(segment);
     125          return insert_merge(std::move(segment));
    100126        // find first element that is smaller than segment
    101127        while (p.first!=this->begin() && !compare(*p.first, segment))
     
    111137        ++p.second;
    112138
    113       return insert_merge(p, segment);
    114     }
     139      return move_insert_merge(p, std::move(segment));
     140    }
     141
    115142
    116143    /**
     
    132159    }
    133160  private:
    134     // used by public functions insert_merge.
     161    // used by functions merge_basic. This function does the actual
     162    // work. Note that it takes an rvalue; functions that only have
     163    // access to an lvalue const& need to create a (temporary) copy
     164    // that can be moved in.
    135165    //
    136166    // p.first points to the first segment that overlaps with \a segment or
     
    138168    // p.second points to the first segment that is greater than \a segment
    139169    typename me::const_iterator
    140     insert_merge(const std::pair<typename me::iterator,
    141                                  typename me::iterator>& p,
    142                  const typename me::value_type& segment)
     170    move_insert_merge(const std::pair<typename me::iterator,
     171                                      typename me::iterator>& p,
     172                      typename me::value_type&& segment)
    143173    {
    144174      YAT_ASSERT(p.first==this->end() || !compare(*p.first, segment));
    145175      YAT_ASSERT(p.second==this->end() || compare(segment, *p.second));
    146176      if (p.first==p.second) { // no overlap between set and segment
    147         return this->container_.insert(p.first, segment);
     177        return this->container_.insert(p.first, std::move(segment));
    148178      }
    149179      /*
     
    166196
    167197      this->container_.erase(p.first, p.second);
    168       return this->container_.insert(p.second, segment2);
     198      return this->container_.insert(p.second, std::move(segment2));
    169199    }
    170200
Note: See TracChangeset for help on using the changeset viewer.