62namespace std _GLIBCXX_VISIBILITY(default)
64_GLIBCXX_BEGIN_NAMESPACE_VERSION
71 template<
typename _RandomAccessIterator,
typename _Distance,
75 __is_heap_until(_RandomAccessIterator __first, _Distance __n,
78 _Distance __parent = 0;
79 for (_Distance __child = 1; __child < __n; ++__child)
81 if (__comp(__first + __parent, __first + __child))
83 if ((__child & 1) == 0)
91 template<
typename _RandomAccessIterator,
typename _Distance>
94 __is_heap(_RandomAccessIterator __first, _Distance __n)
96 __gnu_cxx::__ops::_Iter_less_iter __comp;
97 return std::__is_heap_until(__first, __n, __comp) == __n;
100 template<
typename _RandomAccessIterator,
typename _Compare,
104 __is_heap(_RandomAccessIterator __first, _Compare __comp, _Distance __n)
106 typedef __decltype(__comp) _Cmp;
107 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
108 return std::__is_heap_until(__first, __n, __cmp) == __n;
111 template<
typename _RandomAccessIterator>
114 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
115 {
return std::__is_heap(__first,
std::distance(__first, __last)); }
117 template<
typename _RandomAccessIterator,
typename _Compare>
120 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
123 return std::__is_heap(__first, _GLIBCXX_MOVE(__comp),
130 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp,
134 __push_heap(_RandomAccessIterator __first,
135 _Distance __holeIndex, _Distance __topIndex, _Tp __value,
138 _Distance __parent = (__holeIndex - 1) / 2;
139 while (__holeIndex > __topIndex && __comp(__first + __parent, __value))
141 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
142 __holeIndex = __parent;
143 __parent = (__holeIndex - 1) / 2;
145 *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
158 template<
typename _RandomAccessIterator>
161 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
169 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
170 _RandomAccessIterator>)
171 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
172 __glibcxx_requires_valid_range(__first, __last);
173 __glibcxx_requires_irreflexive(__first, __last);
174 __glibcxx_requires_heap(__first, __last - 1);
176 __gnu_cxx::__ops::_Iter_less_val __comp;
177 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
178 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
179 _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
194 template<
typename _RandomAccessIterator,
typename _Compare>
197 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
206 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
207 _RandomAccessIterator>)
208 __glibcxx_requires_valid_range(__first, __last);
209 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
210 __glibcxx_requires_heap_pred(__first, __last - 1, __comp);
212 __decltype(__gnu_cxx::__ops::__iter_comp_val(_GLIBCXX_MOVE(__comp)))
213 __cmp(_GLIBCXX_MOVE(__comp));
214 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
215 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
216 _DistanceType(0), _GLIBCXX_MOVE(__value), __cmp);
219 template<
typename _RandomAccessIterator,
typename _Distance,
220 typename _Tp,
typename _Compare>
223 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
224 _Distance __len, _Tp __value, _Compare __comp)
226 const _Distance __topIndex = __holeIndex;
227 _Distance __secondChild = __holeIndex;
228 while (__secondChild < (__len - 1) / 2)
230 __secondChild = 2 * (__secondChild + 1);
231 if (__comp(__first + __secondChild,
232 __first + (__secondChild - 1)))
234 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
235 __holeIndex = __secondChild;
237 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
239 __secondChild = 2 * (__secondChild + 1);
240 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
241 + (__secondChild - 1)));
242 __holeIndex = __secondChild - 1;
244 __decltype(__gnu_cxx::__ops::__iter_comp_val(_GLIBCXX_MOVE(__comp)))
245 __cmp(_GLIBCXX_MOVE(__comp));
246 std::__push_heap(__first, __holeIndex, __topIndex,
247 _GLIBCXX_MOVE(__value), __cmp);
250 template<
typename _RandomAccessIterator,
typename _Compare>
253 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
254 _RandomAccessIterator __result, _Compare& __comp)
256 typedef typename iterator_traits<_RandomAccessIterator>::value_type
258 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
261 _ValueType __value = _GLIBCXX_MOVE(*__result);
262 *__result = _GLIBCXX_MOVE(*__first);
263 std::__adjust_heap(__first, _DistanceType(0),
264 _DistanceType(__last - __first),
265 _GLIBCXX_MOVE(__value), __comp);
279 template<
typename _RandomAccessIterator>
282 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
285 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
286 _RandomAccessIterator>)
287 __glibcxx_function_requires(_LessThanComparableConcept<
289 __glibcxx_requires_non_empty_range(__first, __last);
290 __glibcxx_requires_valid_range(__first, __last);
291 __glibcxx_requires_irreflexive(__first, __last);
292 __glibcxx_requires_heap(__first, __last);
294 if (__last - __first > 1)
297 __gnu_cxx::__ops::_Iter_less_iter __comp;
298 std::__pop_heap(__first, __last, __last, __comp);
313 template<
typename _RandomAccessIterator,
typename _Compare>
316 pop_heap(_RandomAccessIterator __first,
317 _RandomAccessIterator __last, _Compare __comp)
320 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
321 _RandomAccessIterator>)
322 __glibcxx_requires_valid_range(__first, __last);
323 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
324 __glibcxx_requires_non_empty_range(__first, __last);
325 __glibcxx_requires_heap_pred(__first, __last, __comp);
327 if (__last - __first > 1)
329 typedef __decltype(__comp) _Cmp;
330 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
332 std::__pop_heap(__first, __last, __last, __cmp);
336 template<
typename _RandomAccessIterator,
typename _Compare>
339 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
342 typedef typename iterator_traits<_RandomAccessIterator>::value_type
344 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
347 if (__last - __first < 2)
350 const _DistanceType __len = __last - __first;
351 _DistanceType __parent = (__len - 2) / 2;
354 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
355 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
371 template<
typename _RandomAccessIterator>
374 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
377 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
378 _RandomAccessIterator>)
379 __glibcxx_function_requires(_LessThanComparableConcept<
381 __glibcxx_requires_valid_range(__first, __last);
382 __glibcxx_requires_irreflexive(__first, __last);
384 __gnu_cxx::__ops::_Iter_less_iter __comp;
385 std::__make_heap(__first, __last, __comp);
398 template<
typename _RandomAccessIterator,
typename _Compare>
401 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
405 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
406 _RandomAccessIterator>)
407 __glibcxx_requires_valid_range(__first, __last);
408 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
410 typedef __decltype(__comp) _Cmp;
411 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
412 std::__make_heap(__first, __last, __cmp);
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::_Iter_less_iter __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 typedef __decltype(__comp) _Cmp;
478 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
479 std::__sort_heap(__first, __last, __cmp);
482#if __cplusplus >= 201103L
493 template<
typename _RandomAccessIterator>
495 inline _RandomAccessIterator
496 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
499 __glibcxx_function_requires(_RandomAccessIteratorConcept<
500 _RandomAccessIterator>)
501 __glibcxx_function_requires(_LessThanComparableConcept<
503 __glibcxx_requires_valid_range(__first, __last);
504 __glibcxx_requires_irreflexive(__first, __last);
506 __gnu_cxx::__ops::_Iter_less_iter __comp;
508 std::__is_heap_until(__first,
std::distance(__first, __last), __comp);
522 template<
typename _RandomAccessIterator,
typename _Compare>
524 inline _RandomAccessIterator
525 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
529 __glibcxx_function_requires(_RandomAccessIteratorConcept<
530 _RandomAccessIterator>)
531 __glibcxx_requires_valid_range(__first, __last);
532 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
534 typedef __decltype(__comp) _Cmp;
535 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
537 + std::__is_heap_until(__first,
std::distance(__first, __last), __cmp);
547 template<
typename _RandomAccessIterator>
550 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
551 {
return std::is_heap_until(__first, __last) == __last; }
561 template<
typename _RandomAccessIterator,
typename _Compare>
564 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
568 __glibcxx_function_requires(_RandomAccessIteratorConcept<
569 _RandomAccessIterator>)
570 __glibcxx_requires_valid_range(__first, __last);
571 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
574 typedef __decltype(__comp) _Cmp;
575 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
576 return std::__is_heap_until(__first, __dist, __cmp) == __dist;
580_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.