37 #include <EASTL/internal/config.h>
38 #include <EASTL/iterator.h>
41 #if defined(EA_PRAGMA_ONCE_SUPPORTED)
54 template <
typename RandomAccessIterator,
typename Distance,
typename T,
typename ValueType>
55 inline void promote_heap_impl(RandomAccessIterator first, Distance topPosition, Distance position, T value)
57 for(Distance parentPosition = (position - 1) >> 1;
59 parentPosition = (position - 1) >> 1)
61 *(first + position) = eastl::forward<ValueType>(*(first + parentPosition));
62 position = parentPosition;
65 *(first + position) = eastl::forward<ValueType>(value);
79 template <
typename RandomAccessIterator,
typename Distance,
typename T>
80 inline void promote_heap(RandomAccessIterator first, Distance topPosition, Distance position,
const T& value)
82 typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
83 promote_heap_impl<RandomAccessIterator, Distance, const T&, const value_type>(first, topPosition, position, value);
98 template <
typename RandomAccessIterator,
typename Distance,
typename T>
99 inline void promote_heap(RandomAccessIterator first, Distance topPosition, Distance position, T&& value)
101 typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
102 promote_heap_impl<RandomAccessIterator, Distance, T&&, value_type>(first, topPosition, position, eastl::forward<T>(value));
106 template <
typename RandomAccessIterator,
typename Distance,
typename T,
typename Compare,
typename ValueType>
107 inline void promote_heap_impl(RandomAccessIterator first, Distance topPosition, Distance position, T value, Compare compare)
109 for(Distance parentPosition = (position - 1) >> 1;
110 (position > topPosition) && compare(*(first + parentPosition), value);
111 parentPosition = (position - 1) >> 1)
113 *(first + position) = eastl::forward<ValueType>(*(first + parentPosition));
114 position = parentPosition;
117 *(first + position) = eastl::forward<ValueType>(value);
132 template <
typename RandomAccessIterator,
typename Distance,
typename T,
typename Compare>
133 inline void promote_heap(RandomAccessIterator first, Distance topPosition, Distance position,
const T& value, Compare compare)
135 typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
136 promote_heap_impl<RandomAccessIterator, Distance, const T&, Compare, const value_type>(first, topPosition, position, value, compare);
151 template <
typename RandomAccessIterator,
typename Distance,
typename T,
typename Compare>
152 inline void promote_heap(RandomAccessIterator first, Distance topPosition, Distance position, T&& value, Compare compare)
154 typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
155 promote_heap_impl<RandomAccessIterator, Distance, T&&, Compare, value_type>(first, topPosition, position, eastl::forward<T>(value), compare);
164 template <
typename RandomAccessIterator,
typename Distance,
typename T,
typename ValueType>
165 void adjust_heap_impl(RandomAccessIterator first, Distance topPosition, Distance heapSize, Distance position, T value)
169 Distance childPosition = (2 * position) + 2;
171 for(; childPosition < heapSize; childPosition = (2 * childPosition) + 2)
175 *(first + position) = eastl::forward<ValueType>(*(first + childPosition));
176 position = childPosition;
179 if(childPosition == heapSize)
181 *(first + position) = eastl::forward<ValueType>(*(first + (childPosition - 1)));
182 position = childPosition - 1;
185 eastl::promote_heap<RandomAccessIterator, Distance, T>(first, topPosition, position, eastl::forward<ValueType>(value));
198 template <
typename RandomAccessIterator,
typename Distance,
typename T>
199 void adjust_heap(RandomAccessIterator first, Distance topPosition, Distance heapSize, Distance position,
const T& value)
201 typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
202 adjust_heap_impl<RandomAccessIterator, Distance, const T&, const value_type>(first, topPosition, heapSize, position, eastl::forward<const T&>(value));
216 template <
typename RandomAccessIterator,
typename Distance,
typename T>
217 void adjust_heap(RandomAccessIterator first, Distance topPosition, Distance heapSize, Distance position, T&& value)
219 typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
220 adjust_heap_impl<RandomAccessIterator, Distance, T&&, value_type>(first, topPosition, heapSize, position, eastl::forward<T>(value));
224 template <
typename RandomAccessIterator,
typename Distance,
typename T,
typename Compare,
typename ValueType>
225 void adjust_heap_impl(RandomAccessIterator first, Distance topPosition, Distance heapSize, Distance position, T value, Compare compare)
229 Distance childPosition = (2 * position) + 2;
231 for(; childPosition < heapSize; childPosition = (2 * childPosition) + 2)
233 if(compare(*(first + childPosition), *(first + (childPosition - 1))))
235 *(first + position) = eastl::forward<ValueType>(*(first + childPosition));
236 position = childPosition;
239 if(childPosition == heapSize)
241 *(first + position) = eastl::forward<ValueType>(*(first + (childPosition - 1)));
242 position = childPosition - 1;
245 eastl::promote_heap<RandomAccessIterator, Distance, T, Compare>(first, topPosition, position, eastl::forward<ValueType>(value), compare);
256 template <
typename RandomAccessIterator,
typename Distance,
typename T,
typename Compare>
257 void adjust_heap(RandomAccessIterator first, Distance topPosition, Distance heapSize, Distance position,
const T& value, Compare compare)
259 typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
260 adjust_heap_impl<RandomAccessIterator, Distance, const T&, Compare, const value_type>(first, topPosition, heapSize, position, eastl::forward<const T&>(value), compare);
272 template <
typename RandomAccessIterator,
typename Distance,
typename T,
typename Compare>
273 void adjust_heap(RandomAccessIterator first, Distance topPosition, Distance heapSize, Distance position, T&& value, Compare compare)
275 typedef typename iterator_traits<RandomAccessIterator>::value_type value_type;
276 adjust_heap_impl<RandomAccessIterator, Distance, T&&, Compare, value_type>(first, topPosition, heapSize, position, eastl::forward<T>(value), compare);
296 template <
typename RandomAccessIterator>
297 inline void push_heap(RandomAccessIterator first, RandomAccessIterator last)
299 typedef typename eastl::iterator_traits<RandomAccessIterator>::difference_type difference_type;
300 typedef typename eastl::iterator_traits<RandomAccessIterator>::value_type value_type;
302 const value_type tempBottom(eastl::forward<value_type>(*(last - 1)));
304 eastl::promote_heap<RandomAccessIterator, difference_type, value_type>
305 (first, (difference_type)0, (difference_type)(last - first - 1), eastl::forward<const value_type>(tempBottom));
319 template <
typename RandomAccessIterator,
typename Compare>
320 inline void push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare compare)
322 typedef typename eastl::iterator_traits<RandomAccessIterator>::difference_type difference_type;
323 typedef typename eastl::iterator_traits<RandomAccessIterator>::value_type value_type;
325 const value_type tempBottom(*(last - 1));
327 eastl::promote_heap<RandomAccessIterator, difference_type, value_type, Compare>
328 (first, (difference_type)0, (difference_type)(last - first - 1), tempBottom, compare);
353 template <
typename RandomAccessIterator>
354 inline void pop_heap(RandomAccessIterator first, RandomAccessIterator last)
356 typedef typename eastl::iterator_traits<RandomAccessIterator>::difference_type difference_type;
357 typedef typename eastl::iterator_traits<RandomAccessIterator>::value_type value_type;
359 value_type tempBottom(eastl::forward<value_type>(*(last - 1)));
360 *(last - 1) = eastl::forward<value_type>(*first);
361 eastl::adjust_heap<RandomAccessIterator, difference_type, value_type>
362 (first, (difference_type)0, (difference_type)(last - first - 1), 0, eastl::forward<value_type>(tempBottom));
377 template <
typename RandomAccessIterator,
typename Compare>
378 inline void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare compare)
380 typedef typename eastl::iterator_traits<RandomAccessIterator>::difference_type difference_type;
381 typedef typename eastl::iterator_traits<RandomAccessIterator>::value_type value_type;
383 value_type tempBottom(eastl::forward<value_type>(*(last - 1)));
384 *(last - 1) = eastl::forward<value_type>(*first);
385 eastl::adjust_heap<RandomAccessIterator, difference_type, value_type, Compare>
386 (first, (difference_type)0, (difference_type)(last - first - 1), 0, eastl::forward<value_type>(tempBottom), compare);
401 template <
typename RandomAccessIterator>
402 void make_heap(RandomAccessIterator first, RandomAccessIterator last)
405 typedef typename eastl::iterator_traits<RandomAccessIterator>::difference_type difference_type;
406 typedef typename eastl::iterator_traits<RandomAccessIterator>::value_type value_type;
408 const difference_type heapSize = last - first;
412 difference_type parentPosition = ((heapSize - 2) >> 1) + 1;
416 value_type temp(eastl::forward<value_type>(*(first + parentPosition)));
417 eastl::adjust_heap<RandomAccessIterator, difference_type, value_type>
418 (first, parentPosition, heapSize, parentPosition, eastl::forward<value_type>(temp));
419 }
while(parentPosition != 0);
424 template <
typename RandomAccessIterator,
typename Compare>
425 void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare compare)
427 typedef typename eastl::iterator_traits<RandomAccessIterator>::difference_type difference_type;
428 typedef typename eastl::iterator_traits<RandomAccessIterator>::value_type value_type;
430 const difference_type heapSize = last - first;
434 difference_type parentPosition = ((heapSize - 2) >> 1) + 1;
438 value_type temp(eastl::forward<value_type>(*(first + parentPosition)));
439 eastl::adjust_heap<RandomAccessIterator, difference_type, value_type, Compare>
440 (first, parentPosition, heapSize, parentPosition, eastl::forward<value_type>(temp), compare);
441 }
while(parentPosition != 0);
462 template <
typename RandomAccessIterator>
463 inline void sort_heap(RandomAccessIterator first, RandomAccessIterator last)
465 for(; (last - first) > 1; --last)
466 eastl::pop_heap<RandomAccessIterator>(first, last);
475 template <
typename RandomAccessIterator,
typename Compare>
476 inline void sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare compare)
478 for(; (last - first) > 1; --last)
479 eastl::pop_heap<RandomAccessIterator, Compare>(first, last, compare);
500 template <
typename RandomAccessIterator,
typename Distance>
501 inline void remove_heap(RandomAccessIterator first, Distance heapSize, Distance position)
503 typedef typename eastl::iterator_traits<RandomAccessIterator>::difference_type difference_type;
504 typedef typename eastl::iterator_traits<RandomAccessIterator>::value_type value_type;
506 const value_type tempBottom(*(first + heapSize - 1));
507 *(first + heapSize - 1) = *(first + position);
508 eastl::adjust_heap<RandomAccessIterator, difference_type, value_type>
509 (first, (difference_type)0, (difference_type)(heapSize - 1), (difference_type)position, tempBottom);
523 template <
typename RandomAccessIterator,
typename Distance,
typename Compare>
524 inline void remove_heap(RandomAccessIterator first, Distance heapSize, Distance position, Compare compare)
526 typedef typename eastl::iterator_traits<RandomAccessIterator>::difference_type difference_type;
527 typedef typename eastl::iterator_traits<RandomAccessIterator>::value_type value_type;
529 const value_type tempBottom(*(first + heapSize - 1));
530 *(first + heapSize - 1) = *(first + position);
531 eastl::adjust_heap<RandomAccessIterator, difference_type, value_type, Compare>
532 (first, (difference_type)0, (difference_type)(heapSize - 1), (difference_type)position, tempBottom, compare);
547 template <
typename RandomAccessIterator,
typename Distance>
548 inline void change_heap(RandomAccessIterator first, Distance heapSize, Distance position)
550 typedef typename eastl::iterator_traits<RandomAccessIterator>::difference_type difference_type;
551 typedef typename eastl::iterator_traits<RandomAccessIterator>::value_type value_type;
553 eastl::remove_heap<RandomAccessIterator, Distance>(first, heapSize, position);
555 value_type tempBottom(*(first + heapSize - 1));
557 eastl::promote_heap<RandomAccessIterator, difference_type, value_type>
558 (first, (difference_type)0, (difference_type)(heapSize - 1), tempBottom);
567 template <
typename RandomAccessIterator,
typename Distance,
typename Compare>
568 inline void change_heap(RandomAccessIterator first, Distance heapSize, Distance position, Compare compare)
570 typedef typename eastl::iterator_traits<RandomAccessIterator>::difference_type difference_type;
571 typedef typename eastl::iterator_traits<RandomAccessIterator>::value_type value_type;
573 eastl::remove_heap<RandomAccessIterator, Distance, Compare>(first, heapSize, position, compare);
575 value_type tempBottom(*(first + heapSize - 1));
577 eastl::promote_heap<RandomAccessIterator, difference_type, value_type, Compare>
578 (first, (difference_type)0, (difference_type)(heapSize - 1), tempBottom, compare);
589 template <
typename RandomAccessIterator>
590 inline RandomAccessIterator
is_heap_until(RandomAccessIterator first, RandomAccessIterator last)
594 for(RandomAccessIterator child = first + 1; child < last; ++child, counter ^= 1)
610 template <
typename RandomAccessIterator,
typename Compare>
611 inline RandomAccessIterator
is_heap_until(RandomAccessIterator first, RandomAccessIterator last, Compare compare)
615 for(RandomAccessIterator child = first + 1; child < last; ++child, counter ^= 1)
617 if(compare(*first, *child))
636 template <
typename RandomAccessIterator>
637 inline bool is_heap(RandomAccessIterator first, RandomAccessIterator last)
648 template <
typename RandomAccessIterator,
typename Compare>
649 inline bool is_heap(RandomAccessIterator first, RandomAccessIterator last, Compare compare)
EA Standard Template Library.
Definition: algorithm.h:288
void sort_heap(RandomAccessIterator first, RandomAccessIterator last)
Definition: heap.h:463
void promote_heap(RandomAccessIterator first, Distance topPosition, Distance position, const T &value)
Definition: heap.h:80
void change_heap(RandomAccessIterator first, Distance heapSize, Distance position)
Definition: heap.h:548
bool is_heap(RandomAccessIterator first, RandomAccessIterator last)
Definition: heap.h:637
void adjust_heap(RandomAccessIterator first, Distance topPosition, Distance heapSize, Distance position, const T &value)
Definition: heap.h:199
void make_heap(RandomAccessIterator first, RandomAccessIterator last)
Definition: heap.h:402
RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessIterator last)
Definition: heap.h:590
void push_heap(RandomAccessIterator first, RandomAccessIterator last)
Definition: heap.h:297
void pop_heap(RandomAccessIterator first, RandomAccessIterator last)
Definition: heap.h:354
void remove_heap(RandomAccessIterator first, Distance heapSize, Distance position)
Definition: heap.h:501
less<T>
Definition: functional_base.h:202