std.range.Chunks/chunks
- multiple declarations
Function chunks
This range iterates over fixed-sized chunks of size chunkSize
of a
source
range. Source
must be an input range.
chunkSize
must be greater than zero.
If !isInfinite!Source
and source
is not evenly
divisible by chunkSize
, the back element of this range will contain
fewer than chunkSize
elements.
If Source
is a forward range, the resulting range will be forward ranges as
well. Otherwise, the resulting chunks will be input ranges consuming the same
input
iterating over front
will shrink the chunk such that subsequent
invocations of front
will no longer return the full chunk, and calling
popFront
on the outer range will invalidate any lingering references to
previous values of front
.
Parameters
Name | Description |
---|---|
source | Range from which the chunks will be selected |
chunkSize | Chunk size |
See Also
Returns
Range of chunks.
Example
import std .algorithm .comparison : equal;
auto source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
auto chunks = chunks(source, 4);
writeln(chunks[0]); // [1, 2, 3, 4]
writeln(chunks[1]); // [5, 6, 7, 8]
writeln(chunks[2]); // [9, 10]
writeln(chunks .back); // chunks[2]
writeln(chunks .front); // chunks[0]
writeln(chunks .length); // 3
assert(equal(retro(array(chunks)), array(retro(chunks))));
Example
Non-forward input ranges are supported, but with limited semantics.
import std .algorithm .comparison : equal;
int i;
// The generator doesn't save state, so it cannot be a forward range.
auto inputRange = generate!(() => ++i) .take(10);
// We can still process it in chunks, but it will be single-pass only.
auto chunked = inputRange .chunks(2);
assert(chunked .front .equal([1, 2]));
assert(chunked .front .empty); // Iterating the chunk has consumed it
chunked .popFront;
assert(chunked .front .equal([3, 4]));
Struct Chunks
This range iterates over fixed-sized chunks of size chunkSize
of a
source
range. Source
must be an input range.
chunkSize
must be greater than zero.
struct Chunks(Source)
if (isInputRange!Source);
If !isInfinite!Source
and source
is not evenly
divisible by chunkSize
, the back element of this range will contain
fewer than chunkSize
elements.
If Source
is a forward range, the resulting range will be forward ranges as
well. Otherwise, the resulting chunks will be input ranges consuming the same
Constructors
Name | Description |
---|---|
this
(source, chunkSize)
|
Standard constructor |
Properties
Name | Type | Description |
---|---|---|
back [get]
|
auto | Bidirectional range primitives. Provided only if both
hasSlicing!Source and hasLength!Source are true .
|
empty [get]
|
bool | Input range primitives. Always present. |
front [get]
|
auto | Input range primitives. Always present. |
length [get]
|
size_t | Length. Only if hasLength!Source is true
|
save [get]
|
typeof(this) | Forward range primitives. Only present if Source is a forward range.
|
Methods
Name | Description |
---|---|
opIndex
(index)
|
Indexing and slicing operations. Provided only if
hasSlicing!Source is true .
|
opSlice
(lower, upper)
|
Indexing and slicing operations. Provided only if
hasSlicing!Source is true .
|
popBack
()
|
Bidirectional range primitives. Provided only if both
hasSlicing!Source and hasLength!Source are true .
|
popFront
()
|
Input range primitives. Always present. |
input
iterating over front
will shrink the chunk such that subsequent
invocations of front
will no longer return the full chunk, and calling
popFront
on the outer range will invalidate any lingering references to
previous values of front
.
Parameters
Name | Description |
---|---|
source | Range from which the chunks will be selected |
chunkSize | Chunk size |
See Also
Returns
Range of chunks.
Example
import std .algorithm .comparison : equal;
auto source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
auto chunks = chunks(source, 4);
writeln(chunks[0]); // [1, 2, 3, 4]
writeln(chunks[1]); // [5, 6, 7, 8]
writeln(chunks[2]); // [9, 10]
writeln(chunks .back); // chunks[2]
writeln(chunks .front); // chunks[0]
writeln(chunks .length); // 3
assert(equal(retro(array(chunks)), array(retro(chunks))));
Example
Non-forward input ranges are supported, but with limited semantics.
import std .algorithm .comparison : equal;
int i;
// The generator doesn't save state, so it cannot be a forward range.
auto inputRange = generate!(() => ++i) .take(10);
// We can still process it in chunks, but it will be single-pass only.
auto chunked = inputRange .chunks(2);
assert(chunked .front .equal([1, 2]));
assert(chunked .front .empty); // Iterating the chunk has consumed it
chunked .popFront;
assert(chunked .front .equal([3, 4]));
Authors
Andrei Alexandrescu, David Simcha, Jonathan M Davis, and Jack Stouffer. Credit for some of the ideas in building this module goes to Leonardo Maffi.