Incomputing, a group ofparallel arrays (also known asstructure of arrays or SoA) is a form ofimplicit data structure that uses multiplearrays to represent a singular array ofrecords. 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 asarray 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 inC using parallel arrays:
intages[]={0,17,2,52,25};char*names[]={"None","Mike","Billy","Tom","Stan"};intparent[]={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]]);}
inPerl (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, inPython:
first_names=["Joe","Bob","Frank","Hans"]last_names=["Smith","Seger","Sinatra","Schultze"]heights_in_cm=[169,158,201,199]foriinrange(len(first_names)):print("Name:%s%s"%(first_names[i],last_names[i]))print("Height in cm:%s"%heights_in_cm[i])# Using zip:forfirst_name,last_name,height_in_cminzip(first_names,last_names,heights_in_cm):print(f"Name:{first_name}{last_name}")print(f"Height in cm:{height_in_cm}")
This sectiondoes notcite anysources. Please helpimprove this section byadding citations to reliable sources. Unsourced material may be challenged andremoved. Find sources: "Parallel array" – news ·newspapers ·books ·scholar ·JSTOR(January 2018) (Learn how and when to remove this message) |
This articlecontains apro and con list. Please helprewrite it into consolidated sections based on topics.(May 2021) |
Parallel arrays have a number of practical advantages over the normal approach:
Several of these advantages depend strongly on the particular programming language and implementation in use.
However, parallel arrays also have several strong disadvantages, which serves to explain why they are not generally preferred:
The bad locality of reference can be alleviated in some cases: if a structure can be divided into groups of fields that are generally accessed together, an array can be constructed for each group, and its elements are records containing only these subsets of the larger structure's fields. (seedata-oriented design). This is a valuable way of speeding up access to very large structures with many members, while keeping the portions of the structure tied together. An alternative to tying them together using array indexes is to usereferences to tie the portions together, but this can be less efficient in time and space.
Another alternative is to use a single array, where each entry is a record structure. Many language provide a way to declare actual records, and arrays of them. In other languages it may be feasible to simulate this by declaring an array of n*m size, where m is the size of all the fields together, packing the fields into what is effectively a record, even though the particular language lacks direct support for records. Somecompiler optimizations, particularly forvector processors, are able to perform this transformation automatically when arrays of structures are created in the program.[citation needed]