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 _InputIterator2, 58 class _OutputIterator> 59 void __half_inplace_merge(_InputIterator1 __first1, _InputIterator1 __last1, 60 _InputIterator2 __first2, _InputIterator2 __last2, 61 _OutputIterator __result, _Compare __comp) 62 { 63 for (; __first1 != __last1; ++__result) 64 { 65 if (__first2 == __last2) 66 { 67 // TODO(alg-policy): pass `_AlgPolicy` once it's supported by `move`. 68 _VSTD::move(__first1, __last1, __result); 69 return; 70 } 71 72 if (__comp(*__first2, *__first1)) 73 { 74 *__result = _IterOps<_AlgPolicy>::__iter_move(__first2); 75 ++__first2; 76 } 77 else 78 { 79 *__result = _IterOps<_AlgPolicy>::__iter_move(__first1); 80 ++__first1; 81 } 82 } 83 // __first2 through __last2 are already in the right spot. 84 } 85 86 template <class _AlgPolicy, class _Compare, class _BidirectionalIterator> 87 void 88 __buffered_inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 89 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1, 90 typename iterator_traits<_BidirectionalIterator>::difference_type __len2, 91 typename iterator_traits<_BidirectionalIterator>::value_type* __buff) 92 { 93 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type; 94 __destruct_n __d(0); 95 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d); 96 if (__len1 <= __len2) 97 { 98 value_type* __p = __buff; 99 for (_BidirectionalIterator __i = __first; __i != __middle; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p) 100 ::new ((void*)__p) value_type(_IterOps<_AlgPolicy>::__iter_move(__i)); 101 std::__half_inplace_merge<_AlgPolicy, _Compare>(__buff, __p, __middle, __last, __first, __comp); 102 } 103 else 104 { 105 value_type* __p = __buff; 106 for (_BidirectionalIterator __i = __middle; __i != __last; __d.template __incr<value_type>(), (void) ++__i, (void) ++__p) 107 ::new ((void*)__p) value_type(_IterOps<_AlgPolicy>::__iter_move(__i)); 108 typedef reverse_iterator<_BidirectionalIterator> _RBi; 109 typedef reverse_iterator<value_type*> _Rv; 110 typedef __invert<_Compare> _Inverted; 111 std::__half_inplace_merge<_AlgPolicy, _Inverted>(_Rv(__p), _Rv(__buff), 112 _RBi(__middle), _RBi(__first), 113 _RBi(__last), _Inverted(__comp)); 114 } 115 } 116 117 template <class _AlgPolicy, class _Compare, class _BidirectionalIterator> 118 void 119 __inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last, 120 _Compare __comp, typename iterator_traits<_BidirectionalIterator>::difference_type __len1, 121 typename iterator_traits<_BidirectionalIterator>::difference_type __len2, 122 typename iterator_traits<_BidirectionalIterator>::value_type* __buff, ptrdiff_t __buff_size) 123 { 124 using _Ops = _IterOps<_AlgPolicy>; 125 126 typedef typename iterator_traits<_BidirectionalIterator>::difference_type difference_type; 127 while (true) 128 { 129 // if __middle == __last, we're done 130 if (__len2 == 0) 131 return; 132 if (__len1 <= __buff_size || __len2 <= __buff_size) 133 return std::__buffered_inplace_merge<_AlgPolicy, _Compare> 134 (__first, __middle, __last, __comp, __len1, __len2, __buff); 135 // shrink [__first, __middle) as much as possible (with no moves), returning if it shrinks to 0 136 for (; true; ++__first, (void) --__len1) 137 { 138 if (__len1 == 0) 139 return; 140 if (__comp(*__middle, *__first)) 141 break; 142 } 143 // __first < __middle < __last 144 // *__first > *__middle 145 // partition [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) such that 146 // all elements in: 147 // [__first, __m1) <= [__middle, __m2) 148 // [__middle, __m2) < [__m1, __middle) 149 // [__m1, __middle) <= [__m2, __last) 150 // and __m1 or __m2 is in the middle of its range 151 _BidirectionalIterator __m1; // "median" of [__first, __middle) 152 _BidirectionalIterator __m2; // "median" of [__middle, __last) 153 difference_type __len11; // distance(__first, __m1) 154 difference_type __len21; // distance(__middle, __m2) 155 // binary search smaller range 156 if (__len1 < __len2) 157 { // __len >= 1, __len2 >= 2 158 __len21 = __len2 / 2; 159 __m2 = __middle; 160 _Ops::advance(__m2, __len21); 161 // TODO: replace _ClassicAlgPolicy and __identity with _AlgPolicy and projection 162 __m1 = std::__upper_bound<_ClassicAlgPolicy>(__first, __middle, *__m2, __comp, std::__identity()); 163 __len11 = _Ops::distance(__first, __m1); 164 } 165 else 166 { 167 if (__len1 == 1) 168 { // __len1 >= __len2 && __len2 > 0, therefore __len2 == 1 169 // It is known *__first > *__middle 170 _Ops::iter_swap(__first, __middle); 171 return; 172 } 173 // __len1 >= 2, __len2 >= 1 174 __len11 = __len1 / 2; 175 __m1 = __first; 176 _Ops::advance(__m1, __len11); 177 __m2 = std::lower_bound(__middle, __last, *__m1, __comp); 178 __len21 = _Ops::distance(__middle, __m2); 179 } 180 difference_type __len12 = __len1 - __len11; // distance(__m1, __middle) 181 difference_type __len22 = __len2 - __len21; // distance(__m2, __last) 182 // [__first, __m1) [__m1, __middle) [__middle, __m2) [__m2, __last) 183 // swap middle two partitions 184 // TODO(alg-policy): pass `_AlgPolicy` once it's supported by `rotate`. 185 __middle = _VSTD::rotate(__m1, __middle, __m2); 186 // __len12 and __len21 now have swapped meanings 187 // merge smaller range with recursive call and larger with tail recursion elimination 188 if (__len11 + __len21 < __len12 + __len22) 189 { 190 std::__inplace_merge<_AlgPolicy, _Compare>( 191 __first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size); 192 // _VSTD::__inplace_merge<_Compare>(__middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size); 193 __first = __middle; 194 __middle = __m2; 195 __len1 = __len12; 196 __len2 = __len22; 197 } 198 else 199 { 200 std::__inplace_merge<_AlgPolicy, _Compare>( 201 __middle, __m2, __last, __comp, __len12, __len22, __buff, __buff_size); 202 // _VSTD::__inplace_merge<_Compare>(__first, __m1, __middle, __comp, __len11, __len21, __buff, __buff_size); 203 __last = __middle; 204 __middle = __m1; 205 __len1 = __len11; 206 __len2 = __len21; 207 } 208 } 209 } 210 211 template <class _BidirectionalIterator, class _Compare> 212 inline _LIBCPP_INLINE_VISIBILITY 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 = _VSTD::distance(__first, __middle); 220 difference_type __len2 = _VSTD::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 typedef typename __comp_ref_type<_Compare>::type _Comp_ref; 228 return _VSTD::__inplace_merge<_ClassicAlgPolicy, _Comp_ref>(__first, __middle, __last, __comp, __len1, __len2, 229 __buf.first, __buf.second); 230 } 231 232 template <class _BidirectionalIterator> 233 inline _LIBCPP_INLINE_VISIBILITY 234 void 235 inplace_merge(_BidirectionalIterator __first, _BidirectionalIterator __middle, _BidirectionalIterator __last) 236 { 237 _VSTD::inplace_merge(__first, __middle, __last, 238 __less<typename iterator_traits<_BidirectionalIterator>::value_type>()); 239 } 240 241 _LIBCPP_END_NAMESPACE_STD 242 243 _LIBCPP_POP_MACROS 244 245 #endif // _LIBCPP___ALGORITHM_INPLACE_MERGE_H 246