2015-11-17 78 views
0
template tupIndexToRange(alias Tup, Indices...){ 
    import std.meta; 
    static if(Indicies.length == 0){ 
    alias tupIndexToRange = AliasSeq!(); 
    } 
    else{ 
    alias tupIndexToRange = AliasSeq!(Tup[ Indices[0] ][], tupIndexToRange!(Tup,Indices[1..$])); 
    } 
} 

void main{ 
    alias Integrals = AliasSeq!(Array!int, Array!float, Array!double); 
    Integrals integrals; 

    alias IntegralRange = tupIndexToRange!(integrals,0,1); 
} 

我想要實現這樣的過濾容器與指數

auto range = zip(tupIndexToRange!(integrals,0,1)); 

我認爲主要的問題是,Tup[ Indices[0] ]不工作,我一個元組應該已經擴展到這個AliasSeq!(itegrals[0][],integrals[1][]);

source/app.d(108,22): Error: inout method std.container.array.Array!int.Array.opIndex is not callable using a mutable object source/app.d(108,22): Error: inout method std.container.array.Array!int.Array.opIndex is not callable using a mutable object source/app.d(108,22): Error: only one index allowed to index int source/app.d(108,22): Error: only one index allowed to index int source/app.d(108,54): Error: template instance app.main.tupIndex!(__integrals_field_0, 1) error instantiating source/app.d(108,54): instantiated from here: tupIndex!(__integrals_field_0, 0, 1) source/app.d(108,54):
instantiated from here: tupIndex!(__integrals_field_0, __integrals_field_2, 0, 1) source/app.d(155,3): instantiated from here: tupIndex!(__integrals_field_0, __integrals_field_1, __integrals_field_2, 0, 1) dmd failed with exit code 1

這大概是我想達到

什麼

但不是auto range = zip(tuple(integrals[0][],integrals[1][]).expand);我希望它是通用auto range = zip(tupIndexToRange!(integrals, AliasSeq!(0, 1)).expand);

也許我需要用混入?

回答

0

我能夠通過使用mixins來解決這個問題。

string tupleToRange(Indicies...)(string name){ 
    string r = "zip("; 
    foreach(i,idx;Indicies){ 
    r ~= name ~ "[" ~ idx.stringof ~"][]"; 
    if(i < Indicies.length -1){ 
     r~=","; 
    } 
    } 
    r~=")"; 
    return r; 
} 

void main() 
{ 
    import std.meta; 
    import std.range; 
    import std.container; 
    import std.stdio; 

    alias Integrals = AliasSeq!(Array!int, Array!float, Array!double); 
    Integrals integrals; 
    integrals[0].insertBack(1); 
    integrals[1].insertBack(2); 
    integrals[2].insertBack(3); 

    auto range = mixin(tupleToRange!(0,1)("integrals")); 
    writeln(range); 
    foreach(e;range){ 
    writeln("element: ",e); 
    } 
} 

但我仍然懷疑,如果沒有mixin是可能的。