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_heap(__first, __last - 1);
164 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
165 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
166 _DistanceType(0), _GLIBCXX_MOVE(__value),
167 __gnu_cxx::__ops::__iter_less_val());
182 template<
typename _RandomAccessIterator,
typename _Compare>
184 push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
187 typedef typename iterator_traits<_RandomAccessIterator>::value_type
189 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
193 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
194 _RandomAccessIterator>)
195 __glibcxx_requires_valid_range(__first, __last);
196 __glibcxx_requires_heap_pred(__first, __last - 1, __comp);
198 _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
199 std::__push_heap(__first, _DistanceType((__last - __first) - 1),
200 _DistanceType(0), _GLIBCXX_MOVE(__value),
201 __gnu_cxx::__ops::__iter_comp_val(__comp));
204 template<
typename _RandomAccessIterator,
typename _Distance,
205 typename _Tp,
typename _Compare>
207 __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
208 _Distance __len, _Tp __value, _Compare __comp)
210 const _Distance __topIndex = __holeIndex;
211 _Distance __secondChild = __holeIndex;
212 while (__secondChild < (__len - 1) / 2)
214 __secondChild = 2 * (__secondChild + 1);
215 if (__comp(__first + __secondChild,
216 __first + (__secondChild - 1)))
218 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
219 __holeIndex = __secondChild;
221 if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2)
223 __secondChild = 2 * (__secondChild + 1);
224 *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first
225 + (__secondChild - 1)));
226 __holeIndex = __secondChild - 1;
228 std::__push_heap(__first, __holeIndex, __topIndex,
229 _GLIBCXX_MOVE(__value),
230 __gnu_cxx::__ops::__iter_comp_val(__comp));
233 template<
typename _RandomAccessIterator,
typename _Compare>
235 __pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
236 _RandomAccessIterator __result, _Compare __comp)
238 typedef typename iterator_traits<_RandomAccessIterator>::value_type
240 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
243 _ValueType __value = _GLIBCXX_MOVE(*__result);
244 *__result = _GLIBCXX_MOVE(*__first);
245 std::__adjust_heap(__first, _DistanceType(0),
246 _DistanceType(__last - __first),
247 _GLIBCXX_MOVE(__value), __comp);
261 template<
typename _RandomAccessIterator>
263 pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
265 typedef typename iterator_traits<_RandomAccessIterator>::value_type
269 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
270 _RandomAccessIterator>)
271 __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
272 __glibcxx_requires_non_empty_range(__first, __last);
273 __glibcxx_requires_valid_range(__first, __last);
274 __glibcxx_requires_heap(__first, __last);
276 if (__last - __first > 1)
279 std::__pop_heap(__first, __last, __last,
280 __gnu_cxx::__ops::__iter_less_iter());
295 template<
typename _RandomAccessIterator,
typename _Compare>
297 pop_heap(_RandomAccessIterator __first,
298 _RandomAccessIterator __last, _Compare __comp)
301 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
302 _RandomAccessIterator>)
303 __glibcxx_requires_valid_range(__first, __last);
304 __glibcxx_requires_non_empty_range(__first, __last);
305 __glibcxx_requires_heap_pred(__first, __last, __comp);
307 if (__last - __first > 1)
310 std::__pop_heap(__first, __last, __last,
311 __gnu_cxx::__ops::__iter_comp_iter(__comp));
315 template<
typename _RandomAccessIterator,
typename _Compare>
317 __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
320 typedef typename iterator_traits<_RandomAccessIterator>::value_type
322 typedef typename iterator_traits<_RandomAccessIterator>::difference_type
325 if (__last - __first < 2)
328 const _DistanceType __len = __last - __first;
329 _DistanceType __parent = (__len - 2) / 2;
332 _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
333 std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value),
349 template<
typename _RandomAccessIterator>
351 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
354 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
355 _RandomAccessIterator>)
356 __glibcxx_function_requires(_LessThanComparableConcept<
357 typename iterator_traits<_RandomAccessIterator>::value_type>)
358 __glibcxx_requires_valid_range(__first, __last);
360 std::__make_heap(__first, __last,
361 __gnu_cxx::__ops::__iter_less_iter());
374 template<
typename _RandomAccessIterator,
typename _Compare>
376 make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
380 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
381 _RandomAccessIterator>)
382 __glibcxx_requires_valid_range(__first, __last);
384 std::__make_heap(__first, __last,
385 __gnu_cxx::__ops::__iter_comp_iter(__comp));
388 template<
typename _RandomAccessIterator,
typename _Compare>
390 __sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
393 while (__last - __first > 1)
396 std::__pop_heap(__first, __last, __last, __comp);
408 template<
typename _RandomAccessIterator>
410 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
413 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
414 _RandomAccessIterator>)
415 __glibcxx_function_requires(_LessThanComparableConcept<
416 typename iterator_traits<_RandomAccessIterator>::value_type>)
417 __glibcxx_requires_valid_range(__first, __last);
418 __glibcxx_requires_heap(__first, __last);
420 std::__sort_heap(__first, __last,
421 __gnu_cxx::__ops::__iter_less_iter());
434 template<
typename _RandomAccessIterator,
typename _Compare>
436 sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
440 __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
441 _RandomAccessIterator>)
442 __glibcxx_requires_valid_range(__first, __last);
443 __glibcxx_requires_heap_pred(__first, __last, __comp);
445 std::__sort_heap(__first, __last,
446 __gnu_cxx::__ops::__iter_comp_iter(__comp));
449 #if __cplusplus >= 201103L
460 template<
typename _RandomAccessIterator>
461 inline _RandomAccessIterator
462 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last)
465 __glibcxx_function_requires(_RandomAccessIteratorConcept<
466 _RandomAccessIterator>)
467 __glibcxx_function_requires(_LessThanComparableConcept<
468 typename iterator_traits<_RandomAccessIterator>::value_type>)
469 __glibcxx_requires_valid_range(__first, __last);
472 std::__is_heap_until(__first,
std::distance(__first, __last),
473 __gnu_cxx::__ops::__iter_less_iter());
487 template<
typename _RandomAccessIterator,
typename _Compare>
488 inline _RandomAccessIterator
489 is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last,
493 __glibcxx_function_requires(_RandomAccessIteratorConcept<
494 _RandomAccessIterator>)
495 __glibcxx_requires_valid_range(__first, __last);
498 + std::__is_heap_until(__first,
std::distance(__first, __last),
499 __gnu_cxx::__ops::__iter_comp_iter(__comp));
509 template<
typename _RandomAccessIterator>
511 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
522 template<
typename _RandomAccessIterator,
typename _Compare>
524 is_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
529 _GLIBCXX_END_NAMESPACE_VERSION
_RandomAccessIterator is_heap_until(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
Search the end of a heap using comparison functor.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.