The OpenD Programming Language

isPermutation

Checks if both ranges are permutations of each other.

This function can allocate if the Yes.allocateGC flag is passed. This has the benefit of have better complexity than the Yes.allocateGC option. However, this option is only available for ranges whose equality can be determined via each element's toHash method. If customized equality is needed, then the pred template parameter can be passed, and the function will automatically switch to the non-allocating algorithm. See std.functional.binaryFun for more details on how to define pred.

Non-allocating forward range option: O(n^2) Non-allocating forward range option with custom pred: O(n^2) Allocating forward range option: amortized O(r1.length) + O(r2.length)

  1. bool isPermutation(Range1 r1, Range2 r2)
    bool
    isPermutation
    (
    Flag!"allocateGC" allocateGC
    Range1
    Range2
    )
    (
    Range1 r1
    ,
    Range2 r2
    )
    if (
    allocateGC == Yes.allocateGC &&
    &&
    &&
    !isInfinite!Range1
    &&
    !isInfinite!Range2
    )
  2. bool isPermutation(Range1 r1, Range2 r2)

Parameters

allocateGC

Yes.allocateGC/No.allocateGC

r1 Range1

A finite forward range

r2 Range2

A finite forward range

Return Value

Type: bool

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

Examples

import std.typecons : Yes;

assert(isPermutation([1, 2, 3], [3, 2, 1]));
assert(isPermutation([1.1, 2.3, 3.5], [2.3, 3.5, 1.1]));
assert(isPermutation("abc", "bca"));

assert(!isPermutation([1, 2], [3, 4]));
assert(!isPermutation([1, 1, 2, 3], [1, 2, 2, 3]));
assert(!isPermutation([1, 1], [1, 1, 1]));

// Faster, but allocates GC handled memory
assert(isPermutation!(Yes.allocateGC)([1.1, 2.3, 3.5], [2.3, 3.5, 1.1]));
assert(!isPermutation!(Yes.allocateGC)([1, 2], [3, 4]));

Meta