[alg.heap.operations] (original) (raw)

26 Algorithms library [algorithms]

26.8.8 Heap operations [alg.heap.operations]


26.8.8.1 General [alg.heap.operations.general]

26.8.8.2 push_heap [push.heap]

26.8.8.3 pop_heap [pop.heap]

26.8.8.4 make_heap [make.heap]

26.8.8.5 sort_heap [sort.heap]

26.8.8.6 is_heap [is.heap]


26.8.8.1 General [alg.heap.operations.general]

A random access range [a, b) is aheap with respect to comp and projfor a comparator and projection comp and projif its elements are organized such that:

These properties make heaps useful as priority queues.

make_heap converts a range into a heap andsort_heap turns a heap into a sorted sequence.

26.8.8.2 push_heap [push.heap]

template<class RandomAccessIterator> constexpr void push_heap(RandomAccessIterator first, RandomAccessIterator last);template<class RandomAccessIterator, class Compare> constexpr void push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);template<[random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class Comp = ranges::less,class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]")<I, Comp, Proj> constexpr I ranges::push_heap(I first, S last, Comp comp = {}, Proj proj = {});template<[random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]")<iterator_t<R>, Comp, Proj> constexpr borrowed_iterator_t<R> ranges::push_heap(R&& r, Comp comp = {}, Proj proj = {});

Let comp be less{}and proj be identity{}for the overloads with no parameters by those names.

Preconditions: The range [first, last - 1) is a valid heap with respect to comp and proj.

Effects: Places the value in the location last - 1into the resulting heap [first, last).

Returns: last for the overloads in namespace ranges.

Complexity: At most comparisons and twice as many projections.

26.8.8.3 pop_heap [pop.heap]

template<class RandomAccessIterator> constexpr void pop_heap(RandomAccessIterator first, RandomAccessIterator last);template<class RandomAccessIterator, class Compare> constexpr void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);template<[random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class Comp = ranges::less,class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]")<I, Comp, Proj> constexpr I ranges::pop_heap(I first, S last, Comp comp = {}, Proj proj = {});template<[random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]")<iterator_t<R>, Comp, Proj> constexpr borrowed_iterator_t<R> ranges::pop_heap(R&& r, Comp comp = {}, Proj proj = {});

Let comp be less{}and proj be identity{}for the overloads with no parameters by those names.

Preconditions: The range [first, last) is a valid non-empty heap with respect to comp and proj.

Effects: Swaps the value in the location firstwith the value in the locationlast - 1and makes [first, last - 1) into a heap with respect to comp and proj.

Returns: last for the overloads in namespace ranges.

Complexity: At most comparisons and twice as many projections.

26.8.8.4 make_heap [make.heap]

template<class RandomAccessIterator> constexpr void make_heap(RandomAccessIterator first, RandomAccessIterator last);template<class RandomAccessIterator, class Compare> constexpr void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);template<[random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class Comp = ranges::less,class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]")<I, Comp, Proj> constexpr I ranges::make_heap(I first, S last, Comp comp = {}, Proj proj = {});template<[random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]")<iterator_t<R>, Comp, Proj> constexpr borrowed_iterator_t<R> ranges::make_heap(R&& r, Comp comp = {}, Proj proj = {});

Let comp be less{}and proj be identity{}for the overloads with no parameters by those names.

Effects: Constructs a heap with respect to comp and projout of the range [first, last).

Returns: last for the overloads in namespace ranges.

Complexity: At most 3(last - first) comparisons and twice as many projections.

26.8.8.5 sort_heap [sort.heap]

template<class RandomAccessIterator> constexpr void sort_heap(RandomAccessIterator first, RandomAccessIterator last);template<class RandomAccessIterator, class Compare> constexpr void sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);template<[random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class Comp = ranges::less,class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]")<I, Comp, Proj> constexpr I ranges::sort_heap(I first, S last, Comp comp = {}, Proj proj = {});template<[random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Comp = ranges::less, class Proj = identity> requires [sortable](alg.req.sortable#concept:sortable "24.3.7.8 Concept sortable [alg.req.sortable]")<iterator_t<R>, Comp, Proj> constexpr borrowed_iterator_t<R> ranges::sort_heap(R&& r, Comp comp = {}, Proj proj = {});

