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);
155 template<
typename _RandomAccessIterator>
157 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
159 typedef typename iterator_traits<_RandomAccessIterator>::value_type
161 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
165 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
166 _RandomAccessIterator>)
167 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
168 __glibcxx_requires_valid_range(__first, __last);
169 __glibcxx_requires_heap(__first, __last - 1);
171 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
172 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
173 _DistanceType(0), _GLIBCXX_MOVE(__value));
176 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp,
179 __push_heap(_RandomAccessIterator __first, _Distance __holeIndex,
180 _Distance __topIndex, _Tp __value, _Compare __comp)
182 _Distance __parent = (__holeIndex - 1) / 2;
183 while (__holeIndex > __topIndex
184 && __comp(*(__first + __parent), __value))
186 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
187 __holeIndex = __parent;
188 __parent = (__holeIndex - 1) / 2;
190 *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
205 template<
typename _RandomAccessIterator,
typename _Compare>
207 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
210 typedef typename iterator_traits<_RandomAccessIterator>::value_type
212 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
216 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
217 _RandomAccessIterator>)
218 __glibcxx_requires_valid_range(__first, __last);
219 __glibcxx_requires_heap_pred(__first, __last - 1, __comp);
221 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
222 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
223 _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
226 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp>
228 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
229 _Distance __len, _Tp __value)
231 const _Distance __topIndex = __holeIndex;
232 _Distance __secondChild = __holeIndex;
233 while (__secondChild < (__len - 1) / 2)
235 __secondChild = 2 * (__secondChild + 1);
236 if (*(__first + __secondChild) < *(__first + (__secondChild - 1)))
238 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
239 __holeIndex = __secondChild;
241 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
243 __secondChild = 2 * (__secondChild + 1);
244 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
245 + (__secondChild - 1)));
246 __holeIndex = __secondChild - 1;
248 std::__push_heap(__first, __holeIndex, __topIndex,
249 _GLIBCXX_MOVE(__value));
252 template<
typename _RandomAccessIterator>
254 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
255 _RandomAccessIterator __result)
257 typedef typename iterator_traits<_RandomAccessIterator>::value_type
259 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
262 _ValueType __value = _GLIBCXX_MOVE(*__result);
263 *__result = _GLIBCXX_MOVE(*__first);
264 std::__adjust_heap(__first, _DistanceType(0),
265 _DistanceType(__last - __first),
266 _GLIBCXX_MOVE(__value));
280 template<
typename _RandomAccessIterator>
282 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
284 typedef typename iterator_traits<_RandomAccessIterator>::value_type
288 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
289 _RandomAccessIterator>)
290 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
291 __glibcxx_requires_non_empty_range(__first, __last);
292 __glibcxx_requires_valid_range(__first, __last);
293 __glibcxx_requires_heap(__first, __last);
296 std::__pop_heap(__first, __last, __last);
299 template<
typename _RandomAccessIterator,
typename _Distance,
300 typename _Tp,
typename _Compare>
302 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
303 _Distance __len, _Tp __value, _Compare __comp)
305 const _Distance __topIndex = __holeIndex;
306 _Distance __secondChild = __holeIndex;
307 while (__secondChild < (__len - 1) / 2)
309 __secondChild = 2 * (__secondChild + 1);
310 if (__comp(*(__first + __secondChild),
311 *(__first + (__secondChild - 1))))
313 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
314 __holeIndex = __secondChild;
316 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
318 __secondChild = 2 * (__secondChild + 1);
319 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
320 + (__secondChild - 1)));
321 __holeIndex = __secondChild - 1;
323 std::__push_heap(__first, __holeIndex, __topIndex,
324 _GLIBCXX_MOVE(__value), __comp);
327 template<
typename _RandomAccessIterator,
typename _Compare>
329 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
330 _RandomAccessIterator __result, _Compare __comp)
332 typedef typename iterator_traits<_RandomAccessIterator>::value_type
334 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
337 _ValueType __value = _GLIBCXX_MOVE(*__result);
338 *__result = _GLIBCXX_MOVE(*__first);
339 std::__adjust_heap(__first, _DistanceType(0),
340 _DistanceType(__last - __first),
341 _GLIBCXX_MOVE(__value), __comp);
355 template<
typename _RandomAccessIterator,
typename _Compare>
357 pop_heap(_RandomAccessIterator __first,
358 _RandomAccessIterator __last, _Compare __comp)
361 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
362 _RandomAccessIterator>)
363 __glibcxx_requires_valid_range(__first, __last);
364 __glibcxx_requires_non_empty_range(__first, __last);
365 __glibcxx_requires_heap_pred(__first, __last, __comp);
368 std::__pop_heap(__first, __last, __last, __comp);
379 template<
typename _RandomAccessIterator>
381 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
383 typedef typename iterator_traits<_RandomAccessIterator>::value_type
385 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
389 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
390 _RandomAccessIterator>)
391 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
392 __glibcxx_requires_valid_range(__first, __last);
394 if (__last - __first < 2)
397 const _DistanceType __len = __last - __first;
398 _DistanceType __parent = (__len - 2) / 2;
401 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
402 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value));
419 template<
typename _RandomAccessIterator,
typename _Compare>
421 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
424 typedef typename iterator_traits<_RandomAccessIterator>::value_type
426 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
430 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
431 _RandomAccessIterator>)
432 __glibcxx_requires_valid_range(__first, __last);
434 if (__last - __first < 2)
437 const _DistanceType __len = __last - __first;
438 _DistanceType __parent = (__len - 2) / 2;
441 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
442 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
458 template<
typename _RandomAccessIterator>
460 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
463 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
464 _RandomAccessIterator>)
465 __glibcxx_function_requires(_LessThanComparableConcept<
466 typename iterator_traits<_RandomAccessIterator>::value_type>)
467 __glibcxx_requires_valid_range(__first, __last);
468 __glibcxx_requires_heap(__first, __last);
470 while (__last - __first > 1)
473 std::__pop_heap(__first, __last, __last);
487 template<
typename _RandomAccessIterator,
typename _Compare>
489 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
493 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
494 _RandomAccessIterator>)
495 __glibcxx_requires_valid_range(__first, __last);
496 __glibcxx_requires_heap_pred(__first, __last, __comp);
498 while (__last - __first > 1)
501 std::__pop_heap(__first, __last, __last, __comp);
505 #ifdef __GXX_EXPERIMENTAL_CXX0X__
516 template<
typename _RandomAccessIterator>
517 inline _RandomAccessIterator
518 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
521 __glibcxx_function_requires(_RandomAccessIteratorConcept<
522 _RandomAccessIterator>)
523 __glibcxx_function_requires(_LessThanComparableConcept<
524 typename iterator_traits<_RandomAccessIterator>::value_type>)
525 __glibcxx_requires_valid_range(__first, __last);
527 return __first + std::__is_heap_until(__first,
std::distance(__first,
542 template<
typename _RandomAccessIterator,
typename _Compare>
543 inline _RandomAccessIterator
544 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
548 __glibcxx_function_requires(_RandomAccessIteratorConcept<
549 _RandomAccessIterator>)
550 __glibcxx_requires_valid_range(__first, __last);
552 return __first + std::__is_heap_until(__first,
std::distance(__first,
564 template<
typename _RandomAccessIterator>
566 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
577 template<
typename _RandomAccessIterator,
typename _Compare>
579 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
584 _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.