]>
Commit | Line | Data |
---|---|---|
42526146 PE |
1 | // Heap implementation -*- C++ -*- |
2 | ||
a945c346 | 3 | // Copyright (C) 2001-2024 Free Software Foundation, Inc. |
42526146 PE |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
42526146 PE |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
42526146 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
42526146 | 24 | |
725dc051 BK |
25 | /* |
26 | * | |
27 | * Copyright (c) 1994 | |
28 | * Hewlett-Packard Company | |
29 | * | |
30 | * Permission to use, copy, modify, distribute and sell this software | |
31 | * and its documentation for any purpose is hereby granted without fee, | |
32 | * provided that the above copyright notice appear in all copies and | |
33 | * that both that copyright notice and this permission notice appear | |
34 | * in supporting documentation. Hewlett-Packard Company makes no | |
35 | * representations about the suitability of this software for any | |
36 | * purpose. It is provided "as is" without express or implied warranty. | |
37 | * | |
38 | * Copyright (c) 1997 | |
39 | * Silicon Graphics Computer Systems, Inc. | |
40 | * | |
41 | * Permission to use, copy, modify, distribute and sell this software | |
42 | * and its documentation for any purpose is hereby granted without fee, | |
43 | * provided that the above copyright notice appear in all copies and | |
44 | * that both that copyright notice and this permission notice appear | |
45 | * in supporting documentation. Silicon Graphics makes no | |
46 | * representations about the suitability of this software for any | |
47 | * purpose. It is provided "as is" without express or implied warranty. | |
48 | */ | |
49 | ||
f910786b | 50 | /** @file bits/stl_heap.h |
729e3d3f | 51 | * This is an internal header file, included by other library headers. |
f910786b | 52 | * Do not attempt to use it directly. @headername{queue} |
725dc051 BK |
53 | */ |
54 | ||
3d7c150e BK |
55 | #ifndef _STL_HEAP_H |
56 | #define _STL_HEAP_H 1 | |
725dc051 | 57 | |
285b36d6 | 58 | #include <debug/debug.h> |
ca0f8fd1 | 59 | #include <bits/move.h> |
ea89b248 | 60 | #include <bits/predefined_ops.h> |
a4438054 | 61 | #include <bits/stl_iterator_base_funcs.h> |
285b36d6 | 62 | |
12ffa228 BK |
63 | namespace std _GLIBCXX_VISIBILITY(default) |
64 | { | |
65 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 66 | |
5b9daa7e | 67 | /** |
5ab3a5af | 68 | * @defgroup heap_algorithms Heap |
5b9daa7e BK |
69 | * @ingroup sorting_algorithms |
70 | */ | |
71 | ||
285b36d6 | 72 | template<typename _RandomAccessIterator, typename _Distance, |
e69f1bad | 73 | typename _Compare> |
3a66e68a | 74 | _GLIBCXX20_CONSTEXPR |
e69f1bad PC |
75 | _Distance |
76 | __is_heap_until(_RandomAccessIterator __first, _Distance __n, | |
437f43cc | 77 | _Compare& __comp) |
285b36d6 BK |
78 | { |
79 | _Distance __parent = 0; | |
ffa67767 PC |
80 | for (_Distance __child = 1; __child < __n; ++__child) |
81 | { | |
ea89b248 | 82 | if (__comp(__first + __parent, __first + __child)) |
e69f1bad | 83 | return __child; |
ffa67767 PC |
84 | if ((__child & 1) == 0) |
85 | ++__parent; | |
86 | } | |
e69f1bad | 87 | return __n; |
285b36d6 BK |
88 | } |
89 | ||
e69f1bad PC |
90 | // __is_heap, a predicate testing whether or not a range is a heap. |
91 | // This function is an extension, not part of the C++ standard. | |
92 | template<typename _RandomAccessIterator, typename _Distance> | |
3a66e68a | 93 | _GLIBCXX20_CONSTEXPR |
e69f1bad PC |
94 | inline bool |
95 | __is_heap(_RandomAccessIterator __first, _Distance __n) | |
ea89b248 | 96 | { |
437f43cc JW |
97 | __gnu_cxx::__ops::_Iter_less_iter __comp; |
98 | return std::__is_heap_until(__first, __n, __comp) == __n; | |
ea89b248 | 99 | } |
e69f1bad PC |
100 | |
101 | template<typename _RandomAccessIterator, typename _Compare, | |
102 | typename _Distance> | |
3a66e68a | 103 | _GLIBCXX20_CONSTEXPR |
e69f1bad PC |
104 | inline bool |
105 | __is_heap(_RandomAccessIterator __first, _Compare __comp, _Distance __n) | |
ea89b248 | 106 | { |
8d85abab JW |
107 | typedef __decltype(__comp) _Cmp; |
108 | __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp)); | |
437f43cc | 109 | return std::__is_heap_until(__first, __n, __cmp) == __n; |
ea89b248 | 110 | } |
e69f1bad | 111 | |
285b36d6 | 112 | template<typename _RandomAccessIterator> |
3a66e68a | 113 | _GLIBCXX20_CONSTEXPR |
bad333ff | 114 | inline bool |
285b36d6 BK |
115 | __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) |
116 | { return std::__is_heap(__first, std::distance(__first, __last)); } | |
117 | ||
e69f1bad | 118 | template<typename _RandomAccessIterator, typename _Compare> |
3a66e68a | 119 | _GLIBCXX20_CONSTEXPR |
bad333ff | 120 | inline bool |
285b36d6 | 121 | __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, |
e69f1bad | 122 | _Compare __comp) |
9ade9945 JW |
123 | { |
124 | return std::__is_heap(__first, _GLIBCXX_MOVE(__comp), | |
125 | std::distance(__first, __last)); | |
126 | } | |
725dc051 | 127 | |
e69f1bad PC |
128 | // Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap, |
129 | // + is_heap and is_heap_until in C++0x. | |
02d92e3b | 130 | |
ea89b248 FD |
131 | template<typename _RandomAccessIterator, typename _Distance, typename _Tp, |
132 | typename _Compare> | |
7a91c710 | 133 | _GLIBCXX20_CONSTEXPR |
ed6814f7 | 134 | void |
02d92e3b | 135 | __push_heap(_RandomAccessIterator __first, |
ea89b248 | 136 | _Distance __holeIndex, _Distance __topIndex, _Tp __value, |
437f43cc | 137 | _Compare& __comp) |
02d92e3b SW |
138 | { |
139 | _Distance __parent = (__holeIndex - 1) / 2; | |
ea89b248 | 140 | while (__holeIndex > __topIndex && __comp(__first + __parent, __value)) |
ffa67767 | 141 | { |
d70e9d81 | 142 | *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent)); |
ffa67767 PC |
143 | __holeIndex = __parent; |
144 | __parent = (__holeIndex - 1) / 2; | |
145 | } | |
d70e9d81 | 146 | *(__first + __holeIndex) = _GLIBCXX_MOVE(__value); |
02d92e3b SW |
147 | } |
148 | ||
119dbb1f JQ |
149 | /** |
150 | * @brief Push an element onto a heap. | |
93c66bc6 BK |
151 | * @param __first Start of heap. |
152 | * @param __last End of heap + element. | |
5b9daa7e | 153 | * @ingroup heap_algorithms |
119dbb1f | 154 | * |
93c66bc6 BK |
155 | * This operation pushes the element at last-1 onto the valid heap |
156 | * over the range [__first,__last-1). After completion, | |
157 | * [__first,__last) is a valid heap. | |
119dbb1f | 158 | */ |
02d92e3b | 159 | template<typename _RandomAccessIterator> |
7a91c710 | 160 | _GLIBCXX20_CONSTEXPR |
ed6814f7 | 161 | inline void |
02d92e3b SW |
162 | push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) |
163 | { | |
164 | typedef typename iterator_traits<_RandomAccessIterator>::value_type | |
165 | _ValueType; | |
166 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type | |
167 | _DistanceType; | |
168 | ||
169 | // concept requirements | |
3d7c150e | 170 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4d16bdbb | 171 | _RandomAccessIterator>) |
3d7c150e | 172 | __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>) |
285b36d6 | 173 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 174 | __glibcxx_requires_irreflexive(__first, __last); |
bad333ff | 175 | __glibcxx_requires_heap(__first, __last - 1); |
02d92e3b | 176 | |
437f43cc | 177 | __gnu_cxx::__ops::_Iter_less_val __comp; |
d70e9d81 | 178 | _ValueType __value = _GLIBCXX_MOVE(*(__last - 1)); |
ffa67767 | 179 | std::__push_heap(__first, _DistanceType((__last - __first) - 1), |
437f43cc | 180 | _DistanceType(0), _GLIBCXX_MOVE(__value), __comp); |
02d92e3b SW |
181 | } |
182 | ||
119dbb1f JQ |
183 | /** |
184 | * @brief Push an element onto a heap using comparison functor. | |
93c66bc6 BK |
185 | * @param __first Start of heap. |
186 | * @param __last End of heap + element. | |
187 | * @param __comp Comparison functor. | |
5b9daa7e | 188 | * @ingroup heap_algorithms |
119dbb1f | 189 | * |
93c66bc6 BK |
190 | * This operation pushes the element at __last-1 onto the valid |
191 | * heap over the range [__first,__last-1). After completion, | |
192 | * [__first,__last) is a valid heap. Compare operations are | |
193 | * performed using comp. | |
119dbb1f | 194 | */ |
02d92e3b | 195 | template<typename _RandomAccessIterator, typename _Compare> |
7a91c710 | 196 | _GLIBCXX20_CONSTEXPR |
ed6814f7 | 197 | inline void |
02d92e3b SW |
198 | push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, |
199 | _Compare __comp) | |
200 | { | |
201 | typedef typename iterator_traits<_RandomAccessIterator>::value_type | |
202 | _ValueType; | |
203 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type | |
204 | _DistanceType; | |
205 | ||
206 | // concept requirements | |
3d7c150e | 207 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4d16bdbb | 208 | _RandomAccessIterator>) |
285b36d6 | 209 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 210 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
285b36d6 | 211 | __glibcxx_requires_heap_pred(__first, __last - 1, __comp); |
02d92e3b | 212 | |
437f43cc JW |
213 | __decltype(__gnu_cxx::__ops::__iter_comp_val(_GLIBCXX_MOVE(__comp))) |
214 | __cmp(_GLIBCXX_MOVE(__comp)); | |
d70e9d81 | 215 | _ValueType __value = _GLIBCXX_MOVE(*(__last - 1)); |
ffa67767 | 216 | std::__push_heap(__first, _DistanceType((__last - __first) - 1), |
437f43cc | 217 | _DistanceType(0), _GLIBCXX_MOVE(__value), __cmp); |
02d92e3b SW |
218 | } |
219 | ||
ea89b248 FD |
220 | template<typename _RandomAccessIterator, typename _Distance, |
221 | typename _Tp, typename _Compare> | |
7a91c710 | 222 | _GLIBCXX20_CONSTEXPR |
ed6814f7 | 223 | void |
02d92e3b | 224 | __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex, |
ea89b248 | 225 | _Distance __len, _Tp __value, _Compare __comp) |
02d92e3b | 226 | { |
ffa67767 | 227 | const _Distance __topIndex = __holeIndex; |
bad333ff PC |
228 | _Distance __secondChild = __holeIndex; |
229 | while (__secondChild < (__len - 1) / 2) | |
ffa67767 | 230 | { |
bad333ff | 231 | __secondChild = 2 * (__secondChild + 1); |
ea89b248 FD |
232 | if (__comp(__first + __secondChild, |
233 | __first + (__secondChild - 1))) | |
ffa67767 | 234 | __secondChild--; |
d70e9d81 | 235 | *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild)); |
ffa67767 | 236 | __holeIndex = __secondChild; |
ffa67767 | 237 | } |
bad333ff | 238 | if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2) |
ffa67767 | 239 | { |
bad333ff | 240 | __secondChild = 2 * (__secondChild + 1); |
d70e9d81 CJ |
241 | *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first |
242 | + (__secondChild - 1))); | |
ffa67767 PC |
243 | __holeIndex = __secondChild - 1; |
244 | } | |
437f43cc JW |
245 | __decltype(__gnu_cxx::__ops::__iter_comp_val(_GLIBCXX_MOVE(__comp))) |
246 | __cmp(_GLIBCXX_MOVE(__comp)); | |
247 | std::__push_heap(__first, __holeIndex, __topIndex, | |
248 | _GLIBCXX_MOVE(__value), __cmp); | |
02d92e3b SW |
249 | } |
250 | ||
ea89b248 | 251 | template<typename _RandomAccessIterator, typename _Compare> |
7a91c710 | 252 | _GLIBCXX20_CONSTEXPR |
ed6814f7 | 253 | inline void |
02d92e3b | 254 | __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, |
437f43cc | 255 | _RandomAccessIterator __result, _Compare& __comp) |
02d92e3b | 256 | { |
d70e9d81 CJ |
257 | typedef typename iterator_traits<_RandomAccessIterator>::value_type |
258 | _ValueType; | |
ffa67767 | 259 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
d70e9d81 CJ |
260 | _DistanceType; |
261 | ||
262 | _ValueType __value = _GLIBCXX_MOVE(*__result); | |
263 | *__result = _GLIBCXX_MOVE(*__first); | |
264 | std::__adjust_heap(__first, _DistanceType(0), | |
265 | _DistanceType(__last - __first), | |
437f43cc | 266 | _GLIBCXX_MOVE(__value), __comp); |
02d92e3b SW |
267 | } |
268 | ||
119dbb1f JQ |
269 | /** |
270 | * @brief Pop an element off a heap. | |
93c66bc6 BK |
271 | * @param __first Start of heap. |
272 | * @param __last End of heap. | |
a8028a3e | 273 | * @pre [__first, __last) is a valid, non-empty range. |
5b9daa7e | 274 | * @ingroup heap_algorithms |
119dbb1f | 275 | * |
93c66bc6 BK |
276 | * This operation pops the top of the heap. The elements __first |
277 | * and __last-1 are swapped and [__first,__last-1) is made into a | |
278 | * heap. | |
119dbb1f | 279 | */ |
02d92e3b | 280 | template<typename _RandomAccessIterator> |
7a91c710 | 281 | _GLIBCXX20_CONSTEXPR |
02d92e3b SW |
282 | inline void |
283 | pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) | |
284 | { | |
02d92e3b | 285 | // concept requirements |
3d7c150e | 286 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4d16bdbb | 287 | _RandomAccessIterator>) |
ec494945 FD |
288 | __glibcxx_function_requires(_LessThanComparableConcept< |
289 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
a8028a3e | 290 | __glibcxx_requires_non_empty_range(__first, __last); |
285b36d6 | 291 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 292 | __glibcxx_requires_irreflexive(__first, __last); |
285b36d6 | 293 | __glibcxx_requires_heap(__first, __last); |
02d92e3b | 294 | |
177d2b74 PC |
295 | if (__last - __first > 1) |
296 | { | |
297 | --__last; | |
437f43cc JW |
298 | __gnu_cxx::__ops::_Iter_less_iter __comp; |
299 | std::__pop_heap(__first, __last, __last, __comp); | |
177d2b74 | 300 | } |
02d92e3b SW |
301 | } |
302 | ||
119dbb1f JQ |
303 | /** |
304 | * @brief Pop an element off a heap using comparison functor. | |
93c66bc6 BK |
305 | * @param __first Start of heap. |
306 | * @param __last End of heap. | |
307 | * @param __comp Comparison functor to use. | |
5b9daa7e | 308 | * @ingroup heap_algorithms |
119dbb1f | 309 | * |
93c66bc6 BK |
310 | * This operation pops the top of the heap. The elements __first |
311 | * and __last-1 are swapped and [__first,__last-1) is made into a | |
312 | * heap. Comparisons are made using comp. | |
119dbb1f | 313 | */ |
02d92e3b | 314 | template<typename _RandomAccessIterator, typename _Compare> |
7a91c710 | 315 | _GLIBCXX20_CONSTEXPR |
ed6814f7 | 316 | inline void |
02d92e3b SW |
317 | pop_heap(_RandomAccessIterator __first, |
318 | _RandomAccessIterator __last, _Compare __comp) | |
319 | { | |
320 | // concept requirements | |
3d7c150e | 321 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4d16bdbb | 322 | _RandomAccessIterator>) |
285b36d6 | 323 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 324 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
e13afbac | 325 | __glibcxx_requires_non_empty_range(__first, __last); |
285b36d6 | 326 | __glibcxx_requires_heap_pred(__first, __last, __comp); |
02d92e3b | 327 | |
177d2b74 PC |
328 | if (__last - __first > 1) |
329 | { | |
8d85abab JW |
330 | typedef __decltype(__comp) _Cmp; |
331 | __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp)); | |
177d2b74 | 332 | --__last; |
437f43cc | 333 | std::__pop_heap(__first, __last, __last, __cmp); |
177d2b74 | 334 | } |
02d92e3b SW |
335 | } |
336 | ||
ea89b248 | 337 | template<typename _RandomAccessIterator, typename _Compare> |
7a91c710 | 338 | _GLIBCXX20_CONSTEXPR |
ed6814f7 | 339 | void |
ea89b248 | 340 | __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, |
437f43cc | 341 | _Compare& __comp) |
02d92e3b SW |
342 | { |
343 | typedef typename iterator_traits<_RandomAccessIterator>::value_type | |
344 | _ValueType; | |
345 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type | |
346 | _DistanceType; | |
347 | ||
ffa67767 PC |
348 | if (__last - __first < 2) |
349 | return; | |
350 | ||
ed6814f7 | 351 | const _DistanceType __len = __last - __first; |
ffa67767 PC |
352 | _DistanceType __parent = (__len - 2) / 2; |
353 | while (true) | |
354 | { | |
d70e9d81 | 355 | _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent)); |
ea89b248 | 356 | std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value), |
9ade9945 | 357 | __comp); |
ffa67767 PC |
358 | if (__parent == 0) |
359 | return; | |
360 | __parent--; | |
361 | } | |
02d92e3b | 362 | } |
45ab93d9 | 363 | |
ea89b248 FD |
364 | /** |
365 | * @brief Construct a heap over a range. | |
366 | * @param __first Start of heap. | |
367 | * @param __last End of heap. | |
368 | * @ingroup heap_algorithms | |
369 | * | |
370 | * This operation makes the elements in [__first,__last) into a heap. | |
371 | */ | |
372 | template<typename _RandomAccessIterator> | |
7a91c710 | 373 | _GLIBCXX20_CONSTEXPR |
ea89b248 FD |
374 | inline void |
375 | make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) | |
376 | { | |
377 | // concept requirements | |
378 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< | |
379 | _RandomAccessIterator>) | |
380 | __glibcxx_function_requires(_LessThanComparableConcept< | |
381 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
382 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 383 | __glibcxx_requires_irreflexive(__first, __last); |
ea89b248 | 384 | |
437f43cc JW |
385 | __gnu_cxx::__ops::_Iter_less_iter __comp; |
386 | std::__make_heap(__first, __last, __comp); | |
ea89b248 | 387 | } |
02d92e3b | 388 | |
119dbb1f JQ |
389 | /** |
390 | * @brief Construct a heap over a range using comparison functor. | |
93c66bc6 BK |
391 | * @param __first Start of heap. |
392 | * @param __last End of heap. | |
393 | * @param __comp Comparison functor to use. | |
5b9daa7e | 394 | * @ingroup heap_algorithms |
119dbb1f | 395 | * |
93c66bc6 BK |
396 | * This operation makes the elements in [__first,__last) into a heap. |
397 | * Comparisons are made using __comp. | |
119dbb1f | 398 | */ |
02d92e3b | 399 | template<typename _RandomAccessIterator, typename _Compare> |
7a91c710 | 400 | _GLIBCXX20_CONSTEXPR |
ea89b248 | 401 | inline void |
02d92e3b SW |
402 | make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, |
403 | _Compare __comp) | |
404 | { | |
02d92e3b | 405 | // concept requirements |
3d7c150e | 406 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4d16bdbb | 407 | _RandomAccessIterator>) |
285b36d6 | 408 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 409 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
ed6814f7 | 410 | |
8d85abab JW |
411 | typedef __decltype(__comp) _Cmp; |
412 | __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp)); | |
437f43cc | 413 | std::__make_heap(__first, __last, __cmp); |
ea89b248 | 414 | } |
ffa67767 | 415 | |
ea89b248 | 416 | template<typename _RandomAccessIterator, typename _Compare> |
7a91c710 | 417 | _GLIBCXX20_CONSTEXPR |
ea89b248 FD |
418 | void |
419 | __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, | |
437f43cc | 420 | _Compare& __comp) |
ea89b248 FD |
421 | { |
422 | while (__last - __first > 1) | |
ffa67767 | 423 | { |
ea89b248 | 424 | --__last; |
9ade9945 | 425 | std::__pop_heap(__first, __last, __last, __comp); |
ffa67767 | 426 | } |
02d92e3b SW |
427 | } |
428 | ||
119dbb1f JQ |
429 | /** |
430 | * @brief Sort a heap. | |
93c66bc6 BK |
431 | * @param __first Start of heap. |
432 | * @param __last End of heap. | |
5b9daa7e | 433 | * @ingroup heap_algorithms |
119dbb1f | 434 | * |
93c66bc6 | 435 | * This operation sorts the valid heap in the range [__first,__last). |
119dbb1f | 436 | */ |
02d92e3b | 437 | template<typename _RandomAccessIterator> |
7a91c710 | 438 | _GLIBCXX20_CONSTEXPR |
ea89b248 | 439 | inline void |
02d92e3b SW |
440 | sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) |
441 | { | |
442 | // concept requirements | |
3d7c150e | 443 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4d16bdbb | 444 | _RandomAccessIterator>) |
3d7c150e | 445 | __glibcxx_function_requires(_LessThanComparableConcept< |
4d16bdbb | 446 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
285b36d6 | 447 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 448 | __glibcxx_requires_irreflexive(__first, __last); |
bad333ff | 449 | __glibcxx_requires_heap(__first, __last); |
02d92e3b | 450 | |
437f43cc JW |
451 | __gnu_cxx::__ops::_Iter_less_iter __comp; |
452 | std::__sort_heap(__first, __last, __comp); | |
02d92e3b SW |
453 | } |
454 | ||
119dbb1f JQ |
455 | /** |
456 | * @brief Sort a heap using comparison functor. | |
93c66bc6 BK |
457 | * @param __first Start of heap. |
458 | * @param __last End of heap. | |
459 | * @param __comp Comparison functor to use. | |
5b9daa7e | 460 | * @ingroup heap_algorithms |
119dbb1f | 461 | * |
93c66bc6 BK |
462 | * This operation sorts the valid heap in the range [__first,__last). |
463 | * Comparisons are made using __comp. | |
119dbb1f | 464 | */ |
02d92e3b | 465 | template<typename _RandomAccessIterator, typename _Compare> |
7a91c710 | 466 | _GLIBCXX20_CONSTEXPR |
ea89b248 | 467 | inline void |
02d92e3b SW |
468 | sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, |
469 | _Compare __comp) | |
470 | { | |
471 | // concept requirements | |
3d7c150e | 472 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4d16bdbb | 473 | _RandomAccessIterator>) |
285b36d6 | 474 | __glibcxx_requires_valid_range(__first, __last); |
630a286a | 475 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
285b36d6 | 476 | __glibcxx_requires_heap_pred(__first, __last, __comp); |
02d92e3b | 477 | |
8d85abab JW |
478 | typedef __decltype(__comp) _Cmp; |
479 | __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp)); | |
437f43cc | 480 | std::__sort_heap(__first, __last, __cmp); |
02d92e3b | 481 | } |
725dc051 | 482 | |
734f5023 | 483 | #if __cplusplus >= 201103L |
e69f1bad PC |
484 | /** |
485 | * @brief Search the end of a heap. | |
93c66bc6 BK |
486 | * @param __first Start of range. |
487 | * @param __last End of range. | |
4b7ed13a | 488 | * @return An iterator pointing to the first element not in the heap. |
5b9daa7e | 489 | * @ingroup heap_algorithms |
e69f1bad | 490 | * |
93c66bc6 BK |
491 | * This operation returns the last iterator i in [__first, __last) for which |
492 | * the range [__first, i) is a heap. | |
e69f1bad PC |
493 | */ |
494 | template<typename _RandomAccessIterator> | |
df483ebd | 495 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
e69f1bad PC |
496 | inline _RandomAccessIterator |
497 | is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last) | |
498 | { | |
4b7ed13a PC |
499 | // concept requirements |
500 | __glibcxx_function_requires(_RandomAccessIteratorConcept< | |
501 | _RandomAccessIterator>) | |
502 | __glibcxx_function_requires(_LessThanComparableConcept< | |
503 | typename iterator_traits<_RandomAccessIterator>::value_type>) | |
504 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 505 | __glibcxx_requires_irreflexive(__first, __last); |
4b7ed13a | 506 | |
437f43cc | 507 | __gnu_cxx::__ops::_Iter_less_iter __comp; |
45ab93d9 | 508 | return __first + |
437f43cc | 509 | std::__is_heap_until(__first, std::distance(__first, __last), __comp); |
e69f1bad PC |
510 | } |
511 | ||
512 | /** | |
513 | * @brief Search the end of a heap using comparison functor. | |
93c66bc6 BK |
514 | * @param __first Start of range. |
515 | * @param __last End of range. | |
516 | * @param __comp Comparison functor to use. | |
4b7ed13a | 517 | * @return An iterator pointing to the first element not in the heap. |
5b9daa7e | 518 | * @ingroup heap_algorithms |
e69f1bad | 519 | * |
93c66bc6 BK |
520 | * This operation returns the last iterator i in [__first, __last) for which |
521 | * the range [__first, i) is a heap. Comparisons are made using __comp. | |
e69f1bad PC |
522 | */ |
523 | template<typename _RandomAccessIterator, typename _Compare> | |
df483ebd | 524 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
e69f1bad PC |
525 | inline _RandomAccessIterator |
526 | is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, | |
527 | _Compare __comp) | |
528 | { | |
4b7ed13a PC |
529 | // concept requirements |
530 | __glibcxx_function_requires(_RandomAccessIteratorConcept< | |
531 | _RandomAccessIterator>) | |
532 | __glibcxx_requires_valid_range(__first, __last); | |
630a286a | 533 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
4b7ed13a | 534 | |
8d85abab JW |
535 | typedef __decltype(__comp) _Cmp; |
536 | __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp)); | |
ea89b248 | 537 | return __first |
437f43cc | 538 | + std::__is_heap_until(__first, std::distance(__first, __last), __cmp); |
e69f1bad | 539 | } |
4f39bf5c PC |
540 | |
541 | /** | |
542 | * @brief Determines whether a range is a heap. | |
93c66bc6 BK |
543 | * @param __first Start of range. |
544 | * @param __last End of range. | |
4f39bf5c | 545 | * @return True if range is a heap, false otherwise. |
5b9daa7e | 546 | * @ingroup heap_algorithms |
4f39bf5c PC |
547 | */ |
548 | template<typename _RandomAccessIterator> | |
df483ebd | 549 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
4f39bf5c PC |
550 | inline bool |
551 | is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) | |
552 | { return std::is_heap_until(__first, __last) == __last; } | |
553 | ||
554 | /** | |
555 | * @brief Determines whether a range is a heap using comparison functor. | |
93c66bc6 BK |
556 | * @param __first Start of range. |
557 | * @param __last End of range. | |
558 | * @param __comp Comparison functor to use. | |
4f39bf5c | 559 | * @return True if range is a heap, false otherwise. |
5b9daa7e | 560 | * @ingroup heap_algorithms |
4f39bf5c PC |
561 | */ |
562 | template<typename _RandomAccessIterator, typename _Compare> | |
df483ebd | 563 | _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR |
4f39bf5c PC |
564 | inline bool |
565 | is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, | |
566 | _Compare __comp) | |
45b48129 | 567 | { |
437f43cc JW |
568 | // concept requirements |
569 | __glibcxx_function_requires(_RandomAccessIteratorConcept< | |
570 | _RandomAccessIterator>) | |
571 | __glibcxx_requires_valid_range(__first, __last); | |
572 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); | |
573 | ||
574 | const auto __dist = std::distance(__first, __last); | |
8d85abab JW |
575 | typedef __decltype(__comp) _Cmp; |
576 | __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp)); | |
437f43cc | 577 | return std::__is_heap_until(__first, __dist, __cmp) == __dist; |
45b48129 | 578 | } |
e69f1bad PC |
579 | #endif |
580 | ||
12ffa228 BK |
581 | _GLIBCXX_END_NAMESPACE_VERSION |
582 | } // namespace | |
725dc051 | 583 | |
3d7c150e | 584 | #endif /* _STL_HEAP_H */ |