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 return std::__is_heap_until(__first, __n,
95 __gnu_cxx::__ops::__iter_less_iter()) == __n;
98 template<
typename _RandomAccessIterator,
typename _Compare,
101 __is_heap(_RandomAccessIterator __first, _Compare __comp, _Distance __n)
103 return std::__is_heap_until(__first, __n,
104 __gnu_cxx::__ops::__iter_comp_iter(__comp)) == __n;
107 template<
typename _RandomAccessIterator>
109 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
110 {
return std::__is_heap(__first,
std::distance(__first, __last)); }
112 template<
typename _RandomAccessIterator,
typename _Compare>
114 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
116 {
return std::__is_heap(__first, __comp,
std::distance(__first, __last)); }
121 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp,
124 __push_heap(_RandomAccessIterator __first,
125 _Distance __holeIndex, _Distance __topIndex, _Tp __value,
128 _Distance __parent = (__holeIndex - 1) / 2;
129 while (__holeIndex > __topIndex && __comp(__first + __parent, __value))
131 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
132 __holeIndex = __parent;
133 __parent = (__holeIndex - 1) / 2;
135 *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
148 template<
typename _RandomAccessIterator>
150 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
152 typedef typename iterator_traits<_RandomAccessIterator>::value_type
154 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
158 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
159 _RandomAccessIterator>)
160 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
161 __glibcxx_requires_valid_range(__first, __last);
162 __glibcxx_requires_irreflexive(__first, __last);
163 __glibcxx_requires_heap(__first, __last - 1);
165 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
166 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
167 _DistanceType(0), _GLIBCXX_MOVE(__value),
168 __gnu_cxx::__ops::__iter_less_val());
183 template<
typename _RandomAccessIterator,
typename _Compare>
185 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
188 typedef typename iterator_traits<_RandomAccessIterator>::value_type
190 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
194 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
195 _RandomAccessIterator>)
196 __glibcxx_requires_valid_range(__first, __last);
197 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
198 __glibcxx_requires_heap_pred(__first, __last - 1, __comp);
200 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
201 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
202 _DistanceType(0), _GLIBCXX_MOVE(__value),
203 __gnu_cxx::__ops::__iter_comp_val(__comp));
206 template<
typename _RandomAccessIterator,
typename _Distance,
207 typename _Tp,
typename _Compare>
209 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
210 _Distance __len, _Tp __value, _Compare __comp)
212 const _Distance __topIndex = __holeIndex;
213 _Distance __secondChild = __holeIndex;
214 while (__secondChild < (__len - 1) / 2)
216 __secondChild = 2 * (__secondChild + 1);
217 if (__comp(__first + __secondChild,
218 __first + (__secondChild - 1)))
220 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
221 __holeIndex = __secondChild;
223 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
225 __secondChild = 2 * (__secondChild + 1);
226 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
227 + (__secondChild - 1)));
228 __holeIndex = __secondChild - 1;
230 std::__push_heap(__first, __holeIndex, __topIndex,
231 _GLIBCXX_MOVE(__value),
232 __gnu_cxx::__ops::__iter_comp_val(__comp));
235 template<
typename _RandomAccessIterator,
typename _Compare>
237 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
238 _RandomAccessIterator __result, _Compare __comp)
240 typedef typename iterator_traits<_RandomAccessIterator>::value_type
242 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
245 _ValueType __value = _GLIBCXX_MOVE(*__result);
246 *__result = _GLIBCXX_MOVE(*__first);
247 std::__adjust_heap(__first, _DistanceType(0),
248 _DistanceType(__last - __first),
249 _GLIBCXX_MOVE(__value), __comp);
263 template<
typename _RandomAccessIterator>
265 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
268 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
269 _RandomAccessIterator>)
270 __glibcxx_function_requires(_LessThanComparableConcept<
271 typename iterator_traits<_RandomAccessIterator>::value_type>)
272 __glibcxx_requires_non_empty_range(__first, __last);
273 __glibcxx_requires_valid_range(__first, __last);
274 __glibcxx_requires_irreflexive(__first, __last);
275 __glibcxx_requires_heap(__first, __last);
277 if (__last - __first > 1)
280 std::__pop_heap(__first, __last, __last,
281 __gnu_cxx::__ops::__iter_less_iter());
296 template<
typename _RandomAccessIterator,
typename _Compare>
298 pop_heap(_RandomAccessIterator __first,
299 _RandomAccessIterator __last, _Compare __comp)
302 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
303 _RandomAccessIterator>)
304 __glibcxx_requires_valid_range(__first, __last);
305 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
306 __glibcxx_requires_non_empty_range(__first, __last);
307 __glibcxx_requires_heap_pred(__first, __last, __comp);
309 if (__last - __first > 1)
312 std::__pop_heap(__first, __last, __last,
313 __gnu_cxx::__ops::__iter_comp_iter(__comp));
317 template<
typename _RandomAccessIterator,
typename _Compare>
319 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
322 typedef typename iterator_traits<_RandomAccessIterator>::value_type
324 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
327 if (__last - __first < 2)
330 const _DistanceType __len = __last - __first;
331 _DistanceType __parent = (__len - 2) / 2;
334 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
335 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
351 template<
typename _RandomAccessIterator>
353 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
356 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
357 _RandomAccessIterator>)
358 __glibcxx_function_requires(_LessThanComparableConcept<
359 typename iterator_traits<_RandomAccessIterator>::value_type>)
360 __glibcxx_requires_valid_range(__first, __last);
361 __glibcxx_requires_irreflexive(__first, __last);
363 std::__make_heap(__first, __last,
364 __gnu_cxx::__ops::__iter_less_iter());
377 template<
typename _RandomAccessIterator,
typename _Compare>
379 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
383 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
384 _RandomAccessIterator>)
385 __glibcxx_requires_valid_range(__first, __last);
386 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
388 std::__make_heap(__first, __last,
389 __gnu_cxx::__ops::__iter_comp_iter(__comp));
392 template<
typename _RandomAccessIterator,
typename _Compare>
394 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
397 while (__last - __first > 1)
400 std::__pop_heap(__first, __last, __last, __comp);
412 template<
typename _RandomAccessIterator>
414 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
417 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
418 _RandomAccessIterator>)
419 __glibcxx_function_requires(_LessThanComparableConcept<
420 typename iterator_traits<_RandomAccessIterator>::value_type>)
421 __glibcxx_requires_valid_range(__first, __last);
422 __glibcxx_requires_irreflexive(__first, __last);
423 __glibcxx_requires_heap(__first, __last);
425 std::__sort_heap(__first, __last,
426 __gnu_cxx::__ops::__iter_less_iter());
439 template<
typename _RandomAccessIterator,
typename _Compare>
441 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
445 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
446 _RandomAccessIterator>)
447 __glibcxx_requires_valid_range(__first, __last);
448 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
449 __glibcxx_requires_heap_pred(__first, __last, __comp);
451 std::__sort_heap(__first, __last,
452 __gnu_cxx::__ops::__iter_comp_iter(__comp));
455 #if __cplusplus >= 201103L 466 template<
typename _RandomAccessIterator>
467 inline _RandomAccessIterator
468 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
471 __glibcxx_function_requires(_RandomAccessIteratorConcept<
472 _RandomAccessIterator>)
473 __glibcxx_function_requires(_LessThanComparableConcept<
474 typename iterator_traits<_RandomAccessIterator>::value_type>)
475 __glibcxx_requires_valid_range(__first, __last);
476 __glibcxx_requires_irreflexive(__first, __last);
479 std::__is_heap_until(__first,
std::distance(__first, __last),
480 __gnu_cxx::__ops::__iter_less_iter());
494 template<
typename _RandomAccessIterator,
typename _Compare>
495 inline _RandomAccessIterator
496 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
500 __glibcxx_function_requires(_RandomAccessIteratorConcept<
501 _RandomAccessIterator>)
502 __glibcxx_requires_valid_range(__first, __last);
503 __glibcxx_requires_irreflexive_pred(__first, __last, __comp);
506 + std::__is_heap_until(__first,
std::distance(__first, __last),
507 __gnu_cxx::__ops::__iter_comp_iter(__comp));
517 template<
typename _RandomAccessIterator>
519 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
530 template<
typename _RandomAccessIterator,
typename _Compare>
532 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
537 _GLIBCXX_END_NAMESPACE_VERSION
_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.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.