61 namespace std _GLIBCXX_VISIBILITY(default)
63 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70 template<
typename _RandomAccessIterator,
typename _Distance>
72 __is_heap_until(_RandomAccessIterator __first, _Distance __n)
74 _Distance __parent = 0;
75 for (_Distance __child = 1; __child < __n; ++__child)
77 if (__first[__parent] < __first[__child])
79 if ((__child & 1) == 0)
85 template<
typename _RandomAccessIterator,
typename _Distance,
88 __is_heap_until(_RandomAccessIterator __first, _Distance __n,
91 _Distance __parent = 0;
92 for (_Distance __child = 1; __child < __n; ++__child)
94 if (__comp(__first[__parent], __first[__child]))
96 if ((__child & 1) == 0)
104 template<
typename _RandomAccessIterator,
typename _Distance>
106 __is_heap(_RandomAccessIterator __first, _Distance __n)
107 {
return std::__is_heap_until(__first, __n) == __n; }
109 template<
typename _RandomAccessIterator,
typename _Compare,
112 __is_heap(_RandomAccessIterator __first, _Compare __comp, _Distance __n)
113 {
return std::__is_heap_until(__first, __n, __comp) == __n; }
115 template<
typename _RandomAccessIterator>
117 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
118 {
return std::__is_heap(__first,
std::distance(__first, __last)); }
120 template<
typename _RandomAccessIterator,
typename _Compare>
122 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
124 {
return std::__is_heap(__first, __comp,
std::distance(__first, __last)); }
129 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp>
131 __push_heap(_RandomAccessIterator __first,
132 _Distance __holeIndex, _Distance __topIndex, _Tp __value)
134 _Distance __parent = (__holeIndex - 1) / 2;
135 while (__holeIndex > __topIndex && *(__first + __parent) < __value)
137 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
138 __holeIndex = __parent;
139 __parent = (__holeIndex - 1) / 2;
141 *(__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);
204 template<
typename _RandomAccessIterator,
typename _Compare>
206 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
209 typedef typename iterator_traits<_RandomAccessIterator>::value_type
211 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
215 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
216 _RandomAccessIterator>)
217 __glibcxx_requires_valid_range(__first, __last);
218 __glibcxx_requires_heap_pred(__first, __last - 1, __comp);
220 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
221 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
222 _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
225 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp>
227 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
228 _Distance __len, _Tp __value)
230 const _Distance __topIndex = __holeIndex;
231 _Distance __secondChild = __holeIndex;
232 while (__secondChild < (__len - 1) / 2)
234 __secondChild = 2 * (__secondChild + 1);
235 if (*(__first + __secondChild) < *(__first + (__secondChild - 1)))
237 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
238 __holeIndex = __secondChild;
240 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
242 __secondChild = 2 * (__secondChild + 1);
243 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
244 + (__secondChild - 1)));
245 __holeIndex = __secondChild - 1;
247 std::__push_heap(__first, __holeIndex, __topIndex,
248 _GLIBCXX_MOVE(__value));
251 template<
typename _RandomAccessIterator>
253 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
254 _RandomAccessIterator __result)
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));
279 template<
typename _RandomAccessIterator>
281 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
283 typedef typename iterator_traits<_RandomAccessIterator>::value_type
287 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
288 _RandomAccessIterator>)
289 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
290 __glibcxx_requires_non_empty_range(__first, __last);
291 __glibcxx_requires_valid_range(__first, __last);
292 __glibcxx_requires_heap(__first, __last);
294 if (__last - __first > 1)
297 std::__pop_heap(__first, __last, __last);
301 template<
typename _RandomAccessIterator,
typename _Distance,
302 typename _Tp,
typename _Compare>
304 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
305 _Distance __len, _Tp __value, _Compare __comp)
307 const _Distance __topIndex = __holeIndex;
308 _Distance __secondChild = __holeIndex;
309 while (__secondChild < (__len - 1) / 2)
311 __secondChild = 2 * (__secondChild + 1);
312 if (__comp(*(__first + __secondChild),
313 *(__first + (__secondChild - 1))))
315 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
316 __holeIndex = __secondChild;
318 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
320 __secondChild = 2 * (__secondChild + 1);
321 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
322 + (__secondChild - 1)));
323 __holeIndex = __secondChild - 1;
325 std::__push_heap(__first, __holeIndex, __topIndex,
326 _GLIBCXX_MOVE(__value), __comp);
329 template<
typename _RandomAccessIterator,
typename _Compare>
331 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
332 _RandomAccessIterator __result, _Compare __comp)
334 typedef typename iterator_traits<_RandomAccessIterator>::value_type
336 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
339 _ValueType __value = _GLIBCXX_MOVE(*__result);
340 *__result = _GLIBCXX_MOVE(*__first);
341 std::__adjust_heap(__first, _DistanceType(0),
342 _DistanceType(__last - __first),
343 _GLIBCXX_MOVE(__value), __comp);
357 template<
typename _RandomAccessIterator,
typename _Compare>
359 pop_heap(_RandomAccessIterator __first,
360 _RandomAccessIterator __last, _Compare __comp)
363 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
364 _RandomAccessIterator>)
365 __glibcxx_requires_valid_range(__first, __last);
366 __glibcxx_requires_non_empty_range(__first, __last);
367 __glibcxx_requires_heap_pred(__first, __last, __comp);
369 if (__last - __first > 1)
372 std::__pop_heap(__first, __last, __last, __comp);
384 template<
typename _RandomAccessIterator>
386 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
388 typedef typename iterator_traits<_RandomAccessIterator>::value_type
390 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
394 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
395 _RandomAccessIterator>)
396 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
397 __glibcxx_requires_valid_range(__first, __last);
399 if (__last - __first < 2)
402 const _DistanceType __len = __last - __first;
403 _DistanceType __parent = (__len - 2) / 2;
406 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
407 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value));
424 template<
typename _RandomAccessIterator,
typename _Compare>
426 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
429 typedef typename iterator_traits<_RandomAccessIterator>::value_type
431 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
435 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
436 _RandomAccessIterator>)
437 __glibcxx_requires_valid_range(__first, __last);
439 if (__last - __first < 2)
442 const _DistanceType __len = __last - __first;
443 _DistanceType __parent = (__len - 2) / 2;
446 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
447 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
463 template<
typename _RandomAccessIterator>
465 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
468 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
469 _RandomAccessIterator>)
470 __glibcxx_function_requires(_LessThanComparableConcept<
471 typename iterator_traits<_RandomAccessIterator>::value_type>)
472 __glibcxx_requires_valid_range(__first, __last);
473 __glibcxx_requires_heap(__first, __last);
475 while (__last - __first > 1)
478 std::__pop_heap(__first, __last, __last);
492 template<
typename _RandomAccessIterator,
typename _Compare>
494 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
498 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
499 _RandomAccessIterator>)
500 __glibcxx_requires_valid_range(__first, __last);
501 __glibcxx_requires_heap_pred(__first, __last, __comp);
503 while (__last - __first > 1)
506 std::__pop_heap(__first, __last, __last, __comp);
510 #if __cplusplus >= 201103L
521 template<
typename _RandomAccessIterator>
522 inline _RandomAccessIterator
523 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
526 __glibcxx_function_requires(_RandomAccessIteratorConcept<
527 _RandomAccessIterator>)
528 __glibcxx_function_requires(_LessThanComparableConcept<
529 typename iterator_traits<_RandomAccessIterator>::value_type>)
530 __glibcxx_requires_valid_range(__first, __last);
532 return __first + std::__is_heap_until(__first,
std::distance(__first,
547 template<
typename _RandomAccessIterator,
typename _Compare>
548 inline _RandomAccessIterator
549 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
553 __glibcxx_function_requires(_RandomAccessIteratorConcept<
554 _RandomAccessIterator>)
555 __glibcxx_requires_valid_range(__first, __last);
557 return __first + std::__is_heap_until(__first,
std::distance(__first,
569 template<
typename _RandomAccessIterator>
571 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
582 template<
typename _RandomAccessIterator,
typename _Compare>
584 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
589 _GLIBCXX_END_NAMESPACE_VERSION
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
ISO C++ entities toplevel namespace is std.
_RandomAccessIterator is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Search the end of a heap using comparison functor.