Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1.   // Heap-manipulation functions: push_heap, pop_heap, make_heap, sort_heap,
  2.   // + is_heap and is_heap_until in C++0x.
  3.  
  4.   template<typename _RandomAccessIterator, typename _Distance, typename _Tp>
  5.     void
  6.     __push_heap(_RandomAccessIterator __first,
  7.                 _Distance __holeIndex, _Distance __topIndex, _Tp __value)
  8.     {
  9.       _Distance __parent = (__holeIndex - 1) / 2;
  10.       while (__holeIndex > __topIndex && *(__first + __parent) < __value)
  11.         {
  12.           *(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
  13.           __holeIndex = __parent;
  14.           __parent = (__holeIndex - 1) / 2;
  15.         }
  16.       *(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
  17.     }
  18.  
  19.   /**
  20.    *  @brief  Push an element onto a heap.
  21.    *  @param  first  Start of heap.
  22.    *  @param  last   End of heap + element.
  23.    *  @ingroup heap_algorithms
  24.    *
  25.    *  This operation pushes the element at last-1 onto the valid heap over the
  26.    *  range [first,last-1).  After completion, [first,last) is a valid heap.
  27.   */
  28.   template<typename _RandomAccessIterator>
  29.     inline void
  30.     push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
  31.     {
  32.       typedef typename iterator_traits<_RandomAccessIterator>::value_type
  33.           _ValueType;
  34.       typedef typename iterator_traits<_RandomAccessIterator>::difference_type
  35.           _DistanceType;
  36.  
  37.       // concept requirements
  38.       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
  39.             _RandomAccessIterator>)
  40.       __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
  41.       __glibcxx_requires_valid_range(__first, __last);
  42.       __glibcxx_requires_heap(__first, __last - 1);
  43.  
  44.       _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
  45.       std::__push_heap(__first, _DistanceType((__last - __first) - 1),
  46.                        _DistanceType(0), _GLIBCXX_MOVE(__value));
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement