View source code
Display the source code in std/algorithm/comparison.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Module std.algorithm.comparison

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

Cheat Sheet
Function Name Description
among Checks 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.
clamp clamp(1, 3, 6) returns 3. clamp(4, 3, 6) returns 4.
cmp cmp("abc", "abcd") is -1, cmp("abc", "aba") is 1, and cmp("abc", "abc") is 0.
either Return first parameter p that passes an if (p) test, e.g. either(0, 42, 43) returns 42.
equal Compares ranges for element-by-element equality, e.g. equal([1, 2, 3], [1.0, 2.0, 3.0]) returns true.
isPermutation isPermutation([1, 2], [2, 1]) returns true.
isSameLength isSameLength([1, 2, 3], [4, 5, 6]) returns true.
levenshteinDistance levenshteinDistance("kitten", "sitting") returns 3 by using the Levenshtein distance algorithm.
levenshteinDistanceAndPath levenshteinDistanceAndPath("kitten", "sitting") returns tuple(3, "snnnsni") by using the Levenshtein distance algorithm.
max max(3, 4, 2) returns 4.
min min(3, 4, 2) returns 2.
mismatch mismatch("oh hi", "ohayo") returns tuple(" hi", "ayo").
predSwitch 2.predSwitch(1, "one", 2, "two", 3, "three") returns "two".

Functions

NameDescription
among(value, 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(switchObject) Executes and returns one of a collection of handlers based on the type of the switch object.
clamp(val, lower, upper) Clamps a value into the given bounds.
cmp(r1, 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(first, 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(r1, r2) Checks if both ranges are permutations of each other.
isSameLength(r1, r2) Checks if the two 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(s, 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 Ο(s.length * t.length) evaluations of equals and occupies Ο(min(s.length, t.length)) storage.
levenshteinDistanceAndPath(s, t) Returns the Levenshtein distance and the edit path between s and t.
max(args) Iterates the passed arguments and returns the maximum value.
min(args) Iterates the passed arguments and returns the minimum value.
mismatch(r1, r2) Sequentially compares elements in r1 and r2 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 Ο(min(r1.length, r2.length)) evaluations of pred.
predSwitch(switchExpression, choices) Returns one of a collection of expressions based on the value of the switch expression.

Enums

NameDescription
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.

Templates

NameDescription
among 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 Compares two ranges for equality, as defined by predicate pred (which is == by default).

Authors

Andrei Alexandrescu

License

Boost License 1.0.