From eecc4cbf5a6e28f910130cd4a58444e9d4701ea9 Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Mon, 27 Jan 2014 17:42:19 -0800 Subject: KenLM 5cc905bc2d214efa7de2db56a9a672b749a95591 --- klm/util/joint_sort.hh | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'klm/util/joint_sort.hh') diff --git a/klm/util/joint_sort.hh b/klm/util/joint_sort.hh index 1b43ddcf..13a52b67 100644 --- a/klm/util/joint_sort.hh +++ b/klm/util/joint_sort.hh @@ -9,7 +9,6 @@ #include #include -#include namespace util { @@ -35,9 +34,10 @@ template class JointIter { return *this; } - void swap(const JointIter &other) { - std::swap(key_, other.key_); - std::swap(value_, other.value_); + friend void swap(JointIter &first, JointIter &second) { + using std::swap; + swap(first.key_, second.key_); + swap(first.value_, second.value_); } private: @@ -83,9 +83,11 @@ template class JointProxy { return *(inner_.key_); } - void swap(JointProxy &other) { - std::swap(*inner_.key_, *other.inner_.key_); - std::swap(*inner_.value_, *other.inner_.value_); + 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_); } private: @@ -138,14 +140,4 @@ template void JointSort(const KeyIter &key_begi } // namespace util -namespace std { -template void swap(util::detail::JointIter &left, util::detail::JointIter &right) { - left.swap(right); -} - -template void swap(util::detail::JointProxy &left, util::detail::JointProxy &right) { - left.swap(right); -} -} // namespace std - #endif // UTIL_JOINT_SORT__ -- cgit v1.2.3 From c2be67fa0c5f294da5ac40ca7d5db41a36a56c64 Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Mon, 27 Jan 2014 22:06:31 -0800 Subject: Fix C++11 compiler error --- klm/util/joint_sort.hh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'klm/util/joint_sort.hh') diff --git a/klm/util/joint_sort.hh b/klm/util/joint_sort.hh index 13a52b67..5589c558 100644 --- a/klm/util/joint_sort.hh +++ b/klm/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.DeepSwap(second); } private: -- cgit v1.2.3 From 827eae2d1d2f62c00db21e1d43aabf904dd8ff8d Mon Sep 17 00:00:00 2001 From: Kenneth Heafield Date: Mon, 27 Jan 2014 22:18:14 -0800 Subject: Another attempted fix at the sorting iterator for C++11 --- klm/util/joint_sort.hh | 2 +- klm/util/joint_sort_test.cc | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'klm/util/joint_sort.hh') diff --git a/klm/util/joint_sort.hh b/klm/util/joint_sort.hh index 5589c558..b1ec48e2 100644 --- a/klm/util/joint_sort.hh +++ b/klm/util/joint_sort.hh @@ -90,7 +90,7 @@ template class JointProxy { } friend void swap(JointProxy first, JointProxy second) { - first.DeepSwap(second); + first.Inner().DeepSwap(second.Inner()); } private: diff --git a/klm/util/joint_sort_test.cc b/klm/util/joint_sort_test.cc index 4dc85916..b24c602c 100644 --- a/klm/util/joint_sort_test.cc +++ b/klm/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 -- cgit v1.2.3