Boost logo

Boost :

Subject: Re: [boost] interest in structure of arrays container?
From: Larry Evans (cppljevans_at_[hidden])
Date: 2016-10-15 17:06:04


On 10/15/2016 03:14 AM, Michael Marcin wrote:
[snip]
>
> Arrays of Structures is the normal programming model.
>
> Let's take a toy example, say I'm looking at some census data and have a
> structure that looks like:
>
> struct citizen_t {
> string first_name;
> string last_name;
> int salary;
> int age;
> };
>
> Then the Array of Structures container would be:
> vector<citizen_t> aos_citizens;
>
> The Structure of Arrays version of the same data would look like:
>
> struct soa_citizens_t {
> vector<string> first_names;
> vector<string> last_names;
> vector<int> salary;
> vector<int> age;
> };
>
> soa_citizens_t soa_citizens;
[snip]
> // Structures of Arrays
> int avg = 0;
> int t = 1;
> for ( int salary : soa_citizens.salary ) {
> avg += (salary - avg) / t;
> ++t;
> }
>
>
To generalize, what about:

   template<std::size_t N, typename... Fields>
   using soa_citizens_t=
     std::tuple
     < std::array<N,Fields>...
>
     ;

? Then wouldn't:

   std::size_t n=5;
   soa_citizens_t<n,string,string,int,int> soa_citizens;
   int avg = 0;
   int t = 1;
   std::size_t const salary=2
   for( int salary: get<salary>(soa_citizens)) {
     avg += (salary - avg) / t;
     ++t;
   }

calculate the same thing as the avg in your example?
(CONFESSION: Not tested).

-regards,
Larry


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk