View source code
							
							
						
								Display the source code in std/algorithm/searching.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.searching
This is a submodule of std.
It contains generic searching algorithms.
| Function Name | Description | 
|---|---|
| all | all!"a > 0"([1, 2, 3, 4])returnstruebecause all elements
        are positive | 
| any | any!"a > 0"([1, 2, -3, -4])returnstruebecause at least one
        element is positive | 
| balancedParens | balancedParens("((1 + 1) / 2)")returnstruebecause the
        string has balanced parentheses. | 
| boyerMooreFinder | find("hello world", boyerMooreFinder("or"))returns"orld"using the         Boyer-Moore algorithm. | 
| canFind | canFind("hello world", "or")returnstrue. | 
| count | Counts elements that are equal to a specified value or satisfy a
        predicate. count([1, 2, 1], 1)returns2andcount!"a < 0"([1, -3, 0])returns1. | 
| countUntil | countUntil(a, b)returns the number of steps taken inato
        reachb; for example,countUntil("hello!", "o")returns4. | 
| commonPrefix | commonPrefix("parakeet", "parachute")returns"para". | 
| endsWith | endsWith("rocks", "ks")returnstrue. | 
| find | find("hello world", "or")returns"orld"using linear search.
        (For binary search refer tostd.) | 
| findAdjacent | findAdjacent([1, 2, 3, 3, 4])returns the subrange starting with
        two equal adjacent elements, i.e.[3, 3, 4]. | 
| findAmong | findAmong("abcd", "qcx")returns"cd"because'c'is
        among"qcx". | 
| findSkip | If a = "abcde", thenfindSkip(a, "x")returnsfalseand
        leavesaunchanged, whereasfindSkip(a, "c")advancesato"de"and returnstrue. | 
| findSplit | findSplit("abcdefg", "de")returns the three ranges"abc","de", and"fg". | 
| findSplitAfter | findSplitAfter("abcdefg", "de")returns the two ranges"abcde"and"fg". | 
| findSplitBefore | findSplitBefore("abcdefg", "de")returns the two ranges"abc"and"defg". | 
| minCount | minCount([2, 1, 1, 4, 1])returnstuple(1, 3). | 
| maxCount | maxCount([2, 4, 1, 4, 1])returnstuple(4, 2). | 
| minElement | Selects the minimal element of a range. minElement([3, 4, 1, 2])returns1. | 
| maxElement | Selects the maximal element of a range. maxElement([3, 4, 1, 2])returns4. | 
| minIndex | Index of the minimal element of a range. minElement([3, 4, 1, 2])returns2. | 
| maxIndex | Index of the maximal element of a range. maxElement([3, 4, 1, 2])returns1. | 
| minPos | minPos([2, 3, 1, 3, 4, 1])returns the subrange[1, 3, 4, 1],
        i.e., positions the range at the first occurrence of its minimal
        element. | 
| maxPos | maxPos([2, 3, 1, 3, 4, 1])returns the subrange[4, 1],
        i.e., positions the range at the first occurrence of its maximal
        element. | 
| skipOver | Assume a = "blah". ThenskipOver(a, "bi")leavesaunchanged and returnsfalse, whereasskipOver(a, "bl")advancesato refer to"ah"and returnstrue. | 
| startsWith | startsWith("hello, world", "hello")returnstrue. | 
| until | Lazily iterates a range until a specific value is found. | 
Functions
| Name | Description | 
|---|---|
| 
									balancedParens(r, lPar, rPar, maxNestingLevel)
								 | Checks whether rhas "balanced parentheses", i.e. all instances
oflParare closed by corresponding instances ofrPar. The
parametermaxNestingLevelcontrols the nesting level allowed. The
most common uses are the default or0. In the latter case, no
nesting is allowed. | 
| 
									boyerMooreFinder(needle)
								 | Sets up Boyer-Moore matching for use with findbelow.
 By default, elements are compared for equality. | 
| 
									commonPrefix(r1, r2)
								 | Returns the common prefix of two ranges. | 
| 
									count(haystack, needle)
								 | The first version counts the number of elements xinrfor
whichpred(x, value)istrue.preddefaults to
equality. Performs Ο(haystack) evaluations ofpred. | 
| 
									countUntil(haystack, needles)
								 | Counts elements in the given
    forward range
    until the given predicate is true for one of the given needles. | 
| 
									endsWith(doesThisEnd, withOneOfThese)
								 | Checks if the given range ends with (one of) the given needle(s).
