-
Notifications
You must be signed in to change notification settings - Fork 2
/
traits.d
375 lines (331 loc) · 11 KB
/
traits.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Written in the D programming language
/**
This module is a grab-bag of templates used by the other modules, mostly
as guards by other templates (isRangeOfRanges, isTupleRange, ...).
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.traits;
import std.conv,
std.exception,
std.functional,
std.metastrings,
std.range,
std.stdio,
std.traits,
std.typecons,
std.typetuple;
/**
Returns true iff F is a function type (a function, a delegate or something definining opCall as a function).
*/
template isFunctionType(F) {
enum bool isFunctionType = (is(F == function) || is(F == delegate) || __traits(hasMember, F, "opCall"));
}
/**
Is true iff fun is a function/delegate or defines opCall.
*/
template isFunction(alias fun) {
static if (is(typeof(fun)))
enum bool isFunction = isFunctionType!(typeof(fun));
else
enum bool isFunction = false;
}
template isFunction(F)
{
enum bool isFunction = (is(F == function) || is(F == delegate) || __traits(hasMember, F, "opCall"));
}
unittest
{
int a;
int foo() { return 0;}
int bar(int a, int b) { return 0;}
struct F { int opCall() { return 0;}}
assert(!isFunction!a);
assert(isFunction!foo);
assert(isFunction!bar);
F f;
assert(isFunction!f);
}
// doesn't work.
template isVariadic(alias fun) {
enum bool isVariadic = isFunction!fun && !is(ParameterTypeTuple!fun);
}
/**
Extract the element types from a variadic list of ranges.
*/
template ElementTypes(R, Rest...) {
static if (Rest.length > 0) {
alias TypeTuple!(ElementType!R, ElementTypes!(Rest)) ElementTypes;
}
else {
alias ElementType!R ElementTypes;
}
}
unittest {
auto r1 = [0,1];
auto r2 = [2.0, 3.1];
auto r3 = cycle(['a','b','c']);
assert(is(ElementTypes!(typeof(r1), typeof(r2), typeof(r3)) == TypeTuple!(int, double, dchar)), "ElementTypes!R test.");
}
/**
Given a variadic list of ranges, find the common type to all their element types.
If no common type is found, CommonElementType is void.
*/
template CommonElementType(R...) if (allSatisfy!(isInputRange, R)) {
alias CommonType!(staticMap!(ElementType, R)) CommonElementType;
}
unittest
{
auto r1 = [0,1,2,3,4];
auto r2 = [0.0,1.0,2.0];
auto s = cast(dchar[])"abc";
assert(is(CommonElementType!(typeof(r1), typeof(r2)) == double));
assert(is(CommonElementType!(typeof(r1), typeof(s)) == uint)); // because dchar is implictly converted into an uint
assert(is(CommonElementType!(typeof(r1), typeof(r2), typeof(s)) == double));
assert(isImplicitlyConvertible!(immutable(char), double));
}
/**
Is true iff the ranges R... have a common element type.
*/
template CompatibleRanges(R...) {
enum bool CompatibleRanges = !is(CommonElementType!R == void);
}
unittest
{
auto r1 = [0,1,2,3,4];
auto r2 = [0.0,1.0,2.0];
string s = "abc";
auto ss = ["abc", "def", "fgh"];
int[][] r3 = [[0,1], [2,3]];
assert(CompatibleRanges!(typeof(r1), typeof(r2), typeof(s)));
assert(!CompatibleRanges!(typeof(r1), typeof(ss)));
assert(!CompatibleRanges!(typeof(r1), typeof(r3)));
}
/**
Is true iff all the R either have a length or are infinite.
TODO: replace it.
*/
template allHaveLength(R, Rest...) {
static if (Rest.length)
enum bool allHaveLength = (hasLength!R || isInfinite!R) && allHaveLength!Rest;
else
enum bool allHaveLength = (hasLength!R || isInfinite!R);
}
/**
Could be replaced by allSatisfy!(isInfinite, R)?
TODO: replace it.
*/
template allAreInfinite(R, Rest...) {
static if (Rest.length > 0) {
enum bool allAreInfinite = isInfinite!R && allAreInfinite!Rest;
}
else {
enum bool allAreInfinite = isInfinite!R;
}
}
/**
Is true iff R is a tuple-producing range (ie its elements are std.typecons.tuples).
*/
template isTupleRange(R)
{
enum bool isTupleRange = isForwardRange!R && is(ElementType!R.Types);
}
/**
Is true if R is a range of ranges: a (forward) range whose elements
are themselves forward ranges.
*/
template isRangeOfRanges(R) {
enum bool isRangeOfRanges = isForwardRange!R && isForwardRange!(ElementType!R);
}
/**
Is true if R is a simple range: a (forward) range whose elements are not (forward) ranges.
*/
template isSimpleRange(R) {
enum bool isSimpleRange = isForwardRange!R && !isForwardRange!(ElementType!R);
}
/**
Is true iff R is an array of arrays (ie T[][] for some T).
*/
template isArrayOfArrays(R) {
enum bool isArrayOfArrays = isArray!R && isArray!(ElementType!(R));
}
/**
Is true iff R is a simple array T[] and not deeper (no U such that
U[][] == T[] == R).
*/
template isSimpleArray(R) {
enum bool isSimpleArray = isArray!R && !isArray!(ElementType!(R));
}
/+
/**
Alternate template to std.range.hasLength, as hasLength doesn't work
if R has length defined inside a static if.
*/
template hasLength2(R) {
enum bool hasLength2 = __traits(compiles, R.length);
}
unittest
{
assert(hasLength!(int[]));
assert(hasLength!(int[3]));
assert(hasLength!(string));
assert(!hasLength!(int));
}
+/
/**
Gives the rank of an array or a range. For a simple (flat, non-nested) range
of array, rank is 1. A range of ranges is depth 2 and so on...
A non-range is considered to be an element and is rank 0.
Example:
----
int[] r1 = [0,1,2,3];
int[][] r2 = [r1, [4], r1];
int[][][] r3 = [r2, [[5,6]], r2];
assert(rank!(typeof(r1)) == 1); // No nesting, flat range
assert(rank!(typeof(r2)) == 2); // range of ranges
assert(rank!(typeof(r3)) == 3); // range of ranges of ranges
----
*/
template rank(R) if (isSimpleRange!R)
{
enum size_t rank = 1;
}
/// ditto
template rank(R) if (isRangeOfRanges!R)
{
enum size_t rank = 1 + rank!(ElementType!R);
}
template rank(R) if (!isForwardRange!R)
{
enum size_t rank = 0;
}
unittest {
int[] r1 = [0,1,2,3][];
int[][] r2 = [r1, [4][], r1];
int[][][] r3 = [r2, [[5,6][]][], r2];
assert(rank!(typeof(r1)) == 1); // No nesting, flat range
assert(rank!(typeof(r2)) == 2); // range of ranges
assert(rank!(typeof(r3)) == 3); // range of ranges of ranges
}
/**
alias itself to the innermost type of a nested array.
For example BaseElementType!(int[][][]) is int, in constrast with std.range.ElementType!(R) which would become int[][].
*/
template BaseElementType(R) if(isSimpleRange!(R)) {
alias ElementType!(R) BaseElementType;
}
/// ditto
template BaseElementType(R) if(isRangeOfRanges!(R)) {
alias BaseElementType!(ElementType!(R)) BaseElementType;
}
unittest {
int[][] arr = [[0,1][], [2,3][]];
auto arr2 = [arr, [[4][]][], arr][];
assert(is(BaseElementType!(typeof(arr)) == int), "BaseElementType test int[][] array.");
assert(is(BaseElementType!(typeof(arr2)) == int), "BaseElementType test nested arrays.");
assert(is(BaseElementType!(int[]) == int));
}
/**
Alias itself to the inner array type of a nested array, up to some maximum depth.
If no maximum depth is given, it goes down to the innermost type, as BaseElementType.
Example:
----
int[][][][] array4;
BaseElementTypeUpTo!(typeof(array4), 0) == int[][][][]
BaseElementTypeUpTo!(typeof(array4), 1) == int[][][]
BaseElementTypeUpTo!(typeof(array4), 2) == int[][]
BaseElementTypeUpTo!(typeof(array4), 3) == int[]
BaseElementTypeUpTo!(typeof(array4)) == int
----
*/
template BaseElementTypeUpTo(R, int maxDepth : 0) if (isForwardRange!R) {
alias R BaseElementTypeUpTo;
}
/// ditto
template BaseElementTypeUpTo(R, int maxDepth = int.max) if (isSimpleRange!(R)) {
alias ElementType!(R) BaseElementTypeUpTo;
}
/// ditto
template BaseElementTypeUpTo(R, int maxDepth = int.max) if(isRangeOfRanges!(R)) {
alias BaseElementTypeUpTo!(ElementType!(R), maxDepth-1) BaseElementTypeUpTo;
}
unittest {
int[][][][] array4;
assert(is(BaseElementTypeUpTo!(typeof(array4), 0) == int[][][][]));
assert(is(BaseElementTypeUpTo!(typeof(array4), 1) == int[][][] ));
assert(is(BaseElementTypeUpTo!(typeof(array4), 2) == int[][] ));
assert(is(BaseElementTypeUpTo!(typeof(array4), 3) == int[] ));
assert(is(BaseElementTypeUpTo!(typeof(array4), 4) == int ));
assert(is(BaseElementTypeUpTo!(typeof(array4)) == int ));
}
/**
The complementary template of std.traits.allSatisfy: is true
iff some types in R satisfy predicate pred.
*/
template someSatisfy(alias pred, R...) {
static if (R.length == 0)
enum bool someSatisfy = false;
else
enum bool someSatisfy = pred!(R[0]) || someSatisfy!(pred, R[1..$]);
}
unittest
{
static assert(someSatisfy!(isIntegral, int, double));
static assert(someSatisfy!(isIntegral, int, long));
static assert(someSatisfy!(hasLength, int[], int[2])); // does not work with std.range.hasLength
static assert(!someSatisfy!(hasLength, int, string)); // DMD 2.041: strings do _not_ have a length
static assert(!someSatisfy!(hasLength, int, real));
}
/**
The complementary template of std.traits.Unsigned. Gives the signed
counterpart of integral types (byte, short, int, long, char, dchar, wchar).
Does nothing with floating-point types, as these are already signed.
Completly based on std.traits.Unsigned.
Example:
----
alias TypeTuple!(int, uint, ulong, double, ushort) Test;
assert(is(staticMap!(Signed, Test) == TypeTuple!(int,int,long,double,short)));
----
*/
template Signed(T) {
static if (is(T == byte)) alias byte Signed;
else static if (is(T == short)) alias short Signed;
else static if (is(T == int)) alias int Signed;
else static if (is(T == long)) alias long Signed;
else static if (is(T == ubyte)) alias byte Signed;
else static if (is(T == ushort)) alias short Signed;
else static if (is(T == uint)) alias int Signed;
else static if (is(T == ulong)) alias long Signed;
else static if (is(T == char)) alias char Signed;
else static if (is(T == wchar)) alias wchar Signed;
else static if (is(T == dchar)) alias dchar Signed;
else static if (is(T == float)) alias float Signed;
else static if (is(T == double)) alias double Signed;
else static if (is(T == real)) alias real Signed;
else static if(is(T == enum))
{
static if (T.sizeof == 1) alias byte Signed;
else static if (T.sizeof == 2) alias short Signed;
else static if (T.sizeof == 4) alias int Signed;
else static if (T.sizeof == 8) alias long Signed;
else static assert(false, "Type " ~ T.stringof
~ " does not have an Signed counterpart");
}
else static if (is(T == immutable))
{
alias immutable(Signed!(Unqual!T)) Signed;
}
else static if (is(T == const))
{
alias const(Signed!(Unqual!T)) Signed;
}
else static assert(false, "Type " ~ T.stringof
~ " does not have an Signed counterpart");
}
unittest
{
alias TypeTuple!(int, uint, ulong, double, ushort) Test;
assert(is(staticMap!(Signed, Test) == TypeTuple!(int,int,long,double,short)));
}