The OpenD Programming Language

equal

Compares two or more ranges for equality, as defined by predicate pred (which is == by default).

template equal(alias pred = "a == b")
bool
equal
(
Ranges...
)
(
Ranges rs
)
if (
rs.length > 1 &&
&&
&&
is(typeof(binaryFun!pred(rs[0].front, rs[1].front)))
&&
(
rs.length == 2 ||
is(typeof(equal!pred(rs[1 .. $])) == bool)
)
)

Members

Functions

equal
bool equal(Ranges rs)

Compares two or more ranges for equality. The ranges may have different element types, as long as all are comparable by means of the pred. Performs O(min(rs[0].length, rs[1].length, ...)) evaluations of pred. However, if equal is invoked with the default predicate, the implementation may take the liberty to use faster implementations that have the theoretical worst-case O(max(rs[0].length, rs[1].length, ...)).

Examples

import std.algorithm.comparison : equal;
import std.math.operations : isClose;

int[4] a = [ 1, 2, 4, 3 ];
assert(!equal(a[], a[1..$]));
assert(equal(a[], a[]));
assert(equal!((a, b) => a == b)(a[], a[]));

// different types
double[4] b = [ 1.0, 2, 4, 3];
assert(!equal(a[], b[1..$]));
assert(equal(a[], b[]));

// predicated: ensure that two vectors are approximately equal
double[4] c = [ 1.0000000005, 2, 4, 3];
assert(equal!isClose(b[], c[]));

Tip: equal can itself be used as a predicate to other functions. This can be very useful when the element type of a range is itself a range. In particular, equal can be its own predicate, allowing range of range (of range...) comparisons.

import std.algorithm.comparison : equal;
import std.range : iota, chunks;
assert(equal!(equal!equal)(
    [[[0, 1], [2, 3]], [[4, 5], [6, 7]]],
    iota(0, 8).chunks(2).chunks(2)
));

Meta