rangeextra
Some extra ranges that were not included in std.range.
Author:
David Simcha
- template
isIterable
(T)
- Tests whether T can be iterated over using foreach. This is a superset
of isInputRange, as it also accepts things that use opApply, builtin
arrays, builtin associative arrays, etc. Useful when all you need is
lowest common denominator iteration functionality and you don't care about
more advanced range features.
- template
IterType
(T)
- Determine the iterable type of any iterable object, regardless of whether
it uses ranges, opApply, etc. This is typeof(elem) if one does
foreach(elem; T.init) {}.
- struct
Reindex
(alias lambda,T) if (isRandomAccessRange!(T));
- Reindex!(lambda,T)
reindex
(alias lambda, T)(T range);
- Reindex a random-access range by translating its indices via the function
lambda. All properties and methods of the underlying range that do not
involve indexing are forwarded to the underlying range using alias this.
Examples:
// An array indexed by only even numbers.
auto evenArray = reindex!"a / 2"([0,2,4,6,8,10].dup);
for(uint i = 0; i <= 10; i += 2) {
assert(evenArray[i] == i);
}
// A one-indexed array.
auto oneArray = reindex!"a - 1"([1,2,3,4,5].dup);
for(uint i = 1; i <= 5; i++) {
assert(oneArray[i] == i);
}
- struct
Rotated
(T) if (isRandomAccessRange!(T) && hasLength!(T));
- Rotated!(T)
rotated
(T)(T range, size_t startPos = 0);
- Gives a
rotated
view of a random access range without modifying the
underlying range. Also funtions as an output range. Given an underlying
range of length L, a Rotated range remembers the last L elements inserted
via its put() interface, in the order that they were inserted. Insertions
are O(1), but to the user of the API, the effect is similar to shifting
an array on each insertion.
Examples:
uint[] arr = [5,6,7,8,9];
auto rot = rotated(arr, 2);
assert(rot[0] == 7);
assert(rot[3] == 5);
assert(rot.length == 5);
rot.put(0);
rot.put(1);
rot.put(2);
rot.put(3);
rot.put(4);
foreach(i; 0..5) {
assert(rot[i] == i);
}
- struct
StaticArray
(E,size_t N);
- A struct wrapper for a static array, to get around the fact that static
arrays can't be returned from functions. Alias this is used to forward
all operations to the underlying static array.
- E[N]
values
;
- struct
Comb
(size_t N,T) if (isForwardRange!(T));
- Comb!(N,T)
comb
(uint N, T)(T range);
- A struct to iterate over all possible unordered combinations of elements
of size N, in a forward range. This struct is itself a forward range.
Each call to front() returns a StaticArray of size N, containing the
relevant elements of the range.
Examples:
auto foo = map!(to!(uint, string))(cast(string[]) ["1", "2", "3"]);
auto c = comb!2(foo);
assert(c.front == [1,2]);
c.popFront;
assert(c.front == [1,3]);
c.popFront;
assert(c.front == [2,3]);
c.popFront;
assert(c.empty);
- struct
Broadcast
(T...) if (allSatisfy!(isForwardRange,T));
- Broadcast!(T)
broadcast
(T...)(T ranges);
- Given a set of forward ranges, iterate over all possible tuples composed of
[range1Element, range2Element, ..., rangeNElement]. Calling front()
returns a StaticArray if the element types of all of the ranges are
identical and a std.typecons.Tuple otherwise.
Examples:
// Iterate over all possible RNA codons.
immutable bases = "ACGU";
auto codonRange = broadcast(bases, bases, bases);
assert(codonRange.front == "AAA");
codonRange.popFront;
assert(codonRange.front == "AAC");
- struct
TNew
(T);
- An array with a capacity field. It's based on D's builtin arrays and
forwards everything except appending and resizing to the underlying builtin
array using alias this. Therefore, the compile time interface and semantics
of a
TNew
are the same as those of a builtin array, modulo a few bugs in
alias this.
Examples:
TNew!uint ab, ab2;
foreach(i; 0..5) {
ab ~= i;
ab2 ~= [i];
}
assert(ab == cast(uint[]) [0,1,2,3,4]);
assert(ab2 == cast(uint[]) [0,1,2,3,4]);
- T[]
arr
;
- Access underlying array directly. Useful for getting around some
alias this bugs.
- this(T[] newData);
- template
opCatAssign
(U) if (Appendable!(T,U) && (Appendable!(T,U[]) || !isArray!(U)))
- Append a single element.
- typeof(this)
opCatAssign
(U elem);
- Append a single element.
- template
opCatAssign
(U) if (Appendable!(T,U) && !(Appendable!(T,U[]) || !isArray!(U)))
- Append an array.
- typeof(this)
opCatAssign
(U elems);
- Append an array.
- typeof(this)
opAssign
(T[] newData);
- size_t
length
(size_t newLen);
- void
reserve
(size_t nElem);
- Make the capacity big enough to hold at least nElem elements
without further GC activity.
- TNew!(T)
tNew
(T)(T[] data);
- Array builder literals.
|