merge_sort.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #if defined(BOOST_CLANG) || (defined(BOOST_GCC) && (BOOST_GCC >= 40600))
  32. #pragma GCC diagnostic push
  33. #pragma GCC diagnostic ignored "-Wsign-conversion"
  34. #endif
  35. namespace boost {
  36. namespace movelib {
  37. // @cond
  38. static const unsigned MergeSortInsertionSortThreshold = 16;
  39. template <class RandIt, class Compare>
  40. void inplace_stable_sort(RandIt first, RandIt last, Compare comp)
  41. {
  42. typedef typename iter_size<RandIt>::type size_type;
  43. if (size_type(last - first) <= size_type(MergeSortInsertionSortThreshold)) {
  44. insertion_sort(first, last, comp);
  45. return;
  46. }
  47. RandIt middle = first + (last - first) / 2;
  48. inplace_stable_sort(first, middle, comp);
  49. inplace_stable_sort(middle, last, comp);
  50. merge_bufferless_ONlogN_recursive
  51. (first, middle, last, size_type(middle - first), size_type(last - middle), comp);
  52. }
  53. // @endcond
  54. template<class RandIt, class RandIt2, class Compare>
  55. void merge_sort_copy( RandIt first, RandIt last
  56. , RandIt2 dest, Compare comp)
  57. {
  58. typedef typename iter_size<RandIt>::type size_type;
  59. size_type const count = size_type(last - first);
  60. if(count <= MergeSortInsertionSortThreshold){
  61. insertion_sort_copy(first, last, dest, comp);
  62. }
  63. else{
  64. size_type const half = size_type(count/2u);
  65. merge_sort_copy(first + half, last , dest+half , comp);
  66. merge_sort_copy(first , first + half, first + half, comp);
  67. merge_with_right_placed
  68. ( first + half, first + half + half
  69. , dest, dest+half, dest + count
  70. , comp);
  71. }
  72. }
  73. template<class RandIt, class RandItRaw, class Compare>
  74. void merge_sort_uninitialized_copy( RandIt first, RandIt last
  75. , RandItRaw uninitialized
  76. , Compare comp)
  77. {
  78. typedef typename iter_size<RandIt>::type size_type;
  79. typedef typename iterator_traits<RandIt>::value_type value_type;
  80. size_type const count = size_type(last - first);
  81. if(count <= MergeSortInsertionSortThreshold){
  82. insertion_sort_uninitialized_copy(first, last, uninitialized, comp);
  83. }
  84. else{
  85. size_type const half = count/2;
  86. merge_sort_uninitialized_copy(first + half, last, uninitialized + half, comp);
  87. destruct_n<value_type, RandItRaw> d(uninitialized+half);
  88. d.incr(size_type(count-half));
  89. merge_sort_copy(first, first + half, first + half, comp);
  90. uninitialized_merge_with_right_placed
  91. ( first + half, first + half + half
  92. , uninitialized, uninitialized+half, uninitialized+count
  93. , comp);
  94. d.release();
  95. }
  96. }
  97. template<class RandIt, class RandItRaw, class Compare>
  98. void merge_sort( RandIt first, RandIt last, Compare comp
  99. , RandItRaw uninitialized)
  100. {
  101. typedef typename iter_size<RandIt>::type size_type;
  102. typedef typename iterator_traits<RandIt>::value_type value_type;
  103. size_type const count = size_type(last - first);
  104. if(count <= MergeSortInsertionSortThreshold){
  105. insertion_sort(first, last, comp);
  106. }
  107. else{
  108. size_type const half = size_type(count/2u);
  109. size_type const rest = size_type(count - half);
  110. RandIt const half_it = first + half;
  111. RandIt const rest_it = first + rest;
  112. merge_sort_uninitialized_copy(half_it, last, uninitialized, comp);
  113. destruct_n<value_type, RandItRaw> d(uninitialized);
  114. d.incr(rest);
  115. merge_sort_copy(first, half_it, rest_it, comp);
  116. merge_with_right_placed
  117. ( uninitialized, uninitialized + rest
  118. , first, rest_it, last, antistable<Compare>(comp));
  119. }
  120. }
  121. ///@cond
  122. template<class RandIt, class RandItRaw, class Compare>
  123. void merge_sort_with_constructed_buffer( RandIt first, RandIt last, Compare comp, RandItRaw buffer)
  124. {
  125. typedef typename iter_size<RandIt>::type size_type;
  126. size_type const count = size_type(last - first);
  127. if(count <= MergeSortInsertionSortThreshold){
  128. insertion_sort(first, last, comp);
  129. }
  130. else{
  131. size_type const half = size_type(count/2);
  132. size_type const rest = size_type(count - half);
  133. RandIt const half_it = first + half;
  134. RandIt const rest_it = first + rest;
  135. merge_sort_copy(half_it, last, buffer, comp);
  136. merge_sort_copy(first, half_it, rest_it, comp);
  137. merge_with_right_placed
  138. (buffer, buffer + rest
  139. , first, rest_it, last, antistable<Compare>(comp));
  140. }
  141. }
  142. template<typename RandIt, typename Pointer,
  143. typename Distance, typename Compare>
  144. void stable_sort_ONlogN_recursive(RandIt first, RandIt last, Pointer buffer, Distance buffer_size, Compare comp)
  145. {
  146. typedef typename iter_size<RandIt>::type size_type;
  147. if (size_type(last - first) <= size_type(MergeSortInsertionSortThreshold)) {
  148. insertion_sort(first, last, comp);
  149. }
  150. else {
  151. const size_type len = size_type(last - first) / 2u;
  152. const RandIt middle = first + len;
  153. if (len > ((buffer_size+1)/2)){
  154. stable_sort_ONlogN_recursive(first, middle, buffer, buffer_size, comp);
  155. stable_sort_ONlogN_recursive(middle, last, buffer, buffer_size, comp);
  156. }
  157. else{
  158. merge_sort_with_constructed_buffer(first, middle, comp, buffer);
  159. merge_sort_with_constructed_buffer(middle, last, comp, buffer);
  160. }
  161. merge_adaptive_ONlogN_recursive(first, middle, last,
  162. size_type(middle - first),
  163. size_type(last - middle),
  164. buffer, buffer_size,
  165. comp);
  166. }
  167. }
  168. template<typename BidirectionalIterator, typename Compare, typename RandRawIt>
  169. void stable_sort_adaptive_ONlogN2(BidirectionalIterator first,
  170. BidirectionalIterator last,
  171. Compare comp,
  172. RandRawIt uninitialized,
  173. std::size_t uninitialized_len)
  174. {
  175. typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
  176. ::boost::movelib::adaptive_xbuf<value_type, RandRawIt> xbuf(uninitialized, uninitialized_len);
  177. xbuf.initialize_until(uninitialized_len, *first);
  178. stable_sort_ONlogN_recursive(first, last, uninitialized, uninitialized_len, comp);
  179. }
  180. ///@endcond
  181. }} //namespace boost { namespace movelib{
  182. #if defined(BOOST_CLANG) || (defined(BOOST_GCC) && (BOOST_GCC >= 40600))
  183. #pragma GCC diagnostic pop
  184. #endif
  185. #include <boost/move/detail/config_end.hpp>
  186. #endif //#ifndef BOOST_MOVE_DETAIL_MERGE_SORT_HPP