Let comp be less{}and proj be identity{}for the overloads with no parameters by those names.

Preconditions: The range [first, last) is a valid heap with respect to comp and proj.

Effects: Sorts elements in the heap [first, last) with respect to comp and proj.

Returns: last for the overloads in namespace ranges.

Complexity: At most comparisons, where , and twice as many projections.

26.8.8.6 is_heap [is.heap]

template<class RandomAccessIterator> constexpr bool is_heap(RandomAccessIterator first, RandomAccessIterator last);

Effects: Equivalent to: return is_heap_until(first, last) == last;

template<class ExecutionPolicy, class RandomAccessIterator> bool is_heap(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last);

Effects: Equivalent to:return is_heap_until(std::forward<ExecutionPolicy>(exec), first, last) == last;

template<class RandomAccessIterator, class Compare> constexpr bool is_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);

Effects: Equivalent to: return is_heap_until(first, last, comp) == last;

template<class ExecutionPolicy, class RandomAccessIterator, class Compare> bool is_heap(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last, Compare comp);

Effects: Equivalent to:return is_heap_until(std::forward<ExecutionPolicy>(exec), first, last, comp) == last;

template<[random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> constexpr bool ranges::is_heap(I first, S last, Comp comp = {}, Proj proj = {});template<[random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> constexpr bool ranges::is_heap(R&& r, Comp comp = {}, Proj proj = {});

Effects: Equivalent to:return ranges​::​is_heap_until(first, last, comp, proj) == last;

template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<I> S,class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> bool ranges::is_heap(Ep&& exec, I first, S last, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [_sized-random-access-range_](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> bool ranges::is_heap(Ep&& exec, R&& r, Comp comp = {}, Proj proj = {});

Effects: Equivalent to:return ranges::is_heap_until(std::forward<Ep>(exec), first, last, comp, proj) == last;

template<class RandomAccessIterator> constexpr RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessIterator last);template<class ExecutionPolicy, class RandomAccessIterator> RandomAccessIterator is_heap_until(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last);template<class RandomAccessIterator, class Compare> constexpr RandomAccessIterator is_heap_until(RandomAccessIterator first, RandomAccessIterator last, Compare comp);template<class ExecutionPolicy, class RandomAccessIterator, class Compare> RandomAccessIterator is_heap_until(ExecutionPolicy&& exec, RandomAccessIterator first, RandomAccessIterator last, Compare comp);template<[random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "24.3.4.7 Concept sentinel_­for [iterator.concept.sentinel]")<I> S, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> constexpr I ranges::is_heap_until(I first, S last, Comp comp = {}, Proj proj = {});template<[random_access_range](range.refinements#concept:random%5Faccess%5Frange "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> constexpr borrowed_iterator_t<R> ranges::is_heap_until(R&& r, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "24.3.4.13 Concept random_­access_­iterator [iterator.concept.random.access]") I, [sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "24.3.4.8 Concept sized_­sentinel_­for [iterator.concept.sizedsentinel]")<I> S,class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<I, Proj>> Comp = ranges::less> I ranges::is_heap_until(Ep&& exec, I first, S last, Comp comp = {}, Proj proj = {});template<[_execution-policy_](algorithms.parallel.defns#concept:execution-policy "26.3.1 Preamble [algorithms.parallel.defns]") Ep, [_sized-random-access-range_](range.refinements#concept:sized-random-access-range "25.4.6 Other range refinements [range.refinements]") R, class Proj = identity,[indirect_strict_weak_order](indirectcallable.indirectinvocable#concept:indirect%5Fstrict%5Fweak%5Forder "24.3.6.3 Indirect callables [indirectcallable.indirectinvocable]")<projected<iterator_t<R>, Proj>> Comp = ranges::less> borrowed_iterator_t<R> ranges::is_heap_until(Ep&& exec, R&& r, Comp comp = {}, Proj proj = {});

Let comp be less{}and proj be identity{}for the overloads with no parameters by those names.

Returns: The last iterator i in [first, last] for which the range [first, i) is a heap with respect to comp and proj.