forked from MartinNowak/dranges
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deepdup.d
57 lines (47 loc) · 1.37 KB
/
deepdup.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
/**
Tentative recursive deep-dupping.
*/
module dranges.deepdup;
template TypeofDeepdup(T)
{
alias typeof(deepdup(T.init)) TypeofDeepdup;
}
ref Unqual!T deepdup(T)(T t) if (is(T == struct) && !is(T.Types))
{
staticMap!(TypeofDeepdup, typeof(t.tupleof)) tup;
foreach(i,Type; tup) { tup[i] = deepdup(t.tupleof[i]);}
return Unqual!T(tup);
}
Tuple!(staticMap!(TypeofDeepdup, T.Types))
deepdup(T)(T t) if (is(T.Types)) // Tuples
{
staticMap!(TypeofDeepdup, T.Types) tup;
foreach(i,Type; tup) { tup[i] = deepdup(t.field[i]);}
return tuple(tup);
}
Unqual!T deepdup(T)(T t) if (is(T == class))
{
staticMap!(TypeofDeepdup, typeof(t.tupleof)) tup;
foreach(i,Type; tup) { tup[i] = deepdup(t.tupleof[i]);}
return new Unqual!T(tup);
}
TypeofDeepdup!(ElementType!T)[] deepdup(T)(T t) if (isDynamicArray!T)
{
auto result = new TypeofDeepdup!(ElementType!T)[](t.length);
foreach(i, elem; t) result[i] = deepdup(elem);
return result;
}
TypeofDeepdup!(ElementType!T)[T.length] deepdup(T)(T t) if (isStaticArray!T)
{
TypeofDeepdup!(ElementType!T)[T.length] result = t;
foreach(ref elem; result) elem = deepdup(elem);
return result;
}
TypeofDeepdup!T* deepdup(T)(T* t)
{
return &deepdup(*t);
}
Unqual!T deepdup(T)(T t) if (!is(T == struct) && !is(T == class) && !isDynamicArray!T && !is(T.Types) && !isPointer!T)
{
return cast(Unqual!T)t;
}