Content deleted Content added
https://xn--htl4dsukss-n7at7k.xn--tckwe/m/userarea.php?games&head=games&gid=pp Tags: Reverted Mobile edit Mobile web edit |
m →Applications: Minor wording changes |
||
(43 intermediate revisions by 29 users not shown) | |||
Line 1:
{{short description|Type of data structure}}
{{About|the byte-layout-level structure|the abstract data type|Array (data type)}}
{{Use dmy dates|date=November 2020}}
{{More citations needed|date=September 2008}}
In [[computer science]], an '''array''' is a [[data structure]] consisting of a collection of ''elements'' ([[value (computer science)|values]] or [[variable (programming)|variables]]), of same memory size, each identified by at least one ''array index'' or ''key'', a collection of which may be a [[tuple]], known as an index tuple. An array is stored such that the position (memory address) of each element can be computed from its index tuple by a mathematical formula.<ref>{{cite web|url=https://xlinux.nist.gov/dads/HTML/array.html|title=array|last=Black|first=Paul E.|date=13 November 2008|work=[[Dictionary of Algorithms and Data Structures]]|publisher=[[National Institute of Standards and Technology]]|access-date=22 August 2010}}</ref><ref name="andres">{{cite arXiv |eprint=1008.2909 |author1=Bjoern Andres |author2=Ullrich Koethe |author3=Thorben Kroeger |author4=Hamprecht |title=Runtime-Flexible Multi-dimensional Arrays and Views for C++98 and C++0x |class=cs.DS |year=2010}}</ref><ref name="garcia">{{Cite journal|last1=Garcia|first1=Ronald |first2=Andrew |last2=Lumsdaine|year=2005|title=MultiArray: a C++ library for generic programming with arrays|journal=Software: Practice and Experience|volume=35|issue=2|pages=159–188|issn=0038-0644|doi=10.1002/spe.630|s2cid=10890293 }}</ref> The simplest type of data structure is a linear array, also called a one-dimensional array.
For example, an array of ten [[32-bit]] (4-byte) integer variables, with indices 0 through 9, may be stored as ten [[Word (data type)|words]] at memory addresses 2000, 2004, 2008, ..., 2036, (in [[hexadecimal]]: <code>0x7D0</code>, <code>0x7D4</code>, <code>0x7D8</code>, ..., <code>0x7F4</code>) so that the element with index ''i'' has the address 2000 + (''i'' × 4).<ref>David R. Richardson (2002), The Book on Data Structures. iUniverse, 1112 pages. {{ISBN|0-595-24039-9}}, {{ISBN|978-0-595-24039-5}}.</ref>
The memory address of the first element of an array is called first address, foundation address, or base address.
Because the mathematical concept of a [[matrix (mathematics)|matrix]] can be represented as a two-dimensional grid, two-dimensional arrays are also sometimes called "matrices". In some cases the term "vector" is used in computing to refer to an array, although [[tuple]]s rather than [[vector space|vectors]] are the more mathematically correct equivalent. [[table (information)|Table]]s are often implemented in the form of arrays, especially [[lookup table]]s; the word "table" is sometimes used as a synonym of array.
Arrays are among the oldest and most important data structures, and are used by almost every program. They are also used to implement many other data structures, such as [[list (computing)|list]]s and [[string (computer science)|string]]s. They effectively exploit the addressing logic of computers. In most modern computers and many [[external storage]] devices, the memory is a one-dimensional array of words, whose indices are their addresses. [[Central processing unit|Processors]], especially [[vector processor]]s, are often optimized for array operations.
Arrays are useful mostly because the element indices can be computed at [[Run time (program lifecycle phase)|run time]]. Among other things, this feature allows a single iterative [[statement (programming)|statement]] to process arbitrarily many elements of an array. For that reason, the elements of an array data structure are required to have the same size and should use the same data representation. The set of valid index tuples and the addresses of the elements (and hence the element addressing formula) are usually,<ref name="garcia" /><ref name="veldhuizen">{{cite conference |first1=Todd L. |last1=Veldhuizen |title=Arrays in Blitz++ |publisher=Springer |___location=Berlin |conference=Computing in Object-Oriented Parallel Environments |date=December 1998 |isbn=978-3-540-65387-5 |pages=223–230 |series=Lecture Notes in Computer Science |volume=1505 |doi=10.1007/3-540-49372-7_24 }}{{dead link|date=November 2023}}</ref> but not always,<ref name="andres" /> fixed while the array is in use.
The term "array" may also refer to an [[array data type]], a kind of [[data type]] provided by most [[high-level programming language]]s that consists of a collection of values or variables that can be selected by one or more indices computed at run-time. Array types are often implemented by array structures; however, in some languages they may be implemented by [[hash table]]s, [[linked list]]s, [[search tree]]s, or other data structures.
The term is also used, especially in the description of [[algorithm]]s, to mean [[associative array]] or "abstract array", a [[theoretical computer science]] model (an [[abstract data type]] or ADT) intended to capture the essential properties of arrays.
==History==
The first digital computers used machine-language programming to set up and access array structures for data tables, vector and matrix computations, and for many other purposes. [[John von Neumann]] wrote the first array-sorting program ([[merge sort]]) in 1945, during the building of the [[EDVAC|first stored-program computer]].<ref>
Assembly languages generally have no special support for arrays, other than what the machine itself provides. The earliest high-level programming languages, including [[Fortran|FORTRAN]] (1957), [[Lisp (programming language)|Lisp]] (1958), [[COBOL]] (1960), and [[ALGOL|ALGOL 60]] (1960), had support for multi-dimensional arrays, and so has [[C (programming language)|C]] (1972). In [[C++]] (1983), class templates exist for multi-dimensional arrays whose dimension is fixed at runtime<ref name="garcia" /><ref name="veldhuizen" /> as well as for runtime-flexible arrays.<ref name="andres" />
Line 13 ⟶ 31:
One or more large arrays are sometimes used to emulate in-program [[dynamic memory allocation]], particularly [[memory pool]] allocation. Historically, this has sometimes been the only way to allocate "dynamic memory" portably.
Arrays can be used to determine partial or complete [[control flow]] in programs, as a compact alternative to (otherwise repetitive) multiple <code>IF</code> statements.
==Element identifier and addressing formulas==
Line 42 ⟶ 60:
===One-dimensional arrays===
[[File:1D array diagram.svg|thumb|Diagram of a typical 1D array]]
A one-dimensional array (or single dimension array) is a type of linear array. Accessing its elements involves a single subscript which can either represent a row or column index.
As an example consider the C declaration <code>int anArrayName[10];</code> which declares a one-dimensional array of ten integers. Here, the array can store ten elements of type <code>int</code> . This array has indices starting from zero through nine. For example, the expressions <code>anArrayName[0]</code> and <code>anArrayName[9]</code> are the first and last elements respectively.
For a vector with linear addressing, the element with index ''i'' is located at the address {{nowrap|''B'' + ''c''
If the valid element indices begin at 0, the constant ''B'' is simply the address of the first element of the array. For this reason, the [[C (programming language)|C programming language]] specifies that array indices always begin at 0; and many programmers will call that element "[[zero-based numbering|zeroth]]" rather than "first".
However, one can choose the index of the first element by an appropriate choice of the base address ''B''. For example, if the array has five elements, indexed 1 through 5, and the base address ''B'' is replaced by {{nowrap|''B'' + 30''c''}}, then the indices of those same elements will be 31 to 35. If the numbering does not start at 0, the constant ''B'' may not be the address of any element.
[[File:2D array diagram.svg|thumb|Diagram of a typical 2D array]]
===Multidimensional arrays===
[[File:3D array diagram.svg|thumb|Diagram of a typical 3D array]]
For a multidimensional array, the element with indices ''i'',''j'' would have address ''B'' + ''c'' · ''i'' + ''d'' · ''j'', where the coefficients ''c'' and ''d'' are the ''row'' and ''column address increments'', respectively.
Line 69 ⟶ 89:
===Dope vectors===
{{Main|Dope vector}}
The addressing formula is completely defined by the dimension ''d'', the base address ''B'', and the increments ''c''<sub>1</sub>, ''c''<sub>2</sub>, ..., ''c''<sub>''k''</sub>. It is often useful to pack these parameters into a record called the array's ''descriptor'' or ''stride vector'' or ''[[dope vector]]''.<ref name="andres" /><ref name="garcia" /> The size of each element, and the minimum and maximum values allowed for each index may also be included in the dope vector. The dope vector is a complete [[handle (computing)|handle]] for the array, and is a convenient way to pass arrays as arguments to [[subroutine|procedures]]. Many useful [[array slicing]] operations (such as selecting a sub-array, swapping indices, or reversing the direction of the indices) can be performed very efficiently by manipulating the dope vector.<ref name="andres" />▼
▲The addressing formula is completely defined by the dimension ''d'', the base address ''B'', and the increments ''c''<sub>1</sub>, ''c''<sub>2</sub>, ..., ''c''<sub>''k''</sub>. It is often useful to pack these parameters into a record called the array's
===Compact layouts===
Line 99 ⟶ 121:
For arrays with three or more indices, "row major order" puts in consecutive positions any two elements whose index tuples differ only by one in the ''last'' index. "Column major order" is analogous with respect to the ''first'' index.
In systems which use [[processor cache]] or [[virtual memory]], scanning an array is much faster if successive elements are stored in consecutive positions in memory, rather than sparsely scattered. This is known as spatial locality, which is a type of [[locality of reference]]. Many algorithms that use multidimensional arrays will scan them in a predictable order. A programmer (or a sophisticated compiler) may use this information to choose between row- or column-major layout for each array. For example, when computing the product ''A''·''B'' of two matrices, it would be best to have ''A'' stored in row-major order, and ''B'' in column-major order.
===Resizing===
Line 158 ⟶ 180:
{{Commons category|Array data structure}}
{{Wiktionary|array}}
* {{Wikibooks
{{
{{Data structures}}
{{Parallel computing}}
|