module watt.algorithm

Functions implementing generally applicable algorithms.

Code Map

//! Functions implementing generally applicable algorithms.
module watt.algorithm;


//! Takes two indices of elements to compare. Return true if the first
//! parameter should go before the second.
alias CmpDg = scope (bool delegate(size_t, size_t));
//! Takes two indices of elements to swap.
alias SwapDg = scope (void delegate(size_t, size_t));

//! Sort something via delegates.
fn runSort(numElements: size_t, cmp: scope (CmpDg), swap: scope (SwapDg)) { }
//! Sort an array of integers in place.
fn sort(ints: i32[]) { }
//! Return the maximum of two values.
fn max(a: i32, b: i32) i32 { }
//! Return the maximum of two values.
fn max(a: i64, b: i64) i64 { }
//! Return the maximum of two values.
fn max(a: u32, b: u32) u32 { }
//! Return the maximum of two values.
fn max(a: u64, b: u64) u64 { }
//! Return the maximum of two values.
fn max(f64, f64) f64;
//! Return the minimum of two values.
fn min(a: i32, b: i32) i32 { }
//! Return the minimum of two values.
fn min(a: i64, b: i64) i64 { }
//! Return the minimum of two values.
fn min(a: u32, b: u32) u32 { }
//! Return the minimum of two values.
fn min(a: u64, b: u64) u64 { }
//! Return the minimum of two values.
fn min(f64, f64) f64;
alias CmpDg

Takes two indices of elements to compare. Return true if the first parameter should go before the second.

alias SwapDg

Takes two indices of elements to swap.

fn runSort(numElements: size_t, cmp: scope (CmpDg), swap: scope (SwapDg))

Sort something via delegates.

Parameters

numElements

The number of elements being sorted. If this is 1 or 0, runSort will return immediately.

cmp

A delegate that takes two indices. Compare two elements, return true if the first parameter should be given precedence over the second.

swap

A delegate that gives two indices to swap.

fn sort(ints: i32[])

Sort an array of integers in place.

Example

a := [3, 1, 2];
sort(a);
assert(a == [1, 2, 3]);
fn max(a: i32, b: i32) i32

Return the maximum of two values.

Examples

assert(max(32, 16) == 32);
assert(max(32, 32) == 32);
assert(max(-32, 0) == 0);
fn min(a: i32, b: i32) i32

Return the minimum of two values.

Examples

assert(min(2, 4) == 2);
assert(min(2, 2) == 2);
assert(min(-2, 4) == -2);