-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.d
More file actions
72 lines (66 loc) · 1.63 KB
/
util.d
File metadata and controls
72 lines (66 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module stdex.util;
import std.typecons;
import std.typetuple;
/++
Throws an AssertError if test is false. The args can be used to provide a
format string.
+/
void assertf(T, string file = __FILE__, int line = __LINE__, Args...)
(lazy T test, lazy Args args)
{
//version(assert)
{
import std.string : format;
import core.exception : AssertError;
if (!test)
{
throw new AssertError(format(args), file, line);
}
}
}
/++
Produces an array of key-value pair tuples from an associative array.
The order of items produces is the same order produced by foreach
iteration, which is unspecified.
+/
Tuple!(Key, Value)[] keyValueArray(Key, Value)(Value[Key] aa)
{
alias Tuple!(Key, Value) Element;
Element[] arr = new Element[aa.length];
size_t i = 0;
foreach (key, value; aa)
arr[i++] = Element(key, value);
return arr;
}
/++
Interleaves two type tuples.
+/
template Interleave(A...)
{
template and(B...)
if (A.length == B.length)
{
static if (A.length == 1)
{
alias TypeTuple!(A[0], B[0]) and;
}
else
{
alias TypeTuple!(A[0], B[0], Interleave!(A[1..$]).and!(B[1..$])) and;
}
}
}
/++
Constructs a tuple with named members.
namedTuple!("a", "b")(a, b) is equivalent to
Tuple!(typeof(a), "a", typeof(b), "b")(a, b).
+/
template namedTuple(names...)
{
auto namedTuple(Args...)(Args args)
if (Args.length == names.length &&
args.length > 0)
{
return Tuple!(Interleave!(Args).and!(names))(args);
}
}