Move internal stuff to std.algorithm.internal

This commit is contained in:
sinkuu 2015-02-13 13:38:59 +09:00
parent 492ab0df90
commit b55c07c63a
11 changed files with 103 additions and 95 deletions

View file

@ -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) \

View file

@ -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));

78
std/algorithm/internal.d Normal file
View file

@ -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; }

View file

@ -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;

View file

@ -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,

View file

@ -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;
}
}

View file

@ -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())
{

View file

@ -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;

View file

@ -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;

View file

@ -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)

View file

@ -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)