1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___ALGORITHM_INPLACE_MERGE_H 10 #define _LIBCPP___ALGORITHM_INPLACE_MERGE_H 11 12 #include <__algorithm/comp.h> 13 #include <__algorithm/comp_ref_type.h> 14 #include <__algorithm/iterator_operations.h> 15 #include <__algorithm/lower_bound.h> 16 #include <__algorithm/min.h> 17 #include <__algorithm/move.h> 18 #include <__algorithm/rotate.h> 19 #include <__algorithm/upper_bound.h> 20 #include <__config> 21 #include <__functional/identity.h> 22 #include <__iterator/advance.h> 23 #include <__iterator/distance.h> 24 #include <__iterator/iterator_traits.h> 25 #include <__iterator/reverse_iterator.h> 26 #include <memory> 27 28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 29 # pragma GCC system_header 30 #endif 31 32 _LIBCPP_PUSH_MACROS 33 #include <__undef_macros> 34 35 _LIBCPP_BEGIN_NAMESPACE_STD 36 37 template <class _Predicate> 38 class __invert // invert the sense of a comparison 39 { 40 private: 41 _Predicate __p_; 42 public: 43 _LIBCPP_INLINE_VISIBILITY __invert() {} 44 45 _LIBCPP_INLINE_VISIBILITY 46 explicit __invert(_Predicate __p) : __p_(__p) {} 47 48 template <class _T1> 49 _LIBCPP_INLINE_VISIBILITY 50 bool operator()(const _T1& __x) {return !__p_(__x);} 51 52 template <class _T1, class _T2> 53 _LIBCPP_INLINE_VISIBILITY 54 bool operator()(const _T1& __x, const _T2& __y) {return __p_(__y, __x);} 55 }; 56 57 template <class _AlgPolicy, class _Compare, class _InputIterator1, class _Sent1, 58 class _InputIterator2, class _Sent2, class _OutputIterator> 59 void __half_inplace_merge(_InputIterator1 __first1, _Sent1 __last1, 60 _InputIterator2 __first2, _Sent2 __last2, 61 _OutputIterator __result, _Compare&& __comp) 62 { 63 for (; __first1 != __last1; ++__result) 64 { 65 if (__first2 == __last2) 66 { 67 std::__move<_AlgPolicy>(__first1, __last1, __result); 68 return; 69 } 70 71 if (__comp(*__first2, *__first1)) 72 { 73 *__result = _IterOps<_AlgPolicy>::__iter_move(__first2); 74 ++__first2; 75 } 76 else 77 { 78 *__result = _IterOps<_AlgPolicy>::__iter_move(__first1); 79 ++__first1; 80 } 81 } 82 // __first2 through __last2 are already in the right spot. 83 } 84 85 template <class _AlgPolicy, class _Compare, class _BidirectionalIterator> 86 void __buffered_inplace_merge( 87 _BidirectionalIterator __first, 88 _BidirectionalIterator __middle, 89 _BidirectionalIterator __last, 90 _Compare&& __comp, 91 typename iterator_traits<_BidirectionalIterator>::difference_type __len1, 92 typename iterator_traits<_BidirectionalIterator>::difference_type __len2, 93 typename iterator_traits<_BidirectionalIterator>::value_type* __buff) { 94 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 95 __destruct_n __d(0); 96 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d); 97 if (__len1 <= __len2) 98 { 99 value_type* __p = __buff; 100 for (_BidirectionalIterator __i = __first; __i != __middle; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p) 101 ::new ((void*)__p) value_type(_IterOps<_AlgPolicy>::__iter_move(__i)); 102 std::__half_inplace_merge<_AlgPolicy>(__buff, __p, __middle, __last, __first, __comp); 103 } 104 else 105 { 106 value_type* __p = __buff; 107 for (_BidirectionalIterator __i = __middle; __i != __last; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p) 108 ::new ((void*)__p) value_type(_IterOps<_AlgPolicy>::__iter_move(__i)); 109 typedef __unconstrained_reverse_iterator<_BidirectionalIterator> _RBi; 110 typedef __unconstrained_reverse_iterator<value_type*> _Rv; 111 typedef __invert<_Compare> _Inverted; 112 std::__half_inplace_merge<_AlgPolicy>(_Rv(__p), _Rv(__buff), 113 _RBi(__middle), _RBi(__first), 114 _RBi(__last), _Inverted(__comp)); 115 } 116 } 117 118 template <class _AlgPolicy, class _Compare, class _BidirectionalIterator> 119 void __inplace_merge( 120 _BidirectionalIterator __first, 121 _BidirectionalIterator __middle, 122 _BidirectionalIterator __last, 123 _Compare&& __comp, 124 typename iterator_traits<_BidirectionalIterator>::difference_type __len1, 125 typename iterator_traits<_BidirectionalIterator>::difference_type __len2, 126 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, 127 ptrdiff_t __buff_size) { 128 using _Ops = _IterOps<_AlgPolicy>; 129 130 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 131 while (true) 132 { 133 // if __middle == __last, we're done 134 if (__len2 == 0) 135 return; 136 if (__len1 <= __buff_size || __len2 <= __buff_size) 137 return std::__buffered_inplace_merge<_AlgPolicy> 138 (__first, __middle, __last, __comp, __len1, __len2, __buff); 139 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0 140 for (; true; ++__first, (void) --__len1) 141 { 142 if (__len1 == 0) 143 return; 144 if (__comp(*__middle, *__first)) 145 break; 146 } 147 // __first < __middle < __last 148 // *__first > *__middle 149 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that 150 // all elements in: 151 // [__first, __m1) <= [__middle, __m2) 152 // [__middle, __m2) < [__m1, __middle) 153 // [__m1, __middle) <= [__m2, __last) 154 // and __m1 or __m2 is in the middle of its range 155 _BidirectionalIterator __m1; // "median" of [__first, __middle) 156 _BidirectionalIterator __m2; // "median" of [__middle, __last) 157 difference_type __len11; // distance(__first, __m1) 158 difference_type __len21; // distance(__middle, __m2) 159 // binary search smaller range 160 if (__len1 < __len2) 161 { // __len >= 1, __len2 >= 2 162 __len21 = __len2 / 2; 163 __m2 = __middle; 164 _Ops::advance(__m2, __len21); 165 __m1 = std::__upper_bound<_AlgPolicy>(__first, __middle, *__m2, __comp, std::__identity()); 166 __len11 = _Ops::distance(__first, __m1); 167 } 168 else 169 { 170 if (__len1 == 1) 171 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1 172 // It is known *__first > *__middle 173 _Ops::iter_swap(__first, __middle); 174 return; 175 } 176 // __len1 >= 2, __len2 >= 1 177 __len11 = __len1 / 2; 178 __m1 = __first; 179 _Ops::advance(__m1, __len11); 180 __m2 = std::lower_bound(__middle, __last, *__m1, __comp); 181 __len21 = _Ops::distance(__middle, __m2); 182 } 183 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle) 184 difference_type __len22 = __len2 - __len21; // distance(__m2, __last) 185 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) 186 // swap middle two partitions 187 __middle = std::__rotate<_AlgPolicy>(__m1, __middle, __m2).first; 188 // __len12 and __len21 now have swapped meanings 189 // merge smaller range with recursive call and larger with tail recursion elimination 190 if (__len11 + __len21 < __len12 + __len22) 191 { 192 std::__inplace_merge<_AlgPolicy>( 193 __first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size); 194 __first = __middle; 195 __middle = __m2; 196 __len1 = __len12; 197 __len2 = __len22; 198 } 199 else 200 { 201 std::__inplace_merge<_AlgPolicy>( 202 __middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size); 203 __last = __middle; 204 __middle = __m1; 205 __len1 = __len11; 206 __len2 = __len21; 207 } 208 } 209 } 210 211 template <class _AlgPolicy, class _BidirectionalIterator, class _Compare> 212 _LIBCPP_HIDE_FROM_ABI 213 void 214 __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 215 _Compare&& __comp) 216 { 217 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 218 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 219 difference_type __len1 = _IterOps<_AlgPolicy>::distance(__first, __middle); 220 difference_type __len2 = _IterOps<_AlgPolicy>::distance(__middle, __last); 221 difference_type __buf_size = _VSTD::min(__len1, __len2); 222 // TODO: Remove the use of std::get_temporary_buffer 223 _LIBCPP_SUPPRESS_DEPRECATED_PUSH 224 pair<value_type*, ptrdiff_t> __buf = _VSTD::get_temporary_buffer<value_type>(__buf_size); 225 _LIBCPP_SUPPRESS_DEPRECATED_POP 226 unique_ptr<value_type, __return_temporary_buffer> __h(__buf.first); 227 return std::__inplace_merge<_AlgPolicy>( 228 std::move(__first), std::move(__middle), std::move(__last), __comp, __len1, __len2, __buf.first, __buf.second); 229 } 230 231 template <class _BidirectionalIterator, class _Compare> 232 inline _LIBCPP_HIDE_FROM_ABI void inplace_merge( 233 _BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, _Compare __comp) { 234 typedef typename __comp_ref_type<_Compare>::type _Comp_ref; 235 std::__inplace_merge<_ClassicAlgPolicy>( 236 std::move(__first), std::move(__middle), std::move(__last), static_cast<_Comp_ref>(__comp)); 237 } 238 239 template <class _BidirectionalIterator> 240 inline _LIBCPP_HIDE_FROM_ABI 241 void 242 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last) 243 { 244 std::inplace_merge(std::move(__first), std::move(__middle), std::move(__last), 245 __less<typename iterator_traits<_BidirectionalIterator>::value_type>()); 246 } 247 248 _LIBCPP_END_NAMESPACE_STD 249 250 _LIBCPP_POP_MACROS 251 252 #endif // _LIBCPP___ALGORITHM_INPLACE_MERGE_H 253