diff options
| author | Eugen Wissner <belka@caraus.de> | 2019-01-28 08:22:38 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2019-01-28 08:30:54 +0100 |
| commit | 4566cf7857ee25b5c3c52f93a46e8d0e903d3f0a (patch) | |
| tree | 652dc0a5213571b725013d49d99da929f10ab0f0 /source | |
| parent | 0a2798cc9693525f81617670201bb4a8adc23391 (diff) | |
| download | tanya-4566cf7857ee25b5c3c52f93a46e8d0e903d3f0a.tar.gz | |
meta.metafunction: Add Enumerate and EnumerateFrom
Diffstat (limited to 'source')
| -rw-r--r-- | source/tanya/meta/metafunction.d | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/source/tanya/meta/metafunction.d b/source/tanya/meta/metafunction.d index eff3ca0..6da6b47 100644 --- a/source/tanya/meta/metafunction.d +++ b/source/tanya/meta/metafunction.d @@ -1801,3 +1801,62 @@ if (T.length == 2) static assert(is(Select!(true, int, float) == int)); static assert(is(Select!(false, int, float) == float)); } + +/** + * Attaces a numeric index to each element from $(D_PARAM Args). + * + * $(D_PSYMBOL EnumerateFrom) returns a sequence of tuples ($(D_PSYMBOL Pack)s) + * consisting of the index of each element and the element itself. + * + * Params: + * start = Enumeration initial value. + * Args = Enumerated sequence. + * + * See_Also: $(D_PSYMBOL Enumerate). + */ +template EnumerateFrom(size_t start, Args...) +{ + static if (Args.length == 0) + { + alias EnumerateFrom = AliasSeq!(); + } + else + { + alias EnumerateFrom = AliasSeq!(Pack!(start, Args[0]), EnumerateFrom!(start + 1, Args[1 .. $])); + } +} + +/// +@nogc nothrow pure @safe unittest +{ + static assert(EnumerateFrom!(0, int, uint, bool).length == 3); +} + +/// +@nogc nothrow pure @safe unittest +{ + alias Expected = AliasSeq!(Pack!(cast(size_t) 0, int), + Pack!(cast(size_t) 1, uint)); + static assert(is(EnumerateFrom!(0, int, uint) == Expected)); +} + +/** + * Attaces a numeric index to each element from $(D_PARAM Args). + * + * $(D_PSYMBOL EnumerateFrom) returns a sequence of tuples ($(D_PSYMBOL Pack)s) + * consisting of the index of each element and the element itself. + * + * Params: + * Args = Enumerated sequence. + * + * See_Also: $(D_PSYMBOL EnumerateFrom). + */ +alias Enumerate(Args...) = EnumerateFrom!(0, Args); + +/// +@nogc nothrow pure @safe unittest +{ + alias Expected = AliasSeq!(Pack!(cast(size_t) 0, int), + Pack!(cast(size_t) 1, uint)); + static assert(is(Enumerate!(int, uint) == Expected)); +} |
