![]() |
Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
|
#include <algorithm>#include <iterator>#include <concepts>#include <vector>#include <type_traits>#include <functional>Go to the source code of this file.
Functions | |
| template<std::forward_iterator Iter, std::random_access_iterator BufIter, typename Comparator = std::less<std::iter_value_t<Iter>>> | |
| void | nix::mergeSortedRunsInPlace (Iter begin, Iter middle, Iter end, BufIter workingBegin, Comparator comp={}) |
| template<std::bidirectional_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>> | |
| void | nix::insertionsort (Iter begin, Iter end, Comparator comp={}) |
| template<std::forward_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>> | |
| Iter | nix::strictlyDecreasingPrefix (Iter begin, Iter end, const Comparator &comp={}) |
| template<std::bidirectional_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>> | |
| Iter | nix::strictlyDecreasingSuffix (Iter begin, Iter end, const Comparator &comp={}) |
| template<std::bidirectional_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>> | |
| Iter | nix::weaklyIncreasingPrefix (Iter begin, Iter end, const Comparator &comp={}) |
| template<std::bidirectional_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>> | |
| Iter | nix::weaklyIncreasingSuffix (Iter begin, Iter end, const Comparator &comp={}) |
| template<std::random_access_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>> requires std::is_default_constructible_v<std::iter_value_t<Iter>> | |
| void | nix::peeksort (Iter begin, Iter end, Comparator comp={}) |
In-house implementation of sorting algorithms. Used for cases when several properties need to be upheld regardless of the stdlib implementation of std::sort or std::stable_sort.
PeekSort implementation is adapted from reference implementation https://github.com/sebawild/powersort licensed under the MIT License.
| void nix::insertionsort | ( | Iter | begin, |
| Iter | end, | ||
| Comparator | comp = {} ) |
Simple insertion sort.
Does not require that the std::iter_value_t<Iter> is copyable.
| begin | Start of the range to sort. |
| end | End of the range to sort. |
| comp | Comparator the defines the ordering. Order of elements if the comp is not strict weak ordering is not specified. |
| Nothing. |
Note on exception safety: this function provides weak exception safety guarantees. To elaborate: if the comparator throws or move assignment throws (value type is not nothrow_move_assignable) then the range is left in a consistent, but unspecified state.
| void nix::mergeSortedRunsInPlace | ( | Iter | begin, |
| Iter | middle, | ||
| Iter | end, | ||
| BufIter | workingBegin, | ||
| Comparator | comp = {} ) |
Merge sorted runs [begin, middle) with [middle, end) in-place [begin, end). Uses a temporary working buffer by first copying [begin, end) to it.
| begin | Start of the first subrange to be sorted. |
| middle | End of the first sorted subrange and the start of the second. |
| end | End of the second sorted subrange. |
| workingBegin | Start of the working buffer. |
| comp | Comparator implementing an operator()(const ValueType& lhs, const ValueType& rhs). |
| void nix::peeksort | ( | Iter | begin, |
| Iter | end, | ||
| Comparator | comp = {} ) |
Peeksort stable sorting algorithm. Sorts elements in-place. Allocates additional memory as needed.
PeekSort is a stable, near-optimal natural mergesort. Most importantly, like any other mergesort it upholds the "Ord safety" property. Meaning that even for comparator predicates that don't satisfy strict weak ordering it can't result in infinite loops/out of bounds memory accesses or other undefined behavior.
As a quick reminder, strict weak ordering relation operator< must satisfy the following properties. Keep in mind that in C++ an equivalence relation is specified in terms of operator< like so: a ~ b iff !(a < b) && !(b < a).
The order of elements when comp is not strict weak ordering is not specified, but is not undefined. The output is always some permutation of the input, regardless of the comparator provided. Relying on ordering in such cases is erroneous, but this implementation will happily accept broken comparators and will not crash.
| begin | Start of the range to be sorted. |
| end | End of the range to be sorted. |
| comp | Comparator implementing an operator()(const ValueType& lhs, const ValueType& rhs). |
| std::bad_alloc | if the temporary buffer can't be allocated. |
Note on exception safety: this function provides weak exception safety guarantees. To elaborate: if the comparator throws or move assignment throws (value type is not nothrow_move_assignable) then the range is left in a consistent, but unspecified state.
| Iter nix::strictlyDecreasingPrefix | ( | Iter | begin, |
| Iter | end, | ||
| const Comparator & | comp = {} ) |
Find maximal i <= end such that [begin, i) is strictly decreasing according to the specified comparator.
| Iter nix::strictlyDecreasingSuffix | ( | Iter | begin, |
| Iter | end, | ||
| const Comparator & | comp = {} ) |
Find minimal i >= start such that [i, end) is strictly decreasing according to the specified comparator.
| Iter nix::weaklyIncreasingPrefix | ( | Iter | begin, |
| Iter | end, | ||
| const Comparator & | comp = {} ) |
Find maximal i <= end such that [begin, i) is weakly increasing according to the specified comparator.
| Iter nix::weaklyIncreasingSuffix | ( | Iter | begin, |
| Iter | end, | ||
| const Comparator & | comp = {} ) |
Find minimal i >= start such that [i, end) is weakly increasing according to the specified comparator.