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.
algoFormat()10 package template algoFormat()
11 {
12 import std.format : format;
13 alias algoFormat = format;
14 }
15
16 // Internal random array generators
version(unittest)17 version (unittest)
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 : Random, unpredictableSeed, uniform;
25
26 static Random rnd;
27 static bool first = true;
28 if (first)
29 {
30 rnd = Random(unpredictableSeed);
31 first = false;
32 }
33 string[] result =
34 new string[uniform(minArraySize, maxArraySize, rnd)];
35 string alpha = "abcdefghijABCDEFGHIJ";
36 foreach (ref s; result)
37 {
38 foreach (i; 0 .. uniform(0u, 20u, rnd))
39 {
40 auto j = uniform(0, alpha.length - 1, rnd);
41 s ~= alpha[j];
42 }
43 }
44 return result;
45 }
46
47 package int[] rndstuff(T : int)()
48 {
49 import std.random : Random, unpredictableSeed, uniform;
50
51 static Random rnd;
52 static bool first = true;
53 if (first)
54 {
55 rnd = Random(unpredictableSeed);
56 first = false;
57 }
58 int[] result = new int[uniform(minArraySize, maxArraySize, rnd)];
59 foreach (ref i; result)
60 {
61 i = uniform(-100, 100, rnd);
62 }
63 return result;
64 }
65
66 package double[] rndstuff(T : double)()
67 {
68 double[] result;
69 foreach (i; rndstuff!(int)())
70 {
71 result ~= i / 50.0;
72 }
73 return result;
74 }
75 }
76
package(std)77 package(std) T* addressOf(T)(ref T val) { return &val; }
78