adaptive_sort_merge.hpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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. //
  12. // Stable sorting that works in O(N*log(N)) worst time
  13. // and uses O(1) extra memory
  14. //
  15. //////////////////////////////////////////////////////////////////////////////
  16. //
  17. // The main idea of the adaptive_sort algorithm was developed by Andrey Astrelin
  18. // and explained in the article from the russian collaborative blog
  19. // Habrahabr (http://habrahabr.ru/post/205290/). The algorithm is based on
  20. // ideas from B-C. Huang and M. A. Langston explained in their article
  21. // "Fast Stable Merging and Sorting in Constant Extra Space (1989-1992)"
  22. // (http://comjnl.oxfordjournals.org/content/35/6/643.full.pdf).
  23. //
  24. // This implementation by Ion Gaztanaga uses previous ideas with additional changes:
  25. //
  26. // - Use of GCD-based rotation.
  27. // - Non power of two buffer-sizes.
  28. // - Tries to find sqrt(len)*2 unique keys, so that the merge sort
  29. // phase can form up to sqrt(len)*4 segments if enough keys are found.
  30. // - The merge-sort phase can take advantage of external memory to
  31. // save some additional combination steps.
  32. // - Combination phase: Blocks are selection sorted and merged in parallel.
  33. // - The combination phase is performed alternating merge to left and merge
  34. // to right phases minimizing swaps due to internal buffer repositioning.
  35. // - When merging blocks special optimizations are made to avoid moving some
  36. // elements twice.
  37. //
  38. // The adaptive_merge algorithm was developed by Ion Gaztanaga reusing some parts
  39. // from the sorting algorithm and implementing an additional block merge algorithm
  40. // without moving elements to left or right.
  41. //////////////////////////////////////////////////////////////////////////////
  42. #ifndef BOOST_MOVE_ADAPTIVE_SORT_MERGE_HPP
  43. #define BOOST_MOVE_ADAPTIVE_SORT_MERGE_HPP
  44. #include <boost/move/detail/config_begin.hpp>
  45. #include <boost/move/detail/reverse_iterator.hpp>
  46. #include <boost/move/algo/move.hpp>
  47. #include <boost/move/algo/detail/merge.hpp>
  48. #include <boost/move/adl_move_swap.hpp>
  49. #include <boost/move/algo/detail/insertion_sort.hpp>
  50. #include <boost/move/algo/detail/merge_sort.hpp>
  51. #include <boost/move/algo/detail/heap_sort.hpp>
  52. #include <boost/move/algo/detail/merge.hpp>
  53. #include <boost/move/algo/detail/is_sorted.hpp>
  54. #include <boost/assert.hpp>
  55. #include <boost/cstdint.hpp>
  56. #ifndef BOOST_MOVE_ADAPTIVE_SORT_STATS_LEVEL
  57. #define BOOST_MOVE_ADAPTIVE_SORT_STATS_LEVEL 1
  58. #endif
  59. #ifdef BOOST_MOVE_ADAPTIVE_SORT_STATS
  60. #if BOOST_MOVE_ADAPTIVE_SORT_STATS_LEVEL == 2
  61. #define BOOST_MOVE_ADAPTIVE_SORT_PRINT_L1(STR, L) \
  62. print_stats(STR, L)\
  63. //
  64. #define BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(STR, L) \
  65. print_stats(STR, L)\
  66. //
  67. #else
  68. #define BOOST_MOVE_ADAPTIVE_SORT_PRINT_L1(STR, L) \
  69. print_stats(STR, L)\
  70. //
  71. #define BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(STR, L)
  72. #endif
  73. #else
  74. #define BOOST_MOVE_ADAPTIVE_SORT_PRINT_L1(STR, L)
  75. #define BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(STR, L)
  76. #endif
  77. #ifdef BOOST_MOVE_ADAPTIVE_SORT_INVARIANTS
  78. #define BOOST_MOVE_ADAPTIVE_SORT_INVARIANT BOOST_ASSERT
  79. #else
  80. #define BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(L)
  81. #endif
  82. namespace boost {
  83. namespace movelib {
  84. #if defined(BOOST_MOVE_ADAPTIVE_SORT_INVARIANTS)
  85. bool is_sorted(::order_perf_type *first, ::order_perf_type *last, ::order_type_less)
  86. {
  87. if (first != last) {
  88. const order_perf_type *next = first, *cur(first);
  89. while (++next != last) {
  90. if (!(cur->key < next->key || (cur->key == next->key && cur->val < next->val)))
  91. return false;
  92. cur = next;
  93. }
  94. }
  95. return true;
  96. }
  97. #endif //BOOST_MOVE_ADAPTIVE_SORT_INVARIANTS
  98. namespace detail_adaptive {
  99. static const std::size_t AdaptiveSortInsertionSortThreshold = 16;
  100. //static const std::size_t AdaptiveSortInsertionSortThreshold = 4;
  101. BOOST_STATIC_ASSERT((AdaptiveSortInsertionSortThreshold&(AdaptiveSortInsertionSortThreshold-1)) == 0);
  102. #if defined BOOST_HAS_INTPTR_T
  103. typedef ::boost::uintptr_t uintptr_t;
  104. #else
  105. typedef std::size_t uintptr_t;
  106. #endif
  107. template<class T>
  108. const T &min_value(const T &a, const T &b)
  109. {
  110. return a < b ? a : b;
  111. }
  112. template<class T>
  113. const T &max_value(const T &a, const T &b)
  114. {
  115. return a > b ? a : b;
  116. }
  117. template<class ForwardIt, class Pred, class V>
  118. typename iterator_traits<ForwardIt>::size_type
  119. count_if_with(ForwardIt first, ForwardIt last, Pred pred, const V &v)
  120. {
  121. typedef typename iterator_traits<ForwardIt>::size_type size_type;
  122. size_type count = 0;
  123. while(first != last) {
  124. count += static_cast<size_type>(0 != pred(*first, v));
  125. ++first;
  126. }
  127. return count;
  128. }
  129. template<class RandIt, class Compare>
  130. RandIt skip_until_merge
  131. ( RandIt first1, RandIt const last1
  132. , const typename iterator_traits<RandIt>::value_type &next_key, Compare comp)
  133. {
  134. while(first1 != last1 && !comp(next_key, *first1)){
  135. ++first1;
  136. }
  137. return first1;
  138. }
  139. template<class RandItKeys, class RandIt>
  140. void swap_and_update_key
  141. ( RandItKeys const key_next
  142. , RandItKeys const key_range2
  143. , RandItKeys &key_mid
  144. , RandIt const begin
  145. , RandIt const end
  146. , RandIt const with)
  147. {
  148. if(begin != with){
  149. ::boost::adl_move_swap_ranges(begin, end, with);
  150. ::boost::adl_move_swap(*key_next, *key_range2);
  151. if(key_next == key_mid){
  152. key_mid = key_range2;
  153. }
  154. else if(key_mid == key_range2){
  155. key_mid = key_next;
  156. }
  157. }
  158. }
  159. template<class RandItKeys>
  160. void update_key
  161. (RandItKeys const key_next
  162. , RandItKeys const key_range2
  163. , RandItKeys &key_mid)
  164. {
  165. if (key_next != key_range2) {
  166. ::boost::adl_move_swap(*key_next, *key_range2);
  167. if (key_next == key_mid) {
  168. key_mid = key_range2;
  169. }
  170. else if (key_mid == key_range2) {
  171. key_mid = key_next;
  172. }
  173. }
  174. }
  175. template<class RandItKeys, class RandIt, class RandIt2, class Op>
  176. RandIt2 buffer_and_update_key
  177. (RandItKeys const key_next
  178. , RandItKeys const key_range2
  179. , RandItKeys &key_mid
  180. , RandIt begin
  181. , RandIt end
  182. , RandIt with
  183. , RandIt2 buffer
  184. , Op op)
  185. {
  186. if (begin != with) {
  187. while(begin != end) {
  188. op(three_way_t(), begin++, with++, buffer++);
  189. }
  190. ::boost::adl_move_swap(*key_next, *key_range2);
  191. if (key_next == key_mid) {
  192. key_mid = key_range2;
  193. }
  194. else if (key_mid == key_range2) {
  195. key_mid = key_next;
  196. }
  197. }
  198. return buffer;
  199. }
  200. ///////////////////////////////////////////////////////////////////////////////
  201. //
  202. // MERGE BUFFERLESS
  203. //
  204. ///////////////////////////////////////////////////////////////////////////////
  205. // [first1, last1) merge [last1,last2) -> [first1,last2)
  206. template<class RandIt, class Compare>
  207. RandIt partial_merge_bufferless_impl
  208. (RandIt first1, RandIt last1, RandIt const last2, bool *const pis_range1_A, Compare comp)
  209. {
  210. if(last1 == last2){
  211. return first1;
  212. }
  213. bool const is_range1_A = *pis_range1_A;
  214. if(first1 != last1 && comp(*last1, last1[-1])){
  215. do{
  216. RandIt const old_last1 = last1;
  217. last1 = boost::movelib::lower_bound(last1, last2, *first1, comp);
  218. first1 = rotate_gcd(first1, old_last1, last1);//old_last1 == last1 supported
  219. if(last1 == last2){
  220. return first1;
  221. }
  222. do{
  223. ++first1;
  224. } while(last1 != first1 && !comp(*last1, *first1) );
  225. } while(first1 != last1);
  226. }
  227. *pis_range1_A = !is_range1_A;
  228. return last1;
  229. }
  230. // [first1, last1) merge [last1,last2) -> [first1,last2)
  231. template<class RandIt, class Compare>
  232. RandIt partial_merge_bufferless
  233. (RandIt first1, RandIt last1, RandIt const last2, bool *const pis_range1_A, Compare comp)
  234. {
  235. return *pis_range1_A ? partial_merge_bufferless_impl(first1, last1, last2, pis_range1_A, comp)
  236. : partial_merge_bufferless_impl(first1, last1, last2, pis_range1_A, antistable<Compare>(comp));
  237. }
  238. template<class SizeType>
  239. static SizeType needed_keys_count(SizeType n_block_a, SizeType n_block_b)
  240. {
  241. return n_block_a + n_block_b;
  242. }
  243. template<class RandItKeys, class KeyCompare, class RandIt, class Compare>
  244. typename iterator_traits<RandIt>::size_type
  245. find_next_block
  246. ( RandItKeys const key_first
  247. , KeyCompare key_comp
  248. , RandIt const first
  249. , typename iterator_traits<RandIt>::size_type const l_block
  250. , typename iterator_traits<RandIt>::size_type const ix_first_block
  251. , typename iterator_traits<RandIt>::size_type const ix_last_block
  252. , Compare comp)
  253. {
  254. typedef typename iterator_traits<RandIt>::size_type size_type;
  255. typedef typename iterator_traits<RandIt>::value_type value_type;
  256. typedef typename iterator_traits<RandItKeys>::value_type key_type;
  257. BOOST_ASSERT(ix_first_block <= ix_last_block);
  258. size_type ix_min_block = 0u;
  259. for (size_type szt_i = ix_first_block; szt_i < ix_last_block; ++szt_i) {
  260. const value_type &min_val = first[ix_min_block*l_block];
  261. const value_type &cur_val = first[szt_i*l_block];
  262. const key_type &min_key = key_first[ix_min_block];
  263. const key_type &cur_key = key_first[szt_i];
  264. bool const less_than_minimum = comp(cur_val, min_val) ||
  265. (!comp(min_val, cur_val) && key_comp(cur_key, min_key));
  266. if (less_than_minimum) {
  267. ix_min_block = szt_i;
  268. }
  269. }
  270. return ix_min_block;
  271. }
  272. template<class RandItKeys, class KeyCompare, class RandIt, class Compare>
  273. void merge_blocks_bufferless
  274. ( RandItKeys const key_first
  275. , KeyCompare key_comp
  276. , RandIt const first
  277. , typename iterator_traits<RandIt>::size_type const l_block
  278. , typename iterator_traits<RandIt>::size_type const l_irreg1
  279. , typename iterator_traits<RandIt>::size_type const n_block_a
  280. , typename iterator_traits<RandIt>::size_type const n_block_b
  281. , typename iterator_traits<RandIt>::size_type const l_irreg2
  282. , Compare comp)
  283. {
  284. typedef typename iterator_traits<RandIt>::size_type size_type;
  285. size_type const key_count = needed_keys_count(n_block_a, n_block_b); (void)key_count;
  286. //BOOST_ASSERT(n_block_a || n_block_b);
  287. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted_and_unique(key_first, key_first + key_count, key_comp));
  288. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(!n_block_b || n_block_a == count_if_with(key_first, key_first + key_count, key_comp, key_first[n_block_a]));
  289. size_type n_bef_irreg2 = 0;
  290. bool l_irreg_pos_count = true;
  291. RandItKeys key_mid(key_first + n_block_a);
  292. RandIt const first_irr2 = first + l_irreg1 + (n_block_a+n_block_b)*l_block;
  293. RandIt const last_irr2 = first_irr2 + l_irreg2;
  294. { //Selection sort blocks
  295. size_type n_block_left = n_block_b + n_block_a;
  296. RandItKeys key_range2(key_first);
  297. size_type min_check = n_block_a == n_block_left ? 0u : n_block_a;
  298. size_type max_check = min_value<size_type>(min_check+1, n_block_left);
  299. for (RandIt f = first+l_irreg1; n_block_left; --n_block_left, ++key_range2, f += l_block, min_check -= min_check != 0, max_check -= max_check != 0) {
  300. size_type const next_key_idx = find_next_block(key_range2, key_comp, f, l_block, min_check, max_check, comp);
  301. RandItKeys const key_next(key_range2 + next_key_idx);
  302. max_check = min_value<size_type>(max_value<size_type>(max_check, next_key_idx+size_type(2)), n_block_left);
  303. RandIt const first_min = f + next_key_idx*l_block;
  304. //Check if irregular b block should go here.
  305. //If so, break to the special code handling the irregular block
  306. if (l_irreg_pos_count && l_irreg2 && comp(*first_irr2, *first_min)){
  307. l_irreg_pos_count = false;
  308. }
  309. n_bef_irreg2 += l_irreg_pos_count;
  310. swap_and_update_key(key_next, key_range2, key_mid, f, f + l_block, first_min);
  311. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(f, f+l_block, comp));
  312. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first_min, first_min + l_block, comp));
  313. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT((f == (first+l_irreg1)) || !comp(*f, *(f-l_block)));
  314. }
  315. }
  316. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first+l_irreg1+n_bef_irreg2*l_block, first_irr2, comp));
  317. RandIt first1 = first;
  318. RandIt last1 = first+l_irreg1;
  319. RandItKeys const key_end (key_first+n_bef_irreg2);
  320. bool is_range1_A = true;
  321. for(RandItKeys key_next = key_first; key_next != key_end; ++key_next){
  322. bool is_range2_A = key_mid == (key_first+key_count) || key_comp(*key_next, *key_mid);
  323. first1 = is_range1_A == is_range2_A
  324. ? last1 : partial_merge_bufferless(first1, last1, last1 + l_block, &is_range1_A, comp);
  325. last1 += l_block;
  326. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first, first1, comp));
  327. }
  328. merge_bufferless(is_range1_A ? first1 : last1, first_irr2, last_irr2, comp);
  329. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first, last_irr2, comp));
  330. }
  331. // Complexity: 2*distance(first, last)+max_collected^2/2
  332. //
  333. // Tries to collect at most n_keys unique elements from [first, last),
  334. // in the begining of the range, and ordered according to comp
  335. //
  336. // Returns the number of collected keys
  337. template<class RandIt, class Compare, class XBuf>
  338. typename iterator_traits<RandIt>::size_type
  339. collect_unique
  340. ( RandIt const first, RandIt const last
  341. , typename iterator_traits<RandIt>::size_type const max_collected, Compare comp
  342. , XBuf & xbuf)
  343. {
  344. typedef typename iterator_traits<RandIt>::size_type size_type;
  345. size_type h = 0;
  346. if(max_collected){
  347. ++h; // first key is always here
  348. RandIt h0 = first;
  349. RandIt u = first; ++u;
  350. RandIt search_end = u;
  351. if(xbuf.capacity() >= max_collected){
  352. typename XBuf::iterator const ph0 = xbuf.add(first);
  353. while(u != last && h < max_collected){
  354. typename XBuf::iterator const r = boost::movelib::lower_bound(ph0, xbuf.end(), *u, comp);
  355. //If key not found add it to [h, h+h0)
  356. if(r == xbuf.end() || comp(*u, *r) ){
  357. RandIt const new_h0 = boost::move(search_end, u, h0);
  358. search_end = u;
  359. ++search_end;
  360. ++h;
  361. xbuf.insert(r, u);
  362. h0 = new_h0;
  363. }
  364. ++u;
  365. }
  366. boost::move_backward(first, h0, h0+h);
  367. boost::move(xbuf.data(), xbuf.end(), first);
  368. }
  369. else{
  370. while(u != last && h < max_collected){
  371. RandIt const r = boost::movelib::lower_bound(h0, search_end, *u, comp);
  372. //If key not found add it to [h, h+h0)
  373. if(r == search_end || comp(*u, *r) ){
  374. RandIt const new_h0 = rotate_gcd(h0, search_end, u);
  375. search_end = u;
  376. ++search_end;
  377. ++h;
  378. rotate_gcd(r+(new_h0-h0), u, search_end);
  379. h0 = new_h0;
  380. }
  381. ++u;
  382. }
  383. rotate_gcd(first, h0, h0+h);
  384. }
  385. }
  386. return h;
  387. }
  388. template<class Unsigned>
  389. Unsigned floor_sqrt(Unsigned const n)
  390. {
  391. Unsigned x = n;
  392. Unsigned y = x/2 + (x&1);
  393. while (y < x){
  394. x = y;
  395. y = (x + n / x)/2;
  396. }
  397. return x;
  398. }
  399. template<class Unsigned>
  400. Unsigned ceil_sqrt(Unsigned const n)
  401. {
  402. Unsigned r = floor_sqrt(n);
  403. return r + Unsigned((n%r) != 0);
  404. }
  405. template<class Unsigned>
  406. Unsigned floor_merge_multiple(Unsigned const n, Unsigned &base, Unsigned &pow)
  407. {
  408. Unsigned s = n;
  409. Unsigned p = 0;
  410. while(s > AdaptiveSortInsertionSortThreshold){
  411. s /= 2;
  412. ++p;
  413. }
  414. base = s;
  415. pow = p;
  416. return s << p;
  417. }
  418. template<class Unsigned>
  419. Unsigned ceil_merge_multiple(Unsigned const n, Unsigned &base, Unsigned &pow)
  420. {
  421. Unsigned fm = floor_merge_multiple(n, base, pow);
  422. if(fm != n){
  423. if(base < AdaptiveSortInsertionSortThreshold){
  424. ++base;
  425. }
  426. else{
  427. base = AdaptiveSortInsertionSortThreshold/2 + 1;
  428. ++pow;
  429. }
  430. }
  431. return base << pow;
  432. }
  433. template<class Unsigned>
  434. Unsigned ceil_sqrt_multiple(Unsigned const n, Unsigned *pbase = 0)
  435. {
  436. Unsigned const r = ceil_sqrt(n);
  437. Unsigned pow = 0;
  438. Unsigned base = 0;
  439. Unsigned const res = ceil_merge_multiple(r, base, pow);
  440. if(pbase) *pbase = base;
  441. return res;
  442. }
  443. struct less
  444. {
  445. template<class T>
  446. bool operator()(const T &l, const T &r)
  447. { return l < r; }
  448. };
  449. ///////////////////////////////////////////////////////////////////////////////
  450. //
  451. // MERGE BLOCKS
  452. //
  453. ///////////////////////////////////////////////////////////////////////////////
  454. //#define ADAPTIVE_SORT_MERGE_SLOW_STABLE_SORT_IS_NLOGN
  455. #if defined ADAPTIVE_SORT_MERGE_SLOW_STABLE_SORT_IS_NLOGN
  456. template<class RandIt, class Compare>
  457. void slow_stable_sort
  458. ( RandIt const first, RandIt const last, Compare comp)
  459. {
  460. boost::movelib::inplace_stable_sort(first, last, comp);
  461. }
  462. #else //ADAPTIVE_SORT_MERGE_SLOW_STABLE_SORT_IS_NLOGN
  463. template<class RandIt, class Compare>
  464. void slow_stable_sort
  465. ( RandIt const first, RandIt const last, Compare comp)
  466. {
  467. typedef typename iterator_traits<RandIt>::size_type size_type;
  468. size_type L = size_type(last - first);
  469. { //Use insertion sort to merge first elements
  470. size_type m = 0;
  471. while((L - m) > size_type(AdaptiveSortInsertionSortThreshold)){
  472. insertion_sort(first+m, first+m+size_type(AdaptiveSortInsertionSortThreshold), comp);
  473. m += AdaptiveSortInsertionSortThreshold;
  474. }
  475. insertion_sort(first+m, last, comp);
  476. }
  477. size_type h = AdaptiveSortInsertionSortThreshold;
  478. for(bool do_merge = L > h; do_merge; h*=2){
  479. do_merge = (L - h) > h;
  480. size_type p0 = 0;
  481. if(do_merge){
  482. size_type const h_2 = 2*h;
  483. while((L-p0) > h_2){
  484. merge_bufferless(first+p0, first+p0+h, first+p0+h_2, comp);
  485. p0 += h_2;
  486. }
  487. }
  488. if((L-p0) > h){
  489. merge_bufferless(first+p0, first+p0+h, last, comp);
  490. }
  491. }
  492. }
  493. #endif //ADAPTIVE_SORT_MERGE_SLOW_STABLE_SORT_IS_NLOGN
  494. //Returns new l_block and updates use_buf
  495. template<class Unsigned>
  496. Unsigned lblock_for_combine
  497. (Unsigned const l_block, Unsigned const n_keys, Unsigned const l_data, bool &use_buf)
  498. {
  499. BOOST_ASSERT(l_data > 1);
  500. //We need to guarantee lblock >= l_merged/(n_keys/2) keys for the combination.
  501. //We have at least 4 keys guaranteed (which are the minimum to merge 2 ranges)
  502. //If l_block != 0, then n_keys is already enough to merge all blocks in all
  503. //phases as we've found all needed keys for that buffer and length before.
  504. //If l_block == 0 then see if half keys can be used as buffer and the rest
  505. //as keys guaranteeing that n_keys >= (2*l_merged)/lblock =
  506. if(!l_block){
  507. //If l_block == 0 then n_keys is power of two
  508. //(guaranteed by build_params(...))
  509. BOOST_ASSERT(n_keys >= 4);
  510. //BOOST_ASSERT(0 == (n_keys &(n_keys-1)));
  511. //See if half keys are at least 4 and if half keys fulfill
  512. Unsigned const new_buf = n_keys/2;
  513. Unsigned const new_keys = n_keys-new_buf;
  514. use_buf = new_keys >= 4 && new_keys >= l_data/new_buf;
  515. if(use_buf){
  516. return new_buf;
  517. }
  518. else{
  519. return l_data/n_keys;
  520. }
  521. }
  522. else{
  523. use_buf = true;
  524. return l_block;
  525. }
  526. }
  527. template<class RandIt, class Compare, class XBuf>
  528. void stable_sort( RandIt first, RandIt last, Compare comp, XBuf & xbuf)
  529. {
  530. typedef typename iterator_traits<RandIt>::size_type size_type;
  531. size_type const len = size_type(last - first);
  532. size_type const half_len = len/2 + (len&1);
  533. if(std::size_t(xbuf.capacity() - xbuf.size()) >= half_len) {
  534. merge_sort(first, last, comp, xbuf.data()+xbuf.size());
  535. }
  536. else{
  537. slow_stable_sort(first, last, comp);
  538. }
  539. }
  540. template<class RandIt, class Comp, class XBuf>
  541. void unstable_sort( RandIt first, RandIt last
  542. , Comp comp
  543. , XBuf & xbuf)
  544. {
  545. heap_sort(first, last, comp);(void)xbuf;
  546. }
  547. template<class RandIt, class Compare, class XBuf>
  548. void stable_merge
  549. ( RandIt first, RandIt const middle, RandIt last
  550. , Compare comp
  551. , XBuf &xbuf)
  552. {
  553. BOOST_ASSERT(xbuf.empty());
  554. typedef typename iterator_traits<RandIt>::size_type size_type;
  555. size_type const len1 = size_type(middle-first);
  556. size_type const len2 = size_type(last-middle);
  557. size_type const l_min = min_value<size_type>(len1, len2);
  558. if(xbuf.capacity() >= l_min){
  559. buffered_merge(first, middle, last, comp, xbuf);
  560. xbuf.clear();
  561. }
  562. else{
  563. //merge_bufferless(first, middle, last, comp);
  564. merge_adaptive_ONlogN(first, middle, last, comp, xbuf.begin(), xbuf.capacity());
  565. }
  566. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first, last, boost::movelib::unantistable(comp)));
  567. }
  568. template<class RandIt, class Comp, class XBuf>
  569. void initialize_keys( RandIt first, RandIt last
  570. , Comp comp
  571. , XBuf & xbuf)
  572. {
  573. unstable_sort(first, last, comp, xbuf);
  574. BOOST_ASSERT(boost::movelib::is_sorted_and_unique(first, last, comp));
  575. }
  576. template<class RandIt, class U>
  577. void initialize_keys( RandIt first, RandIt last
  578. , less
  579. , U &)
  580. {
  581. typedef typename iterator_traits<RandIt>::value_type value_type;
  582. std::size_t count = std::size_t(last - first);
  583. for(std::size_t i = 0; i != count; ++i){
  584. *first = static_cast<value_type>(i);
  585. ++first;
  586. }
  587. }
  588. template <class Unsigned>
  589. Unsigned calculate_total_combined(Unsigned const len, Unsigned const l_prev_merged, Unsigned *pl_irreg_combined = 0)
  590. {
  591. typedef Unsigned size_type;
  592. size_type const l_combined = 2*l_prev_merged;
  593. size_type l_irreg_combined = len%l_combined;
  594. size_type l_total_combined = len;
  595. if(l_irreg_combined <= l_prev_merged){
  596. l_total_combined -= l_irreg_combined;
  597. l_irreg_combined = 0;
  598. }
  599. if(pl_irreg_combined)
  600. *pl_irreg_combined = l_irreg_combined;
  601. return l_total_combined;
  602. }
  603. template<class RandItKeys, class KeyCompare, class SizeType, class XBuf>
  604. void combine_params
  605. ( RandItKeys const keys
  606. , KeyCompare key_comp
  607. , SizeType l_combined
  608. , SizeType const l_prev_merged
  609. , SizeType const l_block
  610. , XBuf & xbuf
  611. //Output
  612. , SizeType &n_block_a
  613. , SizeType &n_block_b
  614. , SizeType &l_irreg1
  615. , SizeType &l_irreg2
  616. //Options
  617. , bool do_initialize_keys = true)
  618. {
  619. typedef SizeType size_type;
  620. //Initial parameters for selection sort blocks
  621. l_irreg1 = l_prev_merged%l_block;
  622. l_irreg2 = (l_combined-l_irreg1)%l_block;
  623. BOOST_ASSERT(((l_combined-l_irreg1-l_irreg2)%l_block) == 0);
  624. size_type const n_reg_block = (l_combined-l_irreg1-l_irreg2)/l_block;
  625. n_block_a = l_prev_merged/l_block;
  626. n_block_b = n_reg_block - n_block_a;
  627. BOOST_ASSERT(n_reg_block>=n_block_a);
  628. //Key initialization
  629. if (do_initialize_keys) {
  630. initialize_keys(keys, keys + needed_keys_count(n_block_a, n_block_b), key_comp, xbuf);
  631. }
  632. }
  633. //////////////////////////////////
  634. //
  635. // partial_merge
  636. //
  637. //////////////////////////////////
  638. template<class InputIt1, class InputIt2, class OutputIt, class Compare, class Op>
  639. OutputIt op_partial_merge_impl
  640. (InputIt1 &r_first1, InputIt1 const last1, InputIt2 &r_first2, InputIt2 const last2, OutputIt d_first, Compare comp, Op op)
  641. {
  642. InputIt1 first1(r_first1);
  643. InputIt2 first2(r_first2);
  644. if(first2 != last2 && last1 != first1)
  645. while(1){
  646. if(comp(*first2, *first1)) {
  647. op(first2++, d_first++);
  648. if(first2 == last2){
  649. break;
  650. }
  651. }
  652. else{
  653. op(first1++, d_first++);
  654. if(first1 == last1){
  655. break;
  656. }
  657. }
  658. }
  659. r_first1 = first1;
  660. r_first2 = first2;
  661. return d_first;
  662. }
  663. template<class InputIt1, class InputIt2, class OutputIt, class Compare, class Op>
  664. OutputIt op_partial_merge
  665. (InputIt1 &r_first1, InputIt1 const last1, InputIt2 &r_first2, InputIt2 const last2, OutputIt d_first, Compare comp, Op op, bool is_stable)
  666. {
  667. return is_stable ? op_partial_merge_impl(r_first1, last1, r_first2, last2, d_first, comp, op)
  668. : op_partial_merge_impl(r_first1, last1, r_first2, last2, d_first, antistable<Compare>(comp), op);
  669. }
  670. //////////////////////////////////
  671. //////////////////////////////////
  672. //////////////////////////////////
  673. //
  674. // op_partial_merge_and_save
  675. //
  676. //////////////////////////////////
  677. //////////////////////////////////
  678. //////////////////////////////////
  679. template<class InputIt1, class InputIt2, class OutputIt, class Compare, class Op>
  680. OutputIt op_partial_merge_and_swap_impl
  681. (InputIt1 &r_first1, InputIt1 const last1, InputIt2 &r_first2, InputIt2 const last2, InputIt2 &r_first_min, OutputIt d_first, Compare comp, Op op)
  682. {
  683. InputIt1 first1(r_first1);
  684. InputIt2 first2(r_first2);
  685. if(first2 != last2 && last1 != first1) {
  686. InputIt2 first_min(r_first_min);
  687. bool non_empty_ranges = true;
  688. do{
  689. if(comp(*first_min, *first1)) {
  690. op(three_way_t(), first2++, first_min++, d_first++);
  691. non_empty_ranges = first2 != last2;
  692. }
  693. else{
  694. op(first1++, d_first++);
  695. non_empty_ranges = first1 != last1;
  696. }
  697. } while(non_empty_ranges);
  698. r_first_min = first_min;
  699. r_first1 = first1;
  700. r_first2 = first2;
  701. }
  702. return d_first;
  703. }
  704. template<class RandIt, class InputIt2, class OutputIt, class Compare, class Op>
  705. OutputIt op_partial_merge_and_swap
  706. (RandIt &r_first1, RandIt const last1, InputIt2 &r_first2, InputIt2 const last2, InputIt2 &r_first_min, OutputIt d_first, Compare comp, Op op, bool is_stable)
  707. {
  708. return is_stable ? op_partial_merge_and_swap_impl(r_first1, last1, r_first2, last2, r_first_min, d_first, comp, op)
  709. : op_partial_merge_and_swap_impl(r_first1, last1, r_first2, last2, r_first_min, d_first, antistable<Compare>(comp), op);
  710. }
  711. template<class RandIt1, class RandIt2, class RandItB, class Compare, class Op>
  712. RandItB op_buffered_partial_merge_and_swap_to_range1_and_buffer
  713. ( RandIt1 first1, RandIt1 const last1
  714. , RandIt2 &rfirst2, RandIt2 const last2, RandIt2 &rfirst_min
  715. , RandItB &rfirstb, Compare comp, Op op )
  716. {
  717. RandItB firstb = rfirstb;
  718. RandItB lastb = firstb;
  719. RandIt2 first2 = rfirst2;
  720. //Move to buffer while merging
  721. //Three way moves need less moves when op is swap_op so use it
  722. //when merging elements from range2 to the destination occupied by range1
  723. if(first1 != last1 && first2 != last2){
  724. RandIt2 first_min = rfirst_min;
  725. op(four_way_t(), first2++, first_min++, first1++, lastb++);
  726. while(first1 != last1){
  727. if(first2 == last2){
  728. lastb = op(forward_t(), first1, last1, firstb);
  729. break;
  730. }
  731. if(comp(*first_min, *firstb)){
  732. op( four_way_t(), first2++, first_min++, first1++, lastb++);
  733. }
  734. else{
  735. op(three_way_t(), firstb++, first1++, lastb++);
  736. }
  737. }
  738. rfirst2 = first2;
  739. rfirstb = firstb;
  740. rfirst_min = first_min;
  741. }
  742. return lastb;
  743. }
  744. template<class RandIt1, class RandIt2, class RandItB, class Compare, class Op>
  745. RandItB op_buffered_partial_merge_to_range1_and_buffer
  746. ( RandIt1 first1, RandIt1 const last1
  747. , RandIt2 &rfirst2, RandIt2 const last2
  748. , RandItB &rfirstb, Compare comp, Op op )
  749. {
  750. RandItB firstb = rfirstb;
  751. RandItB lastb = firstb;
  752. RandIt2 first2 = rfirst2;
  753. //Move to buffer while merging
  754. //Three way moves need less moves when op is swap_op so use it
  755. //when merging elements from range2 to the destination occupied by range1
  756. if(first1 != last1 && first2 != last2){
  757. op(three_way_t(), first2++, first1++, lastb++);
  758. while(true){
  759. if(first1 == last1){
  760. break;
  761. }
  762. if(first2 == last2){
  763. lastb = op(forward_t(), first1, last1, firstb);
  764. break;
  765. }
  766. if (comp(*first2, *firstb)) {
  767. op(three_way_t(), first2++, first1++, lastb++);
  768. }
  769. else {
  770. op(three_way_t(), firstb++, first1++, lastb++);
  771. }
  772. }
  773. rfirst2 = first2;
  774. rfirstb = firstb;
  775. }
  776. return lastb;
  777. }
  778. template<class RandIt, class RandItBuf, class Compare, class Op>
  779. RandIt op_partial_merge_and_save_impl
  780. ( RandIt first1, RandIt const last1, RandIt &rfirst2, RandIt last2, RandIt first_min
  781. , RandItBuf &buf_first1_in_out, RandItBuf &buf_last1_in_out
  782. , Compare comp, Op op
  783. )
  784. {
  785. RandItBuf buf_first1 = buf_first1_in_out;
  786. RandItBuf buf_last1 = buf_last1_in_out;
  787. RandIt first2(rfirst2);
  788. bool const do_swap = first2 != first_min;
  789. if(buf_first1 == buf_last1){
  790. //Skip any element that does not need to be moved
  791. RandIt new_first1 = skip_until_merge(first1, last1, *first_min, comp);
  792. buf_first1 += (new_first1-first1);
  793. first1 = new_first1;
  794. buf_last1 = do_swap ? op_buffered_partial_merge_and_swap_to_range1_and_buffer(first1, last1, first2, last2, first_min, buf_first1, comp, op)
  795. : op_buffered_partial_merge_to_range1_and_buffer (first1, last1, first2, last2, buf_first1, comp, op);
  796. first1 = last1;
  797. }
  798. else{
  799. BOOST_ASSERT((last1-first1) == (buf_last1 - buf_first1));
  800. }
  801. //Now merge from buffer
  802. first1 = do_swap ? op_partial_merge_and_swap_impl(buf_first1, buf_last1, first2, last2, first_min, first1, comp, op)
  803. : op_partial_merge_impl (buf_first1, buf_last1, first2, last2, first1, comp, op);
  804. buf_first1_in_out = buf_first1;
  805. buf_last1_in_out = buf_last1;
  806. rfirst2 = first2;
  807. return first1;
  808. }
  809. template<class RandIt, class RandItBuf, class Compare, class Op>
  810. RandIt op_partial_merge_and_save
  811. ( RandIt first1, RandIt const last1, RandIt &rfirst2, RandIt last2, RandIt first_min
  812. , RandItBuf &buf_first1_in_out
  813. , RandItBuf &buf_last1_in_out
  814. , Compare comp
  815. , Op op
  816. , bool is_stable)
  817. {
  818. return is_stable
  819. ? op_partial_merge_and_save_impl
  820. (first1, last1, rfirst2, last2, first_min, buf_first1_in_out, buf_last1_in_out, comp, op)
  821. : op_partial_merge_and_save_impl
  822. (first1, last1, rfirst2, last2, first_min, buf_first1_in_out, buf_last1_in_out, antistable<Compare>(comp), op)
  823. ;
  824. }
  825. //////////////////////////////////
  826. //////////////////////////////////
  827. //////////////////////////////////
  828. //
  829. // op_merge_blocks_with_irreg
  830. //
  831. //////////////////////////////////
  832. //////////////////////////////////
  833. //////////////////////////////////
  834. template<class RandItKeys, class KeyCompare, class RandIt, class RandIt2, class OutputIt, class Compare, class Op>
  835. OutputIt op_merge_blocks_with_irreg
  836. ( RandItKeys key_first
  837. , RandItKeys key_mid
  838. , KeyCompare key_comp
  839. , RandIt first_reg
  840. , RandIt2 &first_irr
  841. , RandIt2 const last_irr
  842. , OutputIt dest
  843. , typename iterator_traits<RandIt>::size_type const l_block
  844. , typename iterator_traits<RandIt>::size_type n_block_left
  845. , typename iterator_traits<RandIt>::size_type min_check
  846. , typename iterator_traits<RandIt>::size_type max_check
  847. , Compare comp, bool const is_stable, Op op)
  848. {
  849. typedef typename iterator_traits<RandIt>::size_type size_type;
  850. for(; n_block_left; --n_block_left, ++key_first, min_check -= min_check != 0, max_check -= max_check != 0){
  851. size_type next_key_idx = find_next_block(key_first, key_comp, first_reg, l_block, min_check, max_check, comp);
  852. max_check = min_value<size_type>(max_value<size_type>(max_check, next_key_idx+size_type(2)), n_block_left);
  853. RandIt const last_reg = first_reg + l_block;
  854. RandIt first_min = first_reg + next_key_idx*l_block;
  855. RandIt const last_min = first_min + l_block; (void)last_min;
  856. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first_reg, last_reg, comp));
  857. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(!next_key_idx || boost::movelib::is_sorted(first_min, last_min, comp));
  858. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT((!next_key_idx || !comp(*first_reg, *first_min )));
  859. OutputIt orig_dest = dest; (void)orig_dest;
  860. dest = next_key_idx ? op_partial_merge_and_swap(first_irr, last_irr, first_reg, last_reg, first_min, dest, comp, op, is_stable)
  861. : op_partial_merge (first_irr, last_irr, first_reg, last_reg, dest, comp, op, is_stable);
  862. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(orig_dest, dest, comp));
  863. if(first_reg == dest){
  864. dest = next_key_idx ? ::boost::adl_move_swap_ranges(first_min, last_min, first_reg)
  865. : last_reg;
  866. }
  867. else{
  868. dest = next_key_idx ? op(three_way_forward_t(), first_reg, last_reg, first_min, dest)
  869. : op(forward_t(), first_reg, last_reg, dest);
  870. }
  871. RandItKeys const key_next(key_first + next_key_idx);
  872. swap_and_update_key(key_next, key_first, key_mid, last_reg, last_reg, first_min);
  873. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(orig_dest, dest, comp));
  874. first_reg = last_reg;
  875. }
  876. return dest;
  877. }
  878. //////////////////////////////////
  879. //////////////////////////////////
  880. //////////////////////////////////
  881. //
  882. // op_merge_blocks_left/right
  883. //
  884. //////////////////////////////////
  885. //////////////////////////////////
  886. //////////////////////////////////
  887. template<class RandItKeys, class KeyCompare, class RandIt, class Compare, class Op>
  888. void op_merge_blocks_left
  889. ( RandItKeys const key_first
  890. , KeyCompare key_comp
  891. , RandIt const first
  892. , typename iterator_traits<RandIt>::size_type const l_block
  893. , typename iterator_traits<RandIt>::size_type const l_irreg1
  894. , typename iterator_traits<RandIt>::size_type const n_block_a
  895. , typename iterator_traits<RandIt>::size_type const n_block_b
  896. , typename iterator_traits<RandIt>::size_type const l_irreg2
  897. , Compare comp, Op op)
  898. {
  899. typedef typename iterator_traits<RandIt>::size_type size_type;
  900. size_type const key_count = needed_keys_count(n_block_a, n_block_b); (void)key_count;
  901. // BOOST_ASSERT(n_block_a || n_block_b);
  902. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted_and_unique(key_first, key_first + key_count, key_comp));
  903. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(!n_block_b || n_block_a == count_if_with(key_first, key_first + key_count, key_comp, key_first[n_block_a]));
  904. size_type n_block_b_left = n_block_b;
  905. size_type n_block_a_left = n_block_a;
  906. size_type n_block_left = n_block_b + n_block_a;
  907. RandItKeys key_mid(key_first + n_block_a);
  908. RandIt buffer = first - l_block;
  909. RandIt first1 = first;
  910. RandIt last1 = first1 + l_irreg1;
  911. RandIt first2 = last1;
  912. RandIt const irreg2 = first2 + n_block_left*l_block;
  913. bool is_range1_A = true;
  914. RandItKeys key_range2(key_first);
  915. ////////////////////////////////////////////////////////////////////////////
  916. //Process all regular blocks before the irregular B block
  917. ////////////////////////////////////////////////////////////////////////////
  918. size_type min_check = n_block_a == n_block_left ? 0u : n_block_a;
  919. size_type max_check = min_value<size_type>(min_check+size_type(1), n_block_left);
  920. for (; n_block_left; --n_block_left, ++key_range2, min_check -= min_check != 0, max_check -= max_check != 0) {
  921. size_type const next_key_idx = find_next_block(key_range2, key_comp, first2, l_block, min_check, max_check, comp);
  922. max_check = min_value<size_type>(max_value<size_type>(max_check, next_key_idx+size_type(2)), n_block_left);
  923. RandIt const first_min = first2 + next_key_idx*l_block;
  924. RandIt const last_min = first_min + l_block; (void)last_min;
  925. RandIt const last2 = first2 + l_block;
  926. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first1, last1, comp));
  927. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first2, last2, comp));
  928. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(!n_block_left || boost::movelib::is_sorted(first_min, last_min, comp));
  929. //Check if irregular b block should go here.
  930. //If so, break to the special code handling the irregular block
  931. if (!n_block_b_left &&
  932. ( (l_irreg2 && comp(*irreg2, *first_min)) || (!l_irreg2 && is_range1_A)) ){
  933. break;
  934. }
  935. RandItKeys const key_next(key_range2 + next_key_idx);
  936. bool const is_range2_A = key_mid == (key_first+key_count) || key_comp(*key_next, *key_mid);
  937. bool const is_buffer_middle = last1 == buffer;
  938. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT( ( is_buffer_middle && size_type(first2-buffer) == l_block && buffer == last1) ||
  939. (!is_buffer_middle && size_type(first1-buffer) == l_block && first2 == last1));
  940. if(is_range1_A == is_range2_A){
  941. BOOST_ASSERT((first1 == last1) || !comp(*first_min, last1[-1]));
  942. if(!is_buffer_middle){
  943. buffer = op(forward_t(), first1, last1, buffer);
  944. }
  945. swap_and_update_key(key_next, key_range2, key_mid, first2, last2, first_min);
  946. first1 = first2;
  947. last1 = last2;
  948. }
  949. else {
  950. RandIt unmerged;
  951. RandIt buf_beg;
  952. RandIt buf_end;
  953. if(is_buffer_middle){
  954. buf_end = buf_beg = first2 - (last1-first1);
  955. unmerged = op_partial_merge_and_save( first1, last1, first2, last2, first_min
  956. , buf_beg, buf_end, comp, op, is_range1_A);
  957. }
  958. else{
  959. buf_beg = first1;
  960. buf_end = last1;
  961. unmerged = op_partial_merge_and_save
  962. (buffer, buffer+(last1-first1), first2, last2, first_min, buf_beg, buf_end, comp, op, is_range1_A);
  963. }
  964. (void)unmerged;
  965. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first-l_block, unmerged, comp));
  966. swap_and_update_key( key_next, key_range2, key_mid, first2, last2
  967. , last_min - size_type(last2 - first2));
  968. if(buf_beg != buf_end){ //range2 exhausted: is_buffer_middle for the next iteration
  969. first1 = buf_beg;
  970. last1 = buf_end;
  971. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(buf_end == (last2-l_block));
  972. buffer = last1;
  973. }
  974. else{ //range1 exhausted: !is_buffer_middle for the next iteration
  975. first1 = first2;
  976. last1 = last2;
  977. buffer = first2 - l_block;
  978. is_range1_A = is_range2_A;
  979. }
  980. }
  981. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT( (is_range2_A && n_block_a_left) || (!is_range2_A && n_block_b_left));
  982. is_range2_A ? --n_block_a_left : --n_block_b_left;
  983. first2 = last2;
  984. }
  985. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(!n_block_b || n_block_a == count_if_with(key_first, key_range2 + n_block_left, key_comp, *key_mid));
  986. BOOST_ASSERT(!n_block_b_left);
  987. ////////////////////////////////////////////////////////////////////////////
  988. //Process remaining range 1 left before the irregular B block
  989. ////////////////////////////////////////////////////////////////////////////
  990. bool const is_buffer_middle = last1 == buffer;
  991. RandIt first_irr2 = irreg2;
  992. RandIt const last_irr2 = first_irr2 + l_irreg2;
  993. if(l_irreg2 && is_range1_A){
  994. if(is_buffer_middle){
  995. first1 = skip_until_merge(first1, last1, *first_irr2, comp);
  996. //Even if we copy backward, no overlapping occurs so use forward copy
  997. //that can be faster specially with trivial types
  998. RandIt const new_first1 = first2 - (last1 - first1);
  999. op(forward_t(), first1, last1, new_first1);
  1000. first1 = new_first1;
  1001. last1 = first2;
  1002. buffer = first1 - l_block;
  1003. }
  1004. buffer = op_partial_merge_impl(first1, last1, first_irr2, last_irr2, buffer, comp, op);
  1005. buffer = op(forward_t(), first1, last1, buffer);
  1006. }
  1007. else if(!is_buffer_middle){
  1008. buffer = op(forward_t(), first1, last1, buffer);
  1009. }
  1010. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first-l_block, buffer, comp));
  1011. ////////////////////////////////////////////////////////////////////////////
  1012. //Process irregular B block and remaining A blocks
  1013. ////////////////////////////////////////////////////////////////////////////
  1014. buffer = op_merge_blocks_with_irreg
  1015. ( key_range2, key_mid, key_comp, first2, first_irr2, last_irr2
  1016. , buffer, l_block, n_block_left, min_check, max_check, comp, false, op);
  1017. buffer = op(forward_t(), first_irr2, last_irr2, buffer);(void)buffer;
  1018. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first-l_block, buffer, comp));
  1019. }
  1020. // first - first element to merge.
  1021. // first[-l_block, 0) - buffer (if use_buf == true)
  1022. // l_block - length of regular blocks. First nblocks are stable sorted by 1st elements and key-coded
  1023. // keys - sequence of keys, in same order as blocks. key<midkey means stream A
  1024. // n_bef_irreg2/n_aft_irreg2 are regular blocks
  1025. // l_irreg2 is a irregular block, that is to be combined after n_bef_irreg2 blocks and before n_aft_irreg2 blocks
  1026. // If l_irreg2==0 then n_aft_irreg2==0 (no irregular blocks).
  1027. template<class RandItKeys, class KeyCompare, class RandIt, class Compare>
  1028. void merge_blocks_left
  1029. ( RandItKeys const key_first
  1030. , KeyCompare key_comp
  1031. , RandIt const first
  1032. , typename iterator_traits<RandIt>::size_type const l_block
  1033. , typename iterator_traits<RandIt>::size_type const l_irreg1
  1034. , typename iterator_traits<RandIt>::size_type const n_block_a
  1035. , typename iterator_traits<RandIt>::size_type const n_block_b
  1036. , typename iterator_traits<RandIt>::size_type const l_irreg2
  1037. , Compare comp
  1038. , bool const xbuf_used)
  1039. {
  1040. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(!n_block_b || n_block_a == count_if_with(key_first, key_first + needed_keys_count(n_block_a, n_block_b), key_comp, key_first[n_block_a]));
  1041. if(xbuf_used){
  1042. op_merge_blocks_left
  1043. (key_first, key_comp, first, l_block, l_irreg1, n_block_a, n_block_b, l_irreg2, comp, move_op());
  1044. }
  1045. else{
  1046. op_merge_blocks_left
  1047. (key_first, key_comp, first, l_block, l_irreg1, n_block_a, n_block_b, l_irreg2, comp, swap_op());
  1048. }
  1049. }
  1050. // first - first element to merge.
  1051. // [first+l_block*(n_bef_irreg2+n_aft_irreg2)+l_irreg2, first+l_block*(n_bef_irreg2+n_aft_irreg2+1)+l_irreg2) - buffer
  1052. // l_block - length of regular blocks. First nblocks are stable sorted by 1st elements and key-coded
  1053. // keys - sequence of keys, in same order as blocks. key<midkey means stream A
  1054. // n_bef_irreg2/n_aft_irreg2 are regular blocks
  1055. // l_irreg2 is a irregular block, that is to be combined after n_bef_irreg2 blocks and before n_aft_irreg2 blocks
  1056. // If l_irreg2==0 then n_aft_irreg2==0 (no irregular blocks).
  1057. template<class RandItKeys, class KeyCompare, class RandIt, class Compare>
  1058. void merge_blocks_right
  1059. ( RandItKeys const key_first
  1060. , KeyCompare key_comp
  1061. , RandIt const first
  1062. , typename iterator_traits<RandIt>::size_type const l_block
  1063. , typename iterator_traits<RandIt>::size_type const n_block_a
  1064. , typename iterator_traits<RandIt>::size_type const n_block_b
  1065. , typename iterator_traits<RandIt>::size_type const l_irreg2
  1066. , Compare comp
  1067. , bool const xbuf_used)
  1068. {
  1069. merge_blocks_left
  1070. ( (make_reverse_iterator)(key_first + needed_keys_count(n_block_a, n_block_b))
  1071. , inverse<KeyCompare>(key_comp)
  1072. , (make_reverse_iterator)(first + ((n_block_a+n_block_b)*l_block+l_irreg2))
  1073. , l_block
  1074. , l_irreg2
  1075. , n_block_b
  1076. , n_block_a
  1077. , 0
  1078. , inverse<Compare>(comp), xbuf_used);
  1079. }
  1080. //////////////////////////////////
  1081. //////////////////////////////////
  1082. //////////////////////////////////
  1083. //
  1084. // op_merge_blocks_with_buf
  1085. //
  1086. //////////////////////////////////
  1087. //////////////////////////////////
  1088. //////////////////////////////////
  1089. template<class RandItKeys, class KeyCompare, class RandIt, class Compare, class Op, class RandItBuf>
  1090. void op_merge_blocks_with_buf
  1091. ( RandItKeys key_first
  1092. , KeyCompare key_comp
  1093. , RandIt const first
  1094. , typename iterator_traits<RandIt>::size_type const l_block
  1095. , typename iterator_traits<RandIt>::size_type const l_irreg1
  1096. , typename iterator_traits<RandIt>::size_type const n_block_a
  1097. , typename iterator_traits<RandIt>::size_type const n_block_b
  1098. , typename iterator_traits<RandIt>::size_type const l_irreg2
  1099. , Compare comp
  1100. , Op op
  1101. , RandItBuf const buf_first)
  1102. {
  1103. typedef typename iterator_traits<RandIt>::size_type size_type;
  1104. size_type const key_count = needed_keys_count(n_block_a, n_block_b); (void)key_count;
  1105. //BOOST_ASSERT(n_block_a || n_block_b);
  1106. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted_and_unique(key_first, key_first + key_count, key_comp));
  1107. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(!n_block_b || n_block_a == count_if_with(key_first, key_first + key_count, key_comp, key_first[n_block_a]));
  1108. size_type n_block_b_left = n_block_b;
  1109. size_type n_block_a_left = n_block_a;
  1110. size_type n_block_left = n_block_b + n_block_a;
  1111. RandItKeys key_mid(key_first + n_block_a);
  1112. RandItBuf buffer = buf_first;
  1113. RandItBuf buffer_end = buffer;
  1114. RandIt first1 = first;
  1115. RandIt last1 = first1 + l_irreg1;
  1116. RandIt first2 = last1;
  1117. RandIt const first_irr2 = first2 + n_block_left*l_block;
  1118. bool is_range1_A = true;
  1119. const size_type len = l_block * n_block_a + l_block * n_block_b + l_irreg1 + l_irreg2; (void)len;
  1120. RandItKeys key_range2(key_first);
  1121. ////////////////////////////////////////////////////////////////////////////
  1122. //Process all regular blocks before the irregular B block
  1123. ////////////////////////////////////////////////////////////////////////////
  1124. size_type min_check = n_block_a == n_block_left ? 0u : n_block_a;
  1125. size_type max_check = min_value<size_type>(min_check+size_type(1), n_block_left);
  1126. for (; n_block_left; --n_block_left, ++key_range2, min_check -= min_check != 0, max_check -= max_check != 0) {
  1127. size_type const next_key_idx = find_next_block(key_range2, key_comp, first2, l_block, min_check, max_check, comp);
  1128. max_check = min_value<size_type>(max_value<size_type>(max_check, next_key_idx+size_type(2)), n_block_left);
  1129. RandIt first_min = first2 + next_key_idx*l_block;
  1130. RandIt const last_min = first_min + l_block; (void)last_min;
  1131. RandIt const last2 = first2 + l_block;
  1132. bool const buffer_empty = buffer == buffer_end; (void)buffer_empty;
  1133. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(buffer_empty ? boost::movelib::is_sorted(first1, last1, comp) : boost::movelib::is_sorted(buffer, buffer_end, comp));
  1134. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first2, last2, comp));
  1135. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(!n_block_left || boost::movelib::is_sorted(first_min, last_min, comp));
  1136. //Check if irregular b block should go here.
  1137. //If so, break to the special code handling the irregular block
  1138. if (!n_block_b_left &&
  1139. ( (l_irreg2 && comp(*first_irr2, *first_min)) || (!l_irreg2 && is_range1_A)) ){
  1140. break;
  1141. }
  1142. RandItKeys const key_next(key_range2 + next_key_idx);
  1143. bool const is_range2_A = key_mid == (key_first+key_count) || key_comp(*key_next, *key_mid);
  1144. if(is_range1_A == is_range2_A){
  1145. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT((first1 == last1) || (buffer_empty ? !comp(*first_min, last1[-1]) : !comp(*first_min, buffer_end[-1])));
  1146. //If buffered, put those elements in place
  1147. RandIt res = op(forward_t(), buffer, buffer_end, first1);
  1148. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" merge_blocks_w_fwd: ", len);
  1149. buffer = buffer_end = buf_first;
  1150. BOOST_ASSERT(buffer_empty || res == last1); (void)res;
  1151. //swap_and_update_key(key_next, key_range2, key_mid, first2, last2, first_min);
  1152. buffer_end = buffer_and_update_key(key_next, key_range2, key_mid, first2, last2, first_min, buffer = buf_first, op);
  1153. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" merge_blocks_w_swp: ", len);
  1154. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first2, last2, comp));
  1155. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first_min, last_min, comp));
  1156. first1 = first2;
  1157. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first, first1, comp));
  1158. }
  1159. else {
  1160. RandIt const unmerged = op_partial_merge_and_save(first1, last1, first2, last2, first_min, buffer, buffer_end, comp, op, is_range1_A);
  1161. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" merge_blocks_w_mrs: ", len);
  1162. bool const is_range_1_empty = buffer == buffer_end;
  1163. BOOST_ASSERT(is_range_1_empty || (buffer_end-buffer) == (last1+l_block-unmerged));
  1164. if(is_range_1_empty){
  1165. buffer = buffer_end = buf_first;
  1166. first_min = last_min - (last2 - first2);
  1167. //swap_and_update_key(key_next, key_range2, key_mid, first2, last2, first_min);
  1168. buffer_end = buffer_and_update_key(key_next, key_range2, key_mid, first2, last2, first_min, buf_first, op);
  1169. }
  1170. else{
  1171. first_min = last_min;
  1172. //swap_and_update_key(key_next, key_range2, key_mid, first2, last2, first_min);
  1173. update_key(key_next, key_range2, key_mid);
  1174. }
  1175. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(!is_range_1_empty || (last_min-first_min) == (last2-unmerged));
  1176. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" merge_blocks_w_swp: ", len);
  1177. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first_min, last_min, comp));
  1178. is_range1_A ^= is_range_1_empty;
  1179. first1 = unmerged;
  1180. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first, unmerged, comp));
  1181. }
  1182. BOOST_ASSERT( (is_range2_A && n_block_a_left) || (!is_range2_A && n_block_b_left));
  1183. is_range2_A ? --n_block_a_left : --n_block_b_left;
  1184. last1 += l_block;
  1185. first2 = last2;
  1186. }
  1187. RandIt res = op(forward_t(), buffer, buffer_end, first1); (void)res;
  1188. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first, res, comp));
  1189. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" merge_blocks_w_fwd: ", len);
  1190. ////////////////////////////////////////////////////////////////////////////
  1191. //Process irregular B block and remaining A blocks
  1192. ////////////////////////////////////////////////////////////////////////////
  1193. RandIt const last_irr2 = first_irr2 + l_irreg2;
  1194. op(forward_t(), first_irr2, first_irr2+l_irreg2, buf_first);
  1195. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" merge_blocks_w_fwir:", len);
  1196. buffer = buf_first;
  1197. buffer_end = buffer+l_irreg2;
  1198. reverse_iterator<RandItBuf> rbuf_beg(buffer_end);
  1199. RandIt dest = op_merge_blocks_with_irreg
  1200. ((make_reverse_iterator)(key_first + n_block_b + n_block_a), (make_reverse_iterator)(key_mid), inverse<KeyCompare>(key_comp)
  1201. , (make_reverse_iterator)(first_irr2), rbuf_beg, (make_reverse_iterator)(buffer), (make_reverse_iterator)(last_irr2)
  1202. , l_block, n_block_left, 0, n_block_left
  1203. , inverse<Compare>(comp), true, op).base();
  1204. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(dest, last_irr2, comp));
  1205. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" merge_blocks_w_irg: ", len);
  1206. buffer_end = rbuf_beg.base();
  1207. BOOST_ASSERT((dest-last1) == (buffer_end-buffer));
  1208. op_merge_with_left_placed(is_range1_A ? first1 : last1, last1, dest, buffer, buffer_end, comp, op);
  1209. BOOST_MOVE_ADAPTIVE_SORT_PRINT_L2(" merge_with_left_plc:", len);
  1210. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(first, last_irr2, comp));
  1211. }
  1212. //////////////////////////////////
  1213. //////////////////////////////////
  1214. //////////////////////////////////
  1215. //
  1216. // op_insertion_sort_step_left/right
  1217. //
  1218. //////////////////////////////////
  1219. //////////////////////////////////
  1220. //////////////////////////////////
  1221. template<class RandIt, class Compare, class Op>
  1222. typename iterator_traits<RandIt>::size_type
  1223. op_insertion_sort_step_left
  1224. ( RandIt const first
  1225. , typename iterator_traits<RandIt>::size_type const length
  1226. , typename iterator_traits<RandIt>::size_type const step
  1227. , Compare comp, Op op)
  1228. {
  1229. typedef typename iterator_traits<RandIt>::size_type size_type;
  1230. size_type const s = min_value<size_type>(step, AdaptiveSortInsertionSortThreshold);
  1231. size_type m = 0;
  1232. while((length - m) > s){
  1233. insertion_sort_op(first+m, first+m+s, first+m-s, comp, op);
  1234. m += s;
  1235. }
  1236. insertion_sort_op(first+m, first+length, first+m-s, comp, op);
  1237. return s;
  1238. }
  1239. template<class RandIt, class Compare, class Op>
  1240. void op_merge_right_step_once
  1241. ( RandIt first_block
  1242. , typename iterator_traits<RandIt>::size_type const elements_in_blocks
  1243. , typename iterator_traits<RandIt>::size_type const l_build_buf
  1244. , Compare comp
  1245. , Op op)
  1246. {
  1247. typedef typename iterator_traits<RandIt>::size_type size_type;
  1248. size_type restk = elements_in_blocks%(2*l_build_buf);
  1249. size_type p = elements_in_blocks - restk;
  1250. BOOST_ASSERT(0 == (p%(2*l_build_buf)));
  1251. if(restk <= l_build_buf){
  1252. op(backward_t(),first_block+p, first_block+p+restk, first_block+p+restk+l_build_buf);
  1253. }
  1254. else{
  1255. op_merge_right(first_block+p, first_block+p+l_build_buf, first_block+p+restk, first_block+p+restk+l_build_buf, comp, op);
  1256. }
  1257. while(p>0){
  1258. p -= 2*l_build_buf;
  1259. op_merge_right(first_block+p, first_block+p+l_build_buf, first_block+p+2*l_build_buf, first_block+p+3*l_build_buf, comp, op);
  1260. }
  1261. }
  1262. //////////////////////////////////
  1263. //////////////////////////////////
  1264. //////////////////////////////////
  1265. //
  1266. // insertion_sort_step
  1267. //
  1268. //////////////////////////////////
  1269. //////////////////////////////////
  1270. //////////////////////////////////
  1271. template<class RandIt, class Compare>
  1272. typename iterator_traits<RandIt>::size_type
  1273. insertion_sort_step
  1274. ( RandIt const first
  1275. , typename iterator_traits<RandIt>::size_type const length
  1276. , typename iterator_traits<RandIt>::size_type const step
  1277. , Compare comp)
  1278. {
  1279. typedef typename iterator_traits<RandIt>::size_type size_type;
  1280. size_type const s = min_value<size_type>(step, AdaptiveSortInsertionSortThreshold);
  1281. size_type m = 0;
  1282. while((length - m) > s){
  1283. insertion_sort(first+m, first+m+s, comp);
  1284. m += s;
  1285. }
  1286. insertion_sort(first+m, first+length, comp);
  1287. return s;
  1288. }
  1289. //////////////////////////////////
  1290. //////////////////////////////////
  1291. //////////////////////////////////
  1292. //
  1293. // op_merge_left_step_multiple
  1294. //
  1295. //////////////////////////////////
  1296. //////////////////////////////////
  1297. //////////////////////////////////
  1298. template<class RandIt, class Compare, class Op>
  1299. typename iterator_traits<RandIt>::size_type
  1300. op_merge_left_step_multiple
  1301. ( RandIt first_block
  1302. , typename iterator_traits<RandIt>::size_type const elements_in_blocks
  1303. , typename iterator_traits<RandIt>::size_type l_merged
  1304. , typename iterator_traits<RandIt>::size_type const l_build_buf
  1305. , typename iterator_traits<RandIt>::size_type l_left_space
  1306. , Compare comp
  1307. , Op op)
  1308. {
  1309. typedef typename iterator_traits<RandIt>::size_type size_type;
  1310. for(; l_merged < l_build_buf && l_left_space >= l_merged; l_merged*=2){
  1311. size_type p0=0;
  1312. RandIt pos = first_block;
  1313. while((elements_in_blocks - p0) > 2*l_merged) {
  1314. op_merge_left(pos-l_merged, pos, pos+l_merged, pos+2*l_merged, comp, op);
  1315. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(pos-l_merged, pos+l_merged, comp));
  1316. p0 += 2*l_merged;
  1317. pos = first_block+p0;
  1318. }
  1319. if((elements_in_blocks-p0) > l_merged) {
  1320. op_merge_left(pos-l_merged, pos, pos+l_merged, first_block+elements_in_blocks, comp, op);
  1321. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(pos-l_merged, pos-l_merged+(first_block+elements_in_blocks-pos), comp));
  1322. }
  1323. else {
  1324. op(forward_t(), pos, first_block+elements_in_blocks, pos-l_merged);
  1325. BOOST_MOVE_ADAPTIVE_SORT_INVARIANT(boost::movelib::is_sorted(pos-l_merged, first_block+elements_in_blocks-l_merged, comp));
  1326. }
  1327. first_block -= l_merged;
  1328. l_left_space -= l_merged;
  1329. }
  1330. return l_merged;
  1331. }
  1332. } //namespace detail_adaptive {
  1333. } //namespace movelib {
  1334. } //namespace boost {
  1335. #include <boost/move/detail/config_end.hpp>
  1336. #endif //#define BOOST_MOVE_ADAPTIVE_SORT_MERGE_HPP