Skip to content

Commit 5c413c6

Browse files
authored
Merge pull request #4973 from wilzbach/add-examples-to-std-algorithm
Add public examples to std.algorithm
2 parents b6bcf75 + e2025c2 commit 5c413c6

File tree

3 files changed

+62
-51
lines changed

3 files changed

+62
-51
lines changed

std/algorithm/mutation.d

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,24 @@ if (s == SwapStrategy.stable
17551755
return result;
17561756
}
17571757

1758+
///
1759+
@safe pure unittest
1760+
{
1761+
import std.typecons : tuple;
1762+
1763+
auto a = [ 0, 1, 2, 3, 4, 5 ];
1764+
assert(remove!(SwapStrategy.stable)(a, 1) == [ 0, 2, 3, 4, 5 ]);
1765+
a = [ 0, 1, 2, 3, 4, 5 ];
1766+
assert(remove!(SwapStrategy.stable)(a, 1, 3) == [ 0, 2, 4, 5] );
1767+
a = [ 0, 1, 2, 3, 4, 5 ];
1768+
assert(remove!(SwapStrategy.stable)(a, 1, tuple(3, 6)) == [ 0, 2 ]);
1769+
1770+
a = [ 0, 1, 2, 3, 4, 5 ];
1771+
assert(remove!(SwapStrategy.unstable)(a, 1) == [0, 5, 2, 3, 4]);
1772+
a = [ 0, 1, 2, 3, 4, 5 ];
1773+
assert(remove!(SwapStrategy.unstable)(a, tuple(1, 4)) == [0]);
1774+
}
1775+
17581776
@safe unittest
17591777
{
17601778
import std.exception : assertThrown;

std/algorithm/searching.d

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,6 @@ if (isInputRange!(Range) && is(typeof(r.front == lPar)))
270270
* invoke the Boyer-Moore matching algorithm for finding of $(D needle) in a
271271
* given haystack.
272272
*/
273-
BoyerMooreFinder!(binaryFun!(pred), Range) boyerMooreFinder
274-
(alias pred = "a == b", Range)
275-
(Range needle) if ((isRandomAccessRange!(Range) && hasSlicing!Range) || isSomeString!Range)
276-
{
277-
return typeof(return)(needle);
278-
}
279-
280-
/// Ditto
281273
struct BoyerMooreFinder(alias pred, Range)
282274
{
283275
private:
@@ -379,6 +371,29 @@ public:
379371
alias opDollar = length;
380372
}
381373

374+
/// Ditto
375+
BoyerMooreFinder!(binaryFun!(pred), Range) boyerMooreFinder
376+
(alias pred = "a == b", Range)
377+
(Range needle) if ((isRandomAccessRange!(Range) && hasSlicing!Range) || isSomeString!Range)
378+
{
379+
return typeof(return)(needle);
380+
}
381+
382+
///
383+
@safe pure nothrow unittest
384+
{
385+
auto bmFinder = boyerMooreFinder("TG");
386+
387+
string r = "TAGTGCCTGA";
388+
// search for the first match in the haystack r
389+
r = bmFinder.beFound(r);
390+
assert(r == "TGCCTGA");
391+
392+
// continue search in haystack
393+
r = bmFinder.beFound(r[2 .. $]);
394+
assert(r == "TGA");
395+
}
396+
382397
/**
383398
Returns the common prefix of two ranges.
384399

std/algorithm/sorting.d

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2938,39 +2938,11 @@ schwartzSort(alias transform, alias less = "a < b",
29382938
return typeof(return)(r);
29392939
}
29402940

2941-
@safe unittest
2942-
{
2943-
// issue 4909
2944-
import std.typecons : Tuple;
2945-
Tuple!(char)[] chars;
2946-
schwartzSort!"a[0]"(chars);
2947-
}
2948-
2949-
@safe unittest
2950-
{
2951-
// issue 5924
2952-
import std.typecons : Tuple;
2953-
Tuple!(char)[] chars;
2954-
schwartzSort!((Tuple!(char) c){ return c[0]; })(chars);
2955-
}
2956-
2941+
///
29572942
@safe unittest
29582943
{
29592944
import std.algorithm.iteration : map;
2960-
import std.math : log2;
2961-
2962-
debug(std_algorithm) scope(success)
2963-
writeln("unittest @", __FILE__, ":", __LINE__, " done.");
2964-
2965-
static double entropy(double[] probs) {
2966-
double result = 0;
2967-
foreach (ref p; probs)
2968-
{
2969-
if (!p) continue;
2970-
result -= p * log2(p);
2971-
}
2972-
return result;
2973-
}
2945+
import std.numeric : entropy;
29742946

29752947
auto lowEnt = [ 1.0, 0, 0 ],
29762948
midEnt = [ 0.1, 0.1, 0.8 ],
@@ -2980,7 +2952,7 @@ schwartzSort(alias transform, alias less = "a < b",
29802952
arr[1] = lowEnt;
29812953
arr[2] = highEnt;
29822954

2983-
schwartzSort!(entropy, q{a > b})(arr);
2955+
schwartzSort!(entropy, "a > b")(arr);
29842956

29852957
assert(arr[0] == highEnt);
29862958
assert(arr[1] == midEnt);
@@ -2991,21 +2963,11 @@ schwartzSort(alias transform, alias less = "a < b",
29912963
@safe unittest
29922964
{
29932965
import std.algorithm.iteration : map;
2994-
import std.math : log2;
2966+
import std.numeric : entropy;
29952967

29962968
debug(std_algorithm) scope(success)
29972969
writeln("unittest @", __FILE__, ":", __LINE__, " done.");
29982970

2999-
static double entropy(double[] probs) {
3000-
double result = 0;
3001-
foreach (ref p; probs)
3002-
{
3003-
if (!p) continue;
3004-
result -= p * log2(p);
3005-
}
3006-
return result;
3007-
}
3008-
30092971
auto lowEnt = [ 1.0, 0, 0 ],
30102972
midEnt = [ 0.1, 0.1, 0.8 ],
30112973
highEnt = [ 0.31, 0.29, 0.4 ];
@@ -3014,14 +2976,30 @@ schwartzSort(alias transform, alias less = "a < b",
30142976
arr[1] = lowEnt;
30152977
arr[2] = highEnt;
30162978

3017-
schwartzSort!(entropy, q{a < b})(arr);
2979+
schwartzSort!(entropy, "a < b")(arr);
30182980

30192981
assert(arr[0] == lowEnt);
30202982
assert(arr[1] == midEnt);
30212983
assert(arr[2] == highEnt);
30222984
assert(isSorted!("a < b")(map!(entropy)(arr)));
30232985
}
30242986

2987+
@safe unittest
2988+
{
2989+
// issue 4909
2990+
import std.typecons : Tuple;
2991+
Tuple!(char)[] chars;
2992+
schwartzSort!"a[0]"(chars);
2993+
}
2994+
2995+
@safe unittest
2996+
{
2997+
// issue 5924
2998+
import std.typecons : Tuple;
2999+
Tuple!(char)[] chars;
3000+
schwartzSort!((Tuple!(char) c){ return c[0]; })(chars);
3001+
}
3002+
30253003
// partialSort
30263004
/**
30273005
Reorders the random-access range $(D r) such that the range $(D r[0

0 commit comments

Comments
 (0)