The reciprocal of startsWith. | 
| 
									find(haystack, needle)
								 | Finds an individual element in an input range.
Elements of haystackare compared withneedleby using predicatepredwithpred(haystack.findperforms Ο(walkLength(haystack)) evaluations ofpred. | 
| 
									find(haystack, needles)
								 | Finds two or more needlesinto ahaystack. The predicatepredis used throughout to compare elements. By default, elements are
compared for equality. | 
| 
									find(haystack, needle)
								 | Finds needleinhaystackefficiently using the
  Boyer-Moore method. | 
| 
									findAdjacent(r)
								 | Advances runtil it finds the first two adjacent elementsa,bthat satisfypred(a, b). Performs Ο(r)
evaluations ofpred. | 
| 
									findAmong(seq, choices)
								 | Searches the given range for an element that matches one of the given choices. | 
| 
									findSkip(haystack, needle)
								 | Finds needleinhaystackand positionshaystackright after the first occurrence ofneedle. | 
| 
									findSplit(haystack, needle)
								 | These functions find the first occurrence of needleinhaystackand then
splithaystackas follows. | 
| 
									findSplitAfter(haystack, needle)
								 | These functions find the first occurrence of needleinhaystackand then
splithaystackas follows. | 
| 
									findSplitBefore(haystack, needle)
								 | These functions find the first occurrence of needleinhaystackand then
splithaystackas follows. | 
| 
									maxCount(range)
								 | Computes the minimum (respectively maximum) of rangealong with its number of
occurrences. Formally, the minimum is a valuexinrangesuch thatpred(a, x)isfalsefor all valuesainrange. Conversely, the maximum is
a valuexinrangesuch thatpred(x, a)isfalsefor all valuesainrange(note the swapped arguments topred). | 
| 
									maxElement(r)
								 | Iterates the passed range and returns the maximal element.
A custom mapping function can be passed to map.
In other languages this is sometimes calledargmax. | 
| 
									maxIndex(range)
								 | Computes the index of the first occurrence of range's maximum element. | 
| 
									maxPos(range)
								 | Computes a subrange of rangestarting at the first occurrence ofrange's
minimum (respectively maximum) and with the same ending asrange, or the
empty range ifrangeitself is empty. | 
| 
									minCount(range)
								 | Computes the minimum (respectively maximum) of rangealong with its number of
occurrences. Formally, the minimum is a valuexinrangesuch thatpred(a, x)isfalsefor all valuesainrange. Conversely, the maximum is
a valuexinrangesuch thatpred(x, a)isfalsefor all valuesainrange(note the swapped arguments topred). | 
| 
									minElement(r)
								 | Iterates the passed range and returns the minimal element.
A custom mapping function can be passed to map.
In other languages this is sometimes calledargmin. | 
| 
									minIndex(range)
								 | Computes the index of the first occurrence of range's minimum element. | 
| 
									minPos(range)
								 | Computes a subrange of rangestarting at the first occurrence ofrange's
minimum (respectively maximum) and with the same ending asrange, or the
empty range ifrangeitself is empty. | 
| 
									startsWith(doesThisStart, withOneOfThese)
								 | Checks whether the given
input range starts with (one
of) the given needle(s) or, if no needles are given,
if its front element fulfils predicate pred. | 
| 
									until(range, sentinel, openRight)
								 | Lazily iterates rangeuntil the elementefor whichpred(e, sentinel)is true. | 
Structs
| Name | Description | 
|---|---|
| 
									BoyerMooreFinder
								 | Sets up Boyer-Moore matching for use with findbelow.
 By default, elements are compared for equality. | 
| 
									Until
								 | Lazily iterates rangeuntil the elementefor whichpred(e, sentinel)is true. | 
Templates
| Name | Description | 
|---|---|
| 
									all
								 | Checks if all of the elements verify pred. | 
| 
									any
								 | Checks if any of the elements verifies pred.!anycan be used to verify that none of the elements verifypred.
This is sometimes calledexistsin other languages. | 
| 
									canFind
								 | Convenience function. Like find, but only returns whether or not the search was successful. | 
| 
									skipOver
								 | Skip over the initial portion of the first given range ( haystack) that matches
any of the additionally given ranges (needles) fully, or
if no second range is given skip over the elements that fulfill pred.
Do nothing if there is no match. | 
Aliases
| Name | Type | Description | 
|---|---|---|
| OpenRight | Flag!("openRight") | Interval option specifier for until(below) and others. | 
Authors
License
					Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.