isPermutation

Checks if both ranges are permutations of each other.

pred must be an equivalence relation, e.i. for all inputs:

// reflexive pred(a, a) == true // symmetric pred(a, b) == pred(b, a) // transitive !(pred(a, b) && pred(b, c)) || pred(a, c)

  1. bool isPermutation(R1 r1, R2 r2)
  2. bool isPermutation(R1 r1, R2 r2, size_t[] index)
  3. bool isPermutation(R1 r1, R2 r2, size_t[] index)
    bool
    isPermutation
    (
    alias pred = "a == b"
    Flag!"nearlySorted" nearlySorted = Yes.nearlySorted
    R1
    R2
    )
    (
    R1 r1
    ,
    R2 r2
    ,
    ref size_t[] index
    )
    if (
    isInputRange!R1 &&
    !isInfinite!R1
    &&
    isRandomAccessRange!R2
    &&
    !isInfinite!R2
    &&
    is(CommonType!(ElementType!R1, ElementType!R2))
    )

Parameters

pred

an optional parameter to change how equality is defined

r1 R1

A finite input range

r2 R2

A finite random access range

index size_t[]

An index into r2 such that r2 permuted by index is a prefix of r1.

Return Value

Type: bool

true if all of the elements in r1 appear the same number of times in r2. Otherwise, returns false.

Examples

enum r1 = [1, 2, 3, 4, 5, 6];
enum r2 = [1, 3, 2, 4, 5, 6];

assert(isPermutation(r1, r2));

auto index = new size_t[r2.length];
assert(isPermutation(r1, r2, index));
assert(index == [0, 2, 1, 3, 4, 5]);

Meta