Report a bug
If you spot a problem with this page, click here to create a Github issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.utility

Generic utilities.
Cheat Sheet
Function Name Description
swap Swaps two values.
min Minimum value.
max Maximum value.
Authors:
Andrei Alexandrescu (original std.* modules), Ilya Yaroshenko
pure nothrow @nogc @trusted void swap(T)(ref T lhs, ref T rhs)
if (isBlitAssignable!T && !is(typeof(lhs.proxySwap(rhs))));

void swap(T)(ref T lhs, ref T rhs)
if (is(typeof(lhs.proxySwap(rhs))));
Swaps lhs and rhs. The instances lhs and rhs are moved in memory, without ever calling opAssign, nor any other function. T need not be assignable at all to be swapped. If lhs and rhs reference the same instance, then nothing is done. lhs and rhs must be mutable. If T is a struct or union, then its fields must also all be (recursively) mutable.
Parameters:
T lhs Data to be swapped with rhs.
T rhs Data to be swapped with lhs.
Examples:
// Swapping POD (plain old data) types:
int a = 42, b = 34;
swap(a, b);
assert(a == 34 && b == 42);

// Swapping structs with indirection:
static struct S { int x; char c; int[] y; }
S s1 = { 0, 'z', [ 1, 2 ] };
S s2 = { 42, 'a', [ 4, 6 ] };
swap(s1, s2);
assert(s1.x == 42);
assert(s1.c == 'a');
assert(s1.y == [ 4, 6 ]);

assert(s2.x == 0);
assert(s2.c == 'z');
assert(s2.y == [ 1, 2 ]);

// Immutables cannot be swapped:
immutable int imm1, imm2;
static assert(!__traits(compiles, swap(imm1, imm2)));
Examples:
// Non-copyable types can still be swapped.
static struct NoCopy
{
    this(this) { assert(0); }
    int n;
    string s;
}
NoCopy nc1, nc2;
nc1.n = 127; nc1.s = "abc";
nc2.n = 513; nc2.s = "uvwxyz";

swap(nc1, nc2);
assert(nc1.n == 513 && nc1.s == "uvwxyz");
assert(nc2.n == 127 && nc2.s == "abc");

swap(nc1, nc1);
swap(nc2, nc2);
assert(nc1.n == 513 && nc1.s == "uvwxyz");
assert(nc2.n == 127 && nc2.s == "abc");

// Types containing non-copyable fields can also be swapped.
static struct NoCopyHolder
{
    NoCopy noCopy;
}
NoCopyHolder h1, h2;
h1.noCopy.n = 31; h1.noCopy.s = "abc";
h2.noCopy.n = 65; h2.noCopy.s = null;

swap(h1, h2);
assert(h1.noCopy.n == 65 && h1.noCopy.s == null);
assert(h2.noCopy.n == 31 && h2.noCopy.s == "abc");

swap(h1, h1);
swap(h2, h2);
assert(h1.noCopy.n == 65 && h1.noCopy.s == null);
assert(h2.noCopy.n == 31 && h2.noCopy.s == "abc");

// Const types cannot be swapped.
const NoCopy const1, const2;
static assert(!__traits(compiles, swap(const1, const2)));
auto min(T...)(T args)
if (T.length >= 2);
Iterates the passed arguments and returns the minimum value.
Parameters:
T args The values to select the minimum from. At least two arguments must be passed, and they must be comparable with <.
Returns:
The minimum of the passed-in values.
Examples:
min is not defined for arguments of mixed signedness because of security reasons. Please unify type or use a Phobos analog.
int a = -10;
uint b = 10;
static assert(!is(typeof(min(a, b))));
auto max(T...)(T args)
if (T.length >= 2);
Iterates the passed arguments and returns the minimum value.
Parameters:
T args The values to select the minimum from. At least two arguments must be passed, and they must be comparable with <.
Returns:
The minimum of the passed-in values.
Examples:
int a = 5;
short b = 6;
double c = 2;
auto d = max(a, b);
static assert(is(typeof(d) == int));
assert(d == 6);
auto e = min(a, b, c);
static assert(is(typeof(e) == double));
assert(e == 2);
Examples:
max is not defined for arguments of mixed signedness because of security reasons. Please unify type or use a Phobos analog.
int a = -10;
uint b = 10;
static assert(!is(typeof(max(a, b))));