View source code
Display the source code in std/range/primitives.d from which this page was generated on
github.
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.
Page wiki
View or edit the community-maintained wiki page associated with this page.
Module std.range.primitives
This module is a submodule of std.range.
It provides basic range functionality by defining several templates for testing
whether a given object
is a range, and what kind of range it is:
|
Tests if something is an input rangedefined to be
something from which one can sequentially read data using the
primitives , , and .
|
|
Tests if something is an output range, defined to be
something to which one can sequentially write data using the
primitive.
|
|
Tests if something is a forward range, defined to be an
input range with the additional capability that one can save one's
current position with the primitive, thus allowing one to
iterate over the same range multiple times.
|
|
Tests if something is a bidirectional range, that is, a
forward range that allows reverse traversal using the primitives and .
|
|
Tests if something is a random access range, which is a
bidirectional range that also supports the array subscripting
operation via the primitive opIndex .
|
It also provides number of templates that test for various range capabilities:
|
Tests if a given range's elements can be moved around using the
primitives
, or .
|
|
Returns the element type of a given range. |
|
Returns the encoding element type of a given range. |
|
Tests if a range is a forward range with swappable elements. |
|
Tests if a range is a forward range with mutable elements. |
|
Tests if a range is a forward range with elements that can be passed by reference and have their address taken. |
|
Tests if a given range has the length attribute.
|
|
Tests if a given range is an infinite range. |
|
Tests if a given range supports the array slicing operation R[x..y] .
|
Finally, it includes some convenience functions for manipulating ranges:
|
Advances a given range by up to n elements. |
|
Advances a given bidirectional range from the right by up to n elements. |
|
Advances a given range by up exactly n elements. |
|
Advances a given bidirectional range from the right by exactly n elements. |
|
Removes the front element of a range.
|
|
Removes the back element of a bidirectional range.
|
|
Removes the i'th element of a random-access range. |
|
Computes the length of any range in O(n) time. |
Functions
Name | Description |
---|---|
back
|
Implements the range interface primitive for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.back is
equivalent to . For narrow strings, automatically returns the last code point as a dchar .
|
empty
|
Implements the range interface primitive for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.empty is
equivalent to .
|
front
|
Implements the range interface primitive for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.front is
equivalent to . For narrow strings, automatically returns the first code point as a dchar .
|
moveAt
|
Moves element at index of out and returns it. Leaves r.front in a destroyable state that does not allocate any resources
(usually equal to its .init value).
|
moveBack
|
Moves the back of out and returns it. Leaves r.back in a
destroyable state that does not allocate any resources (usually equal
to its .init value).
|
moveFront
|
Moves the front of out and returns it. Leaves r.front in a
destroyable state that does not allocate any resources (usually equal
to its .init value).
|
popBack
|
Implements the range interface primitive for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.popBack is
equivalent to . For narrow strings, automatically eliminates the last code point.
|
popBackExactly
|
Eagerly advances itself (not a copy) exactly times (by
calling r.popFront ). takes by ref ,
so it mutates the original range. Completes in Ο(1 ) steps for ranges
that support slicing, and have either length or are infinite.
Completes in Ο( ) time for all other ranges.
|
popBackN
|
Eagerly advances itself (not a copy) up to times (by
calling r.popFront ). takes by ref ,
so it mutates the original range. Completes in Ο(1 ) steps for ranges
that support slicing and have length.
Completes in Ο( ) time for all other ranges.
|
popFront
|
Implements the range interface primitive for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.popFront is
equivalent to . For narrow strings,
automatically advances to the next code
point.
|
popFrontExactly
|
Eagerly advances itself (not a copy) exactly times (by
calling r.popFront ). takes by ref ,
so it mutates the original range. Completes in Ο(1 ) steps for ranges
that support slicing, and have either length or are infinite.
Completes in Ο( ) time for all other ranges.
|
popFrontN
|
Eagerly advances itself (not a copy) up to times (by
calling r.popFront ). takes by ref ,
so it mutates the original range. Completes in Ο(1 ) steps for ranges
that support slicing and have length.
Completes in Ο( ) time for all other ranges.
|
put
|
Outputs to . The exact effect is dependent upon the two
types. Several cases are accepted, as described below. The code snippets
are attempted in order, and the first to compile "wins" and gets
evaluated.
|
save
|
Implements the range interface primitive for built-in
arrays. Due to the fact that nonmember functions can be called with
the first argument using the dot notation, array.save is
equivalent to . The function does not duplicate the
content of the array, it simply returns its argument.
|
walkLength
|
This is a best-effort implementation of length for any kind of
range .
|
Templates
Name | Description |
---|---|
ElementEncodingType
|
The encoding element type of R . For narrow strings (char[] ,
wchar[] and their qualified variants including string and
wstring ), is the character type of the
string. For all other types, is the same as
.
|
ElementType
|
The element type of R . R does not have to be a range. The
element type is determined as the type yielded by r.front for an
object r of type R . For example, is
T if T[] isn't a narrow string; if it is, the element type is
dchar . If R doesn't have , is
void .
|
isInfinite
|
Returns true if R is an infinite input range. An
infinite input range is an input range that has a statically-defined
enumerated member called that is always false ,
for example:
|
Enum values
Name | Type | Description |
---|---|---|
hasAssignableElements
|
Returns true if R is an input range and has mutable
elements. The following code should compile for any range
with assignable elements.
|
|
hasLength
|
Returns true if R has a length member that returns an
integral type. R does not have to be a range. Note that length is an optional primitive as no range must implement it. Some
ranges do not store their length explicitly, some cannot compute it
without actually exhausting the range (e.g. socket streams), and some
other ranges may be infinite.
|
|
hasLvalueElements
|
Tests whether the range R has lvalue elements. These are defined as
elements that can be passed by reference and have their address taken.
The following code should compile for any range with lvalue elements.
|
|
hasMobileElements
|
Returns true iff R is an input range that supports the
primitive, as well as and if it's a
bidirectional or random access range. These may be explicitly implemented, or
may work via the default behavior of the module level functions
and friends. The following code should compile for any range
with mobile elements.
|
|
hasSlicing
|
Returns true if R offers a slicing operator with integral boundaries
that returns a forward range type.
|
|
hasSwappableElements
|
Returns true if R is an input range and has swappable
elements. The following code should compile for any range
with swappable elements.
|
|
isBidirectionalRange
|
Returns true if R is a bidirectional range. A bidirectional
range is a forward range that also offers the primitives and
. The following code should compile for any bidirectional
range.
|
|
isForwardRange
|
Returns true if R is a forward range. A forward range is an
input range r that can save "checkpoints" by saving r.save
to another value of type R . Notable examples of input ranges that
are not forward ranges are file/socket ranges; copying such a
range will not save the position in the stream, and they most likely
reuse an internal buffer as the entire stream does not sit in
memory. Subsequently, advancing either the original or the copy will
advance the stream, so the copies are not independent.
|
|
isInputRange
|
Returns true if R is an input range. An input range must
define the primitives , , and . The
following code should compile for any input range.
|
|
isOutputRange
|
Returns true if R is an output range for elements of type
E . An output range is defined functionally as a range that
supports the operation as defined above.
|
|
isRandomAccessRange
|
Returns true if R is a random-access range. A random-access
range is a bidirectional range that also offers the primitive opIndex , OR an infinite forward range that offers opIndex . In
either case, the range must either offer length or be
infinite. The following code should compile for any random-access
range.
|
Authors
Andrei Alexandrescu, David Simcha, and Jonathan M Davis. Credit for some of the ideas in building this module goes to Leonardo Maffi.