1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric 9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_INPLACE_MERGE_H 10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_INPLACE_MERGE_H 11fe6060f1SDimitry Andric 12fe6060f1SDimitry Andric #include <__algorithm/comp.h> 1304eeddc0SDimitry Andric #include <__algorithm/comp_ref_type.h> 14fcaf7f86SDimitry Andric #include <__algorithm/iterator_operations.h> 15fe6060f1SDimitry Andric #include <__algorithm/lower_bound.h> 16fe6060f1SDimitry Andric #include <__algorithm/min.h> 17fe6060f1SDimitry Andric #include <__algorithm/move.h> 18fe6060f1SDimitry Andric #include <__algorithm/rotate.h> 19fe6060f1SDimitry Andric #include <__algorithm/upper_bound.h> 2004eeddc0SDimitry Andric #include <__config> 21fcaf7f86SDimitry Andric #include <__functional/identity.h> 2281ad6265SDimitry Andric #include <__iterator/advance.h> 2381ad6265SDimitry Andric #include <__iterator/distance.h> 24fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h> 2581ad6265SDimitry Andric #include <__iterator/reverse_iterator.h> 26bdd1243dSDimitry Andric #include <__memory/destruct_n.h> 27bdd1243dSDimitry Andric #include <__memory/temporary_buffer.h> 28bdd1243dSDimitry Andric #include <__memory/unique_ptr.h> 29bdd1243dSDimitry Andric #include <__utility/pair.h> 30bdd1243dSDimitry Andric #include <new> 31fe6060f1SDimitry Andric 32fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 33fe6060f1SDimitry Andric # pragma GCC system_header 34fe6060f1SDimitry Andric #endif 35fe6060f1SDimitry Andric 36fe6060f1SDimitry Andric _LIBCPP_PUSH_MACROS 37fe6060f1SDimitry Andric #include <__undef_macros> 38fe6060f1SDimitry Andric 39fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 40fe6060f1SDimitry Andric 41fe6060f1SDimitry Andric template <class _Predicate> 42fe6060f1SDimitry Andric class __invert // invert the sense of a comparison 43fe6060f1SDimitry Andric { 44fe6060f1SDimitry Andric private: 45fe6060f1SDimitry Andric _Predicate __p_; 46cb14a3feSDimitry Andric 47fe6060f1SDimitry Andric public: 485f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI __invert() {} 49fe6060f1SDimitry Andric 50cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __invert(_Predicate __p) : __p_(__p) {} 51fe6060f1SDimitry Andric 52fe6060f1SDimitry Andric template <class _T1> 53cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool operator()(const _T1& __x) { 54cb14a3feSDimitry Andric return !__p_(__x); 55cb14a3feSDimitry Andric } 56fe6060f1SDimitry Andric 57fe6060f1SDimitry Andric template <class _T1, class _T2> 58cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI bool operator()(const _T1& __x, const _T2& __y) { 59cb14a3feSDimitry Andric return __p_(__y, __x); 60cb14a3feSDimitry Andric } 61fe6060f1SDimitry Andric }; 62fe6060f1SDimitry Andric 63cb14a3feSDimitry Andric template <class _AlgPolicy, 64cb14a3feSDimitry Andric class _Compare, 65cb14a3feSDimitry Andric class _InputIterator1, 66cb14a3feSDimitry Andric class _Sent1, 67cb14a3feSDimitry Andric class _InputIterator2, 68cb14a3feSDimitry Andric class _Sent2, 69cb14a3feSDimitry Andric class _OutputIterator> 70cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __half_inplace_merge( 71cb14a3feSDimitry Andric _InputIterator1 __first1, 72cb14a3feSDimitry Andric _Sent1 __last1, 73cb14a3feSDimitry Andric _InputIterator2 __first2, 74cb14a3feSDimitry Andric _Sent2 __last2, 75cb14a3feSDimitry Andric _OutputIterator __result, 76cb14a3feSDimitry Andric _Compare&& __comp) { 77cb14a3feSDimitry Andric for (; __first1 != __last1; ++__result) { 78cb14a3feSDimitry Andric if (__first2 == __last2) { 7961cfbce3SDimitry Andric std::__move<_AlgPolicy>(__first1, __last1, __result); 80fe6060f1SDimitry Andric return; 81fe6060f1SDimitry Andric } 82fe6060f1SDimitry Andric 83cb14a3feSDimitry Andric if (__comp(*__first2, *__first1)) { 84fcaf7f86SDimitry Andric *__result = _IterOps<_AlgPolicy>::__iter_move(__first2); 85fe6060f1SDimitry Andric ++__first2; 86cb14a3feSDimitry Andric } else { 87fcaf7f86SDimitry Andric *__result = _IterOps<_AlgPolicy>::__iter_move(__first1); 88fe6060f1SDimitry Andric ++__first1; 89fe6060f1SDimitry Andric } 90fe6060f1SDimitry Andric } 91fe6060f1SDimitry Andric // __first2 through __last2 are already in the right spot. 92fe6060f1SDimitry Andric } 93fe6060f1SDimitry Andric 94fcaf7f86SDimitry Andric template <class _AlgPolicy, class _Compare, class _BidirectionalIterator> 95cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __buffered_inplace_merge( 9661cfbce3SDimitry Andric _BidirectionalIterator __first, 9761cfbce3SDimitry Andric _BidirectionalIterator __middle, 9861cfbce3SDimitry Andric _BidirectionalIterator __last, 9961cfbce3SDimitry Andric _Compare&& __comp, 10061cfbce3SDimitry Andric typename iterator_traits<_BidirectionalIterator>::difference_type __len1, 101fe6060f1SDimitry Andric typename iterator_traits<_BidirectionalIterator>::difference_type __len2, 10261cfbce3SDimitry Andric typename iterator_traits<_BidirectionalIterator>::value_type* __buff) { 103fe6060f1SDimitry Andric typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 104fe6060f1SDimitry Andric __destruct_n __d(0); 105fe6060f1SDimitry Andric unique_ptr<value_type, __destruct_n&> __h2(__buff, __d); 106cb14a3feSDimitry Andric if (__len1 <= __len2) { 107fe6060f1SDimitry Andric value_type* __p = __buff; 108cb14a3feSDimitry Andric for (_BidirectionalIterator __i = __first; __i != __middle; 109cb14a3feSDimitry Andric __d.template __incr<value_type>(), (void)++__i, (void)++__p) 110fcaf7f86SDimitry Andric ::new ((void*)__p) value_type(_IterOps<_AlgPolicy>::__iter_move(__i)); 11161cfbce3SDimitry Andric std::__half_inplace_merge<_AlgPolicy>(__buff, __p, __middle, __last, __first, __comp); 112cb14a3feSDimitry Andric } else { 113fe6060f1SDimitry Andric value_type* __p = __buff; 114cb14a3feSDimitry Andric for (_BidirectionalIterator __i = __middle; __i != __last; 115cb14a3feSDimitry Andric __d.template __incr<value_type>(), (void)++__i, (void)++__p) 116fcaf7f86SDimitry Andric ::new ((void*)__p) value_type(_IterOps<_AlgPolicy>::__iter_move(__i)); 117*0fca6ea1SDimitry Andric typedef reverse_iterator<_BidirectionalIterator> _RBi; 118*0fca6ea1SDimitry Andric typedef reverse_iterator<value_type*> _Rv; 119fe6060f1SDimitry Andric typedef __invert<_Compare> _Inverted; 120cb14a3feSDimitry Andric std::__half_inplace_merge<_AlgPolicy>( 121cb14a3feSDimitry Andric _Rv(__p), _Rv(__buff), _RBi(__middle), _RBi(__first), _RBi(__last), _Inverted(__comp)); 122fe6060f1SDimitry Andric } 123fe6060f1SDimitry Andric } 124fe6060f1SDimitry Andric 125fcaf7f86SDimitry Andric template <class _AlgPolicy, class _Compare, class _BidirectionalIterator> 12661cfbce3SDimitry Andric void __inplace_merge( 12761cfbce3SDimitry Andric _BidirectionalIterator __first, 12861cfbce3SDimitry Andric _BidirectionalIterator __middle, 12961cfbce3SDimitry Andric _BidirectionalIterator __last, 13061cfbce3SDimitry Andric _Compare&& __comp, 13161cfbce3SDimitry Andric typename iterator_traits<_BidirectionalIterator>::difference_type __len1, 132fe6060f1SDimitry Andric typename iterator_traits<_BidirectionalIterator>::difference_type __len2, 13361cfbce3SDimitry Andric typename iterator_traits<_BidirectionalIterator>::value_type* __buff, 13461cfbce3SDimitry Andric ptrdiff_t __buff_size) { 135fcaf7f86SDimitry Andric using _Ops = _IterOps<_AlgPolicy>; 136fcaf7f86SDimitry Andric 137fe6060f1SDimitry Andric typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 138cb14a3feSDimitry Andric while (true) { 139fe6060f1SDimitry Andric // if __middle == __last, we're done 140fe6060f1SDimitry Andric if (__len2 == 0) 141fe6060f1SDimitry Andric return; 142fe6060f1SDimitry Andric if (__len1 <= __buff_size || __len2 <= __buff_size) 143cb14a3feSDimitry Andric return std::__buffered_inplace_merge<_AlgPolicy>(__first, __middle, __last, __comp, __len1, __len2, __buff); 144fe6060f1SDimitry Andric // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0 145cb14a3feSDimitry Andric for (; true; ++__first, (void)--__len1) { 146fe6060f1SDimitry Andric if (__len1 == 0) 147fe6060f1SDimitry Andric return; 148fe6060f1SDimitry Andric if (__comp(*__middle, *__first)) 149fe6060f1SDimitry Andric break; 150fe6060f1SDimitry Andric } 151fe6060f1SDimitry Andric // __first < __middle < __last 152fe6060f1SDimitry Andric // *__first > *__middle 153fe6060f1SDimitry Andric // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that 154fe6060f1SDimitry Andric // all elements in: 155fe6060f1SDimitry Andric // [__first, __m1) <= [__middle, __m2) 156fe6060f1SDimitry Andric // [__middle, __m2) < [__m1, __middle) 157fe6060f1SDimitry Andric // [__m1, __middle) <= [__m2, __last) 158fe6060f1SDimitry Andric // and __m1 or __m2 is in the middle of its range 159fe6060f1SDimitry Andric _BidirectionalIterator __m1; // "median" of [__first, __middle) 160fe6060f1SDimitry Andric _BidirectionalIterator __m2; // "median" of [__middle, __last) 161fe6060f1SDimitry Andric difference_type __len11; // distance(__first, __m1) 162fe6060f1SDimitry Andric difference_type __len21; // distance(__middle, __m2) 163fe6060f1SDimitry Andric // binary search smaller range 164cb14a3feSDimitry Andric if (__len1 < __len2) { // __len >= 1, __len2 >= 2 165fe6060f1SDimitry Andric __len21 = __len2 / 2; 166fe6060f1SDimitry Andric __m2 = __middle; 167fcaf7f86SDimitry Andric _Ops::advance(__m2, __len21); 16861cfbce3SDimitry Andric __m1 = std::__upper_bound<_AlgPolicy>(__first, __middle, *__m2, __comp, std::__identity()); 169fcaf7f86SDimitry Andric __len11 = _Ops::distance(__first, __m1); 170cb14a3feSDimitry Andric } else { 171cb14a3feSDimitry Andric if (__len1 == 1) { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1 172fe6060f1SDimitry Andric // It is known *__first > *__middle 173fcaf7f86SDimitry Andric _Ops::iter_swap(__first, __middle); 174fe6060f1SDimitry Andric return; 175fe6060f1SDimitry Andric } 176fe6060f1SDimitry Andric // __len1 >= 2, __len2 >= 1 177fe6060f1SDimitry Andric __len11 = __len1 / 2; 178fe6060f1SDimitry Andric __m1 = __first; 179fcaf7f86SDimitry Andric _Ops::advance(__m1, __len11); 18081ad6265SDimitry Andric __m2 = std::lower_bound(__middle, __last, *__m1, __comp); 181fcaf7f86SDimitry Andric __len21 = _Ops::distance(__middle, __m2); 182fe6060f1SDimitry Andric } 183fe6060f1SDimitry Andric difference_type __len12 = __len1 - __len11; // distance(__m1, __middle) 184fe6060f1SDimitry Andric difference_type __len22 = __len2 - __len21; // distance(__m2, __last) 185fe6060f1SDimitry Andric // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) 186fe6060f1SDimitry Andric // swap middle two partitions 18761cfbce3SDimitry Andric __middle = std::__rotate<_AlgPolicy>(__m1, __middle, __m2).first; 188fe6060f1SDimitry Andric // __len12 and __len21 now have swapped meanings 189fe6060f1SDimitry Andric // merge smaller range with recursive call and larger with tail recursion elimination 190cb14a3feSDimitry Andric if (__len11 + __len21 < __len12 + __len22) { 191cb14a3feSDimitry Andric std::__inplace_merge<_AlgPolicy>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size); 192fe6060f1SDimitry Andric __first = __middle; 193fe6060f1SDimitry Andric __middle = __m2; 194fe6060f1SDimitry Andric __len1 = __len12; 195fe6060f1SDimitry Andric __len2 = __len22; 196cb14a3feSDimitry Andric } else { 197cb14a3feSDimitry Andric std::__inplace_merge<_AlgPolicy>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size); 198fe6060f1SDimitry Andric __last = __middle; 199fe6060f1SDimitry Andric __middle = __m1; 200fe6060f1SDimitry Andric __len1 = __len11; 201fe6060f1SDimitry Andric __len2 = __len21; 202fe6060f1SDimitry Andric } 203fe6060f1SDimitry Andric } 204fe6060f1SDimitry Andric } 205fe6060f1SDimitry Andric 20661cfbce3SDimitry Andric template <class _AlgPolicy, class _BidirectionalIterator, class _Compare> 207cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __inplace_merge( 208cb14a3feSDimitry Andric _BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, _Compare&& __comp) { 209fe6060f1SDimitry Andric typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 210fe6060f1SDimitry Andric typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 21161cfbce3SDimitry Andric difference_type __len1 = _IterOps<_AlgPolicy>::distance(__first, __middle); 21261cfbce3SDimitry Andric difference_type __len2 = _IterOps<_AlgPolicy>::distance(__middle, __last); 2135f757f3fSDimitry Andric difference_type __buf_size = std::min(__len1, __len2); 21481ad6265SDimitry Andric // TODO: Remove the use of std::get_temporary_buffer 21581ad6265SDimitry Andric _LIBCPP_SUPPRESS_DEPRECATED_PUSH 2165f757f3fSDimitry Andric pair<value_type*, ptrdiff_t> __buf = std::get_temporary_buffer<value_type>(__buf_size); 21781ad6265SDimitry Andric _LIBCPP_SUPPRESS_DEPRECATED_POP 218fe6060f1SDimitry Andric unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first); 21961cfbce3SDimitry Andric return std::__inplace_merge<_AlgPolicy>( 22061cfbce3SDimitry Andric std::move(__first), std::move(__middle), std::move(__last), __comp, __len1, __len2, __buf.first, __buf.second); 22161cfbce3SDimitry Andric } 22261cfbce3SDimitry Andric 22361cfbce3SDimitry Andric template <class _BidirectionalIterator, class _Compare> 22461cfbce3SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void inplace_merge( 22561cfbce3SDimitry Andric _BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, _Compare __comp) { 22661cfbce3SDimitry Andric std::__inplace_merge<_ClassicAlgPolicy>( 227bdd1243dSDimitry Andric std::move(__first), std::move(__middle), std::move(__last), static_cast<__comp_ref_type<_Compare> >(__comp)); 228fe6060f1SDimitry Andric } 229fe6060f1SDimitry Andric 230fe6060f1SDimitry Andric template <class _BidirectionalIterator> 231cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void 232cb14a3feSDimitry Andric inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last) { 23306c3fb27SDimitry Andric std::inplace_merge(std::move(__first), std::move(__middle), std::move(__last), __less<>()); 234fe6060f1SDimitry Andric } 235fe6060f1SDimitry Andric 236fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD 237fe6060f1SDimitry Andric 238fe6060f1SDimitry Andric _LIBCPP_POP_MACROS 239fe6060f1SDimitry Andric 240fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_INPLACE_MERGE_H 241