62 namespace std _GLIBCXX_VISIBILITY(default)
64 _GLIBCXX_BEGIN_NAMESPACE_VERSION
71 template<
typename _RandomAccessIterator,
typename _Distance>
73 __is_heap_until(_RandomAccessIterator __first, _Distance __n)
75 _Distance __parent = 0;
76 for (_Distance __child = 1; __child < __n; ++__child)
78 if (__first[__parent] < __first[__child])
80 if ((__child & 1) == 0)
86 template<
typename _RandomAccessIterator,
typename _Distance,
89 __is_heap_until(_RandomAccessIterator __first, _Distance __n,
92 _Distance __parent = 0;
93 for (_Distance __child = 1; __child < __n; ++__child)
95 if (__comp(__first[__parent], __first[__child]))
97 if ((__child & 1) == 0)
105 template<
typename _RandomAccessIterator,
typename _Distance>
107 __is_heap(_RandomAccessIterator __first, _Distance __n)
108 {
return std::__is_heap_until(__first, __n) == __n; }
110 template<
typename _RandomAccessIterator,
typename _Compare,
113 __is_heap(_RandomAccessIterator __first, _Compare __comp, _Distance __n)
114 {
return std::__is_heap_until(__first, __n, __comp) == __n; }
116 template<
typename _RandomAccessIterator>
118 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
119 {
return std::__is_heap(__first,
std::distance(__first, __last)); }
121 template<
typename _RandomAccessIterator,
typename _Compare>
123 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
125 {
return std::__is_heap(__first, __comp,
std::distance(__first, __last)); }
130 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp>
132 __push_heap(_RandomAccessIterator __first,
133 _Distance __holeIndex, _Distance __topIndex, _Tp __value)
135 _Distance __parent = (__holeIndex - 1) / 2;
136 while (__holeIndex > __topIndex && *(__first + __parent) < __value)
138 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
139 __holeIndex = __parent;
140 __parent = (__holeIndex - 1) / 2;
142 *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
154 template<
typename _RandomAccessIterator>
156 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
158 typedef typename iterator_traits<_RandomAccessIterator>::value_type
160 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
164 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
165 _RandomAccessIterator>)
166 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
167 __glibcxx_requires_valid_range(__first, __last);
168 __glibcxx_requires_heap(__first, __last - 1);
170 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
171 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
172 _DistanceType(0), _GLIBCXX_MOVE(__value));
175 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp,
178 __push_heap(_RandomAccessIterator __first, _Distance __holeIndex,
179 _Distance __topIndex, _Tp __value, _Compare __comp)
181 _Distance __parent = (__holeIndex - 1) / 2;
182 while (__holeIndex > __topIndex
183 && __comp(*(__first + __parent), __value))
185 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
186 __holeIndex = __parent;
187 __parent = (__holeIndex - 1) / 2;
189 *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
203 template<
typename _RandomAccessIterator,
typename _Compare>
205 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
208 typedef typename iterator_traits<_RandomAccessIterator>::value_type
210 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
214 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
215 _RandomAccessIterator>)
216 __glibcxx_requires_valid_range(__first, __last);
217 __glibcxx_requires_heap_pred(__first, __last - 1, __comp);
219 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
220 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
221 _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
224 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp>
226 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
227 _Distance __len, _Tp __value)
229 const _Distance __topIndex = __holeIndex;
230 _Distance __secondChild = __holeIndex;
231 while (__secondChild < (__len - 1) / 2)
233 __secondChild = 2 * (__secondChild + 1);
234 if (*(__first + __secondChild) < *(__first + (__secondChild - 1)))
236 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
237 __holeIndex = __secondChild;
239 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
241 __secondChild = 2 * (__secondChild + 1);
242 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
243 + (__secondChild - 1)));
244 __holeIndex = __secondChild - 1;
246 std::__push_heap(__first, __holeIndex, __topIndex,
247 _GLIBCXX_MOVE(__value));
250 template<
typename _RandomAccessIterator>
252 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
253 _RandomAccessIterator __result)
255 typedef typename iterator_traits<_RandomAccessIterator>::value_type
257 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
260 _ValueType __value = _GLIBCXX_MOVE(*__result);
261 *__result = _GLIBCXX_MOVE(*__first);
262 std::__adjust_heap(__first, _DistanceType(0),
263 _DistanceType(__last - __first),
264 _GLIBCXX_MOVE(__value));
276 template<
typename _RandomAccessIterator>
278 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
280 typedef typename iterator_traits<_RandomAccessIterator>::value_type
284 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
285 _RandomAccessIterator>)
286 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
287 __glibcxx_requires_valid_range(__first, __last);
288 __glibcxx_requires_heap(__first, __last);
291 std::__pop_heap(__first, __last, __last);
294 template<
typename _RandomAccessIterator,
typename _Distance,
295 typename _Tp,
typename _Compare>
297 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
298 _Distance __len, _Tp __value, _Compare __comp)
300 const _Distance __topIndex = __holeIndex;
301 _Distance __secondChild = __holeIndex;
302 while (__secondChild < (__len - 1) / 2)
304 __secondChild = 2 * (__secondChild + 1);
305 if (__comp(*(__first + __secondChild),
306 *(__first + (__secondChild - 1))))
308 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
309 __holeIndex = __secondChild;
311 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
313 __secondChild = 2 * (__secondChild + 1);
314 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
315 + (__secondChild - 1)));
316 __holeIndex = __secondChild - 1;
318 std::__push_heap(__first, __holeIndex, __topIndex,
319 _GLIBCXX_MOVE(__value), __comp);
322 template<
typename _RandomAccessIterator,
typename _Compare>
324 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
325 _RandomAccessIterator __result, _Compare __comp)
327 typedef typename iterator_traits<_RandomAccessIterator>::value_type
329 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
332 _ValueType __value = _GLIBCXX_MOVE(*__result);
333 *__result = _GLIBCXX_MOVE(*__first);
334 std::__adjust_heap(__first, _DistanceType(0),
335 _DistanceType(__last - __first),
336 _GLIBCXX_MOVE(__value), __comp);
350 template<
typename _RandomAccessIterator,
typename _Compare>
352 pop_heap(_RandomAccessIterator __first,
353 _RandomAccessIterator __last, _Compare __comp)
356 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
357 _RandomAccessIterator>)
358 __glibcxx_requires_valid_range(__first, __last);
359 __glibcxx_requires_heap_pred(__first, __last, __comp);
362 std::__pop_heap(__first, __last, __last, __comp);
373 template<
typename _RandomAccessIterator>
375 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
377 typedef typename iterator_traits<_RandomAccessIterator>::value_type
379 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
383 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
384 _RandomAccessIterator>)
385 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
386 __glibcxx_requires_valid_range(__first, __last);
388 if (__last - __first < 2)
391 const _DistanceType __len = __last - __first;
392 _DistanceType __parent = (__len - 2) / 2;
395 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
396 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value));
413 template<
typename _RandomAccessIterator,
typename _Compare>
415 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
418 typedef typename iterator_traits<_RandomAccessIterator>::value_type
420 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
424 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
425 _RandomAccessIterator>)
426 __glibcxx_requires_valid_range(__first, __last);
428 if (__last - __first < 2)
431 const _DistanceType __len = __last - __first;
432 _DistanceType __parent = (__len - 2) / 2;
435 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
436 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
452 template<
typename _RandomAccessIterator>
454 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
457 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
458 _RandomAccessIterator>)
459 __glibcxx_function_requires(_LessThanComparableConcept<
460 typename iterator_traits<_RandomAccessIterator>::value_type>)
461 __glibcxx_requires_valid_range(__first, __last);
462 __glibcxx_requires_heap(__first, __last);
464 while (__last - __first > 1)
467 std::__pop_heap(__first, __last, __last);
481 template<
typename _RandomAccessIterator,
typename _Compare>
483 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
487 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
488 _RandomAccessIterator>)
489 __glibcxx_requires_valid_range(__first, __last);
490 __glibcxx_requires_heap_pred(__first, __last, __comp);
492 while (__last - __first > 1)
495 std::__pop_heap(__first, __last, __last, __comp);
499 #ifdef __GXX_EXPERIMENTAL_CXX0X__
510 template<
typename _RandomAccessIterator>
511 inline _RandomAccessIterator
512 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
515 __glibcxx_function_requires(_RandomAccessIteratorConcept<
516 _RandomAccessIterator>)
517 __glibcxx_function_requires(_LessThanComparableConcept<
518 typename iterator_traits<_RandomAccessIterator>::value_type>)
519 __glibcxx_requires_valid_range(__first, __last);
521 return __first + std::__is_heap_until(__first,
std::distance(__first,
536 template<
typename _RandomAccessIterator,
typename _Compare>
537 inline _RandomAccessIterator
538 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
542 __glibcxx_function_requires(_RandomAccessIteratorConcept<
543 _RandomAccessIterator>)
544 __glibcxx_requires_valid_range(__first, __last);
546 return __first + std::__is_heap_until(__first,
std::distance(__first,
558 template<
typename _RandomAccessIterator>
560 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
571 template<
typename _RandomAccessIterator,
typename _Compare>
573 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
578 _GLIBCXX_END_NAMESPACE_VERSION
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.