62 namespace std _GLIBCXX_VISIBILITY(default)
64 _GLIBCXX_BEGIN_NAMESPACE_VERSION
71 template<
typename _RandomAccessIterator,
typename _Distance,
74 __is_heap_until(_RandomAccessIterator __first, _Distance __n,
77 _Distance __parent = 0;
78 for (_Distance __child = 1; __child < __n; ++__child)
80 if (__comp(__first + __parent, __first + __child))
82 if ((__child & 1) == 0)
90 template<
typename _RandomAccessIterator,
typename _Distance>
92 __is_heap(_RandomAccessIterator __first, _Distance __n)
94 __gnu_cxx::__ops::_Iter_less_iter __comp;
95 return std::__is_heap_until(__first, __n, __comp) == __n;
98 template<
typename _RandomAccessIterator,
typename _Compare,
101 __is_heap(_RandomAccessIterator __first, _Compare __comp, _Distance __n)
103 typedef __decltype(__comp) _Cmp;
104 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
105 return std::__is_heap_until(__first, __n, __cmp) == __n;
108 template<
typename _RandomAccessIterator>
110 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
111 {
return std::__is_heap(__first,
std::distance(__first, __last)); }
113 template<
typename _RandomAccessIterator,
typename _Compare>
115 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
118 return std::__is_heap(__first, _GLIBCXX_MOVE(__comp),
125 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp,
128 __push_heap(_RandomAccessIterator __first,
129 _Distance __holeIndex, _Distance __topIndex, _Tp __value,
132 _Distance __parent = (__holeIndex - 1) / 2;
133 while (__holeIndex > __topIndex && __comp(__first + __parent, __value))
135 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
136 __holeIndex = __parent;
137 __parent = (__holeIndex - 1) / 2;
139 *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
152 template<
typename _RandomAccessIterator>
154 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
156 typedef typename iterator_traits<_RandomAccessIterator>::value_type
158 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
162 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
163 _RandomAccessIterator>)
164 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
165 __glibcxx_requires_valid_range(__first, __last);
166 __glibcxx_requires_irreflexive(__first, __last);
167 __glibcxx_requires_heap(__first, __last - 1);
169 __gnu_cxx::__ops::_Iter_less_val __comp;
170 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
171 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
172 _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
187 template<
typename _RandomAccessIterator,
typename _Compare>
189 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
192 typedef typename iterator_traits<_RandomAccessIterator>::value_type
194 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
198 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
199 _RandomAccessIterator>)
200 __glibcxx_requires_valid_range(__first, __last);
201 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
202 __glibcxx_requires_heap_pred(__first, __last - 1, __comp);
204 __decltype(__gnu_cxx::__ops::__iter_comp_val(_GLIBCXX_MOVE(__comp)))
205 __cmp(_GLIBCXX_MOVE(__comp));
206 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
207 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
208 _DistanceType(0), _GLIBCXX_MOVE(__value), __cmp);
211 template<
typename _RandomAccessIterator,
typename _Distance,
212 typename _Tp,
typename _Compare>
214 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
215 _Distance __len, _Tp __value, _Compare __comp)
217 const _Distance __topIndex = __holeIndex;
218 _Distance __secondChild = __holeIndex;
219 while (__secondChild < (__len - 1) / 2)
221 __secondChild = 2 * (__secondChild + 1);
222 if (__comp(__first + __secondChild,
223 __first + (__secondChild - 1)))
225 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
226 __holeIndex = __secondChild;
228 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
230 __secondChild = 2 * (__secondChild + 1);
231 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
232 + (__secondChild - 1)));
233 __holeIndex = __secondChild - 1;
235 __decltype(__gnu_cxx::__ops::__iter_comp_val(_GLIBCXX_MOVE(__comp)))
236 __cmp(_GLIBCXX_MOVE(__comp));
237 std::__push_heap(__first, __holeIndex, __topIndex,
238 _GLIBCXX_MOVE(__value), __cmp);
241 template<
typename _RandomAccessIterator,
typename _Compare>
243 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
244 _RandomAccessIterator __result, _Compare& __comp)
246 typedef typename iterator_traits<_RandomAccessIterator>::value_type
248 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
251 _ValueType __value = _GLIBCXX_MOVE(*__result);
252 *__result = _GLIBCXX_MOVE(*__first);
253 std::__adjust_heap(__first, _DistanceType(0),
254 _DistanceType(__last - __first),
255 _GLIBCXX_MOVE(__value), __comp);
269 template<
typename _RandomAccessIterator>
271 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
274 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
275 _RandomAccessIterator>)
276 __glibcxx_function_requires(_LessThanComparableConcept<
277 typename iterator_traits<_RandomAccessIterator>::value_type>)
278 __glibcxx_requires_non_empty_range(__first, __last);
279 __glibcxx_requires_valid_range(__first, __last);
280 __glibcxx_requires_irreflexive(__first, __last);
281 __glibcxx_requires_heap(__first, __last);
283 if (__last - __first > 1)
286 __gnu_cxx::__ops::_Iter_less_iter __comp;
287 std::__pop_heap(__first, __last, __last, __comp);
302 template<
typename _RandomAccessIterator,
typename _Compare>
304 pop_heap(_RandomAccessIterator __first,
305 _RandomAccessIterator __last, _Compare __comp)
308 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
309 _RandomAccessIterator>)
310 __glibcxx_requires_valid_range(__first, __last);
311 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
312 __glibcxx_requires_non_empty_range(__first, __last);
313 __glibcxx_requires_heap_pred(__first, __last, __comp);
315 if (__last - __first > 1)
317 typedef __decltype(__comp) _Cmp;
318 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
320 std::__pop_heap(__first, __last, __last, __cmp);
324 template<
typename _RandomAccessIterator,
typename _Compare>
326 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
329 typedef typename iterator_traits<_RandomAccessIterator>::value_type
331 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
334 if (__last - __first < 2)
337 const _DistanceType __len = __last - __first;
338 _DistanceType __parent = (__len - 2) / 2;
341 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
342 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
358 template<
typename _RandomAccessIterator>
360 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
363 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
364 _RandomAccessIterator>)
365 __glibcxx_function_requires(_LessThanComparableConcept<
366 typename iterator_traits<_RandomAccessIterator>::value_type>)
367 __glibcxx_requires_valid_range(__first, __last);
368 __glibcxx_requires_irreflexive(__first, __last);
370 __gnu_cxx::__ops::_Iter_less_iter __comp;
371 std::__make_heap(__first, __last, __comp);
384 template<
typename _RandomAccessIterator,
typename _Compare>
386 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
390 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
391 _RandomAccessIterator>)
392 __glibcxx_requires_valid_range(__first, __last);
393 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
395 typedef __decltype(__comp) _Cmp;
396 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
397 std::__make_heap(__first, __last, __cmp);
400 template<
typename _RandomAccessIterator,
typename _Compare>
402 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
405 while (__last - __first > 1)
408 std::__pop_heap(__first, __last, __last, __comp);
420 template<
typename _RandomAccessIterator>
422 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
425 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
426 _RandomAccessIterator>)
427 __glibcxx_function_requires(_LessThanComparableConcept<
428 typename iterator_traits<_RandomAccessIterator>::value_type>)
429 __glibcxx_requires_valid_range(__first, __last);
430 __glibcxx_requires_irreflexive(__first, __last);
431 __glibcxx_requires_heap(__first, __last);
433 __gnu_cxx::__ops::_Iter_less_iter __comp;
434 std::__sort_heap(__first, __last, __comp);
447 template<
typename _RandomAccessIterator,
typename _Compare>
449 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
453 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
454 _RandomAccessIterator>)
455 __glibcxx_requires_valid_range(__first, __last);
456 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
457 __glibcxx_requires_heap_pred(__first, __last, __comp);
459 typedef __decltype(__comp) _Cmp;
460 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
461 std::__sort_heap(__first, __last, __cmp);
464 #if __cplusplus >= 201103L 475 template<
typename _RandomAccessIterator>
476 inline _RandomAccessIterator
477 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
480 __glibcxx_function_requires(_RandomAccessIteratorConcept<
481 _RandomAccessIterator>)
482 __glibcxx_function_requires(_LessThanComparableConcept<
483 typename iterator_traits<_RandomAccessIterator>::value_type>)
484 __glibcxx_requires_valid_range(__first, __last);
485 __glibcxx_requires_irreflexive(__first, __last);
487 __gnu_cxx::__ops::_Iter_less_iter __comp;
489 std::__is_heap_until(__first,
std::distance(__first, __last), __comp);
503 template<
typename _RandomAccessIterator,
typename _Compare>
504 inline _RandomAccessIterator
505 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
509 __glibcxx_function_requires(_RandomAccessIteratorConcept<
510 _RandomAccessIterator>)
511 __glibcxx_requires_valid_range(__first, __last);
512 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
514 typedef __decltype(__comp) _Cmp;
515 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
517 + std::__is_heap_until(__first,
std::distance(__first, __last), __cmp);
527 template<
typename _RandomAccessIterator>
529 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
540 template<
typename _RandomAccessIterator,
typename _Compare>
542 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
546 __glibcxx_function_requires(_RandomAccessIteratorConcept<
547 _RandomAccessIterator>)
548 __glibcxx_requires_valid_range(__first, __last);
549 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
552 typedef __decltype(__comp) _Cmp;
553 __gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
554 return std::__is_heap_until(__first, __dist, __cmp) == __dist;
558 _GLIBCXX_END_NAMESPACE_VERSION
_GLIBCXX17_CONSTEXPR iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
_RandomAccessIterator is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Search the end of a heap using comparison functor.
ISO C++ entities toplevel namespace is std.