Switching back and forth between Array of Structures (AoS) and Structure of Arrays (SoA)

前端 未结 1 621
清歌不尽
清歌不尽 2021-02-14 09:36

One feature that plays a prominent role in many of the writings on data oriented design is that there are many cases where rather than AoS (array of structs):

st         


        
1条回答
  •  囚心锁ツ
    2021-02-14 10:16

    I'm going to choose this syntax: cs.foo[42] to be the single syntax and use typedefs to switch between arrangements:

    So, obviously given C_SoA from your post, the above syntax works and we can have:

    typedef C_SoA Arrangement;
    
    Arrangement cs;
    

    In order to use std::vector instead we are going to have to introduce something else:

    typedef std::vector AOS;
    
    template
    struct Accessor {
        T operator[](size_t index){
                return arr[index].*pMember;
        }
        T (C::*pMember);
        A& arr;
        Accessor(A& a, T (C::*p)): arr(a), pMember(p){}
    };
    
    struct Alt_C_AoS{
        Accessor foo;
        Accessor bar;
    
        AOS aos;
        Alt_C_AoS():foo(aos, &C_AoS::foo), bar(aos, &C_AoS::bar){}
    };
    

    Now we can have:

    //Choose just one arrangement
    typedef Alt_C_AoS Arrangement;
    //typedef C_SoA Arrangement;
    
    Arrangement cs;
    ...
    std::cout << cs.foo[42] << std::endl;
    

    Essentially this converts container dot member index into container index dot member.

    0 讨论(0)
提交回复
热议问题