algorithm

Functions: max, min, minmax, swap, sort, sortBy

max

T& max<T: Comparable>(T& a, T& b)

Gives a reference to the maximum of the two elements. Like C++ std::max, the result borrows one of the arguments, so it must not outlive them. Calls with a const argument, or with only temporaries, use the by-value overload below instead, which copies.

T max<T: Copyable + Comparable>(T a, T b)

Gives the maximum of the two elements, copying Copyable arguments. Unlike the by-reference overload, this accepts consts, and temporaries are safe to pass since the result is a copy.

T* max<T>(T* a, T* b)

Gives the greater of the two pointers, comparing memory addresses.

min

T& min<T: Comparable>(T& a, T& b)

Gives a reference to the minimum of the two elements. Like C++ std::min, the result borrows one of the arguments, so it must not outlive them. Calls with a const argument, or with only temporaries, use the by-value overload below instead, which copies.

T min<T: Copyable + Comparable>(T a, T b)

Gives the minimum of the two elements, copying Copyable arguments. Unlike the by-reference overload, this accepts consts, and temporaries are safe to pass since the result is a copy.

T* min<T>(T* a, T* b)

Gives the lesser of the two pointers, comparing memory addresses.

minmax

(T& min, T& max) minmax<T: Comparable>(T& a, T& b)

Gives references to the minimum and maximum of the two elements. Like C++ std::minmax, the result borrows the arguments, so it must not outlive them. Calls with a const argument, or with only temporaries, use the by-value overload below instead, which copies.

(T min, T max) minmax<T: Copyable + Comparable>(T a, T b)

Gives the minimum and maximum of the two elements, copying Copyable arguments. Unlike the by-reference overload, this accepts consts.

(T* min, T* max) minmax<T>(T* a, T* b)

Gives the lesser and greater of the two pointers, comparing memory addresses.

swap

void swap<E>(E& a, E& b)

Swaps two items in place.

sort

void sort<E: Comparable>(E[] array)

Sorts an array. Uses insertion sort if the array is small enough, and quickSort otherwise.

sortBy

void sortBy<E, Less: bool(E&, E&)>(E[] array, Less less)

Sorts an array using less to order elements, so types without an ordering (or orderings other than the natural one) can be sorted. Like sort, small arrays use stable insertion sort, larger ones (unstable) quickSort.