mirror of
https://github.com/dlang/phobos.git
synced 2025-04-28 22:21:09 +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
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; }
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue