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.
It contains generic comparison algorithms.
| Function Name | Description | 
|---|---|
| among | Checks if a value is among a set of values, e.g. if (vv is 1, 2 or 3 | 
| castSwitch | (new A())returns1. | 
| clamp | clamp(1, 3, 6)returns3.clamp(4, 3, 6)returns4. | 
| cmp | cmp("abc", "abcd")is-1,cmp("abc", "aba")is1,
        andcmp("abc", "abc")is0. | 
| either | Return first parameter pthat passes anif (p)test, e.g.either(0, 42, 43)returns42. | 
| equal | Compares ranges for element-by-element equality, e.g. equal([1, 2, 3], [1.0, 2.0, 3.0])returnstrue. | 
| isPermutation | isPermutation([1, 2], [2, 1])returnstrue. | 
| isSameLength | isSameLength([1, 2, 3], [4, 5, 6])returnstrue. | 
| levenshteinDistance | levenshteinDistance("kitten", "sitting")returns3by using
        the         Levenshtein distance algorithm. | 
| levenshteinDistanceAndPath | levenshteinDistanceAndPath("kitten", "sitting")returnstuple(3, "snnnsni")by using the
                Levenshtein distance algorithm. | 
| max | max(3, 4, 2)returns4. | 
| min | min(3, 4, 2)returns2. | 
| mismatch | mismatch("oh hi", "ohayo")returnstuple(" hi", "ayo"). | 
| predSwitch | 2returns"two". | 
Functions
| Name | Description | 
|---|---|
| 
									among(value, values)
								 | Find valueamongvalues, returning the 1-based index
of the first matching value invalues, or0ifvalueis not amongvalues. The predicatepredis 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 valinto the given bounds. Result has the same type asval. | 
| 
									cmp(r1, r2)
								 | Performs a lexicographical comparison on two
input ranges.
Iterating r1andr2in lockstep,cmpcompares each elemente1ofr1with the corresponding elemente2inr2. If one
of the ranges has been finished,cmpreturns a negative value
ifr1has fewer elements thanr2, a positive value ifr1has more elements thanr2, and0if the ranges have the same
number of elements. | 
| 
									either(first, alternatives)
								 | Get the first argument athat passes anif (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(rs)
								 | Checks if two or more ranges have the same number of elements. This function is
optimized to always take advantage of the lengthmember of either range
if it exists. | 
| 
									levenshteinDistance(s, t)
								 | Returns the Levenshtein
distance between sandt. The Levenshtein distance computes
the minimal amount of edit operations necessary to transformsintot.  Performs Ο(s) evaluations ofequalsand occupies Ο(min(s) storage. | 
| 
									levenshteinDistanceAndPath(s, t)
								 | Returns the Levenshtein distance and the edit path between sandt. | 
| 
									max(args)
								 | Iterates the passed arguments and returns the maximum value. | 
| 
									min(args)
								 | Iterates the passed arguments and returns the minimum value. | 
| 
									mismatch(rs)
								 | Sequentially compares elements in rsin lockstep, and
stops at the first mismatch (according topred, by default
equality). Returns a tuple with the reduced ranges that start with the
two mismatched values. Performs Ο(min(r[0])
evaluations ofpred. | 
| 
									predSwitch(switchExpression, choices)
								 | 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) andt(target), a
sequence ofEditOpencodes the steps that need to be taken to
convertsintot. For example, ifs = "cat"and"cars", the minimal sequence that transformssintotis:
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 valueamongvalues, returning the 1-based index
of the first matching value invalues, or0ifvalueis not amongvalues. The predicatepredis used to
compare values, and uses equality by default. | 
| 
									equal
								 | Compares two or more ranges for equality, as defined by predicate pred(which is==by default). | 
Authors
License
					Copyright © 1999-2024 by the D Language Foundation | Page generated by ddox.