forked from MartinNowak/dranges
-
Notifications
You must be signed in to change notification settings - Fork 1
/
variadic.d
124 lines (109 loc) · 2.84 KB
/
variadic.d
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Written in the D programming language
/**
To Be Documented.
This module defines some range-like templates on variadic lists : mapping, filtering, etc.
License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
Authors: Philippe Sigaud
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
module dranges.variadic;
import dranges.functional,
dranges.templates,
dranges.typetuple;
///
CommonType!T max(T...)(T t) if (T.length) { // I should test for a correct type: one that defines min and opCmp
CommonType!T result = CommonType!T.min;
foreach(i, Type; T) if (result < t[i]) result = t[i];
return result;
}
///
CommonType!T min(T...)(T t) if (T.length) { // I should test for a correct type: one that defines min and opCmp
CommonType!T result = CommonType!T.max;
foreach(i, Type; T) if (result > t[i]) result = t[i];
return result;
}
///
Tuple!(StaticMap!(RT!fun, T)) mapVariadic(alias fun, T...)(T tup)
{
StaticMap!(RT!fun, T) res;
foreach(i, Type; T) res[i] = fun(tup[i]);
return tuple(res);
}
///
StaticScan!(RT2!fun, T)[$-1] reduceVariadic(alias fun, T...)(T ts)
{
alias StaticScan!(RT2!fun, T) RTS;
RTS result;
foreach(i, Type; RTS) {
static if (i == 0)
result[i] = ts[0];
else
result[i] = fun(result[i-1], ts[i]);
}
return result[$-1];
}
///
Tuple!(StaticScan!(RT2!fun, T)) scanVariadic(alias fun, T...)(T ts)
{
alias StaticScan!(RT2!fun, T) RTS;
RTS result;
foreach(i, Type; RTS) {
static if (i == 0)
result[i] = ts[0];
else
result[i] = fun(result[i-1], ts[i]);
}
return tuple(result);
}
///
Tuple!(StaticStride!(n,T)) strideVariadic(size_t n, T...)(T ts) if (n > 0)
{
alias StaticStride!(n,T) StridedTypes;
StridedTypes result;
foreach(i, Type; StridedTypes) result[i] = ts[i*n];
return tuple(result);
}
/**
Finds the maximum value of a variadic list, at compile-time.
Example:
----
assert(Max!1 == 1);
assert(Max!(1,2) == 2);
assert(Max!(1,2,4,3,0) == 4);
----
*/
template Max(alias a, Rest...) {
static if (Rest.length > 0) {
alias Max!(dranges.templates.Max!(a, Rest[0]), Rest[1..$]) Max;
}
else {
alias a Max;
}
}
/**
Finds the minimum value of a variadic list, at compile-time.
Example:
----
assert(Min!1 == 1);
assert(Min!(1,2) == 2);
assert(Min!(1,2,4,3,0) == 4);
----
*/
template Min(alias a, Rest...) {
static if (Rest.length > 0) {
alias Min!(dranges.templates.Min!(a, Rest[0]), Rest[1..$]) Min;
}
else {
alias a Min;
}
}
unittest
{
assert(Max!1 == 1);
assert(Max!(1,2) == 2);
assert(Max!(1,2,4,3,0) == 4);
assert(Min!1 == 1);
assert(Min!(1,2) == 1);
assert(Min!(1,2,4,3,0) == 0);
}