62 _GLIBCXX_BEGIN_NAMESPACE(
std)
69 template<typename _RandomAccessIterator, typename _Distance>
71 __is_heap_until(_RandomAccessIterator __first, _Distance __n)
73 _Distance __parent = 0;
74 for (_Distance __child = 1; __child < __n; ++__child)
76 if (__first[__parent] < __first[__child])
78 if ((__child & 1) == 0)
84 template<
typename _RandomAccessIterator,
typename _Distance,
87 __is_heap_until(_RandomAccessIterator __first, _Distance __n,
90 _Distance __parent = 0;
91 for (_Distance __child = 1; __child < __n; ++__child)
93 if (__comp(__first[__parent], __first[__child]))
95 if ((__child & 1) == 0)
103 template<
typename _RandomAccessIterator,
typename _Distance>
105 __is_heap(_RandomAccessIterator __first, _Distance __n)
106 {
return std::__is_heap_until(__first, __n) == __n; }
108 template<
typename _RandomAccessIterator,
typename _Compare,
111 __is_heap(_RandomAccessIterator __first, _Compare __comp, _Distance __n)
112 {
return std::__is_heap_until(__first, __n, __comp) == __n; }
114 template<
typename _RandomAccessIterator>
116 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
117 {
return std::__is_heap(__first,
std::distance(__first, __last)); }
119 template<
typename _RandomAccessIterator,
typename _Compare>
121 __is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
123 {
return std::__is_heap(__first, __comp,
std::distance(__first, __last)); }
128 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp>
130 __push_heap(_RandomAccessIterator __first,
131 _Distance __holeIndex, _Distance __topIndex, _Tp __value)
133 _Distance __parent = (__holeIndex - 1) / 2;
134 while (__holeIndex > __topIndex && *(__first + __parent) < __value)
136 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
137 __holeIndex = __parent;
138 __parent = (__holeIndex - 1) / 2;
140 *(__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_heap(__first, __last - 1);
168 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
169 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
170 _DistanceType(0), _GLIBCXX_MOVE(__value));
173 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp,
176 __push_heap(_RandomAccessIterator __first, _Distance __holeIndex,
177 _Distance __topIndex, _Tp __value, _Compare __comp)
179 _Distance __parent = (__holeIndex - 1) / 2;
180 while (__holeIndex > __topIndex
181 && __comp(*(__first + __parent), __value))
183 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
184 __holeIndex = __parent;
185 __parent = (__holeIndex - 1) / 2;
187 *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
201 template<
typename _RandomAccessIterator,
typename _Compare>
203 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
206 typedef typename iterator_traits<_RandomAccessIterator>::value_type
208 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
212 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
213 _RandomAccessIterator>)
214 __glibcxx_requires_valid_range(__first, __last);
215 __glibcxx_requires_heap_pred(__first, __last - 1, __comp);
217 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
218 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
219 _DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
222 template<
typename _RandomAccessIterator,
typename _Distance,
typename _Tp>
224 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
225 _Distance __len, _Tp __value)
227 const _Distance __topIndex = __holeIndex;
228 _Distance __secondChild = __holeIndex;
229 while (__secondChild < (__len - 1) / 2)
231 __secondChild = 2 * (__secondChild + 1);
232 if (*(__first + __secondChild) < *(__first + (__secondChild - 1)))
234 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
235 __holeIndex = __secondChild;
237 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
239 __secondChild = 2 * (__secondChild + 1);
240 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
241 + (__secondChild - 1)));
242 __holeIndex = __secondChild - 1;
244 std::__push_heap(__first, __holeIndex, __topIndex,
245 _GLIBCXX_MOVE(__value));
248 template<
typename _RandomAccessIterator>
250 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
251 _RandomAccessIterator __result)
253 typedef typename iterator_traits<_RandomAccessIterator>::value_type
255 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
258 _ValueType __value = _GLIBCXX_MOVE(*__result);
259 *__result = _GLIBCXX_MOVE(*__first);
260 std::__adjust_heap(__first, _DistanceType(0),
261 _DistanceType(__last - __first),
262 _GLIBCXX_MOVE(__value));
274 template<
typename _RandomAccessIterator>
276 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
278 typedef typename iterator_traits<_RandomAccessIterator>::value_type
282 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
283 _RandomAccessIterator>)
284 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
285 __glibcxx_requires_valid_range(__first, __last);
286 __glibcxx_requires_heap(__first, __last);
289 std::__pop_heap(__first, __last, __last);
292 template<
typename _RandomAccessIterator,
typename _Distance,
293 typename _Tp,
typename _Compare>
295 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
296 _Distance __len, _Tp __value, _Compare __comp)
298 const _Distance __topIndex = __holeIndex;
299 _Distance __secondChild = __holeIndex;
300 while (__secondChild < (__len - 1) / 2)
302 __secondChild = 2 * (__secondChild + 1);
303 if (__comp(*(__first + __secondChild),
304 *(__first + (__secondChild - 1))))
306 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
307 __holeIndex = __secondChild;
309 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
311 __secondChild = 2 * (__secondChild + 1);
312 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
313 + (__secondChild - 1)));
314 __holeIndex = __secondChild - 1;
316 std::__push_heap(__first, __holeIndex, __topIndex,
317 _GLIBCXX_MOVE(__value), __comp);
320 template<
typename _RandomAccessIterator,
typename _Compare>
322 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
323 _RandomAccessIterator __result, _Compare __comp)
325 typedef typename iterator_traits<_RandomAccessIterator>::value_type
327 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
330 _ValueType __value = _GLIBCXX_MOVE(*__result);
331 *__result = _GLIBCXX_MOVE(*__first);
332 std::__adjust_heap(__first, _DistanceType(0),
333 _DistanceType(__last - __first),
334 _GLIBCXX_MOVE(__value), __comp);
348 template<
typename _RandomAccessIterator,
typename _Compare>
351 _RandomAccessIterator __last, _Compare __comp)
354 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
355 _RandomAccessIterator>)
356 __glibcxx_requires_valid_range(__first, __last);
357 __glibcxx_requires_heap_pred(__first, __last, __comp);
360 std::__pop_heap(__first, __last, __last, __comp);
371 template<
typename _RandomAccessIterator>
373 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
375 typedef typename iterator_traits<_RandomAccessIterator>::value_type
377 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
381 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
382 _RandomAccessIterator>)
383 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
384 __glibcxx_requires_valid_range(__first, __last);
386 if (__last - __first < 2)
389 const _DistanceType __len = __last - __first;
390 _DistanceType __parent = (__len - 2) / 2;
393 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
394 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value));
411 template<
typename _RandomAccessIterator,
typename _Compare>
413 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
416 typedef typename iterator_traits<_RandomAccessIterator>::value_type
418 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
422 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
423 _RandomAccessIterator>)
424 __glibcxx_requires_valid_range(__first, __last);
426 if (__last - __first < 2)
429 const _DistanceType __len = __last - __first;
430 _DistanceType __parent = (__len - 2) / 2;
433 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
434 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
450 template<
typename _RandomAccessIterator>
452 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
455 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
456 _RandomAccessIterator>)
457 __glibcxx_function_requires(_LessThanComparableConcept<
458 typename iterator_traits<_RandomAccessIterator>::value_type>)
459 __glibcxx_requires_valid_range(__first, __last);
460 __glibcxx_requires_heap(__first, __last);
462 while (__last - __first > 1)
465 std::__pop_heap(__first, __last, __last);
479 template<
typename _RandomAccessIterator,
typename _Compare>
481 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
485 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
486 _RandomAccessIterator>)
487 __glibcxx_requires_valid_range(__first, __last);
488 __glibcxx_requires_heap_pred(__first, __last, __comp);
490 while (__last - __first > 1)
493 std::__pop_heap(__first, __last, __last, __comp);
497 #ifdef __GXX_EXPERIMENTAL_CXX0X__
508 template<
typename _RandomAccessIterator>
509 inline _RandomAccessIterator
513 __glibcxx_function_requires(_RandomAccessIteratorConcept<
514 _RandomAccessIterator>)
515 __glibcxx_function_requires(_LessThanComparableConcept<
516 typename iterator_traits<_RandomAccessIterator>::value_type>)
517 __glibcxx_requires_valid_range(__first, __last);
519 return __first + std::__is_heap_until(__first,
std::distance(__first,
534 template<
typename _RandomAccessIterator,
typename _Compare>
535 inline _RandomAccessIterator
540 __glibcxx_function_requires(_RandomAccessIteratorConcept<
541 _RandomAccessIterator>)
542 __glibcxx_requires_valid_range(__first, __last);
544 return __first + std::__is_heap_until(__first,
std::distance(__first,
556 template<
typename _RandomAccessIterator>
558 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
569 template<
typename _RandomAccessIterator,
typename _Compare>
571 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
576 _GLIBCXX_END_NAMESPACE
void push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Push an element onto a heap using comparison functor.
ISO C++ entities toplevel namespace is std.
void make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Construct a heap over a range using comparison functor.
void sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Sort a heap using comparison functor.
void pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Pop an element off a heap using comparison functor.
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.
bool is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Determines whether a range is a heap using comparison functor.