diff --git a/include/boost/move/algo/detail/merge_sort.hpp b/include/boost/move/algo/detail/merge_sort.hpp index 62d185a..34bbd2e 100644 --- a/include/boost/move/algo/detail/merge_sort.hpp +++ b/include/boost/move/algo/detail/merge_sort.hpp @@ -132,6 +132,74 @@ void merge_sort( RandIt first, RandIt last, Compare comp } } +///@cond + +template +void merge_sort_with_constructed_buffer( RandIt first, RandIt last, Compare comp, RandItRaw buffer) +{ + typedef typename iterator_traits::size_type size_type; + + size_type const count = size_type(last - first); + if(count <= MergeSortInsertionSortThreshold){ + insertion_sort(first, last, comp); + } + else{ + size_type const half = count/2; + size_type const rest = count - half; + RandIt const half_it = first + half; + RandIt const rest_it = first + rest; + + merge_sort_copy(half_it, last, buffer, comp); + merge_sort_copy(first, half_it, rest_it, comp); + merge_with_right_placed + (buffer, buffer + rest + , first, rest_it, last, antistable(comp)); + } +} + +template +void stable_sort_ONlogN_recursive(RandIt first, RandIt last, Pointer buffer, Distance buffer_size, Compare comp) +{ + typedef typename iterator_traits::size_type size_type; + if (size_type(last - first) <= size_type(MergeSortInsertionSortThreshold)) { + insertion_sort(first, last, comp); + } + else { + const size_type len = (last - first) / 2; + const RandIt middle = first + len; + if (len > ((buffer_size+1)/2)){ + stable_sort_ONlogN_recursive(first, middle, buffer, buffer_size, comp); + stable_sort_ONlogN_recursive(middle, last, buffer, buffer_size, comp); + } + else{ + merge_sort_with_constructed_buffer(first, middle, comp, buffer); + merge_sort_with_constructed_buffer(middle, last, comp, buffer); + } + merge_adaptive_ONlogN_recursive(first, middle, last, + size_type(middle - first), + size_type(last - middle), + buffer, buffer_size, + comp); + } +} + +template +void stable_sort_adaptive_ONlogN2(BidirectionalIterator first, + BidirectionalIterator last, + Compare comp, + RandRawIt uninitialized, + std::size_t uninitialized_len) +{ + typedef typename iterator_traits::value_type value_type; + + ::boost::movelib::adaptive_xbuf xbuf(uninitialized, uninitialized_len); + xbuf.initialize_until(uninitialized_len, *first); + stable_sort_ONlogN_recursive(first, last, uninitialized, uninitialized_len, comp); +} + +///@endcond + }} //namespace boost { namespace movelib{ #include