[MemProf] Templatize CallStackRadixTreeBuilder (NFC) (#117014)
[llvm-project.git] / libcxx / include / __algorithm / stable_sort.h
blob43f591ac02b01df614a9db5ab02a337732b51e03
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_STABLE_SORT_H
10 #define _LIBCPP___ALGORITHM_STABLE_SORT_H
12 #include <__algorithm/comp.h>
13 #include <__algorithm/comp_ref_type.h>
14 #include <__algorithm/inplace_merge.h>
15 #include <__algorithm/iterator_operations.h>
16 #include <__algorithm/sort.h>
17 #include <__config>
18 #include <__cstddef/ptrdiff_t.h>
19 #include <__debug_utils/strict_weak_ordering_check.h>
20 #include <__iterator/iterator_traits.h>
21 #include <__memory/destruct_n.h>
22 #include <__memory/unique_ptr.h>
23 #include <__memory/unique_temporary_buffer.h>
24 #include <__type_traits/is_trivially_assignable.h>
25 #include <__utility/move.h>
26 #include <__utility/pair.h>
27 #include <new>
29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
30 # pragma GCC system_header
31 #endif
33 _LIBCPP_PUSH_MACROS
34 #include <__undef_macros>
36 _LIBCPP_BEGIN_NAMESPACE_STD
38 template <class _AlgPolicy, class _Compare, class _BidirectionalIterator>
39 _LIBCPP_HIDE_FROM_ABI void __insertion_sort_move(
40 _BidirectionalIterator __first1,
41 _BidirectionalIterator __last1,
42 typename iterator_traits<_BidirectionalIterator>::value_type* __first2,
43 _Compare __comp) {
44 using _Ops = _IterOps<_AlgPolicy>;
46 typedef typename iterator_traits<_BidirectionalIterator>::value_type value_type;
47 if (__first1 != __last1) {
48 __destruct_n __d(0);
49 unique_ptr<value_type, __destruct_n&> __h(__first2, __d);
50 value_type* __last2 = __first2;
51 ::new ((void*)__last2) value_type(_Ops::__iter_move(__first1));
52 __d.template __incr<value_type>();
53 for (++__last2; ++__first1 != __last1; ++__last2) {
54 value_type* __j2 = __last2;
55 value_type* __i2 = __j2;
56 if (__comp(*__first1, *--__i2)) {
57 ::new ((void*)__j2) value_type(std::move(*__i2));
58 __d.template __incr<value_type>();
59 for (--__j2; __i2 != __first2 && __comp(*__first1, *--__i2); --__j2)
60 *__j2 = std::move(*__i2);
61 *__j2 = _Ops::__iter_move(__first1);
62 } else {
63 ::new ((void*)__j2) value_type(_Ops::__iter_move(__first1));
64 __d.template __incr<value_type>();
67 __h.release();
71 template <class _AlgPolicy, class _Compare, class _InputIterator1, class _InputIterator2>
72 _LIBCPP_HIDE_FROM_ABI void __merge_move_construct(
73 _InputIterator1 __first1,
74 _InputIterator1 __last1,
75 _InputIterator2 __first2,
76 _InputIterator2 __last2,
77 typename iterator_traits<_InputIterator1>::value_type* __result,
78 _Compare __comp) {
79 using _Ops = _IterOps<_AlgPolicy>;
81 typedef typename iterator_traits<_InputIterator1>::value_type value_type;
82 __destruct_n __d(0);
83 unique_ptr<value_type, __destruct_n&> __h(__result, __d);
84 for (; true; ++__result) {
85 if (__first1 == __last1) {
86 for (; __first2 != __last2; ++__first2, (void)++__result, __d.template __incr<value_type>())
87 ::new ((void*)__result) value_type(_Ops::__iter_move(__first2));
88 __h.release();
89 return;
91 if (__first2 == __last2) {
92 for (; __first1 != __last1; ++__first1, (void)++__result, __d.template __incr<value_type>())
93 ::new ((void*)__result) value_type(_Ops::__iter_move(__first1));
94 __h.release();
95 return;
97 if (__comp(*__first2, *__first1)) {
98 ::new ((void*)__result) value_type(_Ops::__iter_move(__first2));
99 __d.template __incr<value_type>();
100 ++__first2;
101 } else {
102 ::new ((void*)__result) value_type(_Ops::__iter_move(__first1));
103 __d.template __incr<value_type>();
104 ++__first1;
109 template <class _AlgPolicy, class _Compare, class _InputIterator1, class _InputIterator2, class _OutputIterator>
110 _LIBCPP_HIDE_FROM_ABI void __merge_move_assign(
111 _InputIterator1 __first1,
112 _InputIterator1 __last1,
113 _InputIterator2 __first2,
114 _InputIterator2 __last2,
115 _OutputIterator __result,
116 _Compare __comp) {
117 using _Ops = _IterOps<_AlgPolicy>;
119 for (; __first1 != __last1; ++__result) {
120 if (__first2 == __last2) {
121 for (; __first1 != __last1; ++__first1, (void)++__result)
122 *__result = _Ops::__iter_move(__first1);
123 return;
125 if (__comp(*__first2, *__first1)) {
126 *__result = _Ops::__iter_move(__first2);
127 ++__first2;
128 } else {
129 *__result = _Ops::__iter_move(__first1);
130 ++__first1;
133 for (; __first2 != __last2; ++__first2, (void)++__result)
134 *__result = _Ops::__iter_move(__first2);
137 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
138 void __stable_sort(_RandomAccessIterator __first,
139 _RandomAccessIterator __last,
140 _Compare __comp,
141 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
142 typename iterator_traits<_RandomAccessIterator>::value_type* __buff,
143 ptrdiff_t __buff_size);
145 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
146 void __stable_sort_move(_RandomAccessIterator __first1,
147 _RandomAccessIterator __last1,
148 _Compare __comp,
149 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
150 typename iterator_traits<_RandomAccessIterator>::value_type* __first2) {
151 using _Ops = _IterOps<_AlgPolicy>;
153 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
154 switch (__len) {
155 case 0:
156 return;
157 case 1:
158 ::new ((void*)__first2) value_type(_Ops::__iter_move(__first1));
159 return;
160 case 2:
161 __destruct_n __d(0);
162 unique_ptr<value_type, __destruct_n&> __h2(__first2, __d);
163 if (__comp(*--__last1, *__first1)) {
164 ::new ((void*)__first2) value_type(_Ops::__iter_move(__last1));
165 __d.template __incr<value_type>();
166 ++__first2;
167 ::new ((void*)__first2) value_type(_Ops::__iter_move(__first1));
168 } else {
169 ::new ((void*)__first2) value_type(_Ops::__iter_move(__first1));
170 __d.template __incr<value_type>();
171 ++__first2;
172 ::new ((void*)__first2) value_type(_Ops::__iter_move(__last1));
174 __h2.release();
175 return;
177 if (__len <= 8) {
178 std::__insertion_sort_move<_AlgPolicy, _Compare>(__first1, __last1, __first2, __comp);
179 return;
181 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
182 _RandomAccessIterator __m = __first1 + __l2;
183 std::__stable_sort<_AlgPolicy, _Compare>(__first1, __m, __comp, __l2, __first2, __l2);
184 std::__stable_sort<_AlgPolicy, _Compare>(__m, __last1, __comp, __len - __l2, __first2 + __l2, __len - __l2);
185 std::__merge_move_construct<_AlgPolicy, _Compare>(__first1, __m, __m, __last1, __first2, __comp);
188 template <class _Tp>
189 struct __stable_sort_switch {
190 static const unsigned value = 128 * is_trivially_copy_assignable<_Tp>::value;
193 template <class _AlgPolicy, class _Compare, class _RandomAccessIterator>
194 void __stable_sort(_RandomAccessIterator __first,
195 _RandomAccessIterator __last,
196 _Compare __comp,
197 typename iterator_traits<_RandomAccessIterator>::difference_type __len,
198 typename iterator_traits<_RandomAccessIterator>::value_type* __buff,
199 ptrdiff_t __buff_size) {
200 typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
201 typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
202 switch (__len) {
203 case 0:
204 case 1:
205 return;
206 case 2:
207 if (__comp(*--__last, *__first))
208 _IterOps<_AlgPolicy>::iter_swap(__first, __last);
209 return;
211 if (__len <= static_cast<difference_type>(__stable_sort_switch<value_type>::value)) {
212 std::__insertion_sort<_AlgPolicy, _Compare>(__first, __last, __comp);
213 return;
215 typename iterator_traits<_RandomAccessIterator>::difference_type __l2 = __len / 2;
216 _RandomAccessIterator __m = __first + __l2;
217 if (__len <= __buff_size) {
218 __destruct_n __d(0);
219 unique_ptr<value_type, __destruct_n&> __h2(__buff, __d);
220 std::__stable_sort_move<_AlgPolicy, _Compare>(__first, __m, __comp, __l2, __buff);
221 __d.__set(__l2, (value_type*)nullptr);
222 std::__stable_sort_move<_AlgPolicy, _Compare>(__m, __last, __comp, __len - __l2, __buff + __l2);
223 __d.__set(__len, (value_type*)nullptr);
224 std::__merge_move_assign<_AlgPolicy, _Compare>(
225 __buff, __buff + __l2, __buff + __l2, __buff + __len, __first, __comp);
226 // std::__merge<_Compare>(move_iterator<value_type*>(__buff),
227 // move_iterator<value_type*>(__buff + __l2),
228 // move_iterator<_RandomAccessIterator>(__buff + __l2),
229 // move_iterator<_RandomAccessIterator>(__buff + __len),
230 // __first, __comp);
231 return;
233 std::__stable_sort<_AlgPolicy, _Compare>(__first, __m, __comp, __l2, __buff, __buff_size);
234 std::__stable_sort<_AlgPolicy, _Compare>(__m, __last, __comp, __len - __l2, __buff, __buff_size);
235 std::__inplace_merge<_AlgPolicy>(__first, __m, __last, __comp, __l2, __len - __l2, __buff, __buff_size);
238 template <class _AlgPolicy, class _RandomAccessIterator, class _Compare>
239 inline _LIBCPP_HIDE_FROM_ABI void
240 __stable_sort_impl(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare& __comp) {
241 using value_type = typename iterator_traits<_RandomAccessIterator>::value_type;
242 using difference_type = typename iterator_traits<_RandomAccessIterator>::difference_type;
244 difference_type __len = __last - __first;
245 __unique_temporary_buffer<value_type> __unique_buf;
246 pair<value_type*, ptrdiff_t> __buf(0, 0);
247 if (__len > static_cast<difference_type>(__stable_sort_switch<value_type>::value)) {
248 __unique_buf = std::__allocate_unique_temporary_buffer<value_type>(__len);
249 __buf.first = __unique_buf.get();
250 __buf.second = __unique_buf.get_deleter().__count_;
253 std::__stable_sort<_AlgPolicy, __comp_ref_type<_Compare> >(__first, __last, __comp, __len, __buf.first, __buf.second);
254 std::__check_strict_weak_ordering_sorted(__first, __last, __comp);
257 template <class _RandomAccessIterator, class _Compare>
258 inline _LIBCPP_HIDE_FROM_ABI void
259 stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
260 std::__stable_sort_impl<_ClassicAlgPolicy>(std::move(__first), std::move(__last), __comp);
263 template <class _RandomAccessIterator>
264 inline _LIBCPP_HIDE_FROM_ABI void stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) {
265 std::stable_sort(__first, __last, __less<>());
268 _LIBCPP_END_NAMESPACE_STD
270 _LIBCPP_POP_MACROS
272 #endif // _LIBCPP___ALGORITHM_STABLE_SORT_H