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
147 &&
bool(__comp(*(__first + __parent), __value)))
149 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
150 __holeIndex = __parent;
151 __parent = (__holeIndex - 1) / 2;
153 *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
166 template<
typename _RandomAccessIterator>
169 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
177 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
178 _RandomAccessIterator>)
179 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
180 __glibcxx_requires_valid_range(__first, __last);
181 __glibcxx_requires_irreflexive(__first, __last);
182 __glibcxx_requires_heap(__first, __last - _DistanceType(1));
184 __gnu_cxx::__ops::less __comp;
185 _ValueType __value = _GLIBCXX_MOVE(*(__last - _DistanceType(1)));
186 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
187 _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
202 template<
typename _RandomAccessIterator,
typename _Compare>
205 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
214 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
215 _RandomAccessIterator>)
216 __glibcxx_requires_valid_range(__first, __last);
217 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
218 __glibcxx_requires_heap_pred(__first, __last - _DistanceType(1), __comp);
220 _ValueType __value = _GLIBCXX_MOVE(*(__last - _DistanceType(1)));
221 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
222 _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
225 template<
typename _RandomAccessIterator,
typename _Distance,
226 typename _Tp,
typename _Compare>
229 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
230 _Distance __len, _Tp __value, _Compare __comp)
232 const _Distance __topIndex = __holeIndex;
233 _Distance __secondChild = __holeIndex;
234 while (__secondChild < (__len - 1) / 2)
236 __secondChild = 2 * (__secondChild + 1);
237 if (__comp(*(__first + __secondChild),
238 *(__first + _Distance(__secondChild - 1))))
240 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
241 __holeIndex = __secondChild;
243 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
245 __secondChild = 2 * (__secondChild + 1);
246 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
247 + (__secondChild - 1)));
248 __holeIndex = __secondChild - 1;
250 std::__push_heap(__first, __holeIndex, __topIndex,
251 _GLIBCXX_MOVE(__value), __comp);
254 template<
typename _RandomAccessIterator,
typename _Compare>
257 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
258 _RandomAccessIterator __result, _Compare& __comp)
265 _ValueType __value = _GLIBCXX_MOVE(*__result);
266 *__result = _GLIBCXX_MOVE(*__first);
267 std::__adjust_heap(__first, _DistanceType(0),
268 _DistanceType(__last - __first),
269 _GLIBCXX_MOVE(__value), __comp);
283 template<
typename _RandomAccessIterator>
286 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
289 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
290 _RandomAccessIterator>)
291 __glibcxx_function_requires(_LessThanComparableConcept<
293 __glibcxx_requires_non_empty_range(__first, __last);
294 __glibcxx_requires_valid_range(__first, __last);
295 __glibcxx_requires_irreflexive(__first, __last);
296 __glibcxx_requires_heap(__first, __last);
298 if (__last - __first > 1)
301 __gnu_cxx::__ops::less __comp;
302 std::__pop_heap(__first, __last, __last, __comp);
317 template<
typename _RandomAccessIterator,
typename _Compare>
320 pop_heap(_RandomAccessIterator __first,
321 _RandomAccessIterator __last, _Compare __comp)
324 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
325 _RandomAccessIterator>)
326 __glibcxx_requires_valid_range(__first, __last);
327 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
328 __glibcxx_requires_non_empty_range(__first, __last);
329 __glibcxx_requires_heap_pred(__first, __last, __comp);
331 if (__last - __first > 1)
334 std::__pop_heap(__first, __last, __last, __comp);
338 template<
typename _RandomAccessIterator,
typename _Compare>
341 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
344 typedef typename iterator_traits<_RandomAccessIterator>::value_type
346 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
349 if (__last - __first < 2)
352 const _DistanceType __len = __last - __first;
353 _DistanceType __parent = (__len - 2) / 2;
356 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
357 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
373 template<
typename _RandomAccessIterator>
376 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
379 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
380 _RandomAccessIterator>)
381 __glibcxx_function_requires(_LessThanComparableConcept<
383 __glibcxx_requires_valid_range(__first, __last);
384 __glibcxx_requires_irreflexive(__first, __last);
386 __gnu_cxx::__ops::less __comp;
387 std::__make_heap(__first, __last, __comp);
400 template<
typename _RandomAccessIterator,
typename _Compare>
403 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
407 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
408 _RandomAccessIterator>)
409 __glibcxx_requires_valid_range(__first, __last);
410 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
412 std::__make_heap(__first, __last, __comp);
415 template<
typename _RandomAccessIterator,
typename _Compare>
418 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
421 while (__last - __first > 1)
424 std::__pop_heap(__first, __last, __last, __comp);
436 template<
typename _RandomAccessIterator>
439 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
442 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
443 _RandomAccessIterator>)
444 __glibcxx_function_requires(_LessThanComparableConcept<
446 __glibcxx_requires_valid_range(__first, __last);
447 __glibcxx_requires_irreflexive(__first, __last);
448 __glibcxx_requires_heap(__first, __last);
450 __gnu_cxx::__ops::less __comp;
451 std::__sort_heap(__first, __last, __comp);
464 template<
typename _RandomAccessIterator,
typename _Compare>
467 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
471 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
472 _RandomAccessIterator>)
473 __glibcxx_requires_valid_range(__first, __last);
474 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
475 __glibcxx_requires_heap_pred(__first, __last, __comp);
477 std::__sort_heap(__first, __last, __comp);
480#if __cplusplus >= 201103L
491 template<
typename _RandomAccessIterator>
492 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
493 inline _RandomAccessIterator
494 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
497 __glibcxx_function_requires(_RandomAccessIteratorConcept<
498 _RandomAccessIterator>)
499 __glibcxx_function_requires(_LessThanComparableConcept<
501 __glibcxx_requires_valid_range(__first, __last);
502 __glibcxx_requires_irreflexive(__first, __last);
504 __gnu_cxx::__ops::less __comp;
506 std::__is_heap_until(__first,
std::distance(__first, __last), __comp);
520 template<
typename _RandomAccessIterator,
typename _Compare>
521 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
522 inline _RandomAccessIterator
523 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
527 __glibcxx_function_requires(_RandomAccessIteratorConcept<
528 _RandomAccessIterator>)
529 __glibcxx_requires_valid_range(__first, __last);
530 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
533 + std::__is_heap_until(__first,
std::distance(__first, __last),
544 template<
typename _RandomAccessIterator>
545 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
547 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
548 {
return std::is_heap_until(__first, __last) == __last; }
558 template<
typename _RandomAccessIterator,
typename _Compare>
559 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
561 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
565 __glibcxx_function_requires(_RandomAccessIteratorConcept<
566 _RandomAccessIterator>)
567 __glibcxx_requires_valid_range(__first, __last);
568 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
571 return std::__is_heap_until(__first, __dist, __comp) == __dist;
575_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.