110 lines
6 KiB
HTML
110 lines
6 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
||
|
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
||
|
<HEAD>
|
||
|
<TITLE>2.5. Arrays</TITLE>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<meta name="description" value=" Arrays">
|
||
|
<meta name="keywords" value="clm">
|
||
|
<meta name="resource-type" value="document">
|
||
|
<meta name="distribution" value="global">
|
||
|
<P>
|
||
|
<b>Common Lisp the Language, 2nd Edition</b>
|
||
|
<BR> <HR><A NAME=tex2html1866 HREF="node30.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1864 HREF="node15.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1858 HREF="node28.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1868 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1869 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html1867 HREF="node30.html"> Vectors</A>
|
||
|
<B>Up:</B> <A NAME=tex2html1865 HREF="node15.html"> Data Types</A>
|
||
|
<B> Previous:</B> <A NAME=tex2html1859 HREF="node28.html"> Lists and Conses</A>
|
||
|
<HR> <P>
|
||
|
<H1><A NAME=SECTION00650000000000000000>2.5. Arrays</A></H1>
|
||
|
<P>
|
||
|
<A NAME=ARRAYTYPESECTION>An</A>
|
||
|
<A NAME=1214><tt>array</tt></A> is an object with components arranged according
|
||
|
to a Cartesian coordinate system.
|
||
|
In general, these components may be any Lisp data objects.
|
||
|
<P>
|
||
|
The number of dimensions of an array is called its <i>rank</i>
|
||
|
(this terminology is borrowed from APL);
|
||
|
the rank is a non-negative integer.
|
||
|
Likewise, each dimension is itself a non-negative integer.
|
||
|
The total number of elements in the array is the product of all the
|
||
|
dimensions.
|
||
|
<P>
|
||
|
An implementation of Common Lisp may impose a limit on the rank of an array,
|
||
|
but this limit may not be smaller than 7. Therefore, any Common Lisp
|
||
|
program may assume the use of arrays of rank 7 or less.
|
||
|
(A program may determine the actual limit on array ranks for
|
||
|
a given implementation by examining the constant <tt>array-rank-limit</tt>.)
|
||
|
<P>
|
||
|
It is permissible for a dimension to be zero. In this case,
|
||
|
the array has no elements, and any attempt to access an element
|
||
|
is in error. However, other properties of the array, such as the
|
||
|
dimensions themselves, may be used.
|
||
|
If the rank is zero, then there are no dimensions, and the
|
||
|
product of the dimensions is then by definition 1.
|
||
|
A zero-rank array therefore has a single element.
|
||
|
<P>
|
||
|
An array element is specified by a sequence of indices.
|
||
|
The length of the sequence must equal the rank of the array.
|
||
|
Each index must be a non-negative integer strictly less than
|
||
|
the corresponding array dimension. Array indexing is
|
||
|
therefore zero-origin, not one-origin as in (the default case of)
|
||
|
Fortran.
|
||
|
<P>
|
||
|
As an example, suppose that the variable <tt>foo</tt> names a 3-by-5 array.
|
||
|
Then the first index may be 0, 1, or 2, and the second index
|
||
|
may be 0, 1, 2, 3, or 4. One may refer to array elements using
|
||
|
the function <tt>aref</tt>; for example, <tt>(aref foo 2 1)</tt>
|
||
|
refers to element (2, 1) of the array. Note that <tt>aref</tt> takes
|
||
|
a variable number of arguments: an array, and as many indices
|
||
|
as the array has dimensions.
|
||
|
A zero-rank array has no dimensions, and therefore
|
||
|
<tt>aref</tt> would take such an array and no indices, and return the sole
|
||
|
element of the array.
|
||
|
<P>
|
||
|
In general, arrays can be multidimensional,
|
||
|
can share their contents with other array objects, and can have their
|
||
|
size altered dynamically (either enlarging or shrinking) after creation.
|
||
|
A one-dimensional array may also have a <i>fill pointer</i>.
|
||
|
<P>
|
||
|
Multidimensional arrays store their components in row-major order;
|
||
|
that is, internally a multidimensional array is stored as a one-dimensional
|
||
|
array, with the multidimensional index sets ordered lexicographically,
|
||
|
last index varying fastest. This is important in two situations:
|
||
|
(1) when arrays with different dimensions share their contents, and
|
||
|
(2) when accessing very large arrays in a virtual-memory implementation.
|
||
|
(The first situation is a matter of semantics; the second, a matter
|
||
|
of efficiency.)
|
||
|
<P>
|
||
|
An array that is not displaced to another array, has no fill pointer, and
|
||
|
is not to have its size adjusted dynamically after creation is called a
|
||
|
<i>simple</i> array. The user may provide declarations that certain arrays
|
||
|
will be simple. Some implementations can handle simple arrays in an
|
||
|
especially efficient manner; for example, simple arrays may have a more
|
||
|
compact representation than non-simple arrays.
|
||
|
<P>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
X3J13 voted in June 1989
|
||
|
(ADJUST-ARRAY-NOT-ADJUSTABLE) <A NAME=1226> </A>
|
||
|
to clarify that if one or more of the <tt>:adjustable</tt>, <tt>:fill-pointer</tt>,
|
||
|
and <tt>:displaced-to</tt> arguments is true when <tt>make-array</tt>
|
||
|
is called, then whether the resulting
|
||
|
array is simple is unspecified; but if all three arguments are false,
|
||
|
then the resulting array is guaranteed to be simple.
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<HR>
|
||
|
<UL>
|
||
|
<LI> <A NAME=tex2html1870 HREF="node30.html#SECTION00651000000000000000"> Vectors</A>
|
||
|
<LI> <A NAME=tex2html1871 HREF="node31.html#SECTION00652000000000000000"> Strings</A>
|
||
|
<LI> <A NAME=tex2html1872 HREF="node32.html#SECTION00653000000000000000"> Bit-Vectors</A>
|
||
|
</UL>
|
||
|
<BR> <HR><A NAME=tex2html1866 HREF="node30.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html1864 HREF="node15.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html1858 HREF="node28.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html1868 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html1869 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html1867 HREF="node30.html"> Vectors</A>
|
||
|
<B>Up:</B> <A NAME=tex2html1865 HREF="node15.html"> Data Types</A>
|
||
|
<B> Previous:</B> <A NAME=tex2html1859 HREF="node28.html"> Lists and Conses</A>
|
||
|
<HR> <P>
|
||
|
<HR>
|
||
|
<P><ADDRESS>
|
||
|
AI.Repository@cs.cmu.edu
|
||
|
</ADDRESS>
|
||
|
</BODY>
|