Function std.algorithm.comparison.isPermutation
Checks if both ranges are permutations of each other.
bool isPermutation(Flag!("allocateGC") allocate_gc, Range1, Range2)
(
Range1 r1,
Range2 r2
)
if (allocate_gc == Yes .allocateGC && isForwardRange!Range1 && isForwardRange!Range2 && !isInfinite!Range1 && !isInfinite!Range2);
bool isPermutation(alias pred, Range1, Range2)
(
Range1 r1,
Range2 r2
)
if (is(typeof(binaryFun!pred)) && isForwardRange!Range1 && isForwardRange!Range2 && !isInfinite!Range1 && !isInfinite!Range2);
This function can allocate if the Yes
flag is passed. This has
the benefit of have better complexity than the Yes
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 binaryFun
for more details on
how to define pred
.
Non-allocating forward range option: Ο(n^2
)
Non-allocating forward range option with custom pred
: Ο(n^2
)
Allocating forward range option: amortized Ο(r1
) + Ο(r2
)
Parameters
Name | Description |
---|---|
pred | an optional parameter to change how equality is defined |
allocate_gc | Yes /No |
r1 | A finite forward range |
r2 | A finite forward range |
Returns
true
if all of the elements in r1
appear the same number of times in r2
.
Otherwise, returns false
.
Example
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]));