| 1 |
// Written in the D programming language. |
| 2 |
|
| 3 |
/// Helper functions for std.algorithm package. |
| 4 |
module std.algorithm.internal; |
| 5 |
|
| 6 |
|
| 7 |
// Same as std.string.format, but "self-importing". |
| 8 |
// Helps reduce code and imports, particularly in static asserts. |
| 9 |
// Also helps with missing imports errors. |
| 10 |
package template algoFormat() |
| 11 |
{ |
| 12 |
import std.format : format; |
| 13 |
alias algoFormat = format; |
| 14 |
} |
| 15 |
|
| 16 |
// Internal random array generators |
| 17 |
version (StdUnittest) |
| 18 |
{ |
| 19 |
package enum size_t maxArraySize = 50; |
| 20 |
package enum size_t minArraySize = maxArraySize - 1; |
| 21 |
|
| 22 |
package string[] rndstuff(T : string)() |
| 23 |
{ |
| 24 |
import std.random : Xorshift, uniform; |
| 25 |
|
| 26 |
static rnd = Xorshift(234_567_891); |
| 27 |
string[] result = |
| 28 |
new string[uniform(minArraySize, maxArraySize, rnd)]; |
| 29 |
string alpha = "abcdefghijABCDEFGHIJ"; |
| 30 |
foreach (ref s; result) |
| 31 |
{ |
| 32 |
foreach (i; 0 .. uniform(0u, 20u, rnd)) |
| 33 |
{ |
| 34 |
auto j = uniform(0, alpha.length - 1, rnd); |
| 35 |
s ~= alpha[j]; |
| 36 |
} |
| 37 |
} |
| 38 |
return result; |
| 39 |
} |
| 40 |
|
| 41 |
package int[] rndstuff(T : int)() |
| 42 |
{ |
| 43 |
import std.random : Xorshift, uniform; |
| 44 |
|
| 45 |
static rnd = Xorshift(345_678_912); |
| 46 |
int[] result = new int[uniform(minArraySize, maxArraySize, rnd)]; |
| 47 |
foreach (ref i; result) |
| 48 |
{ |
| 49 |
i = uniform(-100, 100, rnd); |
| 50 |
} |
| 51 |
return result; |
| 52 |
} |
| 53 |
|
| 54 |
package double[] rndstuff(T : double)() |
| 55 |
{ |
| 56 |
double[] result; |
| 57 |
foreach (i; rndstuff!(int)()) |
| 58 |
{ |
| 59 |
result ~= i / 50.0; |
| 60 |
} |
| 61 |
return result; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// Used instead of `&object.member` when `member` may be |
| 66 |
// either a field or a @property function. |
| 67 |
package(std) T* addressOf(T)(ref T val) { return &val; } |