merge.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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_MERGE_HPP
  12. #define BOOST_MOVE_MERGE_HPP
  13. #include <boost/move/algo/move.hpp>
  14. #include <boost/move/adl_move_swap.hpp>
  15. #include <boost/move/algo/detail/basic_op.hpp>
  16. #include <boost/move/detail/iterator_traits.hpp>
  17. #include <boost/move/detail/destruct_n.hpp>
  18. #include <boost/move/algo/predicate.hpp>
  19. #include <boost/move/detail/iterator_to_raw_pointer.hpp>
  20. #include <boost/assert.hpp>
  21. #include <cstddef>
  22. namespace boost {
  23. namespace movelib {
  24. template<class T, class RandRawIt = T*, class SizeType = typename iterator_traits<RandRawIt>::size_type>
  25. class adaptive_xbuf
  26. {
  27. adaptive_xbuf(const adaptive_xbuf &);
  28. adaptive_xbuf & operator=(const adaptive_xbuf &);
  29. #if !defined(UINTPTR_MAX)
  30. typedef std::size_t uintptr_t;
  31. #endif
  32. public:
  33. typedef RandRawIt iterator;
  34. typedef SizeType size_type;
  35. adaptive_xbuf()
  36. : m_ptr(), m_size(0), m_capacity(0)
  37. {}
  38. adaptive_xbuf(RandRawIt raw_memory, size_type capacity)
  39. : m_ptr(raw_memory), m_size(0), m_capacity(capacity)
  40. {}
  41. template<class RandIt>
  42. void move_assign(RandIt first, size_type n)
  43. {
  44. if(n <= m_size){
  45. boost::move(first, first+n, m_ptr);
  46. size_type size = m_size;
  47. while(size-- != n){
  48. m_ptr[size].~T();
  49. }
  50. m_size = n;
  51. }
  52. else{
  53. RandRawIt result = boost::move(first, first+m_size, m_ptr);
  54. boost::uninitialized_move(first+m_size, first+n, result);
  55. m_size = n;
  56. }
  57. }
  58. template<class RandIt>
  59. void push_back(RandIt first, size_type n)
  60. {
  61. BOOST_ASSERT(m_capacity - m_size >= n);
  62. boost::uninitialized_move(first, first+n, m_ptr+m_size);
  63. m_size += n;
  64. }
  65. template<class RandIt>
  66. iterator add(RandIt it)
  67. {
  68. BOOST_ASSERT(m_size < m_capacity);
  69. RandRawIt p_ret = m_ptr + m_size;
  70. ::new(&*p_ret) T(::boost::move(*it));
  71. ++m_size;
  72. return p_ret;
  73. }
  74. template<class RandIt>
  75. void insert(iterator pos, RandIt it)
  76. {
  77. if(pos == (m_ptr + m_size)){
  78. this->add(it);
  79. }
  80. else{
  81. this->add(m_ptr+m_size-1);
  82. //m_size updated
  83. boost::move_backward(pos, m_ptr+m_size-2, m_ptr+m_size-1);
  84. *pos = boost::move(*it);
  85. }
  86. }
  87. void set_size(size_type size)
  88. {
  89. m_size = size;
  90. }
  91. void shrink_to_fit(size_type const size)
  92. {
  93. if(m_size > size){
  94. for(size_type szt_i = size; szt_i != m_size; ++szt_i){
  95. m_ptr[szt_i].~T();
  96. }
  97. m_size = size;
  98. }
  99. }
  100. void initialize_until(size_type const size, T &t)
  101. {
  102. BOOST_ASSERT(m_size < m_capacity);
  103. if(m_size < size){
  104. BOOST_TRY
  105. {
  106. ::new((void*)&m_ptr[m_size]) T(::boost::move(t));
  107. ++m_size;
  108. for(; m_size != size; ++m_size){
  109. ::new((void*)&m_ptr[m_size]) T(::boost::move(m_ptr[m_size-1]));
  110. }
  111. t = ::boost::move(m_ptr[m_size-1]);
  112. }
  113. BOOST_CATCH(...)
  114. {
  115. while(m_size)
  116. {
  117. --m_size;
  118. m_ptr[m_size].~T();
  119. }
  120. }
  121. BOOST_CATCH_END
  122. }
  123. }
  124. private:
  125. template<class RIt>
  126. static bool is_raw_ptr(RIt)
  127. {
  128. return false;
  129. }
  130. static bool is_raw_ptr(T*)
  131. {
  132. return true;
  133. }
  134. public:
  135. template<class U>
  136. bool supports_aligned_trailing(size_type size, size_type trail_count) const
  137. {
  138. if(this->is_raw_ptr(this->data()) && m_capacity){
  139. uintptr_t u_addr_sz = uintptr_t(&*(this->data()+size));
  140. uintptr_t u_addr_cp = uintptr_t(&*(this->data()+this->capacity()));
  141. u_addr_sz = ((u_addr_sz + sizeof(U)-1)/sizeof(U))*sizeof(U);
  142. return (u_addr_cp >= u_addr_sz) && ((u_addr_cp - u_addr_sz)/sizeof(U) >= trail_count);
  143. }
  144. return false;
  145. }
  146. template<class U>
  147. U *aligned_trailing() const
  148. {
  149. return this->aligned_trailing<U>(this->size());
  150. }
  151. template<class U>
  152. U *aligned_trailing(size_type pos) const
  153. {
  154. uintptr_t u_addr = uintptr_t(&*(this->data()+pos));
  155. u_addr = ((u_addr + sizeof(U)-1)/sizeof(U))*sizeof(U);
  156. return (U*)u_addr;
  157. }
  158. ~adaptive_xbuf()
  159. {
  160. this->clear();
  161. }
  162. size_type capacity() const
  163. { return m_capacity; }
  164. iterator data() const
  165. { return m_ptr; }
  166. iterator begin() const
  167. { return m_ptr; }
  168. iterator end() const
  169. { return m_ptr+m_size; }
  170. size_type size() const
  171. { return m_size; }
  172. bool empty() const
  173. { return !m_size; }
  174. void clear()
  175. {
  176. this->shrink_to_fit(0u);
  177. }
  178. private:
  179. RandRawIt m_ptr;
  180. size_type m_size;
  181. size_type m_capacity;
  182. };
  183. template<class Iterator, class SizeType, class Op>
  184. class range_xbuf
  185. {
  186. range_xbuf(const range_xbuf &);
  187. range_xbuf & operator=(const range_xbuf &);
  188. public:
  189. typedef SizeType size_type;
  190. typedef Iterator iterator;
  191. range_xbuf(Iterator first, Iterator last)
  192. : m_first(first), m_last(first), m_cap(last)
  193. {}
  194. template<class RandIt>
  195. void move_assign(RandIt first, size_type n)
  196. {
  197. BOOST_ASSERT(size_type(n) <= size_type(m_cap-m_first));
  198. m_last = Op()(forward_t(), first, first+n, m_first);
  199. }
  200. ~range_xbuf()
  201. {}
  202. size_type capacity() const
  203. { return m_cap-m_first; }
  204. Iterator data() const
  205. { return m_first; }
  206. Iterator end() const
  207. { return m_last; }
  208. size_type size() const
  209. { return m_last-m_first; }
  210. bool empty() const
  211. { return m_first == m_last; }
  212. void clear()
  213. {
  214. m_last = m_first;
  215. }
  216. template<class RandIt>
  217. iterator add(RandIt it)
  218. {
  219. Iterator pos(m_last);
  220. *pos = boost::move(*it);
  221. ++m_last;
  222. return pos;
  223. }
  224. void set_size(size_type size)
  225. {
  226. m_last = m_first;
  227. m_last += size;
  228. }
  229. private:
  230. Iterator const m_first;
  231. Iterator m_last;
  232. Iterator const m_cap;
  233. };
  234. // @cond
  235. /*
  236. template<typename Unsigned>
  237. inline Unsigned gcd(Unsigned x, Unsigned y)
  238. {
  239. if(0 == ((x &(x-1)) | (y & (y-1)))){
  240. return x < y ? x : y;
  241. }
  242. else{
  243. do
  244. {
  245. Unsigned t = x % y;
  246. x = y;
  247. y = t;
  248. } while (y);
  249. return x;
  250. }
  251. }
  252. */
  253. //Modified version from "An Optimal In-Place Array Rotation Algorithm", Ching-Kuang Shene
  254. template<typename Unsigned>
  255. Unsigned gcd(Unsigned x, Unsigned y)
  256. {
  257. if(0 == ((x &(x-1)) | (y & (y-1)))){
  258. return x < y ? x : y;
  259. }
  260. else{
  261. Unsigned z = 1;
  262. while((!(x&1)) & (!(y&1))){
  263. z <<=1, x>>=1, y>>=1;
  264. }
  265. while(x && y){
  266. if(!(x&1))
  267. x >>=1;
  268. else if(!(y&1))
  269. y >>=1;
  270. else if(x >=y)
  271. x = (x-y) >> 1;
  272. else
  273. y = (y-x) >> 1;
  274. }
  275. return z*(x+y);
  276. }
  277. }
  278. template<typename RandIt>
  279. RandIt rotate_gcd(RandIt first, RandIt middle, RandIt last)
  280. {
  281. typedef typename iterator_traits<RandIt>::size_type size_type;
  282. typedef typename iterator_traits<RandIt>::value_type value_type;
  283. if(first == middle)
  284. return last;
  285. if(middle == last)
  286. return first;
  287. const size_type middle_pos = size_type(middle - first);
  288. RandIt ret = last - middle_pos;
  289. if (middle == ret){
  290. boost::adl_move_swap_ranges(first, middle, middle);
  291. }
  292. else{
  293. const size_type length = size_type(last - first);
  294. for( RandIt it_i(first), it_gcd(it_i + gcd(length, middle_pos))
  295. ; it_i != it_gcd
  296. ; ++it_i){
  297. value_type temp(boost::move(*it_i));
  298. RandIt it_j = it_i;
  299. RandIt it_k = it_j+middle_pos;
  300. do{
  301. *it_j = boost::move(*it_k);
  302. it_j = it_k;
  303. size_type const left = size_type(last - it_j);
  304. it_k = left > middle_pos ? it_j + middle_pos : first + (middle_pos - left);
  305. } while(it_k != it_i);
  306. *it_j = boost::move(temp);
  307. }
  308. }
  309. return ret;
  310. }
  311. template <class RandIt, class T, class Compare>
  312. RandIt lower_bound
  313. (RandIt first, const RandIt last, const T& key, Compare comp)
  314. {
  315. typedef typename iterator_traits
  316. <RandIt>::size_type size_type;
  317. size_type len = size_type(last - first);
  318. RandIt middle;
  319. while (len) {
  320. size_type step = len >> 1;
  321. middle = first;
  322. middle += step;
  323. if (comp(*middle, key)) {
  324. first = ++middle;
  325. len -= step + 1;
  326. }
  327. else{
  328. len = step;
  329. }
  330. }
  331. return first;
  332. }
  333. template <class RandIt, class T, class Compare>
  334. RandIt upper_bound
  335. (RandIt first, const RandIt last, const T& key, Compare comp)
  336. {
  337. typedef typename iterator_traits
  338. <RandIt>::size_type size_type;
  339. size_type len = size_type(last - first);
  340. RandIt middle;
  341. while (len) {
  342. size_type step = len >> 1;
  343. middle = first;
  344. middle += step;
  345. if (!comp(key, *middle)) {
  346. first = ++middle;
  347. len -= step + 1;
  348. }
  349. else{
  350. len = step;
  351. }
  352. }
  353. return first;
  354. }
  355. template<class RandIt, class Compare, class Op>
  356. void op_merge_left( RandIt buf_first
  357. , RandIt first1
  358. , RandIt const last1
  359. , RandIt const last2
  360. , Compare comp
  361. , Op op)
  362. {
  363. for(RandIt first2=last1; first2 != last2; ++buf_first){
  364. if(first1 == last1){
  365. op(forward_t(), first2, last2, buf_first);
  366. return;
  367. }
  368. else if(comp(*first2, *first1)){
  369. op(first2, buf_first);
  370. ++first2;
  371. }
  372. else{
  373. op(first1, buf_first);
  374. ++first1;
  375. }
  376. }
  377. if(buf_first != first1){//In case all remaining elements are in the same place
  378. //(e.g. buffer is exactly the size of the second half
  379. //and all elements from the second half are less)
  380. op(forward_t(), first1, last1, buf_first);
  381. }
  382. }
  383. // [buf_first, first1) -> buffer
  384. // [first1, last1) merge [last1,last2) -> [buf_first,buf_first+(last2-first1))
  385. // Elements from buffer are moved to [last2 - (first1-buf_first), last2)
  386. // Note: distance(buf_first, first1) >= distance(last1, last2), so no overlapping occurs
  387. template<class RandIt, class Compare>
  388. void merge_left
  389. (RandIt buf_first, RandIt first1, RandIt const last1, RandIt const last2, Compare comp)
  390. {
  391. op_merge_left(buf_first, first1, last1, last2, comp, move_op());
  392. }
  393. // [buf_first, first1) -> buffer
  394. // [first1, last1) merge [last1,last2) -> [buf_first,buf_first+(last2-first1))
  395. // Elements from buffer are swapped to [last2 - (first1-buf_first), last2)
  396. // Note: distance(buf_first, first1) >= distance(last1, last2), so no overlapping occurs
  397. template<class RandIt, class Compare>
  398. void swap_merge_left
  399. (RandIt buf_first, RandIt first1, RandIt const last1, RandIt const last2, Compare comp)
  400. {
  401. op_merge_left(buf_first, first1, last1, last2, comp, swap_op());
  402. }
  403. template<class RandIt, class Compare, class Op>
  404. void op_merge_right
  405. (RandIt const first1, RandIt last1, RandIt last2, RandIt buf_last, Compare comp, Op op)
  406. {
  407. RandIt const first2 = last1;
  408. while(first1 != last1){
  409. if(last2 == first2){
  410. op(backward_t(), first1, last1, buf_last);
  411. return;
  412. }
  413. --last2;
  414. --last1;
  415. --buf_last;
  416. if(comp(*last2, *last1)){
  417. op(last1, buf_last);
  418. ++last2;
  419. }
  420. else{
  421. op(last2, buf_last);
  422. ++last1;
  423. }
  424. }
  425. if(last2 != buf_last){ //In case all remaining elements are in the same place
  426. //(e.g. buffer is exactly the size of the first half
  427. //and all elements from the second half are less)
  428. op(backward_t(), first2, last2, buf_last);
  429. }
  430. }
  431. // [last2, buf_last) - buffer
  432. // [first1, last1) merge [last1,last2) -> [first1+(buf_last-last2), buf_last)
  433. // Note: distance[last2, buf_last) >= distance[first1, last1), so no overlapping occurs
  434. template<class RandIt, class Compare>
  435. void merge_right
  436. (RandIt first1, RandIt last1, RandIt last2, RandIt buf_last, Compare comp)
  437. {
  438. op_merge_right(first1, last1, last2, buf_last, comp, move_op());
  439. }
  440. // [last2, buf_last) - buffer
  441. // [first1, last1) merge [last1,last2) -> [first1+(buf_last-last2), buf_last)
  442. // Note: distance[last2, buf_last) >= distance[first1, last1), so no overlapping occurs
  443. template<class RandIt, class Compare>
  444. void swap_merge_right
  445. (RandIt first1, RandIt last1, RandIt last2, RandIt buf_last, Compare comp)
  446. {
  447. op_merge_right(first1, last1, last2, buf_last, comp, swap_op());
  448. }
  449. ///////////////////////////////////////////////////////////////////////////////
  450. //
  451. // BUFFERED MERGE
  452. //
  453. ///////////////////////////////////////////////////////////////////////////////
  454. template<class RandIt, class Compare, class Op, class Buf>
  455. void op_buffered_merge
  456. ( RandIt first, RandIt const middle, RandIt last
  457. , Compare comp, Op op
  458. , Buf &xbuf)
  459. {
  460. if(first != middle && middle != last && comp(*middle, middle[-1])){
  461. typedef typename iterator_traits<RandIt>::size_type size_type;
  462. size_type const len1 = size_type(middle-first);
  463. size_type const len2 = size_type(last-middle);
  464. if(len1 <= len2){
  465. first = boost::movelib::upper_bound(first, middle, *middle, comp);
  466. xbuf.move_assign(first, size_type(middle-first));
  467. op_merge_with_right_placed
  468. (xbuf.data(), xbuf.end(), first, middle, last, comp, op);
  469. }
  470. else{
  471. last = boost::movelib::lower_bound(middle, last, middle[-1], comp);
  472. xbuf.move_assign(middle, size_type(last-middle));
  473. op_merge_with_left_placed
  474. (first, middle, last, xbuf.data(), xbuf.end(), comp, op);
  475. }
  476. }
  477. }
  478. template<class RandIt, class Compare, class XBuf>
  479. void buffered_merge
  480. ( RandIt first, RandIt const middle, RandIt last
  481. , Compare comp
  482. , XBuf &xbuf)
  483. {
  484. op_buffered_merge(first, middle, last, comp, move_op(), xbuf);
  485. }
  486. //Complexity: min(len1,len2)^2 + max(len1,len2)
  487. template<class RandIt, class Compare>
  488. void merge_bufferless_ON2(RandIt first, RandIt middle, RandIt last, Compare comp)
  489. {
  490. if((middle - first) < (last - middle)){
  491. while(first != middle){
  492. RandIt const old_last1 = middle;
  493. middle = boost::movelib::lower_bound(middle, last, *first, comp);
  494. first = rotate_gcd(first, old_last1, middle);
  495. if(middle == last){
  496. break;
  497. }
  498. do{
  499. ++first;
  500. } while(first != middle && !comp(*middle, *first));
  501. }
  502. }
  503. else{
  504. while(middle != last){
  505. RandIt p = boost::movelib::upper_bound(first, middle, last[-1], comp);
  506. last = rotate_gcd(p, middle, last);
  507. middle = p;
  508. if(middle == first){
  509. break;
  510. }
  511. --p;
  512. do{
  513. --last;
  514. } while(middle != last && !comp(last[-1], *p));
  515. }
  516. }
  517. }
  518. static const std::size_t MergeBufferlessONLogNRotationThreshold = 16u;
  519. template <class RandIt, class Compare>
  520. void merge_bufferless_ONlogN_recursive
  521. ( RandIt first, RandIt middle, RandIt last
  522. , typename iterator_traits<RandIt>::size_type len1
  523. , typename iterator_traits<RandIt>::size_type len2
  524. , Compare comp)
  525. {
  526. typedef typename iterator_traits<RandIt>::size_type size_type;
  527. while(1) {
  528. //trivial cases
  529. if (!len2) {
  530. return;
  531. }
  532. else if (!len1) {
  533. return;
  534. }
  535. else if (size_type(len1 | len2) == 1u) {
  536. if (comp(*middle, *first))
  537. adl_move_swap(*first, *middle);
  538. return;
  539. }
  540. else if(size_type(len1+len2) < MergeBufferlessONLogNRotationThreshold){
  541. merge_bufferless_ON2(first, middle, last, comp);
  542. return;
  543. }
  544. RandIt first_cut = first;
  545. RandIt second_cut = middle;
  546. size_type len11 = 0;
  547. size_type len22 = 0;
  548. if (len1 > len2) {
  549. len11 = len1 / 2;
  550. first_cut += len11;
  551. second_cut = boost::movelib::lower_bound(middle, last, *first_cut, comp);
  552. len22 = size_type(second_cut - middle);
  553. }
  554. else {
  555. len22 = len2 / 2;
  556. second_cut += len22;
  557. first_cut = boost::movelib::upper_bound(first, middle, *second_cut, comp);
  558. len11 = size_type(first_cut - first);
  559. }
  560. RandIt new_middle = rotate_gcd(first_cut, middle, second_cut);
  561. //Avoid one recursive call doing a manual tail call elimination on the biggest range
  562. const size_type len_internal = len11+len22;
  563. if( len_internal < (len1 + len2 - len_internal) ) {
  564. merge_bufferless_ONlogN_recursive(first, first_cut, new_middle, len11, len22, comp);
  565. first = new_middle;
  566. middle = second_cut;
  567. len1 -= len11;
  568. len2 -= len22;
  569. }
  570. else {
  571. merge_bufferless_ONlogN_recursive(new_middle, second_cut, last, len1 - len11, len2 - len22, comp);
  572. middle = first_cut;
  573. last = new_middle;
  574. len1 = len11;
  575. len2 = len22;
  576. }
  577. }
  578. }
  579. //Complexity: NlogN
  580. template<class RandIt, class Compare>
  581. void merge_bufferless_ONlogN(RandIt first, RandIt middle, RandIt last, Compare comp)
  582. {
  583. typedef typename iterator_traits<RandIt>::size_type size_type;
  584. merge_bufferless_ONlogN_recursive
  585. (first, middle, last, size_type(middle - first), size_type(last - middle), comp);
  586. }
  587. template<class RandIt, class Compare>
  588. void merge_bufferless(RandIt first, RandIt middle, RandIt last, Compare comp)
  589. {
  590. #define BOOST_ADAPTIVE_MERGE_NLOGN_MERGE
  591. #ifdef BOOST_ADAPTIVE_MERGE_NLOGN_MERGE
  592. merge_bufferless_ONlogN(first, middle, last, comp);
  593. #else
  594. merge_bufferless_ON2(first, middle, last, comp);
  595. #endif //BOOST_ADAPTIVE_MERGE_NLOGN_MERGE
  596. }
  597. // [r_first, r_last) are already in the right part of the destination range.
  598. template <class Compare, class InputIterator, class InputOutIterator, class Op>
  599. void op_merge_with_right_placed
  600. ( InputIterator first, InputIterator last
  601. , InputOutIterator dest_first, InputOutIterator r_first, InputOutIterator r_last
  602. , Compare comp, Op op)
  603. {
  604. BOOST_ASSERT((last - first) == (r_first - dest_first));
  605. while ( first != last ) {
  606. if (r_first == r_last) {
  607. InputOutIterator end = op(forward_t(), first, last, dest_first);
  608. BOOST_ASSERT(end == r_last);
  609. (void)end;
  610. return;
  611. }
  612. else if (comp(*r_first, *first)) {
  613. op(r_first, dest_first);
  614. ++r_first;
  615. }
  616. else {
  617. op(first, dest_first);
  618. ++first;
  619. }
  620. ++dest_first;
  621. }
  622. // Remaining [r_first, r_last) already in the correct place
  623. }
  624. template <class Compare, class InputIterator, class InputOutIterator>
  625. void swap_merge_with_right_placed
  626. ( InputIterator first, InputIterator last
  627. , InputOutIterator dest_first, InputOutIterator r_first, InputOutIterator r_last
  628. , Compare comp)
  629. {
  630. op_merge_with_right_placed(first, last, dest_first, r_first, r_last, comp, swap_op());
  631. }
  632. // [first, last) are already in the right part of the destination range.
  633. template <class Compare, class Op, class BidirIterator, class BidirOutIterator>
  634. void op_merge_with_left_placed
  635. ( BidirOutIterator const first, BidirOutIterator last, BidirOutIterator dest_last
  636. , BidirIterator const r_first, BidirIterator r_last
  637. , Compare comp, Op op)
  638. {
  639. BOOST_ASSERT((dest_last - last) == (r_last - r_first));
  640. while( r_first != r_last ) {
  641. if(first == last) {
  642. BidirOutIterator res = op(backward_t(), r_first, r_last, dest_last);
  643. BOOST_ASSERT(last == res);
  644. (void)res;
  645. return;
  646. }
  647. --r_last;
  648. --last;
  649. if(comp(*r_last, *last)){
  650. ++r_last;
  651. --dest_last;
  652. op(last, dest_last);
  653. }
  654. else{
  655. ++last;
  656. --dest_last;
  657. op(r_last, dest_last);
  658. }
  659. }
  660. // Remaining [first, last) already in the correct place
  661. }
  662. // @endcond
  663. // [first, last) are already in the right part of the destination range.
  664. template <class Compare, class BidirIterator, class BidirOutIterator>
  665. void merge_with_left_placed
  666. ( BidirOutIterator const first, BidirOutIterator last, BidirOutIterator dest_last
  667. , BidirIterator const r_first, BidirIterator r_last
  668. , Compare comp)
  669. {
  670. op_merge_with_left_placed(first, last, dest_last, r_first, r_last, comp, move_op());
  671. }
  672. // [r_first, r_last) are already in the right part of the destination range.
  673. template <class Compare, class InputIterator, class InputOutIterator>
  674. void merge_with_right_placed
  675. ( InputIterator first, InputIterator last
  676. , InputOutIterator dest_first, InputOutIterator r_first, InputOutIterator r_last
  677. , Compare comp)
  678. {
  679. op_merge_with_right_placed(first, last, dest_first, r_first, r_last, comp, move_op());
  680. }
  681. // [r_first, r_last) are already in the right part of the destination range.
  682. // [dest_first, r_first) is uninitialized memory
  683. template <class Compare, class InputIterator, class InputOutIterator>
  684. void uninitialized_merge_with_right_placed
  685. ( InputIterator first, InputIterator last
  686. , InputOutIterator dest_first, InputOutIterator r_first, InputOutIterator r_last
  687. , Compare comp)
  688. {
  689. BOOST_ASSERT((last - first) == (r_first - dest_first));
  690. typedef typename iterator_traits<InputOutIterator>::value_type value_type;
  691. InputOutIterator const original_r_first = r_first;
  692. destruct_n<value_type, InputOutIterator> d(dest_first);
  693. while ( first != last && dest_first != original_r_first ) {
  694. if (r_first == r_last) {
  695. for(; dest_first != original_r_first; ++dest_first, ++first){
  696. ::new((iterator_to_raw_pointer)(dest_first)) value_type(::boost::move(*first));
  697. d.incr();
  698. }
  699. d.release();
  700. InputOutIterator end = ::boost::move(first, last, original_r_first);
  701. BOOST_ASSERT(end == r_last);
  702. (void)end;
  703. return;
  704. }
  705. else if (comp(*r_first, *first)) {
  706. ::new((iterator_to_raw_pointer)(dest_first)) value_type(::boost::move(*r_first));
  707. d.incr();
  708. ++r_first;
  709. }
  710. else {
  711. ::new((iterator_to_raw_pointer)(dest_first)) value_type(::boost::move(*first));
  712. d.incr();
  713. ++first;
  714. }
  715. ++dest_first;
  716. }
  717. d.release();
  718. merge_with_right_placed(first, last, original_r_first, r_first, r_last, comp);
  719. }
  720. /*
  721. // [r_first, r_last) are already in the right part of the destination range.
  722. // [dest_first, r_first) is uninitialized memory
  723. template <class Compare, class BidirOutIterator, class BidirIterator>
  724. void uninitialized_merge_with_left_placed
  725. ( BidirOutIterator dest_first, BidirOutIterator r_first, BidirOutIterator r_last
  726. , BidirIterator first, BidirIterator last
  727. , Compare comp)
  728. {
  729. BOOST_ASSERT((last - first) == (r_last - r_first));
  730. typedef typename iterator_traits<BidirOutIterator>::value_type value_type;
  731. BidirOutIterator const original_r_last = r_last;
  732. destruct_n<value_type> d(&*dest_last);
  733. while ( first != last && dest_first != original_r_first ) {
  734. if (r_first == r_last) {
  735. for(; dest_first != original_r_first; ++dest_first, ++first){
  736. ::new(&*dest_first) value_type(::boost::move(*first));
  737. d.incr();
  738. }
  739. d.release();
  740. BidirOutIterator end = ::boost::move(first, last, original_r_first);
  741. BOOST_ASSERT(end == r_last);
  742. (void)end;
  743. return;
  744. }
  745. else if (comp(*r_first, *first)) {
  746. ::new(&*dest_first) value_type(::boost::move(*r_first));
  747. d.incr();
  748. ++r_first;
  749. }
  750. else {
  751. ::new(&*dest_first) value_type(::boost::move(*first));
  752. d.incr();
  753. ++first;
  754. }
  755. ++dest_first;
  756. }
  757. d.release();
  758. merge_with_right_placed(first, last, original_r_first, r_first, r_last, comp);
  759. }
  760. */
  761. /// This is a helper function for the merge routines.
  762. template<typename BidirectionalIterator1, typename BidirectionalIterator2>
  763. BidirectionalIterator1
  764. rotate_adaptive(BidirectionalIterator1 first,
  765. BidirectionalIterator1 middle,
  766. BidirectionalIterator1 last,
  767. typename iterator_traits<BidirectionalIterator1>::size_type len1,
  768. typename iterator_traits<BidirectionalIterator1>::size_type len2,
  769. BidirectionalIterator2 buffer,
  770. typename iterator_traits<BidirectionalIterator1>::size_type buffer_size)
  771. {
  772. if (len1 > len2 && len2 <= buffer_size)
  773. {
  774. if(len2) //Protect against self-move ranges
  775. {
  776. BidirectionalIterator2 buffer_end = boost::move(middle, last, buffer);
  777. boost::move_backward(first, middle, last);
  778. return boost::move(buffer, buffer_end, first);
  779. }
  780. else
  781. return first;
  782. }
  783. else if (len1 <= buffer_size)
  784. {
  785. if(len1) //Protect against self-move ranges
  786. {
  787. BidirectionalIterator2 buffer_end = boost::move(first, middle, buffer);
  788. BidirectionalIterator1 ret = boost::move(middle, last, first);
  789. boost::move(buffer, buffer_end, ret);
  790. return ret;
  791. }
  792. else
  793. return last;
  794. }
  795. else
  796. return rotate_gcd(first, middle, last);
  797. }
  798. template<typename BidirectionalIterator,
  799. typename Pointer, typename Compare>
  800. void merge_adaptive_ONlogN_recursive
  801. (BidirectionalIterator first,
  802. BidirectionalIterator middle,
  803. BidirectionalIterator last,
  804. typename iterator_traits<BidirectionalIterator>::size_type len1,
  805. typename iterator_traits<BidirectionalIterator>::size_type len2,
  806. Pointer buffer,
  807. typename iterator_traits<BidirectionalIterator>::size_type buffer_size,
  808. Compare comp)
  809. {
  810. typedef typename iterator_traits<BidirectionalIterator>::size_type size_type;
  811. //trivial cases
  812. if (!len2 || !len1) {
  813. return;
  814. }
  815. else if (len1 <= buffer_size || len2 <= buffer_size)
  816. {
  817. range_xbuf<Pointer, size_type, move_op> rxbuf(buffer, buffer + buffer_size);
  818. buffered_merge(first, middle, last, comp, rxbuf);
  819. }
  820. else if (size_type(len1 + len2) == 2u) {
  821. if (comp(*middle, *first))
  822. adl_move_swap(*first, *middle);
  823. return;
  824. }
  825. else if (size_type(len1 + len2) < MergeBufferlessONLogNRotationThreshold) {
  826. merge_bufferless_ON2(first, middle, last, comp);
  827. return;
  828. }
  829. BidirectionalIterator first_cut = first;
  830. BidirectionalIterator second_cut = middle;
  831. size_type len11 = 0;
  832. size_type len22 = 0;
  833. if (len1 > len2) //(len1 < len2)
  834. {
  835. len11 = len1 / 2;
  836. first_cut += len11;
  837. second_cut = boost::movelib::lower_bound(middle, last, *first_cut, comp);
  838. len22 = second_cut - middle;
  839. }
  840. else
  841. {
  842. len22 = len2 / 2;
  843. second_cut += len22;
  844. first_cut = boost::movelib::upper_bound(first, middle, *second_cut, comp);
  845. len11 = first_cut - first;
  846. }
  847. BidirectionalIterator new_middle
  848. = rotate_adaptive(first_cut, middle, second_cut,
  849. size_type(len1 - len11), len22, buffer,
  850. buffer_size);
  851. merge_adaptive_ONlogN_recursive(first, first_cut, new_middle, len11,
  852. len22, buffer, buffer_size, comp);
  853. merge_adaptive_ONlogN_recursive(new_middle, second_cut, last,
  854. len1 - len11, len2 - len22, buffer, buffer_size, comp);
  855. }
  856. template<typename BidirectionalIterator, typename Compare, typename RandRawIt>
  857. void merge_adaptive_ONlogN(BidirectionalIterator first,
  858. BidirectionalIterator middle,
  859. BidirectionalIterator last,
  860. Compare comp,
  861. RandRawIt uninitialized,
  862. typename iterator_traits<BidirectionalIterator>::size_type uninitialized_len)
  863. {
  864. typedef typename iterator_traits<BidirectionalIterator>::value_type value_type;
  865. typedef typename iterator_traits<BidirectionalIterator>::size_type size_type;
  866. if (first == middle || middle == last)
  867. return;
  868. if(uninitialized_len)
  869. {
  870. const size_type len1 = size_type(middle - first);
  871. const size_type len2 = size_type(last - middle);
  872. ::boost::movelib::adaptive_xbuf<value_type, RandRawIt> xbuf(uninitialized, uninitialized_len);
  873. xbuf.initialize_until(uninitialized_len, *first);
  874. merge_adaptive_ONlogN_recursive(first, middle, last, len1, len2, xbuf.begin(), uninitialized_len, comp);
  875. }
  876. else
  877. {
  878. merge_bufferless(first, middle, last, comp);
  879. }
  880. }
  881. } //namespace movelib {
  882. } //namespace boost {
  883. #endif //#define BOOST_MOVE_MERGE_HPP