Simple Application Framework
1
|
The heap data structure and related operations. More...
Functions | |
template<class RanIter , class Pred > | |
void | BalanceDown (RanIter begin, Diff node, Diff bottom, Pred pred) |
Percolate the element node to the bottom of a heap. | |
template<class RanIter , class Pred > | |
void | BalanceUp (RanIter begin, Diff top, Diff node, Pred pred) |
Percolate the element node up in the heap if the heap property is not satisfied. | |
template<class RanIter , class Pred > | |
bool | IsHeap (RanIter begin, RanIter end) |
Check whether the range [begin, end) is a max-heap. | |
template<class RanIter > | |
bool | IsHeap (RanIter begin, RanIter end) |
Check whether the range [begin, end) is a max-heap. | |
template<class RanIter , class Pred > | |
void | MakeHeap (RanIter begin, RanIter end, Pred pred) |
Convert the range [begin, end) to a max-heap. | |
template<class RanIter > | |
void | MakeHeap (RanIter begin, RanIter end) |
Convert the range [begin, end) to a max-heap. | |
template<class RanIter , class Pred > | |
void | PopHeap (RanIter begin, RanIter end, Pred pred) |
Pop the maximum from a heap. | |
template<class RanIter , class Pred > | |
void | PopHeap (RanIter begin, RanIter end) |
Pop the maximum from a heap. | |
template<class RanIter , class Pred > | |
void | PushHeap (RanIter begin, RanIter end, Pred pred) |
Insert a new element to a heap. | |
template<class RanIter > | |
void | PushHeap (RanIter begin, RanIter end) |
Insert a new element to a heap. | |
template<class RanIter , class Pred > | |
void | SortHeap (RanIter begin, RanIter end, Pred pred) |
Sort the elements in a heap range [begin, end) . | |
template<class RanIter > | |
void | SortHeap (RanIter begin, RanIter end) |
Sort the elements in a heap range [begin, end) . |
The heap data structure and related operations.
void Saf::Algo::Struct::Heap::BalanceDown | ( | RanIter | begin, |
Diff | node, | ||
Diff | bottom, | ||
Pred | pred | ||
) | [inline] |
Percolate the element node
to the bottom of a heap.
The element at index node
is moved down to the very bottom of the heap (to a leaf node). In each iteration it exchanges its position with the larger of its two childs.
[in] | begin | Random-access iterator to the beginning of the sequence containing the heap (the root element). |
[in] | node | The index of the element to be moved down. |
[in] | bottom | The index of the element one behind the last element in the heap. |
[in] | pred | Binary comparison predicate. |
void Saf::Algo::Struct::Heap::BalanceUp | ( | RanIter | begin, |
Diff | top, | ||
Diff | node, | ||
Pred | pred | ||
) | [inline] |
Percolate the element node
up in the heap if the heap property is not satisfied.
The element at index node
is moved up in the heap while it does not satisfy the heap property, i.e., while pred(begin[node], begin[parent]) == true
. The percolation stops at index top
.
[in] | begin | Random-access iterator to the beginning of the sequence containing the heap (the root element). |
[in] | top | Sentinel index, i.e., the root of the subtree in which the operation is performed. The node is not pushed above this element in the heap. |
[in] | node | The index of the element to be moved up. |
[in] | pred | Binary comparison predicate. |
bool Saf::Algo::Struct::Heap::IsHeap | ( | RanIter | begin, |
RanIter | end | ||
) | [inline] |
Check whether the range [begin, end)
is a max-heap.
For any index i > 0
of element in the range [begin, end)
the property !pred(begin[(i - 1) / 2], begin[i])
must be satisfied for the range to be a max-heap.
Properties:
[in] | begin | Iterator to the beginning of the sequence. |
[in] | end | Iterator to the end of the sequence. |
[in] | pred | Binary comparison predicate. |
bool Saf::Algo::Struct::Heap::IsHeap | ( | RanIter | begin, |
RanIter | end | ||
) | [inline] |
Check whether the range [begin, end)
is a max-heap.
For any index i > 0
of element in the range [begin, end)
the property begin[(i - 1) / 2] < begin[i]
must be satisfied for the range to be a max-heap.
Properties:
[in] | begin | Iterator to the beginning of the sequence. |
[in] | end | Iterator to the end of the sequence. |
void Saf::Algo::Struct::Heap::MakeHeap | ( | RanIter | begin, |
RanIter | end, | ||
Pred | pred | ||
) | [inline] |
Convert the range [begin, end)
to a max-heap.
The elements in the range [begin, end)
are reordered such that for any index i > 0
in the range it holds that pred(begin[(i - 1) / 2], begin[i]) == false
.
Properties:
[in] | begin | Iterator to the beginning of the sequence. |
[in] | end | Iterator to the end of the sequence. |
[in] | pred | Binary comparison predicate. |
void Saf::Algo::Struct::Heap::MakeHeap | ( | RanIter | begin, |
RanIter | end | ||
) | [inline] |
Convert the range [begin, end)
to a max-heap.
The elements in the range [begin, end)
are reordered such that for any index i > 0
in the range it holds that !(begin[(i - 1) / 2] < begin[i])
.
Properties:
[in] | begin | Iterator to the beginning of the sequence. |
[in] | end | Iterator to the end of the sequence. |
void Saf::Algo::Struct::Heap::PopHeap | ( | RanIter | begin, |
RanIter | end, | ||
Pred | pred | ||
) | [inline] |
Pop the maximum from a heap.
Extracts the maximum from a heap range [begin, end)
. The maximum is placed at the position end - 1
and the heap property in [begin, end - 1)
is restored.
Properties:
[in] | begin | Iterator to the beginning of the sequence. |
[in] | end | Iterator to the end of the sequence. |
[in] | pred | Binary comparison predicate. |
void Saf::Algo::Struct::Heap::PopHeap | ( | RanIter | begin, |
RanIter | end | ||
) | [inline] |
Pop the maximum from a heap.
Extracts the maximum from a heap range [begin, end)
. The maximum is placed at the position end - 1
and the heap property in [begin, end - 1)
is restored.
Properties:
[in] | begin | Iterator to the beginning of the sequence. |
[in] | end | Iterator to the end of the sequence. |
void Saf::Algo::Struct::Heap::PushHeap | ( | RanIter | begin, |
RanIter | end, | ||
Pred | pred | ||
) | [inline] |
Insert a new element to a heap.
This function inserts a new element into a heap. The inserted element is *(end - 1)
and it is assumed that the range [begin, end - 1)
is already a heap.
Properties:
[in] | begin | Iterator to the beginning of the sequence. |
[in] | end | Iterator to the end of the sequence. |
[in] | pred | Binary comparison predicate. |
void Saf::Algo::Struct::Heap::PushHeap | ( | RanIter | begin, |
RanIter | end | ||
) | [inline] |
Insert a new element to a heap.
This function inserts a new element into a heap. The inserted element is *(end - 1)
and it is assumed that the range [begin, end - 1)
is already a heap.
Properties:
[in] | begin | Iterator to the beginning of the sequence. |
[in] | end | Iterator to the end of the sequence. |
void Saf::Algo::Struct::Heap::SortHeap | ( | RanIter | begin, |
RanIter | end, | ||
Pred | pred | ||
) | [inline] |
Sort the elements in a heap range [begin, end)
.
Assuming the range [begin, end)
is a heap the elements in the range are sorted according to the pred
predicate.
Properties:
[in] | begin | Iterator to the beginning of the sequence. |
[in] | end | Iterator to the end of the sequence. |
[in] | pred | Binary comparison predicate. |
void Saf::Algo::Struct::Heap::SortHeap | ( | RanIter | begin, |
RanIter | end | ||
) | [inline] |
Sort the elements in a heap range [begin, end)
.
Assuming the range [begin, end)
is a heap the elements in the range are sorted in an increasing order.
Properties:
[in] | begin | Iterator to the beginning of the sequence. |
[in] | end | Iterator to the end of the sequence. |