adaptive_sort.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. #ifndef BOOST_MOVE_ADAPTIVE_SORT_HPP
  12. #define BOOST_MOVE_ADAPTIVE_SORT_HPP
  13. #include <boost/move/detail/config_begin.hpp>
  14. #include <boost/move/algo/detail/adaptive_sort_merge.hpp>
  15. namespace boost {
  16. namespace movelib {
  17. ///@cond
  18. namespace detail_adaptive {
  19. template<class RandIt>
  20. void move_data_backward( RandIt cur_pos
  21. , typename iterator_traits<RandIt>::size_type const l_data
  22. , RandIt new_pos
  23. , bool const xbuf_used)
  24. {
  25. //Move buffer to the total combination right
  26. if(xbuf_used){
  27. boost::move_backward(cur_pos, cur_pos+l_data, new_pos+l_data);
  28. }
  29. else{
  30. boost::adl_move_swap_ranges_backward(cur_pos, cur_pos+l_data, new_pos+l_data);
  31. //Rotate does less moves but it seems slower due to cache issues
  32. //rotate_gcd(first-l_block, first+len-l_block, first+len);
  33. }
  34. }
  35. template<class RandIt>
  36. void move_data_forward( RandIt cur_pos
  37. , typename iterator_traits<RandIt>::size_type const l_data
  38. , RandIt new_pos
  39. , bool const xbuf_used)
  40. {
  41. //Move buffer to the total combination right
  42. if(xbuf_used){
  43. boost::move(cur_pos, cur_pos+l_data, new_pos);
  44. }
  45. else{
  46. boost::adl_move_swap_ranges(cur_pos, cur_pos+l_data, new_pos);
  47. //Rotate does less moves but it seems slower due to cache issues
  48. //rotate_gcd(first-l_block, first+len-l_block, first+len);
  49. }
  50. }
  51. // build blocks of length 2*l_build_buf. l_build_buf is power of two
  52. // input: [0, l_build_buf) elements are buffer, rest unsorted elements
  53. // output: [0, l_build_buf) elements are buffer, blocks 2*l_build_buf and last subblock sorted
  54. //
  55. // First elements are merged from right to left until elements start
  56. // at first. All old elements [first, first + l_build_buf) are placed at the end
  57. // [first+len-l_build_buf, first+len). To achieve this:
  58. // - If we have external memory to merge, we save elements from the buffer
  59. // so that a non-swapping merge is used. Buffer elements are restored
  60. // at the end of the buffer from the external memory.
  61. //
  62. // - When the external memory is not available or it is insufficient
  63. // for a merge operation, left swap merging is used.
  64. //
  65. // Once elements are merged left to right in blocks of l_build_buf, then a single left
  66. // to right merge step is performed to achieve merged blocks of size 2K.
  67. // If external memory is available, usual merge is used, swap merging otherwise.
  68. //
  69. // As a last step, if auxiliary memory is available in-place merge is performed.
  70. // until all is merged or auxiliary memory is not large enough.
  71. template<class RandIt, class Compare, class XBuf>
  72. typename iterator_traits<RandIt>::size_type
  73. adaptive_sort_build_blocks
  74. ( RandIt const first
  75. , typename iterator_traits<RandIt>::size_type const len
  76. , typename iterator_traits<RandIt>::size_type const l_base
  77. , typename iterator_traits<RandIt>::size_type const l_build_buf
  78. , XBuf & xbuf
  79. , Compare comp)
  80. {
  81. typedef typename iterator_traits<RandIt>::size_type size_type;
  82. BOOST_ASSERT(l_build_buf <= len);
  83. BOOST_ASSERT(0 == ((l_build_buf / l_base)&(l_build_buf/l_base-1)));
  84. //Place the start pointer after the buffer
  85. RandIt first_block = first + l_build_buf;
  86. size_type const elements_in_blocks = len - l_build_buf;
  87. //////////////////////////////////
  88. // Start of merge to left step
  89. //////////////////////////////////
  90. size_type l_merged = 0u;
  91. BOOST_ASSERT(l_build_buf);
  92. //If there is no enough buffer for the insertion sort step, just avoid the external buffer
  93. size_type kbuf = min_value<size_type>(l_build_buf, size_type(xbuf.capacity()));
  94. kbuf = kbuf < l_base ? 0 : kbuf;
  95. if(kbuf){
  96. //Backup internal buffer values in external buffer so they can be overwritten
  97. xbuf.move_assign(first+l_build_buf-kbuf, kbuf);
  98. l_merged = op_insertion_sort_step_left(first_block, elements_in_blocks, l_base, comp, move_op());
  99. //Now combine them using the buffer. Elements from buffer can be
  100. //overwritten since they've been saved to xbuf
  101. l_merged = op_merge_left_step_multiple
  102. ( first_block - l_merged, elements_in_blocks, l_merged, l_build_buf, kbuf - l_merged, comp, move_op());
  103. //Restore internal buffer from external buffer unless kbuf was l_build_buf,
  104. //in that case restoration will happen later
  105. if(kbuf != l_build_buf){
  106. boost::move(xbuf.data()+kbuf-l_merged, xbuf.data() + kbuf, first_block-l_merged+elements_in_blocks);
  107. }
  108. }
  109. else{
  110. l_merged = insertion_sort_step(first_block, elements_in_blocks, l_base, comp);
  111. rotate_gcd(first_block - l_merged, first_block, first_block+elements_in_blocks);
  112. }
  113. //Now combine elements using the buffer. Elements from buffer can't be
  114. //overwritten since xbuf was not big enough, so merge swapping elements.
  115. l_merged = op_merge_left_step_multiple
  116. (first_block - l_merged, elements_in_blocks, l_merged, l_build_buf, l_build_buf - l_merged, comp, swap_op());
  117. BOOST_ASSERT(l_merged == l_build_buf);
  118. //////////////////////////////////
  119. // Start of merge to right step
  120. //////////////////////////////////
  121. //If kbuf is l_build_buf then we can merge right without swapping
  122. //Saved data is still in xbuf
  123. if(kbuf && kbuf == l_build_buf){
  124. op_merge_right_step_once(first, elements_in_blocks, l_build_buf, comp, move_op());
  125. //Restore internal buffer from external buffer if kbuf was l_build_buf.
  126. //as this operation was previously delayed.
  127. boost::move(xbuf.data(), xbuf.data() + kbuf, first);
  128. }
  129. else{
  130. op_merge_right_step_once(first, elements_in_blocks, l_build_buf, comp, swap_op());
  131. }
  132. xbuf.clear();
  133. //2*l_build_buf or total already merged
  134. return min_value<size_type>(elements_in_blocks, 2*l_build_buf);
  135. }
  136. template<class RandItKeys, class KeyCompare, class RandIt, class Compare, class XBuf>
  137. void adaptive_sort_combine_blocks
  138. ( RandItKeys const keys
  139. , KeyCompare key_comp
  140. , RandIt const first
  141. , typename iterator_traits<RandIt>::size_type const len
  142. , typename iterator_traits<RandIt>::size_type const l_prev_merged
  143. , typename iterator_traits<RandIt>::size_type const l_block
  144. , bool const use_buf
  145. , bool const xbuf_used
  146. , XBuf & xbuf
  147. , Compare comp
  148. , bool merge_left)
  149. {
  150. (void)xbuf;
  151. typedef typename iterator_traits<RandIt>::size_type size_type;
  152. size_type const l_reg_combined = 2*l_prev_merged;
  153. size_type l_irreg_combined = 0;
  154. size_type const l_total_combined = calculate_total_combined(len, l_prev_merged, &l_irreg_combined);
  155. size_type const n_reg_combined = len/l_reg_combined;
  156. RandIt combined_first = first;
  157. (void)l_total_combined;
  158. BOOST_ASSERT(l_total_combined <= len);
  159. size_type const max_i = n_reg_combined + (l_irreg_combined != 0);
  160. if(merge_left || !use_buf) {
  161. for( size_type combined_i = 0; combined_i != max_i; ) {
  162. //Now merge blocks
  163. bool const is_last = combined_i==n_reg_combined;
  164. size_type const l_cur_combined = is_last ? l_irreg_combined : l_reg_combined;
  165. range_xbuf<RandIt, size_type, move_op> rbuf( (use_buf && xbuf_used) ? (combined_first-l_block) : combined_first, combined_first);
  166. size_type n_block_a, n_block_b, l_irreg1, l_irreg2;
  167. combine_params( keys, key_comp, l_cur_combined
  168. , l_prev_merged, l_block, rbuf
  169. , n_block_a, n_block_b, l_irreg1, l_irreg2); //Outputs
  170. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" A combpar: ", len + l_block);
  171. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(combined_first, combined_first + n_block_a*l_block+l_irreg1, comp));
  172. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(combined_first + n_block_a*l_block+l_irreg1, combined_first + n_block_a*l_block+l_irreg1+n_block_b*l_block+l_irreg2, comp));
  173. if(!use_buf){
  174. merge_blocks_bufferless
  175. (keys, key_comp, combined_first, l_block, 0u, n_block_a, n_block_b, l_irreg2, comp);
  176. }
  177. else{
  178. merge_blocks_left
  179. (keys, key_comp, combined_first, l_block, 0u, n_block_a, n_block_b, l_irreg2, comp, xbuf_used);
  180. }
  181. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" After merge_blocks_L: ", len + l_block);
  182. ++combined_i;
  183. if(combined_i != max_i)
  184. combined_first += l_reg_combined;
  185. }
  186. }
  187. else{
  188. combined_first += l_reg_combined*(max_i-1);
  189. for( size_type combined_i = max_i; combined_i; ) {
  190. --combined_i;
  191. bool const is_last = combined_i==n_reg_combined;
  192. size_type const l_cur_combined = is_last ? l_irreg_combined : l_reg_combined;
  193. RandIt const combined_last(combined_first+l_cur_combined);
  194. range_xbuf<RandIt, size_type, move_op> rbuf(combined_last, xbuf_used ? (combined_last+l_block) : combined_last);
  195. size_type n_block_a, n_block_b, l_irreg1, l_irreg2;
  196. combine_params( keys, key_comp, l_cur_combined
  197. , l_prev_merged, l_block, rbuf
  198. , n_block_a, n_block_b, l_irreg1, l_irreg2); //Outputs
  199. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" A combpar: ", len + l_block);
  200. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(combined_first, combined_first + n_block_a*l_block+l_irreg1, comp));
  201. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(combined_first + n_block_a*l_block+l_irreg1, combined_first + n_block_a*l_block+l_irreg1+n_block_b*l_block+l_irreg2, comp));
  202. merge_blocks_right
  203. (keys, key_comp, combined_first, l_block, n_block_a, n_block_b, l_irreg2, comp, xbuf_used);
  204. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" After merge_blocks_R: ", len + l_block);
  205. if(combined_i)
  206. combined_first -= l_reg_combined;
  207. }
  208. }
  209. }
  210. //Returns true if buffer is placed in
  211. //[buffer+len-l_intbuf, buffer+len). Otherwise, buffer is
  212. //[buffer,buffer+l_intbuf)
  213. template<class RandIt, class Compare, class XBuf>
  214. bool adaptive_sort_combine_all_blocks
  215. ( RandIt keys
  216. , typename iterator_traits<RandIt>::size_type &n_keys
  217. , RandIt const buffer
  218. , typename iterator_traits<RandIt>::size_type const l_buf_plus_data
  219. , typename iterator_traits<RandIt>::size_type l_merged
  220. , typename iterator_traits<RandIt>::size_type &l_intbuf
  221. , XBuf & xbuf
  222. , Compare comp)
  223. {
  224. typedef typename iterator_traits<RandIt>::size_type size_type;
  225. RandIt const first = buffer + l_intbuf;
  226. size_type const l_data = l_buf_plus_data - l_intbuf;
  227. size_type const l_unique = l_intbuf+n_keys;
  228. //Backup data to external buffer once if possible
  229. bool const common_xbuf = l_data > l_merged && l_intbuf && l_intbuf <= xbuf.capacity();
  230. if(common_xbuf){
  231. xbuf.move_assign(buffer, l_intbuf);
  232. }
  233. bool prev_merge_left = true;
  234. size_type l_prev_total_combined = l_merged, l_prev_block = 0;
  235. bool prev_use_internal_buf = true;
  236. for( size_type n = 0; l_data > l_merged
  237. ; l_merged*=2
  238. , ++n){
  239. //If l_intbuf is non-zero, use that internal buffer.
  240. // Implies l_block == l_intbuf && use_internal_buf == true
  241. //If l_intbuf is zero, see if half keys can be reused as a reduced emergency buffer,
  242. // Implies l_block == n_keys/2 && use_internal_buf == true
  243. //Otherwise, just give up and and use all keys to merge using rotations (use_internal_buf = false)
  244. bool use_internal_buf = false;
  245. size_type const l_block = lblock_for_combine(l_intbuf, n_keys, size_type(2*l_merged), use_internal_buf);
  246. BOOST_ASSERT(!l_intbuf || (l_block == l_intbuf));
  247. BOOST_ASSERT(n == 0 || (!use_internal_buf || prev_use_internal_buf) );
  248. BOOST_ASSERT(n == 0 || (!use_internal_buf || l_prev_block == l_block) );
  249. bool const is_merge_left = (n&1) == 0;
  250. size_type const l_total_combined = calculate_total_combined(l_data, l_merged);
  251. if(n && prev_use_internal_buf && prev_merge_left){
  252. if(is_merge_left || !use_internal_buf){
  253. move_data_backward(first-l_prev_block, l_prev_total_combined, first, common_xbuf);
  254. }
  255. else{
  256. //Put the buffer just after l_total_combined
  257. RandIt const buf_end = first+l_prev_total_combined;
  258. RandIt const buf_beg = buf_end-l_block;
  259. if(l_prev_total_combined > l_total_combined){
  260. size_type const l_diff = l_prev_total_combined - l_total_combined;
  261. move_data_backward(buf_beg-l_diff, l_diff, buf_end-l_diff, common_xbuf);
  262. }
  263. else if(l_prev_total_combined < l_total_combined){
  264. size_type const l_diff = l_total_combined - l_prev_total_combined;
  265. move_data_forward(buf_end, l_diff, buf_beg, common_xbuf);
  266. }
  267. }
  268. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" After move_data : ", l_data + l_intbuf);
  269. }
  270. //Combine to form l_merged*2 segments
  271. if(n_keys){
  272. size_type upper_n_keys_this_iter = 2*l_merged/l_block;
  273. if(upper_n_keys_this_iter > 256){
  274. adaptive_sort_combine_blocks
  275. ( keys, comp, !use_internal_buf || is_merge_left ? first : first-l_block
  276. , l_data, l_merged, l_block, use_internal_buf, common_xbuf, xbuf, comp, is_merge_left);
  277. }
  278. else{
  279. unsigned char uint_keys[256];
  280. adaptive_sort_combine_blocks
  281. ( uint_keys, less(), !use_internal_buf || is_merge_left ? first : first-l_block
  282. , l_data, l_merged, l_block, use_internal_buf, common_xbuf, xbuf, comp, is_merge_left);
  283. }
  284. }
  285. else{
  286. size_type *const uint_keys = xbuf.template aligned_trailing<size_type>();
  287. adaptive_sort_combine_blocks
  288. ( uint_keys, less(), !use_internal_buf || is_merge_left ? first : first-l_block
  289. , l_data, l_merged, l_block, use_internal_buf, common_xbuf, xbuf, comp, is_merge_left);
  290. }
  291. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L1(is_merge_left ? " After comb blocks L: " : " After comb blocks R: ", l_data + l_intbuf);
  292. prev_merge_left = is_merge_left;
  293. l_prev_total_combined = l_total_combined;
  294. l_prev_block = l_block;
  295. prev_use_internal_buf = use_internal_buf;
  296. }
  297. BOOST_ASSERT(l_prev_total_combined == l_data);
  298. bool const buffer_right = prev_use_internal_buf && prev_merge_left;
  299. l_intbuf = prev_use_internal_buf ? l_prev_block : 0u;
  300. n_keys = l_unique - l_intbuf;
  301. //Restore data from to external common buffer if used
  302. if(common_xbuf){
  303. if(buffer_right){
  304. boost::move(xbuf.data(), xbuf.data() + l_intbuf, buffer+l_data);
  305. }
  306. else{
  307. boost::move(xbuf.data(), xbuf.data() + l_intbuf, buffer);
  308. }
  309. }
  310. return buffer_right;
  311. }
  312. template<class RandIt, class Compare, class XBuf>
  313. void adaptive_sort_final_merge( bool buffer_right
  314. , RandIt const first
  315. , typename iterator_traits<RandIt>::size_type const l_intbuf
  316. , typename iterator_traits<RandIt>::size_type const n_keys
  317. , typename iterator_traits<RandIt>::size_type const len
  318. , XBuf & xbuf
  319. , Compare comp)
  320. {
  321. //BOOST_ASSERT(n_keys || xbuf.size() == l_intbuf);
  322. xbuf.clear();
  323. typedef typename iterator_traits<RandIt>::size_type size_type;
  324. size_type const n_key_plus_buf = l_intbuf+n_keys;
  325. if(buffer_right){
  326. //Use stable sort as some buffer elements might not be unique (see non_unique_buf)
  327. stable_sort(first+len-l_intbuf, first+len, comp, xbuf);
  328. stable_merge(first+n_keys, first+len-l_intbuf, first+len, antistable<Compare>(comp), xbuf);
  329. unstable_sort(first, first+n_keys, comp, xbuf);
  330. stable_merge(first, first+n_keys, first+len, comp, xbuf);
  331. }
  332. else{
  333. //Use stable sort as some buffer elements might not be unique (see non_unique_buf)
  334. stable_sort(first, first+n_key_plus_buf, comp, xbuf);
  335. if(xbuf.capacity() >= n_key_plus_buf){
  336. buffered_merge(first, first+n_key_plus_buf, first+len, comp, xbuf);
  337. }
  338. else if(xbuf.capacity() >= min_value<size_type>(l_intbuf, n_keys)){
  339. stable_merge(first+n_keys, first+n_key_plus_buf, first+len, comp, xbuf);
  340. stable_merge(first, first+n_keys, first+len, comp, xbuf);
  341. }
  342. else{
  343. stable_merge(first, first+n_key_plus_buf, first+len, comp, xbuf);
  344. }
  345. }
  346. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L1(" After final_merge : ", len);
  347. }
  348. template<class RandIt, class Compare, class Unsigned, class XBuf>
  349. bool adaptive_sort_build_params
  350. (RandIt first, Unsigned const len, Compare comp
  351. , Unsigned &n_keys, Unsigned &l_intbuf, Unsigned &l_base, Unsigned &l_build_buf
  352. , XBuf & xbuf
  353. )
  354. {
  355. typedef Unsigned size_type;
  356. //Calculate ideal parameters and try to collect needed unique keys
  357. l_base = 0u;
  358. //Try to find a value near sqrt(len) that is 2^N*l_base where
  359. //l_base <= AdaptiveSortInsertionSortThreshold. This property is important
  360. //as build_blocks merges to the left iteratively duplicating the
  361. //merged size and all the buffer must be used just before the final
  362. //merge to right step. This guarantees "build_blocks" produces
  363. //segments of size l_build_buf*2, maximizing the classic merge phase.
  364. l_intbuf = size_type(ceil_sqrt_multiple(len, &l_base));
  365. //The internal buffer can be expanded if there is enough external memory
  366. while(xbuf.capacity() >= l_intbuf*2){
  367. l_intbuf *= 2;
  368. }
  369. //This is the minimum number of keys to implement the ideal algorithm
  370. //
  371. //l_intbuf is used as buffer plus the key count
  372. size_type n_min_ideal_keys = l_intbuf-1;
  373. while(n_min_ideal_keys >= (len-l_intbuf-n_min_ideal_keys)/l_intbuf){
  374. --n_min_ideal_keys;
  375. }
  376. n_min_ideal_keys += 1;
  377. BOOST_ASSERT(n_min_ideal_keys <= l_intbuf);
  378. if(xbuf.template supports_aligned_trailing<size_type>(l_intbuf, (len-l_intbuf-1)/l_intbuf+1)){
  379. n_keys = 0u;
  380. l_build_buf = l_intbuf;
  381. }
  382. else{
  383. //Try to achieve a l_build_buf of length l_intbuf*2, so that we can merge with that
  384. //l_intbuf*2 buffer in "build_blocks" and use half of them as buffer and the other half
  385. //as keys in combine_all_blocks. In that case n_keys >= n_min_ideal_keys but by a small margin.
  386. //
  387. //If available memory is 2*sqrt(l), then only sqrt(l) unique keys are needed,
  388. //(to be used for keys in combine_all_blocks) as the whole l_build_buf
  389. //will be backuped in the buffer during build_blocks.
  390. bool const non_unique_buf = xbuf.capacity() >= l_intbuf;
  391. size_type const to_collect = non_unique_buf ? n_min_ideal_keys : l_intbuf*2;
  392. size_type collected = collect_unique(first, first+len, to_collect, comp, xbuf);
  393. //If available memory is 2*sqrt(l), then for "build_params"
  394. //the situation is the same as if 2*l_intbuf were collected.
  395. if(non_unique_buf && collected == n_min_ideal_keys){
  396. l_build_buf = l_intbuf;
  397. n_keys = n_min_ideal_keys;
  398. }
  399. else if(collected == 2*l_intbuf){
  400. //l_intbuf*2 elements found. Use all of them in the build phase
  401. l_build_buf = l_intbuf*2;
  402. n_keys = l_intbuf;
  403. }
  404. else if(collected == (n_min_ideal_keys+l_intbuf)){
  405. l_build_buf = l_intbuf;
  406. n_keys = n_min_ideal_keys;
  407. }
  408. //If collected keys are not enough, try to fix n_keys and l_intbuf. If no fix
  409. //is possible (due to very low unique keys), then go to a slow sort based on rotations.
  410. else{
  411. BOOST_ASSERT(collected < (n_min_ideal_keys+l_intbuf));
  412. if(collected < 4){ //No combination possible with less that 4 keys
  413. return false;
  414. }
  415. n_keys = l_intbuf;
  416. while(n_keys&(n_keys-1)){
  417. n_keys &= n_keys-1; // make it power or 2
  418. }
  419. while(n_keys > collected){
  420. n_keys/=2;
  421. }
  422. //AdaptiveSortInsertionSortThreshold is always power of two so the minimum is power of two
  423. l_base = min_value<Unsigned>(n_keys, AdaptiveSortInsertionSortThreshold);
  424. l_intbuf = 0;
  425. l_build_buf = n_keys;
  426. }
  427. BOOST_ASSERT((n_keys+l_intbuf) >= l_build_buf);
  428. }
  429. return true;
  430. }
  431. // Main explanation of the sort algorithm.
  432. //
  433. // csqrtlen = ceil(sqrt(len));
  434. //
  435. // * First, 2*csqrtlen unique elements elements are extracted from elements to be
  436. // sorted and placed in the beginning of the range.
  437. //
  438. // * Step "build_blocks": In this nearly-classic merge step, 2*csqrtlen unique elements
  439. // will be used as auxiliary memory, so trailing len-2*csqrtlen elements are
  440. // are grouped in blocks of sorted 4*csqrtlen elements. At the end of the step
  441. // 2*csqrtlen unique elements are again the leading elements of the whole range.
  442. //
  443. // * Step "combine_blocks": pairs of previously formed blocks are merged with a different
  444. // ("smart") algorithm to form blocks of 8*csqrtlen elements. This step is slower than the
  445. // "build_blocks" step and repeated iteratively (forming blocks of 16*csqrtlen, 32*csqrtlen
  446. // elements, etc) of until all trailing (len-2*csqrtlen) elements are merged.
  447. //
  448. // In "combine_blocks" len/csqrtlen elements used are as "keys" (markers) to
  449. // know if elements belong to the first or second block to be merged and another
  450. // leading csqrtlen elements are used as buffer. Explanation of the "combine_blocks" step:
  451. //
  452. // Iteratively until all trailing (len-2*csqrtlen) elements are merged:
  453. // Iteratively for each pair of previously merged block:
  454. // * Blocks are divided groups of csqrtlen elements and
  455. // 2*merged_block/csqrtlen keys are sorted to be used as markers
  456. // * Groups are selection-sorted by first or last element (depending whether they are going
  457. // to be merged to left or right) and keys are reordered accordingly as an imitation-buffer.
  458. // * Elements of each block pair are merged using the csqrtlen buffer taking into account
  459. // if they belong to the first half or second half (marked by the key).
  460. //
  461. // * In the final merge step leading elements (2*csqrtlen) are sorted and merged with
  462. // rotations with the rest of sorted elements in the "combine_blocks" step.
  463. //
  464. // Corner cases:
  465. //
  466. // * If no 2*csqrtlen elements can be extracted:
  467. //
  468. // * If csqrtlen+len/csqrtlen are extracted, then only csqrtlen elements are used
  469. // as buffer in the "build_blocks" step forming blocks of 2*csqrtlen elements. This
  470. // means that an additional "combine_blocks" step will be needed to merge all elements.
  471. //
  472. // * If no csqrtlen+len/csqrtlen elements can be extracted, but still more than a minimum,
  473. // then reduces the number of elements used as buffer and keys in the "build_blocks"
  474. // and "combine_blocks" steps. If "combine_blocks" has no enough keys due to this reduction
  475. // then uses a rotation based smart merge.
  476. //
  477. // * If the minimum number of keys can't be extracted, a rotation-based sorting is performed.
  478. //
  479. // * If auxiliary memory is more or equal than ceil(len/2), half-copying mergesort is used.
  480. //
  481. // * If auxiliary memory is more than csqrtlen+n_keys*sizeof(std::size_t),
  482. // then only csqrtlen elements need to be extracted and "combine_blocks" will use integral
  483. // keys to combine blocks.
  484. //
  485. // * If auxiliary memory is available, the "build_blocks" will be extended to build bigger blocks
  486. // using classic merge and "combine_blocks" will use bigger blocks when merging.
  487. template<class RandIt, class Compare, class XBuf>
  488. void adaptive_sort_impl
  489. ( RandIt first
  490. , typename iterator_traits<RandIt>::size_type const len
  491. , Compare comp
  492. , XBuf & xbuf
  493. )
  494. {
  495. typedef typename iterator_traits<RandIt>::size_type size_type;
  496. //Small sorts go directly to insertion sort
  497. if(len <= size_type(AdaptiveSortInsertionSortThreshold)){
  498. insertion_sort(first, first + len, comp);
  499. }
  500. else if((len-len/2) <= xbuf.capacity()){
  501. merge_sort(first, first+len, comp, xbuf.data());
  502. }
  503. else{
  504. //Make sure it is at least four
  505. BOOST_STATIC_ASSERT(AdaptiveSortInsertionSortThreshold >= 4);
  506. size_type l_base = 0;
  507. size_type l_intbuf = 0;
  508. size_type n_keys = 0;
  509. size_type l_build_buf = 0;
  510. //Calculate and extract needed unique elements. If a minimum is not achieved
  511. //fallback to a slow stable sort
  512. if(!adaptive_sort_build_params(first, len, comp, n_keys, l_intbuf, l_base, l_build_buf, xbuf)){
  513. stable_sort(first, first+len, comp, xbuf);
  514. }
  515. else{
  516. BOOST_ASSERT(l_build_buf);
  517. //Otherwise, continue the adaptive_sort
  518. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L1("\n After collect_unique: ", len);
  519. size_type const n_key_plus_buf = l_intbuf+n_keys;
  520. //l_build_buf is always power of two if l_intbuf is zero
  521. BOOST_ASSERT(l_intbuf || (0 == (l_build_buf & (l_build_buf-1))));
  522. //Classic merge sort until internal buffer and xbuf are exhausted
  523. size_type const l_merged = adaptive_sort_build_blocks
  524. (first+n_key_plus_buf-l_build_buf, len-n_key_plus_buf+l_build_buf, l_base, l_build_buf, xbuf, comp);
  525. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L1(" After build_blocks: ", len);
  526. //Non-trivial merge
  527. bool const buffer_right = adaptive_sort_combine_all_blocks
  528. (first, n_keys, first+n_keys, len-n_keys, l_merged, l_intbuf, xbuf, comp);
  529. //Sort keys and buffer and merge the whole sequence
  530. adaptive_sort_final_merge(buffer_right, first, l_intbuf, n_keys, len, xbuf, comp);
  531. }
  532. }
  533. }
  534. } //namespace detail_adaptive {
  535. ///@endcond
  536. //! <b>Effects</b>: Sorts the elements in the range [first, last) in ascending order according
  537. //! to comparison functor "comp". The sort is stable (order of equal elements
  538. //! is guaranteed to be preserved). Performance is improved if additional raw storage is
  539. //! provided.
  540. //!
  541. //! <b>Requires</b>:
  542. //! - RandIt must meet the requirements of ValueSwappable and RandomAccessIterator.
  543. //! - The type of dereferenced RandIt must meet the requirements of MoveAssignable and MoveConstructible.
  544. //!
  545. //! <b>Parameters</b>:
  546. //! - first, last: the range of elements to sort
  547. //! - comp: comparison function object which returns true if the first argument is is ordered before the second.
  548. //! - uninitialized, uninitialized_len: raw storage starting on "uninitialized", able to hold "uninitialized_len"
  549. //! elements of type iterator_traits<RandIt>::value_type. Maximum performance is achieved when uninitialized_len
  550. //! is ceil(std::distance(first, last)/2).
  551. //!
  552. //! <b>Throws</b>: If comp throws or the move constructor, move assignment or swap of the type
  553. //! of dereferenced RandIt throws.
  554. //!
  555. //! <b>Complexity</b>: Always K x O(Nxlog(N)) comparisons and move assignments/constructors/swaps.
  556. //! Comparisons are close to minimum even with no additional memory. Constant factor for data movement is minimized
  557. //! when uninitialized_len is ceil(std::distance(first, last)/2). Pretty good enough performance is achieved when
  558. //! ceil(sqrt(std::distance(first, last)))*2.
  559. //!
  560. //! <b>Caution</b>: Experimental implementation, not production-ready.
  561. template<class RandIt, class RandRawIt, class Compare>
  562. void adaptive_sort( RandIt first, RandIt last, Compare comp
  563. , RandRawIt uninitialized
  564. , typename iterator_traits<RandIt>::size_type uninitialized_len)
  565. {
  566. typedef typename iterator_traits<RandIt>::size_type size_type;
  567. typedef typename iterator_traits<RandIt>::value_type value_type;
  568. ::boost::movelib::adaptive_xbuf<value_type, RandRawIt, size_type> xbuf(uninitialized, uninitialized_len);
  569. ::boost::movelib::detail_adaptive::adaptive_sort_impl(first, size_type(last - first), comp, xbuf);
  570. }
  571. template<class RandIt, class Compare>
  572. void adaptive_sort( RandIt first, RandIt last, Compare comp)
  573. {
  574. typedef typename iterator_traits<RandIt>::value_type value_type;
  575. adaptive_sort(first, last, comp, (value_type*)0, 0u);
  576. }
  577. } //namespace movelib {
  578. } //namespace boost {
  579. #include <boost/move/detail/config_end.hpp>
  580. #endif //#define BOOST_MOVE_ADAPTIVE_SORT_HPP