std.algorithm.comparison

This is a submodule of std.algorithm. It contains generic comparison algorithms.

Cheat Sheet
Function NameDescription
amongChecks if a value is among a set of values, e.g. if (v.among(1, 2, 3)) // v is 1, 2 or 3
castSwitch(new A()).castSwitch((A a)=>1,(B b)=>2) returns 1.
clampclamp(1, 3, 6) returns 3. clamp(4, 3, 6) returns 4.
cmpcmp("abc", "abcd") is -1, cmp("abc", "aba") is 1, and cmp("abc", "abc") is 0.
eitherReturn first parameter p that passes an if (p) test, e.g. either(0, 42, 43) returns 42.
equalCompares ranges for element-by-element equality, e.g. equal([1, 2, 3], [1.0, 2.0, 3.0]) returns true.
isPermutationisPermutation([1, 2], [2, 1]) returns true.
isSameLengthisSameLength([1, 2, 3], [4, 5, 6]) returns true.
levenshteinDistancelevenshteinDistance("kitten", "sitting") returns 3 by using the Levenshtein distance algorithm.
levenshteinDistanceAndPathlevenshteinDistanceAndPath("kitten", "sitting") returns tuple(3, "snnnsni") by using the Levenshtein distance algorithm.
maxmax(3, 4, 2) returns 4.
minmin(3, 4, 2) returns 2.
mismatchmismatch("oh hi", "ohayo") returns tuple(" hi", "ayo").
predSwitch2.predSwitch(1, "one", 2, "two", 3, "three") returns "two".

Members

Enums

EditOp
enum EditOp

Encodes edit operations necessary to transform one sequence into another. Given sequences s (source) and t (target), a sequence of EditOp encodes the steps that need to be taken to convert s into t. For example, if s = "cat" and "cars", the minimal sequence that transforms s into t is: skip two characters, replace 't' with 'r', and insert an 's'. Working with edit operations is useful in applications such as spell-checkers (to find the closest word to a given misspelled word), approximate searches, diff-style programs that compute the difference between files, efficient encoding of patches, DNA sequence analysis, and plagiarism detection.

Functions

among
uint among(Value value, Values values)

Find value among values, returning the 1-based index of the first matching value in values, or 0 if value is not among values. The predicate pred is used to compare values, and uses equality by default.

castSwitch
auto castSwitch(Object switchObject)

Executes and returns one of a collection of handlers based on the type of the switch object.

clamp
T1 clamp(T1 val, T2 lower, T3 upper)

Clamps val into the given bounds. Result has the same type as val.

cmp
auto cmp(R1 r1, R2 r2)
int cmp(R1 r1, R2 r2)

Performs a lexicographical comparison on two input ranges. Iterating r1 and r2 in lockstep, cmp compares each element e1 of r1 with the corresponding element e2 in r2. If one of the ranges has been finished, cmp returns a negative value if r1 has fewer elements than r2, a positive value if r1 has more elements than r2, and 0 if the ranges have the same number of elements.

either
CommonType!(T, Ts) either(T first, Ts alternatives)

Get the first argument a that passes an if (unaryFun!pred(a)) test. If no argument passes the test, return the last argument.

isPermutation
bool isPermutation(Range1 r1, Range2 r2)

Checks if both ranges are permutations of each other.

isSameLength
bool isSameLength(Ranges rs)

Checks if two or more ranges have the same number of elements. This function is optimized to always take advantage of the length member of either range if it exists.

levenshteinDistance
size_t levenshteinDistance(Range1 s, Range2 t)

Returns the Levenshtein distance between s and t. The Levenshtein distance computes the minimal amount of edit operations necessary to transform s into t. Performs O(s.length * t.length) evaluations of equals and occupies O(min(s.length, t.length)) storage.

levenshteinDistanceAndPath
Tuple!(size_t, EditOp[]) levenshteinDistanceAndPath(Range1 s, Range2 t)

Returns the Levenshtein distance and the edit path between s and t.

max
auto max(T args)
T max(T a, U b)

Iterates the passed arguments and returns the maximum value.

min
auto min(T args)
T min(T a, U b)

Iterates the passed arguments and returns the minimum value.

mismatch
Tuple!(Ranges) mismatch(Ranges rs)

Sequentially compares elements in rs in lockstep, and stops at the first mismatch (according to pred, by default equality). Returns a tuple with the reduced ranges that start with the two mismatched values. Performs O(min(r[0].length, r[1].length, ...)) evaluations of pred.

predSwitch
auto predSwitch(T switchExpression, R choices)

Returns one of a collection of expressions based on the value of the switch expression.

Templates

among
template among(values...)

Find value among values, returning the 1-based index of the first matching value in values, or 0 if value is not among values. The predicate pred is used to compare values, and uses equality by default.

equal
template equal(alias pred = "a == b")

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

Meta