diff --git a/util/joint_sort.hh b/util/joint_sort.hh index 13a52b67b..b1ec48e26 100644 --- a/util/joint_sort.hh +++ b/util/joint_sort.hh @@ -40,6 +40,12 @@ template class JointIter { swap(first.value_, second.value_); } + void DeepSwap(JointIter &other) { + using std::swap; + swap(*key_, *other.key_); + swap(*value_, *other.value_); + } + private: friend class JointProxy; KeyIter key_; @@ -84,10 +90,7 @@ template class JointProxy { } friend void swap(JointProxy first, JointProxy second) { - // Allow argument-dependent lookup. - using std::swap; - swap(*first.inner_.key_, *second.inner_.key_); - swap(*first.inner_.value_, *second.inner_.value_); + first.Inner().DeepSwap(second.Inner()); } private: diff --git a/util/joint_sort_test.cc b/util/joint_sort_test.cc index 4dc859164..b24c602c9 100644 --- a/util/joint_sort_test.cc +++ b/util/joint_sort_test.cc @@ -47,4 +47,16 @@ BOOST_AUTO_TEST_CASE(char_int) { BOOST_CHECK_EQUAL(327, values[3]); } +BOOST_AUTO_TEST_CASE(swap_proxy) { + char keys[2] = {0, 1}; + int values[2] = {2, 3}; + detail::JointProxy first(keys, values); + detail::JointProxy second(keys + 1, values + 1); + swap(first, second); + BOOST_CHECK_EQUAL(1, keys[0]); + BOOST_CHECK_EQUAL(0, keys[1]); + BOOST_CHECK_EQUAL(3, values[0]); + BOOST_CHECK_EQUAL(2, values[1]); +} + }} // namespace anonymous util