equal

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

  1. bool equal(Range1 r1, Range2 r2)
    template equal(alias pred = "a == b")
    bool
    equal
    (
    Range1
    Range2
    )
    (
    Range1 r1
    ,
    Range2 r2
    )
    if (
    !useCodePoint!(Range1, Range2) &&
    &&
    &&
    is(typeof(binaryFun!pred(r1.front, r2.front)))
    )
  2. bool equal(Range1 r1, Range2 r2)

Members

Functions

equal
bool equal(Range1 r1, Range2 r2)

Compares two ranges for equality. The ranges may have different element types, as long as pred(r1.front, r2.front) evaluates to bool. Performs O(min(r1.length, r2.length)) evaluations of pred.

Examples

1 import std.algorithm.comparison : equal;
2 import std.math : approxEqual;
3 
4 int[4] a = [ 1, 2, 4, 3 ];
5 assert(!equal(a[], a[1..$]));
6 assert(equal(a[], a[]));
7 assert(equal!((a, b) => a == b)(a[], a[]));
8 
9 // different types
10 double[4] b = [ 1.0, 2, 4, 3];
11 assert(!equal(a[], b[1..$]));
12 assert(equal(a[], b[]));
13 
14 // predicated: ensure that two vectors are approximately equal
15 double[4] c = [ 1.005, 2, 4, 3];
16 assert(equal!approxEqual(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

Suggestion Box / Bug Report