From b55c07c63a76ce56526afb1253ece5cda063849c Mon Sep 17 00:00:00 2001 From: sinkuu Date: Fri, 13 Feb 2015 13:38:59 +0900 Subject: [PATCH] Move internal stuff to std.algorithm.internal --- posix.mak | 3 +- std/algorithm/comparison.d | 4 +- std/algorithm/internal.d | 78 ++++++++++++++++++++++++++++++++++++++ std/algorithm/iteration.d | 16 ++++---- std/algorithm/mutation.d | 2 +- std/algorithm/package.d | 71 ---------------------------------- std/algorithm/searching.d | 10 ++--- std/algorithm/setops.d | 4 +- std/algorithm/sorting.d | 6 +-- win32.mak | 2 +- win64.mak | 2 +- 11 files changed, 103 insertions(+), 95 deletions(-) create mode 100644 std/algorithm/internal.d diff --git a/posix.mak b/posix.mak index c509da368..8df6c9948 100644 --- a/posix.mak +++ b/posix.mak @@ -231,7 +231,8 @@ EXTRA_MODULES += $(EXTRA_DOCUMENTABLES) $(addprefix \ gammafunction errorfunction) $(addprefix std/internal/, \ cstring processinit unicode_tables scopebuffer\ unicode_comp unicode_decomp unicode_grapheme unicode_norm) \ - $(addprefix std/internal/test/, dummyrange) + $(addprefix std/internal/test/, dummyrange) \ + $(addprefix std/algorithm/, internal) # Aggregate all D modules relevant to this build D_MODULES = $(STD_MODULES) $(EXTRA_MODULES) $(STD_NET_MODULES) \ diff --git a/std/algorithm/comparison.d b/std/algorithm/comparison.d index ac5a04756..a9b2b7f8c 100644 --- a/std/algorithm/comparison.d +++ b/std/algorithm/comparison.d @@ -1204,7 +1204,7 @@ MaxType!T max(T...)(T args) auto b = max(args[($+1)/2 .. $]); alias typeof(b) T1; - import std.algorithm : algoFormat; + import std.algorithm.internal : algoFormat; static assert (is(typeof(a < b)), algoFormat("Invalid arguments: Cannot compare types %s and %s.", T0.stringof, T1.stringof)); @@ -1312,7 +1312,7 @@ MinType!T min(T...)(T args) auto b = min(args[($+1)/2 .. $]); alias typeof(b) T1; - import std.algorithm : algoFormat; + import std.algorithm.internal : algoFormat; static assert (is(typeof(a < b)), algoFormat("Invalid arguments: Cannot compare types %s and %s.", T0.stringof, T1.stringof)); diff --git a/std/algorithm/internal.d b/std/algorithm/internal.d new file mode 100644 index 000000000..3e0b4111d --- /dev/null +++ b/std/algorithm/internal.d @@ -0,0 +1,78 @@ +// Written in the D programming language. + +/// Helper functions for std.algorithm package. +module std.algorithm.internal; + + +// Same as std.string.format, but "self-importing". +// Helps reduce code and imports, particularly in static asserts. +// Also helps with missing imports errors. +package template algoFormat() +{ + import std.format : format; + alias algoFormat = format; +} + +// Internal random array generators +version(unittest) +{ + package enum size_t maxArraySize = 50; + package enum size_t minArraySize = maxArraySize - 1; + + package string[] rndstuff(T : string)() + { + import std.random : Random, unpredictableSeed, uniform; + + static Random rnd; + static bool first = true; + if (first) + { + rnd = Random(unpredictableSeed); + first = false; + } + string[] result = + new string[uniform(minArraySize, maxArraySize, rnd)]; + string alpha = "abcdefghijABCDEFGHIJ"; + foreach (ref s; result) + { + foreach (i; 0 .. uniform(0u, 20u, rnd)) + { + auto j = uniform(0, alpha.length - 1, rnd); + s ~= alpha[j]; + } + } + return result; + } + + package int[] rndstuff(T : int)() + { + import std.random : Random, unpredictableSeed, uniform; + + static Random rnd; + static bool first = true; + if (first) + { + rnd = Random(unpredictableSeed); + first = false; + } + int[] result = new int[uniform(minArraySize, maxArraySize, rnd)]; + foreach (ref i; result) + { + i = uniform(-100, 100, rnd); + } + return result; + } + + package double[] rndstuff(T : double)() + { + double[] result; + foreach (i; rndstuff!(int)()) + { + result ~= i / 50.0; + } + return result; + } +} + +package T* addressOf(T)(ref T val) { return &val; } + diff --git a/std/algorithm/iteration.d b/std/algorithm/iteration.d index af107efe2..a6f1b82cf 100644 --- a/std/algorithm/iteration.d +++ b/std/algorithm/iteration.d @@ -298,7 +298,7 @@ private struct Cache(R, bool bidir) private { - import std.algorithm : algoFormat; + import std.algorithm.internal : algoFormat; import std.typetuple : TypeTuple; alias E = ElementType!R; @@ -2320,7 +2320,7 @@ unittest @safe unittest { - import std.algorithm : algoFormat; // FIXME + import std.algorithm.internal : algoFormat; import std.algorithm.comparison : equal; struct TransientRange @@ -2455,7 +2455,7 @@ template reduce(fun...) if (fun.length >= 1) return reducePreImpl(r, seed); else { - import std.algorithm : algoFormat; + import std.algorithm.internal : algoFormat; static assert(isTuple!S, algoFormat("Seed %s should be a Tuple", S.stringof)); return reducePreImpl(r, seed.expand); } @@ -2474,7 +2474,7 @@ template reduce(fun...) if (fun.length >= 1) private auto reduceImpl(bool mustInitialize, R, Args...)(R r, ref Args args) if (isIterable!R) { - import std.algorithm : algoFormat; + import std.algorithm.internal : algoFormat; static assert(Args.length == fun.length, algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, fun.length)); alias E = Select!(isInputRange!R, ElementType!R, ForeachType!R); @@ -2512,7 +2512,7 @@ private template ReduceSeedType(E) { static template ReduceSeedType(alias fun) { - import std.algorithm : algoFormat; + import std.algorithm.internal : algoFormat; E e = E.init; static alias ReduceSeedType = Unqual!(typeof(fun(e, e))); @@ -3558,7 +3558,7 @@ private struct SplitterResult(alias isTerminator, Range) @safe unittest { - import std.algorithm : algoFormat; // FIXME + import std.algorithm.internal : algoFormat; import std.algorithm.comparison : equal; import std.internal.test.dummyrange; @@ -3595,7 +3595,7 @@ private struct SplitterResult(alias isTerminator, Range) @safe unittest { - import std.algorithm : algoFormat; // FIXME + import std.algorithm.internal : algoFormat; import std.algorithm.comparison : equal; import std.range; @@ -3752,7 +3752,7 @@ if (isSomeChar!C) @safe unittest { - import std.algorithm : algoFormat; // FIXME + import std.algorithm.internal : algoFormat; import std.algorithm.comparison : equal; import std.conv : text; import std.array : split; diff --git a/std/algorithm/mutation.d b/std/algorithm/mutation.d index 9aecf2a51..a489054db 100644 --- a/std/algorithm/mutation.d +++ b/std/algorithm/mutation.d @@ -702,7 +702,7 @@ void initializeAll(Range)(Range range) alias T = ElementType!Range; static if (hasElaborateAssign!T) { - import std.algorithm : addressOf; // FIXME + import std.algorithm.internal : addressOf; //Elaborate opAssign. Must go the memcpy road. //We avoid calling emplace here, because our goal is to initialize to //the static state of T.init, diff --git a/std/algorithm/package.d b/std/algorithm/package.d index efd4563be..811c9cdb5 100644 --- a/std/algorithm/package.d +++ b/std/algorithm/package.d @@ -196,17 +196,6 @@ import std.typetuple; // : TypeTuple, staticMap, allSatisfy, anySatisfy; version(unittest) debug(std_algorithm) import std.stdio; -package T* addressOf(T)(ref T val) { return &val; } - -// Same as std.string.format, but "self-importing". -// Helps reduce code and imports, particularly in static asserts. -// Also helps with missing imports errors. -package template algoFormat() -{ - import std.format : format; - alias algoFormat = format; -} - /** Forwards function arguments with saving ref-ness. */ @@ -313,63 +302,3 @@ enum SortOutput yes, /// Sort output } -// Internal random array generators -version(unittest) -{ - package enum size_t maxArraySize = 50; - package enum size_t minArraySize = maxArraySize - 1; - - package string[] rndstuff(T : string)() - { - import std.random : Random, unpredictableSeed, uniform; - - static Random rnd; - static bool first = true; - if (first) - { - rnd = Random(unpredictableSeed); - first = false; - } - string[] result = - new string[uniform(minArraySize, maxArraySize, rnd)]; - string alpha = "abcdefghijABCDEFGHIJ"; - foreach (ref s; result) - { - foreach (i; 0 .. uniform(0u, 20u, rnd)) - { - auto j = uniform(0, alpha.length - 1, rnd); - s ~= alpha[j]; - } - } - return result; - } - - package int[] rndstuff(T : int)() - { - import std.random : Random, unpredictableSeed, uniform; - - static Random rnd; - static bool first = true; - if (first) - { - rnd = Random(unpredictableSeed); - first = false; - } - int[] result = new int[uniform(minArraySize, maxArraySize, rnd)]; - foreach (ref i; result) - { - i = uniform(-100, 100, rnd); - } - return result; - } - - package double[] rndstuff(T : double)() - { - double[] result; - foreach (i; rndstuff!(int)()) - { - result ~= i / 50.0; - } - return result; - } -} diff --git a/std/algorithm/searching.d b/std/algorithm/searching.d index 9d6f057f2..ef58f9e16 100644 --- a/std/algorithm/searching.d +++ b/std/algorithm/searching.d @@ -1958,7 +1958,7 @@ if (Ranges.length > 1 && is(typeof(startsWith!pred(haystack, needles)))) @safe unittest { - import std.algorithm : rndstuff; // FIXME + import std.algorithm.internal : rndstuff; import std.typetuple : TypeTuple; import std.uni : toUpper; @@ -1989,7 +1989,7 @@ if (Ranges.length > 1 && is(typeof(startsWith!pred(haystack, needles)))) @safe unittest { - import std.algorithm : rndstuff; // FIXME + import std.algorithm.internal : rndstuff; import std.algorithm.comparison : equal; import std.range : retro; import std.typetuple : TypeTuple; @@ -2149,7 +2149,7 @@ template canFind(alias pred="a == b") @safe unittest { - import std.algorithm : rndstuff; // FIXME + import std.algorithm.internal : rndstuff; debug(std_algorithm) scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " done."); auto a = rndstuff!(int)(); @@ -2573,7 +2573,7 @@ minCount(alias pred = "a < b", Range)(Range range) if (isInputRange!Range && !isInfinite!Range && is(typeof(binaryFun!pred(range.front, range.front)))) { - import std.algorithm : algoFormat; + import std.algorithm.internal : algoFormat; import std.exception : enforce; alias T = ElementType!Range; @@ -2627,7 +2627,7 @@ minCount(alias pred = "a < b", Range)(Range range) } else static if (hasLvalueElements!Range) { - import std.algorithm : addressOf; // FIXME + import std.algorithm.internal : addressOf; T* p = addressOf(range.front); for (range.popFront(); !range.empty; range.popFront()) { diff --git a/std/algorithm/setops.d b/std/algorithm/setops.d index be358736a..6a82b5bf1 100644 --- a/std/algorithm/setops.d +++ b/std/algorithm/setops.d @@ -369,7 +369,7 @@ auto cartesianProduct(RR...)(RR ranges) } @property auto front() { - import std.algorithm : algoFormat; // FIXME + import std.algorithm.internal : algoFormat; import std.range : iota; return mixin(algoFormat("tuple(%(current[%d].front%|,%))", iota(0, current.length))); @@ -443,7 +443,7 @@ auto cartesianProduct(R1, R2, RR...)(R1 range1, R2 range2, RR otherRanges) * one level of tuples so that a ternary cartesian product, for example, * returns 3-element tuples instead of nested 2-element tuples. */ - import std.algorithm : algoFormat; // FIXME + import std.algorithm.internal : algoFormat; import std.algorithm.iteration : map; import std.range : iota; diff --git a/std/algorithm/sorting.d b/std/algorithm/sorting.d index 12244f7f2..6e62ca208 100644 --- a/std/algorithm/sorting.d +++ b/std/algorithm/sorting.d @@ -333,7 +333,7 @@ Range partition(alias predicate, @safe unittest { - import std.algorithm : rndstuff; // FIXME + import std.algorithm.internal : rndstuff; static bool even(int a) { return (a & 1) == 0; } // test with random data @@ -514,7 +514,7 @@ makeIndex( if (isForwardRange!(Range) && isRandomAccessRange!(RangeIndex) && is(ElementType!(RangeIndex) : ElementType!(Range)*)) { - import std.algorithm : addressOf; // FIXME + import std.algorithm.internal : addressOf; import std.exception : enforce; // assume collection already ordered @@ -923,7 +923,7 @@ unittest unittest { - import std.algorithm : rndstuff; // FIXME + import std.algorithm.internal : rndstuff; import std.algorithm : swapRanges; // FIXME import std.random : Random, unpredictableSeed, uniform; import std.uni : toUpper; diff --git a/win32.mak b/win32.mak index e2fefa38a..786ed51e0 100644 --- a/win32.mak +++ b/win32.mak @@ -137,7 +137,7 @@ SRC_STD_4= std\uuid.d $(SRC_STD_DIGEST) SRC_STD_ALGO= std\algorithm\package.d std\algorithm\comparison.d \ std\algorithm\iteration.d std\algorithm\mutation.d \ std\algorithm\searching.d std\algorithm\setops.d \ - std\algorithm\sorting.d + std\algorithm\sorting.d std\algorithm\internal.d SRC_STD_5_HEAVY= $(SRC_STD_ALGO) diff --git a/win64.mak b/win64.mak index a4fe3451a..4eb9d8971 100644 --- a/win64.mak +++ b/win64.mak @@ -137,7 +137,7 @@ SRC_STD_4= std\uuid.d $(SRC_STD_DIGEST) SRC_STD_ALGO= std\algorithm\package.d std\algorithm\comparison.d \ std\algorithm\iteration.d std\algorithm\mutation.d \ std\algorithm\searching.d std\algorithm\setops.d \ - std\algorithm\sorting.d + std\algorithm\sorting.d std\algorithm\internal.d SRC_STD_5_HEAVY= $(SRC_STD_ALGO)