Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
sort.hh File Reference
#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={})

Detailed Description

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.

Function Documentation

◆ insertionsort()

template<std::bidirectional_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>>
void nix::insertionsort ( Iter begin,
Iter end,
Comparator comp = {} )

Simple insertion sort.

Does not require that the std::iter_value_t<Iter> is copyable.

Parameters
beginStart of the range to sort.
endEnd of the range to sort.
compComparator the defines the ordering. Order of elements if the comp is not strict weak ordering is not specified.
Exceptions
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.

Note
This can't be implemented in terms of binary search if the strict weak ordering needs to be handled in a well-defined but unspecified manner.

◆ mergeSortedRunsInPlace()

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 = {} )

Merge sorted runs [begin, middle) with [middle, end) in-place [begin, end). Uses a temporary working buffer by first copying [begin, end) to it.

Parameters
beginStart of the first subrange to be sorted.
middleEnd of the first sorted subrange and the start of the second.
endEnd of the second sorted subrange.
workingBeginStart of the working buffer.
compComparator implementing an operator()(const ValueType& lhs, const ValueType& rhs).
Precondition
workingBegin buffer must have at least std::distance(begin, end) elements.
Note
We can't use std::inplace_merge or std::merge, because their behavior is undefined if the comparator is not strict weak ordering.

◆ peeksort()

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 = {} )

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).

  1. a < a === false - relation is irreflexive
  2. a < b, b < c => a < c - transitivity
  3. a ~ b, a ~ b, b ~ c => a ~ c, transitivity of equivalence
See also
https://www.wild-inter.net/publications/munro-wild-2018
https://github.com/Voultapher/sort-research-rs/blob/main/writeup/sort_safety/text.md#property-analysis

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.

Parameters
beginStart of the range to be sorted.
endEnd of the range to be sorted.
compComparator implementing an operator()(const ValueType& lhs, const ValueType& rhs).
Exceptions
std::bad_allocif 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.

◆ strictlyDecreasingPrefix()

template<std::forward_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>>
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.

◆ strictlyDecreasingSuffix()

template<std::bidirectional_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>>
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.

◆ weaklyIncreasingPrefix()

template<std::bidirectional_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>>
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.

◆ weaklyIncreasingSuffix()

template<std::bidirectional_iterator Iter, typename Comparator = std::less<std::iter_value_t<Iter>>>
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.