|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Pattern-defeating quicksort, the algorithm of introsort(). More...
Classes | |
| struct | Partitioned |
| What a partition did: the place of the pivot, whether the range was already partitioned, and how many pairs of items it swapped. More... | |
Typedefs | |
| template<typename T > | |
| using | Pivot_Ref = std::conditional_t< std::is_trivially_copyable_v< T >, const T, const T & > |
| The pivot as a partition reads it: a copy of a trivially copyable item, which the compiler can keep in a register, and else a reference to the item, which stays in its place. | |
Functions | |
| template<typename T , class Compare > | |
| void | sort2 (T *a, T *b, const Compare &cmp) |
Swap *a and *b if *b is less than *a. | |
| template<typename T , class Compare > | |
| void | sort3 (T *a, T *b, T *c, const Compare &cmp) |
Sort *a, *b and *c. | |
| template<bool Guarded, typename T , class Compare > | |
| T * | insert_back (T *begin, T *cur, const Compare &cmp) |
Move *cur, which is less than *(cur - 1), back into the sorted range [begin, cur), after the items not greater than it, and return its new place. | |
| template<bool Guarded, typename T , class Compare > | |
| void | insertion_sort_range (T *begin, T *end, const Compare &cmp) |
Sort [begin, end) by insertion. | |
| template<typename T , class Compare > | |
| bool | partial_insertion_sort_range (T *begin, T *end, const Compare &cmp) |
Sort [begin, end) by insertion, unless that takes more than Partial_Insertion_Limit moves. | |
| template<typename T > | |
| void | swap_offsets (T *first, T *last, const unsigned char *offsets_l, const unsigned char *offsets_r, const long num, const bool use_swaps) |
Move the num pairs of items on the wrong sides, at first + offsets_l[i] and at last - offsets_r[i]. | |
| template<typename T , class Compare > | |
| Partitioned< T > | partition_by_blocks (T *begin, T *end, const Compare &cmp) |
Partition [begin, end) around the pivot *begin by blocks: the items less than the pivot go before it, and the others after it. | |
| template<typename T , class Compare > | |
| Partitioned< T > | partition_by_branches (T *begin, T *end, const Compare &cmp) |
Partition [begin, end) around the pivot *begin with branches, as partition_by_blocks() does by blocks. | |
| template<typename T , class Compare > | |
| T * | partition_left (T *begin, T *end, const Compare &cmp) |
Partition [begin, end) around the pivot *begin when it equals the item before begin, which no item of the range is less than: the items equal to the pivot go before it, where they are in their places, and the greater ones after it. | |
| template<typename T , class Compare > | |
| void | sort_loop (T *begin, T *end, const Compare &cmp, int bad_allowed, bool leftmost, bool by_blocks) |
Sort [begin, end): the loop of introsort(). | |
Variables | |
| constexpr long | Insertion_Threshold = 24 |
| Ranges shorter than this one are sorted by insertion. | |
| constexpr long | Ninther_Threshold = 128 |
| Ranges longer than this one take their pivot from nine items. | |
| constexpr long | Partial_Insertion_Limit = 8 |
| Moves after which the insertion sort of a range that a partition found in order gives up. | |
| constexpr long | Block_Size = 64 |
| Items that a partition by blocks compares at a time. | |
| constexpr long | Few_Swaps_Ratio = 16 |
| A partition that swaps fewer than one item in this many of its range leaves its subranges to the partition with branches: they are nearly in order, and the processor predicts those branches. | |
Pattern-defeating quicksort, the algorithm of introsort().
O. R. L. Peters, "Pattern-defeating Quicksort" (2021), with the partition by blocks of S. Edelkamp and A. Weiss, "BlockQuicksort: How Branch Mispredictions don't affect Quicksort" (2016).
| using Aleph::pdq_detail::Pivot_Ref = typedef std::conditional_t<std::is_trivially_copyable_v<T>, const T, const T &> |
The pivot as a partition reads it: a copy of a trivially copyable item, which the compiler can keep in a register, and else a reference to the item, which stays in its place.
Definition at line 1964 of file tpl_sort_utils.H.
|
inline |
Move *cur, which is less than *(cur - 1), back into the sorted range [begin, cur), after the items not greater than it, and return its new place.
Without Guarded, an item before begin not greater than any item of the range stops the scan. If cmp throws, the item goes back to the hole that it left, so that the range keeps its items.
Definition at line 2003 of file tpl_sort_utils.H.
References Aleph::and, and cmp().
| void Aleph::pdq_detail::insertion_sort_range | ( | T * | begin, |
| T * | end, | ||
| const Compare & | cmp | ||
| ) |
Sort [begin, end) by insertion.
Without Guarded, the range needs an item before begin not greater than any of its items.
Definition at line 2031 of file tpl_sort_utils.H.
References cmp().
| bool Aleph::pdq_detail::partial_insertion_sort_range | ( | T * | begin, |
| T * | end, | ||
| const Compare & | cmp | ||
| ) |
Sort [begin, end) by insertion, unless that takes more than Partial_Insertion_Limit moves.
Definition at line 2047 of file tpl_sort_utils.H.
References cmp(), and Partial_Insertion_Limit.
Referenced by sort_loop().
| Partitioned< T > Aleph::pdq_detail::partition_by_blocks | ( | T * | begin, |
| T * | end, | ||
| const Compare & | cmp | ||
| ) |
Partition [begin, end) around the pivot *begin by blocks: the items less than the pivot go before it, and the others after it.
It compares blocks of up to Block_Size items with the pivot, with no branch on the result, records the offsets of the items on the wrong side, and swaps them with those of a block of the other side. The pivot stays at begin until the end, so that the range keeps its items if cmp throws. The median that chose the pivot leaves after it an item not less than it, which stops the first scan.
Definition at line 2105 of file tpl_sort_utils.H.
References Aleph::and, Block_Size, cmp(), and swap_offsets().
Referenced by sort_loop().
| Partitioned< T > Aleph::pdq_detail::partition_by_branches | ( | T * | begin, |
| T * | end, | ||
| const Compare & | cmp | ||
| ) |
Partition [begin, end) around the pivot *begin with branches, as partition_by_blocks() does by blocks.
The processor predicts its branches when few items are on the wrong side.
Definition at line 2200 of file tpl_sort_utils.H.
References Aleph::and, and cmp().
Referenced by sort_loop().
| T * Aleph::pdq_detail::partition_left | ( | T * | begin, |
| T * | end, | ||
| const Compare & | cmp | ||
| ) |
Partition [begin, end) around the pivot *begin when it equals the item before begin, which no item of the range is less than: the items equal to the pivot go before it, where they are in their places, and the greater ones after it.
Definition at line 2239 of file tpl_sort_utils.H.
References Aleph::and, and cmp().
Referenced by sort_loop().
|
inline |
Swap *a and *b if *b is less than *a.
Definition at line 1979 of file tpl_sort_utils.H.
References cmp().
Referenced by sort3().
|
inline |
Sort *a, *b and *c.
Definition at line 1987 of file tpl_sort_utils.H.
References cmp(), and sort2().
Referenced by sort_loop().
| void Aleph::pdq_detail::sort_loop | ( | T * | begin, |
| T * | end, | ||
| const Compare & | cmp, | ||
| int | bad_allowed, | ||
| bool | leftmost, | ||
| bool | by_blocks | ||
| ) |
Sort [begin, end): the loop of introsort().
Unless leftmost, the item before begin is the pivot of the partition that left the range on its right, which no item of the range is less than.
| begin | The first item of the range. |
| end | The end of the range. |
| cmp | The comparison. |
| bad_allowed | The highly unbalanced partitions after which the range goes to heapsort. |
| leftmost | Whether no item before begin bounds the range. |
| by_blocks | Whether the range is partitioned by blocks, or else with branches. |
Definition at line 2280 of file tpl_sort_utils.H.
References Aleph::and, cmp(), Aleph::faster_heapsort(), Few_Swaps_Ratio, Insertion_Threshold, Ninther_Threshold, partial_insertion_sort_range(), partition_by_blocks(), partition_by_branches(), partition_left(), Aleph::size(), sort3(), and sort_loop().
Referenced by Aleph::introsort(), and sort_loop().
| void Aleph::pdq_detail::swap_offsets | ( | T * | first, |
| T * | last, | ||
| const unsigned char * | offsets_l, | ||
| const unsigned char * | offsets_r, | ||
| const long | num, | ||
| const bool | use_swaps | ||
| ) |
Move the num pairs of items on the wrong sides, at first + offsets_l[i] and at last - offsets_r[i].
With as many items on each side, it swaps them, which keeps a range in descending order linear; else it moves them along one cycle.
Definition at line 2069 of file tpl_sort_utils.H.
Referenced by partition_by_blocks().
|
inlineconstexpr |
Items that a partition by blocks compares at a time.
Definition at line 1951 of file tpl_sort_utils.H.
Referenced by partition_by_blocks().
|
inlineconstexpr |
A partition that swaps fewer than one item in this many of its range leaves its subranges to the partition with branches: they are nearly in order, and the processor predicts those branches.
Definition at line 1957 of file tpl_sort_utils.H.
Referenced by sort_loop().
|
inlineconstexpr |
Ranges shorter than this one are sorted by insertion.
Definition at line 1940 of file tpl_sort_utils.H.
Referenced by sort_loop().
|
inlineconstexpr |
Ranges longer than this one take their pivot from nine items.
Definition at line 1943 of file tpl_sort_utils.H.
Referenced by sort_loop().
|
inlineconstexpr |
Moves after which the insertion sort of a range that a partition found in order gives up.
Definition at line 1948 of file tpl_sort_utils.H.
Referenced by partial_insertion_sort_range().