merge_sort.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2016.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. //! \file
  12. #ifndef BOOST_MOVE_DETAIL_MERGE_SORT_HPP
  13. #define BOOST_MOVE_DETAIL_MERGE_SORT_HPP
  14. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #
  18. #if defined(BOOST_HAS_PRAGMA_ONCE)
  19. # pragma once
  20. #endif
  21. #include <boost/move/detail/config_begin.hpp>
  22. #include <boost/move/detail/workaround.hpp>
  23. #include <boost/move/utility_core.hpp>
  24. #include <boost/move/algo/move.hpp>
  25. #include <boost/move/algo/detail/merge.hpp>
  26. #include <boost/move/detail/iterator_traits.hpp>
  27. #include <boost/move/adl_move_swap.hpp>
  28. #include <boost/move/detail/destruct_n.hpp>
  29. #include <boost/move/algo/detail/insertion_sort.hpp>
  30. #include <cassert>
  31. namespace boost {
  32. namespace movelib {
  33. // @cond
  34. static const unsigned MergeSortInsertionSortThreshold = 16;
  35. template <class RandIt, class Compare>
  36. void inplace_stable_sort(RandIt first, RandIt last, Compare comp)
  37. {
  38. typedef typename iterator_traits<RandIt>::size_type size_type;
  39. if (size_type(last - first) <= size_type(MergeSortInsertionSortThreshold)) {
  40. insertion_sort(first, last, comp);
  41. return;
  42. }
  43. RandIt middle = first + (last - first) / 2;
  44. inplace_stable_sort(first, middle, comp);
  45. inplace_stable_sort(middle, last, comp);
  46. merge_bufferless_ONlogN_recursive
  47. (first, middle, last, size_type(middle - first), size_type(last - middle), comp);
  48. }
  49. // @endcond
  50. template<class RandIt, class RandIt2, class Compare>
  51. void merge_sort_copy( RandIt first, RandIt last
  52. , RandIt2 dest, Compare comp)
  53. {
  54. typedef typename iterator_traits<RandIt>::size_type size_type;
  55. size_type const count = size_type(last - first);
  56. if(count <= MergeSortInsertionSortThreshold){
  57. insertion_sort_copy(first, last, dest, comp);
  58. }
  59. else{
  60. size_type const half = count/2;
  61. merge_sort_copy(first + half, last , dest+half , comp);
  62. merge_sort_copy(first , first + half, first + half, comp);
  63. merge_with_right_placed
  64. ( first + half, first + half + half
  65. , dest, dest+half, dest + count
  66. , comp);
  67. }
  68. }
  69. template<class RandIt, class RandItRaw, class Compare>
  70. void merge_sort_uninitialized_copy( RandIt first, RandIt last
  71. , RandItRaw uninitialized
  72. , Compare comp)
  73. {
  74. typedef typename iterator_traits<RandIt>::size_type size_type;
  75. typedef typename iterator_traits<RandIt>::value_type value_type;
  76. size_type const count = size_type(last - first);
  77. if(count <= MergeSortInsertionSortThreshold){
  78. insertion_sort_uninitialized_copy(first, last, uninitialized, comp);
  79. }
  80. else{
  81. size_type const half = count/2;
  82. merge_sort_uninitialized_copy(first + half, last, uninitialized + half, comp);
  83. destruct_n<value_type, RandItRaw> d(uninitialized+half);
  84. d.incr(count-half);
  85. merge_sort_copy(first, first + half, first + half, comp);
  86. uninitialized_merge_with_right_placed
  87. ( first + half, first + half + half
  88. , uninitialized, uninitialized+half, uninitialized+count
  89. , comp);
  90. d.release();
  91. }
  92. }
  93. template<class RandIt, class RandItRaw, class Compare>
  94. void merge_sort( RandIt first, RandIt last, Compare comp
  95. , RandItRaw uninitialized)
  96. {
  97. typedef typename iterator_traits<RandIt>::size_type size_type;
  98. typedef typename iterator_traits<RandIt>::value_type value_type;
  99. size_type const count = size_type(last - first);
  100. if(count <= MergeSortInsertionSortThreshold){
  101. insertion_sort(first, last, comp);
  102. }
  103. else{
  104. size_type const half = count/2;
  105. size_type const rest = count - half;
  106. RandIt const half_it = first + half;
  107. RandIt const rest_it = first + rest;
  108. merge_sort_uninitialized_copy(half_it, last, uninitialized, comp);
  109. destruct_n<value_type, RandItRaw> d(uninitialized);
  110. d.incr(rest);
  111. merge_sort_copy(first, half_it, rest_it, comp);
  112. merge_with_right_placed
  113. ( uninitialized, uninitialized + rest
  114. , first, rest_it, last, antistable<Compare>(comp));
  115. }
  116. }
  117. ///@cond
  118. template<class RandIt, class RandItRaw, class Compare>
  119. void merge_sort_with_constructed_buffer( RandIt first, RandIt last, Compare comp, RandItRaw buffer)
  120. {
  121. typedef typename iterator_traits<RandIt>::size_type size_type;
  122. size_type const count = size_type(last - first);
  123. if(count <= MergeSortInsertionSortThreshold){
  124. insertion_sort(first, last, comp);
  125. }
  126. else{
  127. size_type const half = count/2;
  128. size_type const rest = count - half;
  129. RandIt const half_it = first + half;
  130. RandIt const rest_it = first + rest;
  131. merge_sort_copy(half_it, last, buffer, comp);
  132. merge_sort_copy(first, half_it, rest_it, comp);
  133. merge_with_right_placed
  134. (buffer, buffer + rest
  135. , first, rest_it, last, antistable<Compare>(comp));
  136. }
  137. }
  138. template<typename RandIt, typename Pointer,
  139. typename Distance, typename Compare>
  140. void stable_sort_ONlogN_recursive(RandIt first, RandIt last, Pointer buffer, Distance buffer_size, Compare comp)
  141. {
  142. typedef typename iterator_traits<RandIt>::size_type size_type;
  143. if (size_type(last - first) <= size_type(MergeSortInsertionSortThreshold)) {
  144. insertion_sort(first, last, comp);
  145. }
  146. else {
  147. const size_type len = (last - first) / 2;
  148. const RandIt middle = first + len;
  149. if (len > ((buffer_size+1)/2)){
  150. stable_sort_ONlogN_recursive(first, middle, buffer, buffer_size, comp);
  151. stable_sort_ONlogN_recursive(middle, last, buffer, buffer_size, comp);
  152. }
  153. else{
  154. merge_sort_with_constructed_buffer(first, middle, comp, buffer);
  155. merge_sort_with_constructed_buffer(middle, last, comp, buffer);
  156. }
  157. merge_adaptive_ONlogN_recursive(first, middle, last,
  158. size_type(middle - first),
  159. size_type(last - middle),
  160. buffer, buffer_size,
  161. comp);
  162. }
  163. }
  164. template<typename BidirectionalIterator, typename Compare, typename RandRawIt>
  165. void stable_sort_adaptive_ONlogN2(BidirectionalIterator first,
  166. BidirectionalIterator last,
  167. Compare comp,
  168. RandRawIt uninitialized,
  169. std::size_t uninitialized_len)
  170. {
  171. typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
  172. ::boost::movelib::adaptive_xbuf<value_type, RandRawIt> xbuf(uninitialized, uninitialized_len);
  173. xbuf.initialize_until(uninitialized_len, *first);
  174. stable_sort_ONlogN_recursive(first, last, uninitialized, uninitialized_len, comp);
  175. }
  176. ///@endcond
  177. }} //namespace boost { namespace movelib{
  178. #include <boost/move/detail/config_end.hpp>
  179. #endif //#ifndef BOOST_MOVE_DETAIL_MERGE_SORT_HPP