63namespace std _GLIBCXX_VISIBILITY(default)
65_GLIBCXX_BEGIN_NAMESPACE_VERSION
72 template<
typename _RandomAccessIterator,
typename _Distance,
76 __is_heap_until(_RandomAccessIterator __first, _Distance __n,
79#if __cplusplus >= 201103L
81 static_assert(
is_same<
typename _IterTraits::difference_type,
83 "Argument 'n' must be the iterator's difference type");
85 _Distance __parent = 0;
86 for (_Distance __child = 1; __child < __n; ++__child)
88 if (__comp(__first[__parent], __first[__child]))
90 if ((__child & 1) == 0)
98 template<
typename _RandomAccessIterator,
typename _Distance>
101 __is_heap(_RandomAccessIterator __first, _Distance __n)
104 __gnu_cxx::__ops::less __comp;
105 return std::__is_heap_until(__first, __d, __comp) == __n;
108 template<
typename _RandomAccessIterator,
typename _Compare,
112 __is_heap(_RandomAccessIterator __first, _Compare __comp, _Distance __n)
115 return std::__is_heap_until(__first, __d, __comp) == __n;
118 template<
typename _RandomAccessIterator>
121 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
122 {
return std::__is_heap(__first,
std::distance(__first, __last)); }
124 template<
typename _RandomAccessIterator,
typename _Compare>
127 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
130 return std::__is_heap(__first, _GLIBCXX_MOVE(__comp),
137 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp,
141 __push_heap(_RandomAccessIterator __first,
142 _Distance __holeIndex, _Distance __topIndex, _Tp __value,
145 _Distance __parent = (__holeIndex - 1) / 2;
146 while (__holeIndex > __topIndex && __comp(__first[__parent], __value))
148 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
149 __holeIndex = __parent;
150 __parent = (__holeIndex - 1) / 2;
152 *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
165 template<
typename _RandomAccessIterator>
168 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
176 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
177 _RandomAccessIterator>)
178 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
179 __glibcxx_requires_valid_range(__first, __last);
180 __glibcxx_requires_irreflexive(__first, __last);
181 __glibcxx_requires_heap(__first, __last - _DistanceType(1));
183 __gnu_cxx::__ops::less __comp;
184 _ValueType __value = _GLIBCXX_MOVE(*(__last - _DistanceType(1)));
185 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
186 _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
201 template<
typename _RandomAccessIterator,
typename _Compare>
204 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
213 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
214 _RandomAccessIterator>)
215 __glibcxx_requires_valid_range(__first, __last);
216 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
217 __glibcxx_requires_heap_pred(__first, __last - _DistanceType(1), __comp);
219 _ValueType __value = _GLIBCXX_MOVE(*(__last - _DistanceType(1)));
220 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
221 _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
224 template<
typename _RandomAccessIterator,
typename _Distance,
225 typename _Tp,
typename _Compare>
228 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
229 _Distance __len, _Tp __value, _Compare __comp)
231 const _Distance __topIndex = __holeIndex;
232 _Distance __secondChild = __holeIndex;
233 while (__secondChild < (__len - 1) / 2)
235 __secondChild = 2 * (__secondChild + 1);
236 if (__comp(__first[__secondChild],
237 __first[__secondChild - 1]))
239 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
240 __holeIndex = __secondChild;
242 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
244 __secondChild = 2 * (__secondChild + 1);
245 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
246 + (__secondChild - 1)));
247 __holeIndex = __secondChild - 1;
249 std::__push_heap(__first, __holeIndex, __topIndex,
250 _GLIBCXX_MOVE(__value), __comp);
253 template<
typename _RandomAccessIterator,
typename _Compare>
256 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
257 _RandomAccessIterator __result, _Compare& __comp)
264 _ValueType __value = _GLIBCXX_MOVE(*__result);
265 *__result = _GLIBCXX_MOVE(*__first);
266 std::__adjust_heap(__first, _DistanceType(0),
267 _DistanceType(__last - __first),
268 _GLIBCXX_MOVE(__value), __comp);
282 template<
typename _RandomAccessIterator>
285 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
288 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
289 _RandomAccessIterator>)
290 __glibcxx_function_requires(_LessThanComparableConcept<
292 __glibcxx_requires_non_empty_range(__first, __last);
293 __glibcxx_requires_valid_range(__first, __last);
294 __glibcxx_requires_irreflexive(__first, __last);
295 __glibcxx_requires_heap(__first, __last);
297 if (__last - __first > 1)
300 __gnu_cxx::__ops::less __comp;
301 std::__pop_heap(__first, __last, __last, __comp);
316 template<
typename _RandomAccessIterator,
typename _Compare>
319 pop_heap(_RandomAccessIterator __first,
320 _RandomAccessIterator __last, _Compare __comp)
323 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
324 _RandomAccessIterator>)
325 __glibcxx_requires_valid_range(__first, __last);
326 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
327 __glibcxx_requires_non_empty_range(__first, __last);
328 __glibcxx_requires_heap_pred(__first, __last, __comp);
330 if (__last - __first > 1)
333 std::__pop_heap(__first, __last, __last, __comp);
337 template<
typename _RandomAccessIterator,
typename _Compare>
340 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
343 typedef typename iterator_traits<_RandomAccessIterator>::value_type
345 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
348 if (__last - __first < 2)
351 const _DistanceType __len = __last - __first;
352 _DistanceType __parent = (__len - 2) / 2;
355 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
356 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
372 template<
typename _RandomAccessIterator>
375 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
378 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
379 _RandomAccessIterator>)
380 __glibcxx_function_requires(_LessThanComparableConcept<
382 __glibcxx_requires_valid_range(__first, __last);
383 __glibcxx_requires_irreflexive(__first, __last);
385 __gnu_cxx::__ops::less __comp;
386 std::__make_heap(__first, __last, __comp);
399 template<
typename _RandomAccessIterator,
typename _Compare>
402 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
406 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
407 _RandomAccessIterator>)
408 __glibcxx_requires_valid_range(__first, __last);
409 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
411 std::__make_heap(__first, __last, __comp);
414 template<
typename _RandomAccessIterator,
typename _Compare>
417 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
420 while (__last - __first > 1)
423 std::__pop_heap(__first, __last, __last, __comp);
435 template<
typename _RandomAccessIterator>
438 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
441 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
442 _RandomAccessIterator>)
443 __glibcxx_function_requires(_LessThanComparableConcept<
445 __glibcxx_requires_valid_range(__first, __last);
446 __glibcxx_requires_irreflexive(__first, __last);
447 __glibcxx_requires_heap(__first, __last);
449 __gnu_cxx::__ops::less __comp;
450 std::__sort_heap(__first, __last, __comp);
463 template<
typename _RandomAccessIterator,
typename _Compare>
466 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
470 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
471 _RandomAccessIterator>)
472 __glibcxx_requires_valid_range(__first, __last);
473 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
474 __glibcxx_requires_heap_pred(__first, __last, __comp);
476 std::__sort_heap(__first, __last, __comp);
479#if __cplusplus >= 201103L
490 template<
typename _RandomAccessIterator>
491 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
492 inline _RandomAccessIterator
493 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
496 __glibcxx_function_requires(_RandomAccessIteratorConcept<
497 _RandomAccessIterator>)
498 __glibcxx_function_requires(_LessThanComparableConcept<
500 __glibcxx_requires_valid_range(__first, __last);
501 __glibcxx_requires_irreflexive(__first, __last);
503 __gnu_cxx::__ops::less __comp;
505 std::__is_heap_until(__first,
std::distance(__first, __last), __comp);
519 template<
typename _RandomAccessIterator,
typename _Compare>
520 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
521 inline _RandomAccessIterator
522 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
526 __glibcxx_function_requires(_RandomAccessIteratorConcept<
527 _RandomAccessIterator>)
528 __glibcxx_requires_valid_range(__first, __last);
529 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
532 + std::__is_heap_until(__first,
std::distance(__first, __last),
543 template<
typename _RandomAccessIterator>
544 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
546 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
547 {
return std::is_heap_until(__first, __last) == __last; }
557 template<
typename _RandomAccessIterator,
typename _Compare>
558 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
560 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
564 __glibcxx_function_requires(_RandomAccessIteratorConcept<
565 _RandomAccessIterator>)
566 __glibcxx_requires_valid_range(__first, __last);
567 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
570 return std::__is_heap_until(__first, __dist, __comp) == __dist;
574_GLIBCXX_END_NAMESPACE_VERSION
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Traits class for iterators.