Skip to content

Commit 3a9a8b8

Browse files
authored
Merge pull request #6119 from wilzbach/slide-docs
Move public examples of slides, s.t. they can be detected by ddoc merged-on-behalf-of: Jack Stouffer <[email protected]>
2 parents eea8949 + 425c02a commit 3a9a8b8

File tree

1 file changed

+57
-57
lines changed

1 file changed

+57
-57
lines changed

std/range/package.d

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7752,6 +7752,63 @@ auto slide(Flag!"withPartial" f = Yes.withPartial,
77527752
return Slides!(f, Source)(source, windowSize, stepSize);
77537753
}
77547754

7755+
/// Iterate over ranges with windows
7756+
@safe pure nothrow unittest
7757+
{
7758+
import std.algorithm.comparison : equal;
7759+
7760+
assert([0, 1, 2, 3].slide(2).equal!equal(
7761+
[[0, 1], [1, 2], [2, 3]]
7762+
));
7763+
7764+
assert(5.iota.slide(3).equal!equal(
7765+
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
7766+
));
7767+
}
7768+
7769+
/// set a custom stepsize (default 1)
7770+
@safe pure nothrow unittest
7771+
{
7772+
import std.algorithm.comparison : equal;
7773+
7774+
assert(6.iota.slide(1, 2).equal!equal(
7775+
[[0], [2], [4]]
7776+
));
7777+
7778+
assert(6.iota.slide(2, 4).equal!equal(
7779+
[[0, 1], [4, 5]]
7780+
));
7781+
7782+
assert(iota(7).slide(2, 2).equal!equal(
7783+
[[0, 1], [2, 3], [4, 5], [6]]
7784+
));
7785+
7786+
assert(iota(12).slide(2, 4).equal!equal(
7787+
[[0, 1], [4, 5], [8, 9]]
7788+
));
7789+
}
7790+
7791+
/// Allow the last slide to have fewer elements than windowSize
7792+
@safe pure nothrow unittest
7793+
{
7794+
import std.algorithm.comparison : equal;
7795+
7796+
assert(3.iota.slide!(No.withPartial)(4).empty);
7797+
assert(3.iota.slide!(Yes.withPartial)(4).equal!equal(
7798+
[[0, 1, 2]]
7799+
));
7800+
}
7801+
7802+
/// Count all the possible substrings of length 2
7803+
@safe pure nothrow unittest
7804+
{
7805+
import std.algorithm.iteration : each;
7806+
7807+
int[dstring] d;
7808+
"AGAGA"d.slide!(Yes.withPartial)(2).each!(a => d[a]++);
7809+
assert(d == ["AG"d: 2, "GA"d: 2]);
7810+
}
7811+
77557812
private struct Slides(Flag!"withPartial" withPartial = Yes.withPartial, Source)
77567813
if (isForwardRange!Source)
77577814
{
@@ -8279,63 +8336,6 @@ public:
82798336
}
82808337
}
82818338

8282-
/// Iterate over ranges with windows
8283-
@safe pure nothrow unittest
8284-
{
8285-
import std.algorithm.comparison : equal;
8286-
8287-
assert([0, 1, 2, 3].slide(2).equal!equal(
8288-
[[0, 1], [1, 2], [2, 3]]
8289-
));
8290-
8291-
assert(5.iota.slide(3).equal!equal(
8292-
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
8293-
));
8294-
}
8295-
8296-
/// set a custom stepsize (default 1)
8297-
@safe pure nothrow unittest
8298-
{
8299-
import std.algorithm.comparison : equal;
8300-
8301-
assert(6.iota.slide(1, 2).equal!equal(
8302-
[[0], [2], [4]]
8303-
));
8304-
8305-
assert(6.iota.slide(2, 4).equal!equal(
8306-
[[0, 1], [4, 5]]
8307-
));
8308-
8309-
assert(iota(7).slide(2, 2).equal!equal(
8310-
[[0, 1], [2, 3], [4, 5], [6]]
8311-
));
8312-
8313-
assert(iota(12).slide(2, 4).equal!equal(
8314-
[[0, 1], [4, 5], [8, 9]]
8315-
));
8316-
}
8317-
8318-
/// Allow the last slide to have fewer elements than windowSize
8319-
@safe pure nothrow unittest
8320-
{
8321-
import std.algorithm.comparison : equal;
8322-
8323-
assert(3.iota.slide!(No.withPartial)(4).empty);
8324-
assert(3.iota.slide!(Yes.withPartial)(4).equal!equal(
8325-
[[0, 1, 2]]
8326-
));
8327-
}
8328-
8329-
/// Count all the possible substrings of length 2
8330-
@safe pure nothrow unittest
8331-
{
8332-
import std.algorithm.iteration : each;
8333-
8334-
int[dstring] d;
8335-
"AGAGA"d.slide!(Yes.withPartial)(2).each!(a => d[a]++);
8336-
assert(d == ["AG"d: 2, "GA"d: 2]);
8337-
}
8338-
83398339
// test @nogc
83408340
@safe pure nothrow @nogc unittest
83418341
{

0 commit comments

Comments
 (0)