View source code Display the source code in std/algorithm/comparison.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

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.
equal Compares ranges for element-by-element equality, e.g. equal([1, 2, 3], [1.0, 2.0, 3.0]) 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

Name Description
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.
castSwitch Executes and returns one of a collection of handlers based on the type of the switch object.
clamp Clamps a value into the given bounds.
cmp Performs three-way lexicographical comparison on two input ranges according to predicate pred. 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.
levenshteinDistance 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 Ο(s.length * t.length) storage.
levenshteinDistanceAndPath Returns the Levenshtein distance and the edit path between s and t.
max Iterates the passed arguments and return the maximum value.
min Iterates the passed arguments and returns the minimum value.
mismatch 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 Returns one of a collection of expressions based on the value of the switch expression.

Enums

Name Description
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

Name Description
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.

Comments