Wednesday, April 26, 2017

Parallel array

No comments :
            A group of parallel arrays (also known as structure of arrays or SoA) is a form of implicit data structure that uses multiple arrays to represent a singular array of records. It keeps a separate, homogeneous data array for each field of the record, each having the same number of elements. Then, objects located at the same index in each array are implicitly the fields of a single record. Pointers from one object to another are replaced by array indices. This contrasts with the normal approach of storing all fields of each record together in memory (also known as array of structures or AoS). For example, one might declare an array of 100 names, each a string, and 100 ages, each an integer, associating each name with the age that has the same index.
An example in C using parallel arrays:
int  ages[]   = {0,          17,        2,          52,         25};
char *names[] = {"None",     "Mike",    "Billy",    "Tom",      "Stan"};
int  parent[] = {0 /*None*/, 3 /*Tom*/, 1 /*Mike*/, 0 /*None*/, 3 /*Tom*/};

for(i = 1; i <= 4; i++) {
    printf("Name: %s, Age: %d, Parent: %s \n",
           names[i], ages[i], names[parent[i]]);
}
in Perl (using a hash of arrays to hold references to each array):
my %data = (
    first_name   => ['Joe',  'Bob',  'Frank',  'Hans'    ],
    last_name    => ['Smith','Seger','Sinatra','Schultze'],
    height_in_cm => [169,     158,    201,      199      ]);

for $i (0..$#{$data{first_name}}) {
    printf "Name: %s %s\n", $data{first_name}[$i], $data{last_name}[$i];
    printf "Height in CM: %i\n", $data{height_in_cm}[$i];
}
Or, in Python:
first_names   = ['Joe',  'Bob',  'Frank',  'Hans'    ]
last_names    = ['Smith','Seger','Sinatra','Schultze']
heights_in_cm = [169,     158,    201,      199      ]

for i in range(len(first_names)):
    print "Name: %s %s" % (first_names[i], last_names[i])
    print "Height in CM: %s" % heights_in_cm[i]

# Using zip:
for first_name, last_name, height in zip(first_names, last_names, heights_in_cm):
    print "Name: %s %s" % (first_name, last_name)
    print "Height in CM: %s" % height_in_cm

No comments :

Post a Comment