unpackenumerate
This module implements enumerate and unpack for ranges.
Author:
David Simcha
License:
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
- template
isIterable
(T)
- Tests whether T can be iterated over using a foreach statement with one
variable. 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 foreach iteration
and not 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
Enumerate
(R,I) if (isIterable!(R) && isIntegral!(I));
- Enumerate!(R,I)
enumerate
(I = size_t, R)(R range);
- Convenience function that provides an index value when iterating over
an iterable object. This object can be a range, an array, an associative
array, or any user-defined type with an opApply that iterates over a
single variable.
Examples:
uint[] foo = [0,2,4,6];
uint[] bar = [8,10,12,14];
foreach(i, elem; enumerate( chain(foo, bar) )) {
assert(i * 2 == elem);
}
Note:
The default integer type used is a size_t. If this isn't wide
enough, you can use a ulong or a BigInt or something.
- struct
Unpack
(R) if (isIterable!(R) && (is(IterType!(R) == struct) || is(IterType!(R) == class)));
- Unpack!(R)
unpack
(R)(R range);
- If IterType!(R) is a std.typecons.Tuple, a struct, a class, or a
std.range.Zip.Proxy, unpacks the elements while iterating.
Examples;
uint[] foo = [1,2,3,4,5];
uint[] bar = [2,4,6,8,10];
uint[] waldo = [3,6,9,12,15];
foreach(f, b, w; unpack( zip(foo, bar, waldo) )) {
assert(b == 2 * f);
assert(w == 3 * f);
}
|