////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2014-2014. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/move for documentation. // ////////////////////////////////////////////////////////////////////////////// //! \file #ifndef BOOST_MOVE_DETAIL_INSERT_SORT_HPP #define BOOST_MOVE_DETAIL_INSERT_SORT_HPP #ifndef BOOST_CONFIG_HPP # include #endif # #if defined(BOOST_HAS_PRAGMA_ONCE) # pragma once #endif #include #include #include #include #include #include #include #include #include #include namespace lslboost { namespace movelib{ // @cond template void insertion_sort_op(ForwardIterator first1, ForwardIterator last1, BirdirectionalIterator first2, Compare comp, Op op) { if (first1 != last1){ BirdirectionalIterator last2 = first2; op(first1, last2); for (++last2; ++first1 != last1; ++last2){ BirdirectionalIterator j2 = last2; BirdirectionalIterator i2 = j2; if (comp(*first1, *--i2)){ op(i2, j2); for (--j2; i2 != first2 && comp(*first1, *--i2); --j2) { op(i2, j2); } } op(first1, j2); } } } template void insertion_sort_swap(ForwardIterator first1, ForwardIterator last1, BirdirectionalIterator first2, Compare comp) { insertion_sort_op(first1, last1, first2, comp, swap_op()); } template void insertion_sort_copy(ForwardIterator first1, ForwardIterator last1, BirdirectionalIterator first2, Compare comp) { insertion_sort_op(first1, last1, first2, comp, move_op()); } // @endcond template void insertion_sort(BirdirectionalIterator first, BirdirectionalIterator last, Compare comp) { typedef typename lslboost::movelib::iterator_traits::value_type value_type; if (first != last){ BirdirectionalIterator i = first; for (++i; i != last; ++i){ BirdirectionalIterator j = i; if (comp(*i, *--j)) { value_type tmp(::lslboost::move(*i)); *i = ::lslboost::move(*j); for (BirdirectionalIterator k = j; k != first && comp(tmp, *--k); --j) { *j = ::lslboost::move(*k); } *j = ::lslboost::move(tmp); } } } } template void insertion_sort_uninitialized_copy (BirdirectionalIterator first1, BirdirectionalIterator const last1 , BirdirectionalRawIterator const first2 , Compare comp) { typedef typename iterator_traits::value_type value_type; if (first1 != last1){ BirdirectionalRawIterator last2 = first2; ::new((iterator_to_raw_pointer)(last2), boost_move_new_t()) value_type(::lslboost::move(*first1)); destruct_n d(first2); d.incr(); for (++last2; ++first1 != last1; ++last2){ BirdirectionalRawIterator j2 = last2; BirdirectionalRawIterator k2 = j2; if (comp(*first1, *--k2)){ ::new((iterator_to_raw_pointer)(j2), boost_move_new_t()) value_type(::lslboost::move(*k2)); d.incr(); for (--j2; k2 != first2 && comp(*first1, *--k2); --j2) *j2 = ::lslboost::move(*k2); *j2 = ::lslboost::move(*first1); } else{ ::new((iterator_to_raw_pointer)(j2), boost_move_new_t()) value_type(::lslboost::move(*first1)); d.incr(); } } d.release(); } } }} //namespace lslboost { namespace movelib{ #endif //#ifndef BOOST_MOVE_DETAIL_INSERT_SORT_HPP