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