mirror of
https://github.com/dlang/phobos.git
synced 2025-04-30 15:10:46 +03:00
Move internal stuff to std.algorithm.internal
This commit is contained in:
parent
492ab0df90
commit
b55c07c63a
11 changed files with 103 additions and 95 deletions
|
@ -231,7 +231,8 @@ EXTRA_MODULES += $(EXTRA_DOCUMENTABLES) $(addprefix \
|
||||||
gammafunction errorfunction) $(addprefix std/internal/, \
|
gammafunction errorfunction) $(addprefix std/internal/, \
|
||||||
cstring processinit unicode_tables scopebuffer\
|
cstring processinit unicode_tables scopebuffer\
|
||||||
unicode_comp unicode_decomp unicode_grapheme unicode_norm) \
|
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
|
# Aggregate all D modules relevant to this build
|
||||||
D_MODULES = $(STD_MODULES) $(EXTRA_MODULES) $(STD_NET_MODULES) \
|
D_MODULES = $(STD_MODULES) $(EXTRA_MODULES) $(STD_NET_MODULES) \
|
||||||
|
|
|
@ -1204,7 +1204,7 @@ MaxType!T max(T...)(T args)
|
||||||
auto b = max(args[($+1)/2 .. $]);
|
auto b = max(args[($+1)/2 .. $]);
|
||||||
alias typeof(b) T1;
|
alias typeof(b) T1;
|
||||||
|
|
||||||
import std.algorithm : algoFormat;
|
import std.algorithm.internal : algoFormat;
|
||||||
static assert (is(typeof(a < b)),
|
static assert (is(typeof(a < b)),
|
||||||
algoFormat("Invalid arguments: Cannot compare types %s and %s.", T0.stringof, T1.stringof));
|
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 .. $]);
|
auto b = min(args[($+1)/2 .. $]);
|
||||||
alias typeof(b) T1;
|
alias typeof(b) T1;
|
||||||
|
|
||||||
import std.algorithm : algoFormat;
|
import std.algorithm.internal : algoFormat;
|
||||||
static assert (is(typeof(a < b)),
|
static assert (is(typeof(a < b)),
|
||||||
algoFormat("Invalid arguments: Cannot compare types %s and %s.", T0.stringof, T1.stringof));
|
algoFormat("Invalid arguments: Cannot compare types %s and %s.", T0.stringof, T1.stringof));
|
||||||
|
|
||||||
|
|
78
std/algorithm/internal.d
Normal file
78
std/algorithm/internal.d
Normal 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; }
|
||||||
|
|
|
@ -298,7 +298,7 @@ private struct Cache(R, bool bidir)
|
||||||
|
|
||||||
private
|
private
|
||||||
{
|
{
|
||||||
import std.algorithm : algoFormat;
|
import std.algorithm.internal : algoFormat;
|
||||||
import std.typetuple : TypeTuple;
|
import std.typetuple : TypeTuple;
|
||||||
|
|
||||||
alias E = ElementType!R;
|
alias E = ElementType!R;
|
||||||
|
@ -2320,7 +2320,7 @@ unittest
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import std.algorithm : algoFormat; // FIXME
|
import std.algorithm.internal : algoFormat;
|
||||||
import std.algorithm.comparison : equal;
|
import std.algorithm.comparison : equal;
|
||||||
|
|
||||||
struct TransientRange
|
struct TransientRange
|
||||||
|
@ -2455,7 +2455,7 @@ template reduce(fun...) if (fun.length >= 1)
|
||||||
return reducePreImpl(r, seed);
|
return reducePreImpl(r, seed);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
import std.algorithm : algoFormat;
|
import std.algorithm.internal : algoFormat;
|
||||||
static assert(isTuple!S, algoFormat("Seed %s should be a Tuple", S.stringof));
|
static assert(isTuple!S, algoFormat("Seed %s should be a Tuple", S.stringof));
|
||||||
return reducePreImpl(r, seed.expand);
|
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)
|
private auto reduceImpl(bool mustInitialize, R, Args...)(R r, ref Args args)
|
||||||
if (isIterable!R)
|
if (isIterable!R)
|
||||||
{
|
{
|
||||||
import std.algorithm : algoFormat;
|
import std.algorithm.internal : algoFormat;
|
||||||
static assert(Args.length == fun.length,
|
static assert(Args.length == fun.length,
|
||||||
algoFormat("Seed %s does not have the correct amount of fields (should be %s)", Args.stringof, 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);
|
alias E = Select!(isInputRange!R, ElementType!R, ForeachType!R);
|
||||||
|
@ -2512,7 +2512,7 @@ private template ReduceSeedType(E)
|
||||||
{
|
{
|
||||||
static template ReduceSeedType(alias fun)
|
static template ReduceSeedType(alias fun)
|
||||||
{
|
{
|
||||||
import std.algorithm : algoFormat;
|
import std.algorithm.internal : algoFormat;
|
||||||
|
|
||||||
E e = E.init;
|
E e = E.init;
|
||||||
static alias ReduceSeedType = Unqual!(typeof(fun(e, e)));
|
static alias ReduceSeedType = Unqual!(typeof(fun(e, e)));
|
||||||
|
@ -3558,7 +3558,7 @@ private struct SplitterResult(alias isTerminator, Range)
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import std.algorithm : algoFormat; // FIXME
|
import std.algorithm.internal : algoFormat;
|
||||||
import std.algorithm.comparison : equal;
|
import std.algorithm.comparison : equal;
|
||||||
import std.internal.test.dummyrange;
|
import std.internal.test.dummyrange;
|
||||||
|
|
||||||
|
@ -3595,7 +3595,7 @@ private struct SplitterResult(alias isTerminator, Range)
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import std.algorithm : algoFormat; // FIXME
|
import std.algorithm.internal : algoFormat;
|
||||||
import std.algorithm.comparison : equal;
|
import std.algorithm.comparison : equal;
|
||||||
import std.range;
|
import std.range;
|
||||||
|
|
||||||
|
@ -3752,7 +3752,7 @@ if (isSomeChar!C)
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import std.algorithm : algoFormat; // FIXME
|
import std.algorithm.internal : algoFormat;
|
||||||
import std.algorithm.comparison : equal;
|
import std.algorithm.comparison : equal;
|
||||||
import std.conv : text;
|
import std.conv : text;
|
||||||
import std.array : split;
|
import std.array : split;
|
||||||
|
|
|
@ -702,7 +702,7 @@ void initializeAll(Range)(Range range)
|
||||||
alias T = ElementType!Range;
|
alias T = ElementType!Range;
|
||||||
static if (hasElaborateAssign!T)
|
static if (hasElaborateAssign!T)
|
||||||
{
|
{
|
||||||
import std.algorithm : addressOf; // FIXME
|
import std.algorithm.internal : addressOf;
|
||||||
//Elaborate opAssign. Must go the memcpy road.
|
//Elaborate opAssign. Must go the memcpy road.
|
||||||
//We avoid calling emplace here, because our goal is to initialize to
|
//We avoid calling emplace here, because our goal is to initialize to
|
||||||
//the static state of T.init,
|
//the static state of T.init,
|
||||||
|
|
|
@ -196,17 +196,6 @@ import std.typetuple; // : TypeTuple, staticMap, allSatisfy, anySatisfy;
|
||||||
|
|
||||||
version(unittest) debug(std_algorithm) import std.stdio;
|
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.
|
Forwards function arguments with saving ref-ness.
|
||||||
*/
|
*/
|
||||||
|
@ -313,63 +302,3 @@ enum SortOutput
|
||||||
yes, /// Sort output
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1958,7 +1958,7 @@ if (Ranges.length > 1 && is(typeof(startsWith!pred(haystack, needles))))
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import std.algorithm : rndstuff; // FIXME
|
import std.algorithm.internal : rndstuff;
|
||||||
import std.typetuple : TypeTuple;
|
import std.typetuple : TypeTuple;
|
||||||
import std.uni : toUpper;
|
import std.uni : toUpper;
|
||||||
|
|
||||||
|
@ -1989,7 +1989,7 @@ if (Ranges.length > 1 && is(typeof(startsWith!pred(haystack, needles))))
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import std.algorithm : rndstuff; // FIXME
|
import std.algorithm.internal : rndstuff;
|
||||||
import std.algorithm.comparison : equal;
|
import std.algorithm.comparison : equal;
|
||||||
import std.range : retro;
|
import std.range : retro;
|
||||||
import std.typetuple : TypeTuple;
|
import std.typetuple : TypeTuple;
|
||||||
|
@ -2149,7 +2149,7 @@ template canFind(alias pred="a == b")
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import std.algorithm : rndstuff; // FIXME
|
import std.algorithm.internal : rndstuff;
|
||||||
debug(std_algorithm) scope(success)
|
debug(std_algorithm) scope(success)
|
||||||
writeln("unittest @", __FILE__, ":", __LINE__, " done.");
|
writeln("unittest @", __FILE__, ":", __LINE__, " done.");
|
||||||
auto a = rndstuff!(int)();
|
auto a = rndstuff!(int)();
|
||||||
|
@ -2573,7 +2573,7 @@ minCount(alias pred = "a < b", Range)(Range range)
|
||||||
if (isInputRange!Range && !isInfinite!Range &&
|
if (isInputRange!Range && !isInfinite!Range &&
|
||||||
is(typeof(binaryFun!pred(range.front, range.front))))
|
is(typeof(binaryFun!pred(range.front, range.front))))
|
||||||
{
|
{
|
||||||
import std.algorithm : algoFormat;
|
import std.algorithm.internal : algoFormat;
|
||||||
import std.exception : enforce;
|
import std.exception : enforce;
|
||||||
|
|
||||||
alias T = ElementType!Range;
|
alias T = ElementType!Range;
|
||||||
|
@ -2627,7 +2627,7 @@ minCount(alias pred = "a < b", Range)(Range range)
|
||||||
}
|
}
|
||||||
else static if (hasLvalueElements!Range)
|
else static if (hasLvalueElements!Range)
|
||||||
{
|
{
|
||||||
import std.algorithm : addressOf; // FIXME
|
import std.algorithm.internal : addressOf;
|
||||||
T* p = addressOf(range.front);
|
T* p = addressOf(range.front);
|
||||||
for (range.popFront(); !range.empty; range.popFront())
|
for (range.popFront(); !range.empty; range.popFront())
|
||||||
{
|
{
|
||||||
|
|
|
@ -369,7 +369,7 @@ auto cartesianProduct(RR...)(RR ranges)
|
||||||
}
|
}
|
||||||
@property auto front()
|
@property auto front()
|
||||||
{
|
{
|
||||||
import std.algorithm : algoFormat; // FIXME
|
import std.algorithm.internal : algoFormat;
|
||||||
import std.range : iota;
|
import std.range : iota;
|
||||||
return mixin(algoFormat("tuple(%(current[%d].front%|,%))",
|
return mixin(algoFormat("tuple(%(current[%d].front%|,%))",
|
||||||
iota(0, current.length)));
|
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,
|
* one level of tuples so that a ternary cartesian product, for example,
|
||||||
* returns 3-element tuples instead of nested 2-element tuples.
|
* 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.algorithm.iteration : map;
|
||||||
import std.range : iota;
|
import std.range : iota;
|
||||||
|
|
||||||
|
|
|
@ -333,7 +333,7 @@ Range partition(alias predicate,
|
||||||
|
|
||||||
@safe unittest
|
@safe unittest
|
||||||
{
|
{
|
||||||
import std.algorithm : rndstuff; // FIXME
|
import std.algorithm.internal : rndstuff;
|
||||||
static bool even(int a) { return (a & 1) == 0; }
|
static bool even(int a) { return (a & 1) == 0; }
|
||||||
|
|
||||||
// test with random data
|
// test with random data
|
||||||
|
@ -514,7 +514,7 @@ makeIndex(
|
||||||
if (isForwardRange!(Range) && isRandomAccessRange!(RangeIndex)
|
if (isForwardRange!(Range) && isRandomAccessRange!(RangeIndex)
|
||||||
&& is(ElementType!(RangeIndex) : ElementType!(Range)*))
|
&& is(ElementType!(RangeIndex) : ElementType!(Range)*))
|
||||||
{
|
{
|
||||||
import std.algorithm : addressOf; // FIXME
|
import std.algorithm.internal : addressOf;
|
||||||
import std.exception : enforce;
|
import std.exception : enforce;
|
||||||
|
|
||||||
// assume collection already ordered
|
// assume collection already ordered
|
||||||
|
@ -923,7 +923,7 @@ unittest
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
import std.algorithm : rndstuff; // FIXME
|
import std.algorithm.internal : rndstuff;
|
||||||
import std.algorithm : swapRanges; // FIXME
|
import std.algorithm : swapRanges; // FIXME
|
||||||
import std.random : Random, unpredictableSeed, uniform;
|
import std.random : Random, unpredictableSeed, uniform;
|
||||||
import std.uni : toUpper;
|
import std.uni : toUpper;
|
||||||
|
|
|
@ -137,7 +137,7 @@ SRC_STD_4= std\uuid.d $(SRC_STD_DIGEST)
|
||||||
SRC_STD_ALGO= std\algorithm\package.d std\algorithm\comparison.d \
|
SRC_STD_ALGO= std\algorithm\package.d std\algorithm\comparison.d \
|
||||||
std\algorithm\iteration.d std\algorithm\mutation.d \
|
std\algorithm\iteration.d std\algorithm\mutation.d \
|
||||||
std\algorithm\searching.d std\algorithm\setops.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)
|
SRC_STD_5_HEAVY= $(SRC_STD_ALGO)
|
||||||
|
|
||||||
|
|
|
@ -137,7 +137,7 @@ SRC_STD_4= std\uuid.d $(SRC_STD_DIGEST)
|
||||||
SRC_STD_ALGO= std\algorithm\package.d std\algorithm\comparison.d \
|
SRC_STD_ALGO= std\algorithm\package.d std\algorithm\comparison.d \
|
||||||
std\algorithm\iteration.d std\algorithm\mutation.d \
|
std\algorithm\iteration.d std\algorithm\mutation.d \
|
||||||
std\algorithm\searching.d std\algorithm\setops.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)
|
SRC_STD_5_HEAVY= $(SRC_STD_ALGO)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue