Skip to content

Commit

Permalink
Factor out slot_map::underlying_swap from slot_map::partition.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quuxplusone committed Dec 3, 2018
1 parent 85f5713 commit f6ca140
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions SG14/slot_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,24 @@ class slot_map
return 1;
}

constexpr void underlying_swap(const_iterator cit, const_iterator cjt) {
// Swap *it and *jt in the underlying container,
// but then fix up their keys so they don't appear to move.
auto it_value_index = std::distance(this->cbegin(), cit);
auto it = std::next(this->begin(), it_value_index);
auto it_reversemap_iter = std::next(reverse_map_.begin(), it_value_index);
auto it_slot_iter = std::next(slots_.begin(), *it_reversemap_iter);
auto jt_value_index = std::distance(this->cbegin(), cjt);
auto jt = std::next(this->begin(), jt_value_index);
auto jt_reversemap_iter = std::next(reverse_map_.begin(), jt_value_index);
auto jt_slot_iter = std::next(slots_.begin(), *jt_reversemap_iter);

using std::swap;
swap(*it, *jt);
swap(*it_slot_iter, *jt_slot_iter);
swap(*it_reversemap_iter, *jt_reversemap_iter);
}

template<class Pred>
constexpr iterator partition(const Pred& pred) {
iterator it = this->begin();
Expand All @@ -294,19 +312,7 @@ class slot_map
--jt;
if (it == jt) return it;
}
// Swap *it and *jt in the underlying container,
// but then fix up their keys so they don't appear to move.
auto it_value_index = std::distance(values_.begin(), it);
auto it_reversemap_iter = std::next(reverse_map_.begin(), it_value_index);
auto it_slot_iter = std::next(slots_.begin(), *it_reversemap_iter);
auto jt_value_index = std::distance(values_.begin(), jt);
auto jt_reversemap_iter = std::next(reverse_map_.begin(), jt_value_index);
auto jt_slot_iter = std::next(slots_.begin(), *jt_reversemap_iter);

using std::swap;
swap(*it, *jt);
swap(*it_slot_iter, *jt_slot_iter);
swap(*it_reversemap_iter, *jt_reversemap_iter);
this->underlying_swap(it, jt);
++it;
if (it == jt) return it;
}
Expand Down

0 comments on commit f6ca140

Please sign in to comment.