From fc96b0a99d5869ef10f503490c1f41be36d276e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20Nordl=C3=B6w?= Date: Tue, 13 Mar 2018 15:11:40 +0100 Subject: [PATCH] make some traits iterative using static foreach --- .dscanner.ini | 2 +- std/meta.d | 62 ++++++++++++++++++++------------------------------- std/traits.d | 36 ++++++++++++++++++------------ 3 files changed, 47 insertions(+), 53 deletions(-) diff --git a/.dscanner.ini b/.dscanner.ini index 97d423f4b9d..4d33cde6090 100644 --- a/.dscanner.ini +++ b/.dscanner.ini @@ -272,7 +272,7 @@ has_public_example="-etc.c.curl,\ imports_sortedness="+disabled" ;imports_sortedness="-etc.c.curl,-std.algorithm.comparison,-std.algorithm.internal,-std.algorithm.iteration,-std.algorithm.mutation,-std.algorithm.searching,-std.algorithm.setops,-std.algorithm.sorting,-std.array,-std.bigint,-std.bitmanip,-std.c.freebsd.socket,-std.c.linux.pthread,-std.c.process,-std.complex,-std.concurrency,-std.container.array,-std.container.binaryheap,-std.container.dlist,-std.container.rbtree,-std.container.slist,-std.container.util,-std.conv,-std.datetime,-std.datetime.date,-std.datetime.interval,-std.datetime.systime,-std.datetime.timezone,-std.digest,-std.digest.hmac,-std.exception,-std.experimental.allocator,-std.experimental.allocator.building_blocks,-std.experimental.allocator.building_blocks.affix_allocator,-std.experimental.allocator.building_blocks.allocator_list,-std.experimental.allocator.building_blocks.free_list,-std.experimental.allocator.building_blocks.free_tree,-std.experimental.allocator.building_blocks.kernighan_ritchie,-std.experimental.allocator.building_blocks.region,-std.experimental.allocator.common,-std.experimental.allocator.mallocator,-std.experimental.allocator.mmap_allocator,-std.experimental.allocator.showcase,-std.experimental.allocator.typed,-std.experimental.checkedint,-std.experimental.logger.core,-std.experimental.typecons,-std.file,-std.format,-std.functional,-std.getopt,-std.internal.math.biguintcore,-std.internal.test.dummyrange,-std.json,-std.math,-std.meta,-std.mmfile,-std.net.curl,-std.net.isemail,-std.numeric,-std.outbuffer,-std.parallelism,-std.path,-std.process,-std.random,-std.range,-std.range.primitives,-std.regex,-std.regex.internal.backtracking,-std.regex.internal.generator,-std.regex.internal.kickstart,-std.regex.internal.parser,-std.regex.internal.tests,-std.signals,-std.socket,-std.stdio,-std.string,-std.uni,-std.utf,-std.uuid,-std.variant,-std.windows.charset,-std.windows.registry,-std.windows.syserror,-std.zip" ; Checks for labels with the same name as variables -label_var_same_name_check="-std.algorithm.iteration,-std.algorithm.sorting,-std.array,-std.bigint,-std.bitmanip,-std.conv,-std.encoding,-std.experimental.allocator.building_blocks.segregator,-std.experimental.typecons,-std.format,-std.internal.digest.sha_SSSE3,-std.parallelism,-std.process,-std.typecons,-std.utf" +label_var_same_name_check="-std.algorithm.iteration,-std.algorithm.sorting,-std.array,-std.bigint,-std.bitmanip,-std.conv,-std.encoding,-std.experimental.allocator.building_blocks.segregator,-std.experimental.typecons,-std.format,-std.internal.digest.sha_SSSE3,-std.parallelism,-std.process,-std.typecons,-std.utf,-std.traits" ; Checks for subtraction from .length properties length_subtraction_check="+disabled" ;length_subtraction_check="-std.algorithm.internal,-std.algorithm.iteration,-std.algorithm.mutation,-std.algorithm.searching,-std.algorithm.sorting,-std.array,-std.concurrency,-std.container.array,-std.container.binaryheap,-std.conv,-std.datetime.timezone,-std.experimental.allocator.building_blocks.segregator,-std.experimental.logger.core,-std.file,-std.format,-std.getopt,-std.internal.math.biguintcore,-std.internal.math.biguintnoasm,-std.internal.math.biguintx86,-std.internal.scopebuffer,-std.math,-std.net.curl,-std.net.isemail,-std.numeric,-std.parallelism,-std.path,-std.process,-std.range,-std.regex,-std.regex.internal.parser,-std.regex.internal.tests,-std.string,-std.uni,-std.windows.charset,-std.windows.registry,-std.zip" diff --git a/std/meta.d b/std/meta.d index 1bcacbb7b49..e08b0bd8487 100644 --- a/std/meta.d +++ b/std/meta.d @@ -277,13 +277,13 @@ if (!isAggregateType!T || is(Unqual!T == T)) */ template staticIndexOf(T, TList...) { - enum staticIndexOf = genericIndexOf!(T, TList).index; + enum staticIndexOf = genericIndexOf!(T, TList); } /// Ditto template staticIndexOf(alias T, TList...) { - enum staticIndexOf = genericIndexOf!(T, TList).index; + enum staticIndexOf = genericIndexOf!(T, TList); } /// @@ -303,27 +303,17 @@ template staticIndexOf(alias T, TList...) private template genericIndexOf(args...) if (args.length >= 1) { - alias e = OldAlias!(args[0]); - alias tuple = args[1 .. $]; - - static if (tuple.length) + static foreach (idx, arg; args[1 .. $]) { - alias head = OldAlias!(tuple[0]); - alias tail = tuple[1 .. $]; - - static if (isSame!(e, head)) + static if (is(typeof(genericIndexOf) == void) && // not yet defined + isSame!(args[0], arg)) { - enum index = 0; - } - else - { - enum next = genericIndexOf!(e, tail).index; - enum index = (next == -1) ? -1 : 1 + next; + enum genericIndexOf = idx; } } - else + static if (is(typeof(genericIndexOf) == void)) // no hit { - enum index = -1; + enum genericIndexOf = -1; } } @@ -844,19 +834,17 @@ template predicate must be instantiable with all the given items. */ template allSatisfy(alias F, T...) { - static if (T.length == 0) - { - enum allSatisfy = true; - } - else static if (T.length == 1) + static foreach (Ti; T) { - enum allSatisfy = F!(T[0]); + static if (!is(typeof(allSatisfy) == bool) && // not yet defined + !F!(Ti)) + { + enum allSatisfy = false; + } } - else + static if (!is(typeof(allSatisfy) == bool)) // if not yet defined { - enum allSatisfy = - allSatisfy!(F, T[ 0 .. $/2]) && - allSatisfy!(F, T[$/2 .. $ ]); + enum allSatisfy = true; } } @@ -878,19 +866,17 @@ template predicate must be instantiable with one of the given items. */ template anySatisfy(alias F, T...) { - static if (T.length == 0) + static foreach (Ti; T) { - enum anySatisfy = false; - } - else static if (T.length == 1) - { - enum anySatisfy = F!(T[0]); + static if (!is(typeof(anySatisfy) == bool) && // not yet defined + F!(Ti)) + { + enum anySatisfy = true; + } } - else + static if (!is(typeof(anySatisfy) == bool)) // if not yet defined { - enum anySatisfy = - anySatisfy!(F, T[ 0 .. $/2]) || - anySatisfy!(F, T[$/2 .. $ ]); + enum anySatisfy = false; } } diff --git a/std/traits.d b/std/traits.d index 604ce12ac2a..2669c2388c0 100644 --- a/std/traits.d +++ b/std/traits.d @@ -6931,17 +6931,20 @@ template isInstanceOf(alias S, alias T) * * See_Also: $(LREF isTypeTuple). */ -template isExpressions(T ...) +template isExpressions(T...) { - static if (T.length >= 2) - enum bool isExpressions = - isExpressions!(T[0 .. $/2]) && - isExpressions!(T[$/2 .. $]); - else static if (T.length == 1) - enum bool isExpressions = - !is(T[0]) && __traits(compiles, { auto ex = T[0]; }); - else - enum bool isExpressions = true; // default + static foreach (Ti; T) + { + static if (!is(typeof(isExpressions) == bool) && // not yet defined + (is(Ti) || !__traits(compiles, { auto ex = Ti; }))) + { + enum isExpressions = false; + } + } + static if (!is(typeof(isExpressions) == bool)) // if not yet defined + { + enum isExpressions = true; + } } /// @@ -8487,13 +8490,18 @@ private template getSymbolsByUDAImpl(alias symbol, alias attribute, names...) */ template allSameType(T...) { - static if (T.length <= 1) + static foreach (idx, Ti; T) { - enum bool allSameType = true; + static if (idx + 1 < T.length && + !is(typeof(allSameType) == bool) && + !is(T[idx] == T[idx + 1])) + { + enum bool allSameType = false; + } } - else + static if (!is(typeof(allSameType) == bool)) { - enum bool allSameType = is(T[0] == T[1]) && allSameType!(T[1..$]); + enum bool allSameType = true; } }