Advertisement
Guest User

Untitled

a guest
Aug 9th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.32 KB | None | 0 0
  1. import std.stdio;
  2. import std.typetuple;
  3. import std.conv;
  4. import std.traits;
  5.  
  6. template Cat(T...) {
  7.     template Impl(string cont, uint n, T...) {
  8.         static if(T.length == 1) {
  9.             const string Impl = cont ~ "T[" ~ n.to!string ~ "] f" ~ n.to!string ~ ";}";
  10.         } else {
  11.             const string Impl = Impl!(cont ~ "T[" ~ n.to!string ~ "] f" ~ n.to!string ~ ";", n + 1, T[1 .. $]);
  12.         }
  13.     }
  14.     mixin(Impl!("static struct Container {", 0, T));
  15.     alias Cat = Container;
  16. }
  17.  
  18. template CatRawGet(Cat, uint n) {
  19.     alias CatRawGet = FieldTypeTuple!(Cat)[n];
  20. }
  21.  
  22. template CatGet(C, F) {
  23.     alias T = FieldTypeTuple!C;
  24.     alias Field = T[0];
  25.     alias Value = T[1];
  26.     static if(T.length == 2) {
  27.         static if(is(F == Field)) {
  28.             alias CatGet = Value;
  29.         } else {
  30.             static assert(0, "Field " ~ F.stringof ~ " not found.");
  31.         }
  32.     } else static if(is(F == Field)) {
  33.         alias CatGet = Value;
  34.     } else {
  35.         alias CatGet = CatGet!(Cat!(T[2 .. $]), F);
  36.     }
  37. }
  38.  
  39. template CatSet(C, F, V) {
  40.     alias T = FieldTypeTuple!C;
  41.     pragma(msg, FieldTypeTuple!C);
  42.     alias Field = T[0];
  43.     alias Value = T[1];
  44.     static if(T.length == 2) {
  45.         static if(is(F == Field)) {
  46.             alias CatSet = Cat!(F, V);
  47.         } else {
  48.             static assert(0, "Field " ~ F.stringof ~ " not found.");
  49.         }
  50.     } else static if(is(F == Field)) {
  51.         alias CatSet = Cat!(F, V, Cat!(T[2 .. $]));
  52.     } else {
  53.         alias CatSet = Cat!(Field, Value, FieldTypeTuple!(CatSet!(Cat!(T[2 .. $]), F, V)));
  54.     }
  55. }
  56.  
  57. struct PairLeft { }
  58. struct PairRight { }
  59.  
  60. template PairNew(X, Y) {
  61.     alias PairNew = Cat!(PairLeft, X, PairRight, Y);
  62. }
  63.  
  64. struct N0 { }
  65. struct N1 { }
  66.  
  67. template Succ(Num) {
  68.     static if(is(Num == N0)) {
  69.         alias Succ = N1;
  70.     } else static if(is(Num == N1)) {
  71.         alias Succ = PairNew!(N1, N0);
  72.     } else static if(is(CatGet!(Num, PairRight) == N0)) {
  73.         alias Succ = PairNew!(CatGet!(Num, PairLeft), N1);
  74.     } else static if(is(CatGet!(Num, PairRight) == N1)) {
  75.         alias Succ = PairNew!(Succ!(CatGet!(Num, PairLeft)), N0);
  76.     } else {
  77.         static assert(0, "Err");
  78.     }
  79. }
  80.  
  81. void main() {
  82.     alias Q = PairNew!(PairNew!(PairNew!(N1, N1), N1), N1);
  83.     alias R = Succ!(Q);
  84.     alias P = PairNew!(PairNew!(PairNew!(PairNew!(N1, N0), N0), N0), N0);
  85.     static assert(is(R == P));
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement