Skip to content

Commit ba7af41

Browse files
committed
Group documentation in std.algorithm.searching + small fixes
1 parent 31cf21b commit ba7af41

File tree

1 file changed

+70
-109
lines changed

1 file changed

+70
-109
lines changed

std/algorithm/searching.d

Lines changed: 70 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,8 @@ if (isInputRange!R && !isInfinite!R)
748748
`haystack` before reaching an element for which
749749
`startsWith!pred(haystack, needles)` is `true`. If
750750
`startsWith!pred(haystack, needles)` is not `true` for any element in
751-
`haystack`, then `-1` is returned.
751+
`haystack`, then `-1` is returned. If only `pred` is provided,
752+
`pred(haystack)` is tested for each element.
752753
753754
See_Also: $(REF indexOf, std,string)
754755
+/
@@ -898,18 +899,7 @@ if (isInputRange!R &&
898899
assert(countUntil("hello world", "world", 'l') == 2);
899900
}
900901

901-
/++
902-
Similar to the previous overload of `countUntil`, except that this one
903-
evaluates only the predicate `pred`.
904-
905-
Params:
906-
pred = Predicate to when to stop counting.
907-
haystack = An
908-
$(REF_ALTTEXT input range, isInputRange, std,range,primitives) of
909-
elements to be counted.
910-
Returns: The number of elements which must be popped from `haystack`
911-
before `pred(haystack.front)` is `true`.
912-
+/
902+
/// ditto
913903
ptrdiff_t countUntil(alias pred, R)(R haystack)
914904
if (isInputRange!R &&
915905
is(typeof(unaryFun!pred(haystack.front)) : bool))
@@ -1457,35 +1447,51 @@ private auto extremum(alias selector = "a < b", Range,
14571447

14581448
// find
14591449
/**
1460-
Finds an individual element in an input range. Elements of $(D
1461-
haystack) are compared with `needle` by using predicate $(D
1462-
pred). Performs $(BIGOH walkLength(haystack)) evaluations of $(D
1463-
pred).
1450+
Finds an individual element in an $(REF_ALTTEXT input range, isInputRange, std,range,primitives).
1451+
Elements of `haystack` are compared with `needle` by using predicate
1452+
`pred` with `pred(haystack.front, needle)`.
1453+
`find` performs $(BIGOH walkLength(haystack)) evaluations of `pred`.
1454+
1455+
To _find the last occurrence of `needle` in a
1456+
$(REF_ALTTEXT bidirectional, isBidirectionalRange, std,range,primitives) `haystack`,
1457+
call `find(retro(haystack), needle)`. See $(REF retro, std,range).
1458+
1459+
If no `needle` is provided, `pred(haystack.front)` will be evaluated on each
1460+
element of the input range.
14641461
1465-
To _find the last occurrence of `needle` in `haystack`, call $(D
1466-
find(retro(haystack), needle)). See $(REF retro, std,range).
1462+
If `input` is a $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives),
1463+
`needle` can be a $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) too.
1464+
In this case `startsWith!pred(haystack, needle)` is evaluated on each evaluation.
1465+
1466+
Note:
1467+
`find` behaves similar to `dropWhile` in other languages.
1468+
1469+
Complexity:
1470+
`find` performs $(BIGOH walkLength(haystack)) evaluations of `pred`.
1471+
There are specializations that improve performance by taking
1472+
advantage of $(REF_ALTTEXT bidirectional, isBidirectionalRange, std,range,primitives)
1473+
or $(REF_ALTTEXT random access, isRandomAccess, std,range,primitives)
1474+
ranges (where possible).
14671475
14681476
Params:
14691477
1470-
pred = The predicate for comparing each element with the needle, defaulting to
1471-
`"a == b"`.
1472-
The negated predicate `"a != b"` can be used to search instead for the first
1473-
element $(I not) matching the needle.
1478+
pred = The predicate for comparing each element with the needle, defaulting to equality `"a == b"`.
1479+
The negated predicate `"a != b"` can be used to search instead for the first
1480+
element $(I not) matching the needle.
14741481
1475-
haystack = The $(REF_ALTTEXT input range, isInputRange, std,range,primitives)
1476-
searched in.
1482+
haystack = The $(REF_ALTTEXT input range, isInputRange, std,range,primitives)
1483+
searched in.
14771484
1478-
needle = The element searched for.
1485+
needle = The element searched for.
14791486
14801487
Returns:
14811488
1482-
`haystack` advanced such that the front element is the one searched for;
1483-
that is, until `binaryFun!pred(haystack.front, needle)` is `true`. If no
1484-
such position exists, returns an empty `haystack`.
1489+
`haystack` advanced such that the front element is the one searched for;
1490+
that is, until `binaryFun!pred(haystack.front, needle)` is `true`. If no
1491+
such position exists, returns an empty `haystack`.
14851492
1486-
See_Also:
1487-
$(HTTP sgi.com/tech/stl/_find.html, STL's _find)
1488-
*/
1493+
See_ALso: $(LREF findAdjacent), $(LREF findAmong), $(LREF findSkip), $(LREF findSplit), $(LREF startsWith)
1494+
*/
14891495
InputRange find(alias pred = "a == b", InputRange, Element)(InputRange haystack, scope Element needle)
14901496
if (isInputRange!InputRange &&
14911497
is (typeof(binaryFun!pred(haystack.front, needle)) : bool))
@@ -1760,35 +1766,7 @@ if (isInputRange!InputRange &&
17601766
assert([x].find(x).empty == false);
17611767
}
17621768

1763-
/**
1764-
Advances the input range `haystack` by calling `haystack.popFront`
1765-
until either `pred(haystack.front)`, or $(D
1766-
haystack.empty). Performs $(BIGOH haystack.length) evaluations of $(D
1767-
pred).
1768-
1769-
To _find the last element of a
1770-
$(REF_ALTTEXT bidirectional, isBidirectionalRange, std,range,primitives) `haystack` satisfying
1771-
`pred`, call `find!(pred)(retro(haystack))`. See $(REF retro, std,range).
1772-
1773-
`find` behaves similar to `dropWhile` in other languages.
1774-
1775-
Params:
1776-
1777-
pred = The predicate for determining if a given element is the one being
1778-
searched for.
1779-
1780-
haystack = The $(REF_ALTTEXT input range, isInputRange, std,range,primitives) to
1781-
search in.
1782-
1783-
Returns:
1784-
1785-
`haystack` advanced such that the front element is the one searched for;
1786-
that is, until `binaryFun!pred(haystack.front, needle)` is `true`. If no
1787-
such position exists, returns an empty `haystack`.
1788-
1789-
See_Also:
1790-
$(HTTP sgi.com/tech/stl/find_if.html, STL's find_if)
1791-
*/
1769+
/// ditto
17921770
InputRange find(alias pred, InputRange)(InputRange haystack)
17931771
if (isInputRange!InputRange)
17941772
{
@@ -1842,31 +1820,7 @@ if (isInputRange!InputRange)
18421820
assert(find!(a=>a%4 == 0)("日本語") == "本語");
18431821
}
18441822

1845-
/**
1846-
Finds the first occurrence of a forward range in another forward range.
1847-
1848-
Performs $(BIGOH walkLength(haystack) * walkLength(needle)) comparisons in the
1849-
worst case. There are specializations that improve performance by taking
1850-
advantage of $(REF_ALTTEXT bidirectional range, isBidirectionalRange, std,range,primitives)
1851-
or random access in the given ranges (where possible), depending on the statistics
1852-
of the two ranges' content.
1853-
1854-
Params:
1855-
1856-
pred = The predicate to use for comparing respective elements from the haystack
1857-
and the needle. Defaults to simple equality `"a == b"`.
1858-
1859-
haystack = The $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives)
1860-
searched in.
1861-
1862-
needle = The $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives)
1863-
searched for.
1864-
1865-
Returns:
1866-
1867-
`haystack` advanced such that `needle` is a prefix of it (if no
1868-
such position exists, returns `haystack` advanced to termination).
1869-
*/
1823+
/// ditto
18701824
R1 find(alias pred = "a == b", R1, R2)(R1 haystack, scope R2 needle)
18711825
if (isForwardRange!R1 && isForwardRange!R2
18721826
&& is(typeof(binaryFun!pred(haystack.front, needle.front)) : bool))
@@ -2664,8 +2618,7 @@ Returns:
26642618
`seq` advanced to the first matching element, or until empty if there are no
26652619
matching elements.
26662620
2667-
See_Also:
2668-
$(HTTP sgi.com/tech/stl/find_first_of.html, STL's find_first_of)
2621+
See_Also: $(LREF find), $(REF std,algorithm,comparison,among)
26692622
*/
26702623
InputRange findAmong(alias pred = "a == b", InputRange, ForwardRange)(
26712624
InputRange seq, ForwardRange choices)
@@ -2700,6 +2653,11 @@ if (isInputRange!InputRange && isForwardRange!ForwardRange)
27002653
* Finds `needle` in `haystack` and positions `haystack`
27012654
* right after the first occurrence of `needle`.
27022655
*
2656+
* If no needle is provided, the `haystack` is advanced as long as `pred`
2657+
* evaluates to `true`.
2658+
* Similarly, the haystack is positioned so as `pred` evaluates to `false` for
2659+
* `haystack.front`.
2660+
*
27032661
* Params:
27042662
* haystack = The
27052663
* $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) to search
@@ -2711,7 +2669,10 @@ if (isInputRange!InputRange && isForwardRange!ForwardRange)
27112669
*
27122670
* Returns: `true` if the needle was found, in which case `haystack` is
27132671
* positioned after the end of the first occurrence of `needle`; otherwise
2714-
* `false`, leaving `haystack` untouched.
2672+
* `false`, leaving `haystack` untouched. If no needle is provided, it returns
2673+
* the number of times `pred(haystack.front)` returned true.
2674+
*
2675+
* See_Also: $(LREF find)
27152676
*/
27162677
bool findSkip(alias pred = "a == b", R1, R2)(ref R1 haystack, R2 needle)
27172678
if (isForwardRange!R1 && isForwardRange!R2
@@ -2742,18 +2703,7 @@ if (isForwardRange!R1 && isForwardRange!R2
27422703
assert(findSkip(s, "def") && s.empty);
27432704
}
27442705

2745-
/**
2746-
* Advances the `haystack` as long as `pred` evaluates to `true`.
2747-
* The haystack is positioned so as pred evaluates to false for haystack.front.
2748-
*
2749-
* Params:
2750-
* haystack = The
2751-
* $(REF_ALTTEXT forward range, isForwardRange, std,range,primitives) to search
2752-
* in.
2753-
* pred = Custom predicate for comparison of haystack and needle
2754-
*
2755-
* Returns: The number of times `pred(haystack.front)` returned true.
2756-
*/
2706+
/// ditto
27572707
size_t findSkip(alias pred, R1)(ref R1 haystack)
27582708
if (isForwardRange!R1 && ifTestable!(typeof(haystack.front), unaryFun!pred))
27592709
{
@@ -2827,6 +2777,8 @@ A sub-type of `Tuple!()` of the split portions of `haystack` (see above for
28272777
details). This sub-type of `Tuple!()` has `opCast` defined for `bool`. This
28282778
`opCast` returns `true` when the separating `needle` was found
28292779
(`!result[1].empty`) and `false` otherwise.
2780+
2781+
See_Also: $(LREF find)
28302782
*/
28312783
auto findSplit(alias pred = "a == b", R1, R2)(R1 haystack, R2 needle)
28322784
if (isForwardRange!R1 && isForwardRange!R2)
@@ -3192,6 +3144,8 @@ Returns: The minimum, respectively maximum element of a range together with the
31923144
number it occurs in the range.
31933145
31943146
Throws: `Exception` if `range.empty`.
3147+
3148+
See_Also: $(REF min, std,algorithm,comparison), $(LREF minIndex), $(LREF minElement), $(LREF minPos)
31953149
*/
31963150
Tuple!(ElementType!Range, size_t)
31973151
minCount(alias pred = "a < b", Range)(Range range)
@@ -3398,7 +3352,9 @@ Params:
33983352
Returns: The minimal element of the passed-in range.
33993353
34003354
See_Also:
3401-
$(REF min, std,algorithm,comparison)
3355+
3356+
$(LREF maxElement), $(REF min, std,algorithm,comparison), $(LREF minCount),
3357+
$(LREF minIndex), $(LREF minPos)
34023358
*/
34033359
auto minElement(alias map, Range)(Range r)
34043360
if (isInputRange!Range && !isInfinite!Range)
@@ -3511,8 +3467,11 @@ Params:
35113467
35123468
Returns: The maximal element of the passed-in range.
35133469
3470+
35143471
See_Also:
3515-
$(REF max, std,algorithm,comparison)
3472+
3473+
$(LREF minElement), $(REF max, std,algorithm,comparison), $(LREF maxCount),
3474+
$(LREF maxIndex), $(LREF maxPos)
35163475
*/
35173476
auto maxElement(alias map, Range)(Range r)
35183477
if (isInputRange!Range && !isInfinite!Range)
@@ -3637,6 +3596,8 @@ Returns: The position of the minimum (respectively maximum) element of forward
36373596
range `range`, i.e. a subrange of `range` starting at the position of its
36383597
smallest (respectively largest) element and with the same ending as `range`.
36393598
3599+
See_Also:
3600+
$(REF max, std,algorithm,comparison), $(LREF minCount), $(LREF minIndex), $(LREF minElement)
36403601
*/
36413602
Range minPos(alias pred = "a < b", Range)(Range range)
36423603
if (isForwardRange!Range && !isInfinite!Range &&
@@ -3738,15 +3699,15 @@ Params:
37383699
range = The $(REF_ALTTEXT input range, isInputRange, std,range,primitives)
37393700
to search.
37403701
3741-
Complexity: O(n)
3742-
Exactly `n - 1` comparisons are needed.
3702+
Complexity: $(BIGOH range.length)
3703+
Exactly `range.length - 1` comparisons are needed.
37433704
37443705
Returns:
37453706
The index of the first encounter of the minimum element in `range`. If the
37463707
`range` is empty, -1 is returned.
37473708
37483709
See_Also:
3749-
$(REF min, std,algorithm,comparison), $(LREF minCount), $(LREF minElement), $(LREF minPos)
3710+
$(LREF maxIndex), $(REF min, std,algorithm,comparison), $(LREF minCount), $(LREF minElement), $(LREF minPos)
37503711
*/
37513712
sizediff_t minIndex(alias pred = "a < b", Range)(Range range)
37523713
if (isForwardRange!Range && !isInfinite!Range &&
@@ -3852,8 +3813,8 @@ if (isForwardRange!Range && !isInfinite!Range &&
38523813
/**
38533814
Computes the index of the first occurrence of `range`'s maximum element.
38543815
3855-
Complexity: O(n)
3856-
Exactly `n - 1` comparisons are needed.
3816+
Complexity: $(BIGOH range)
3817+
Exactly `range.length - 1` comparisons are needed.
38573818
38583819
Params:
38593820
pred = The ordering predicate to use to determine the maximum element.
@@ -3864,7 +3825,7 @@ Returns:
38643825
`range` is empty, -1 is returned.
38653826
38663827
See_Also:
3867-
$(REF max, std,algorithm,comparison), $(LREF maxCount), $(LREF maxElement), $(LREF maxPos)
3828+
$(LREF minIndex), $(REF max, std,algorithm,comparison), $(LREF maxCount), $(LREF maxElement), $(LREF maxPos)
38683829
*/
38693830
sizediff_t maxIndex(alias pred = "a < b", Range)(Range range)
38703831
if (isInputRange!Range && !isInfinite!Range &&

0 commit comments

Comments
 (0)