145 lines
18 KiB
HTML
145 lines
18 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Function MAKE-ARRAY</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="t_smp_bt.htm">
|
|
<LINK REL=UP HREF="c_arrays.htm">
|
|
<LINK REL=NEXT HREF="f_adjust.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="t_smp_bt.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_arrays.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="f_adjust.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="make-array"><I>Function</I> <B>MAKE-ARRAY</B></A> <P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>make-array</B> <I>dimensions <TT>&key</TT> element-type initial-element initial-contents adjustable fill-pointer displaced-to displaced-index-offset</I><P> => <I>new-array</I><P>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>dimensions</I>---a <A REL=DEFINITION HREF="26_glo_d.htm#designator"><I>designator</I></A> for a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> of <A REL=DEFINITION HREF="26_glo_v.htm#valid_array_dimension"><I>valid array dimensions</I></A>. <P>
|
|
<I>element-type</I>---a <A REL=DEFINITION HREF="26_glo_t.htm#type_specifier"><I>type specifier</I></A>. The default is <A REL=DEFINITION HREF="t_t.htm#t"><B>t</B></A>. <P>
|
|
<I>initial-element</I>---an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P>
|
|
<I>initial-contents</I>---an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P>
|
|
<I>adjustable</I>---a <A REL=DEFINITION HREF="26_glo_g.htm#generalized_boolean"><I>generalized boolean</I></A>. The default is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
|
|
<I>fill-pointer</I>---a <A REL=DEFINITION HREF="26_glo_v.htm#valid_fill_pointer"><I>valid fill pointer</I></A> for the <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> to be created, or <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A> or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. The default is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
|
|
<I>displaced-to</I>---an <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> or <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. The default is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. This option must not be supplied if either <I>initial-element</I> or <I>initial-contents</I> is supplied. <P>
|
|
<I>displaced-index-offset</I>---a <A REL=DEFINITION HREF="26_glo_v.htm#valid_array_row-major_index"><I>valid array row-major index</I></A> for <I>displaced-to</I>. The default is <TT>0</TT>. This option must not be supplied unless a <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A> <I>displaced-to</I> is supplied. <P>
|
|
<I>new-array</I>---an <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
Creates and returns an <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> constructed of the most <A REL=DEFINITION HREF="26_glo_s.htm#specialized"><I>specialized</I></A> <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> that can accommodate elements of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> given by <I>element-type</I>. If <I>dimensions</I> is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> then a zero-dimensional <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> is created. <P>
|
|
<I>Dimensions</I> represents the dimensionality of the new <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A>. <P>
|
|
<I>element-type</I> indicates the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> of the elements intended to be stored in the <I>new-array</I>. The <I>new-array</I> can actually store any <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>objects</I></A> of the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> which results from <I>upgrading</I> <I>element-type</I>; see <A REL=CHILD HREF="15_aba.htm">Section 15.1.2.1 (Array Upgrading)</A>. <P>
|
|
If <I>initial-element</I> is supplied, it is used to initialize each <A REL=DEFINITION HREF="26_glo_e.htm#element"><I>element</I></A> of <I>new-array</I>. If <I>initial-element</I> is supplied, it must be of the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> given by <I>element-type</I>. <I>initial-element</I> cannot be supplied if either the <TT>:initial-contents</TT> option is supplied or <I>displaced-to</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>. If <I>initial-element</I> is not supplied, the consequences of later reading an uninitialized <A REL=DEFINITION HREF="26_glo_e.htm#element"><I>element</I></A> of <I>new-array</I> are undefined unless either <I>initial-contents</I> is supplied or <I>displaced-to</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>. <P>
|
|
<I>initial-contents</I> is used to initialize the contents of <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A>. For example: <P>
|
|
<PRE>
|
|
(make-array '(4 2 3) :initial-contents
|
|
'(((a b c) (1 2 3))
|
|
((d e f) (3 1 2))
|
|
((g h i) (2 3 1))
|
|
((j k l) (0 0 0))))
|
|
</PRE>
|
|
</TT> <P>
|
|
<I>initial-contents</I> is composed of a nested structure of <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequences</I></A>. The numbers of levels in the structure must equal the rank of <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A>. Each leaf of the nested structure must be of the <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> given by <I>element-type</I>. If <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> is zero-dimensional, then <I>initial-contents</I> specifies the single <A REL=DEFINITION HREF="26_glo_e.htm#element"><I>element</I></A>. Otherwise, <I>initial-contents</I> must be a <A REL=DEFINITION HREF="26_glo_s.htm#sequence"><I>sequence</I></A> whose length is equal to the first dimension; each element must be a nested structure for an <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> whose dimensions are the remaining dimensions, and so on. <I>Initial-contents</I> cannot be supplied if either <I>initial-element</I> is supplied or <I>displaced-to</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>. If <I>initial-contents</I> is not supplied, the consequences of later reading an uninitialized <A REL=DEFINITION HREF="26_glo_e.htm#element"><I>element</I></A> of <I>new-array</I> are undefined unless either <I>initial-element</I> is supplied or <I>displaced-to</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>. <P>
|
|
If <I>adjustable</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>, the array is <A REL=DEFINITION HREF="26_glo_e.htm#expressly_adjustable"><I>expressly adjustable</I></A> (and so <A REL=DEFINITION HREF="26_glo_a.htm#actually_adjustable"><I>actually adjustable</I></A>); otherwise, the array is not <A REL=DEFINITION HREF="26_glo_e.htm#expressly_adjustable"><I>expressly adjustable</I></A> (and it is <A REL=DEFINITION HREF="26_glo_i.htm#implementation-dependent"><I>implementation-dependent</I></A> whether the array is <A REL=DEFINITION HREF="26_glo_a.htm#actually_adjustable"><I>actually adjustable</I></A>). <P>
|
|
If <I>fill-pointer</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>, the <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> must be one-dimensional; that is, the <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> must be a <A REL=DEFINITION HREF="26_glo_v.htm#vector"><I>vector</I></A>. If <I>fill-pointer</I> is <A REL=DEFINITION HREF="a_t.htm#t"><B>t</B></A>, the length of the <A REL=DEFINITION HREF="26_glo_v.htm#vector"><I>vector</I></A> is used to initialize the <A REL=DEFINITION HREF="26_glo_f.htm#fill_pointer"><I>fill pointer</I></A>. If <I>fill-pointer</I> is an <A REL=DEFINITION HREF="26_glo_i.htm#integer"><I>integer</I></A>, it becomes the initial <A REL=DEFINITION HREF="26_glo_f.htm#fill_pointer"><I>fill pointer</I></A> for the <A REL=DEFINITION HREF="26_glo_v.htm#vector"><I>vector</I></A>. <P>
|
|
If <I>displaced-to</I> is <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>, <A REL=DEFINITION HREF="#make-array"><B>make-array</B></A> will create a <A REL=DEFINITION HREF="26_glo_d.htm#displaced_array"><I>displaced array</I></A> and <I>displaced-to</I> is the <A REL=DEFINITION HREF="26_glo_t.htm#target"><I>target</I></A> of that <A REL=DEFINITION HREF="26_glo_d.htm#displaced_array"><I>displaced array</I></A>. In that case, the consequences are undefined if the <A REL=DEFINITION HREF="26_glo_a.htm#actual_array_element_type"><I>actual array element type</I></A> of <I>displaced-to</I> is not <A REL=DEFINITION HREF="26_glo_t.htm#type_equivalent"><I>type equivalent</I></A> to the <A REL=DEFINITION HREF="26_glo_a.htm#actual_array_element_type"><I>actual array element type</I></A> of the <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> being created. If <I>displaced-to</I> is <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, the <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> is not a <A REL=DEFINITION HREF="26_glo_d.htm#displaced_array"><I>displaced array</I></A>. <P>
|
|
The <I>displaced-index-offset</I> is made to be the index offset of the <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A>. When an array A is given as the <TT>:displaced-to</TT> <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>argument</I></A> to <A REL=DEFINITION HREF="#make-array"><B>make-array</B></A> when creating array B, then array B is said to be displaced to array A. The total number of elements in an <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A>, called the total size of the <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A>, is calculated as the product of all the dimensions. It is required that the total size of A be no smaller than the sum of the total size of B plus the offset <TT>n</TT> supplied by the <I>displaced-index-offset</I>. The effect of displacing is that array B does not have any elements of its own, but instead maps <A REL=DEFINITION HREF="26_glo_a.htm#access"><I>accesses</I></A> to itself into <A REL=DEFINITION HREF="26_glo_a.htm#access"><I>accesses</I></A> to array A. The mapping treats both <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>arrays</I></A> as if they were one-dimensional by taking the elements in row-major order, and then maps an <A REL=DEFINITION HREF="26_glo_a.htm#access"><I>access</I></A> to element <TT>k</TT> of array B to an <A REL=DEFINITION HREF="26_glo_a.htm#access"><I>access</I></A> to element <TT>k</TT>+<TT>n</TT> of array A. <P>
|
|
If <A REL=DEFINITION HREF="#make-array"><B>make-array</B></A> is called with <I>adjustable</I>, <I>fill-pointer</I>, and <I>displaced-to</I> each <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>, then the result is a <A REL=DEFINITION HREF="26_glo_s.htm#simple_array"><I>simple array</I></A>. If <A REL=DEFINITION HREF="#make-array"><B>make-array</B></A> is called with one or more of <I>adjustable</I>, <I>fill-pointer</I>, or <I>displaced-to</I> being <A REL=DEFINITION HREF="26_glo_t.htm#true"><I>true</I></A>, whether the resulting <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> is a <A REL=DEFINITION HREF="26_glo_s.htm#simple_array"><I>simple array</I></A> is <A REL=DEFINITION HREF="26_glo_i.htm#implementation-dependent"><I>implementation-dependent</I></A>. <P>
|
|
When an array A is given as the <TT>:displaced-to</TT> <A REL=DEFINITION HREF="26_glo_a.htm#argument"><I>argument</I></A> to <A REL=DEFINITION HREF="#make-array"><B>make-array</B></A> when creating array B, then array B is said to be displaced to array A. The total number of elements in an <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A>, called the total size of the <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A>, is calculated as the product of all the dimensions. The consequences are unspecified if the total size of A is smaller than the sum of the total size of B plus the offset <TT>n</TT> supplied by the <I>displaced-index-offset</I>. The effect of displacing is that array B does not have any elements of its own, but instead maps <A REL=DEFINITION HREF="26_glo_a.htm#access"><I>accesses</I></A> to itself into <A REL=DEFINITION HREF="26_glo_a.htm#access"><I>accesses</I></A> to array A. The mapping treats both <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>arrays</I></A> as if they were one-dimensional by taking the elements in row-major order, and then maps an <A REL=DEFINITION HREF="26_glo_a.htm#access"><I>access</I></A> to element <TT>k</TT> of array B to an <A REL=DEFINITION HREF="26_glo_a.htm#access"><I>access</I></A> to <A REL=DEFINITION HREF="26_glo_e.htm#element"><I>element</I></A> <TT>k</TT>+<TT>n</TT> of array A. <P>
|
|
<P><B>Examples:</B><P>
|
|
|
|
<PRE>
|
|
|
|
(make-array 5) ;; Creates a one-dimensional array of five elements.
|
|
(make-array '(3 4) :element-type '(mod 16)) ;; Creates a
|
|
;;two-dimensional array, 3 by 4, with four-bit elements.
|
|
(make-array 5 :element-type 'single-float) ;; Creates an array of single-floats.
|
|
</PRE>
|
|
</TT> <P>
|
|
<PRE>
|
|
(make-array nil :initial-element nil) => #0ANIL
|
|
(make-array 4 :initial-element nil) => #(NIL NIL NIL NIL)
|
|
(make-array '(2 4)
|
|
:element-type '(unsigned-byte 2)
|
|
:initial-contents '((0 1 2 3) (3 2 1 0)))
|
|
=> #2A((0 1 2 3) (3 2 1 0))
|
|
(make-array 6
|
|
:element-type 'character
|
|
:initial-element #\a
|
|
:fill-pointer 3) => "aaa"
|
|
</PRE>
|
|
</TT> <P>
|
|
The following is an example of making a <A REL=DEFINITION HREF="26_glo_d.htm#displaced_array"><I>displaced array</I></A>. <P>
|
|
<PRE>
|
|
(setq a (make-array '(4 3)))
|
|
=> #<ARRAY 4x3 simple 32546632>
|
|
(dotimes (i 4)
|
|
(dotimes (j 3)
|
|
(setf (aref a i j) (list i 'x j '= (* i j)))))
|
|
=> NIL
|
|
(setq b (make-array 8 :displaced-to a
|
|
:displaced-index-offset 2))
|
|
=> #<ARRAY 8 indirect 32550757>
|
|
(dotimes (i 8)
|
|
(print (list i (aref b i))))
|
|
>> (0 (0 X 2 = 0))
|
|
>> (1 (1 X 0 = 0))
|
|
>> (2 (1 X 1 = 1))
|
|
>> (3 (1 X 2 = 2))
|
|
>> (4 (2 X 0 = 0))
|
|
>> (5 (2 X 1 = 2))
|
|
>> (6 (2 X 2 = 4))
|
|
>> (7 (3 X 0 = 0))
|
|
=> NIL
|
|
</PRE>
|
|
</TT> The last example depends on the fact that <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>arrays</I></A> are, in effect, stored in row-major order. <P>
|
|
<PRE>
|
|
(setq a1 (make-array 50))
|
|
=> #<ARRAY 50 simple 32562043>
|
|
(setq b1 (make-array 20 :displaced-to a1 :displaced-index-offset 10))
|
|
=> #<ARRAY 20 indirect 32563346>
|
|
(length b1) => 20
|
|
|
|
(setq a2 (make-array 50 :fill-pointer 10))
|
|
=> #<ARRAY 50 fill-pointer 10 46100216>
|
|
(setq b2 (make-array 20 :displaced-to a2 :displaced-index-offset 10))
|
|
=> #<ARRAY 20 indirect 46104010>
|
|
(length a2) => 10
|
|
(length b2) => 20
|
|
|
|
(setq a3 (make-array 50 :fill-pointer 10))
|
|
=> #<ARRAY 50 fill-pointer 10 46105663>
|
|
(setq b3 (make-array 20 :displaced-to a3 :displaced-index-offset 10
|
|
:fill-pointer 5))
|
|
=> #<ARRAY 20 indirect, fill-pointer 5 46107432>
|
|
(length a3) => 10
|
|
(length b3) => 5
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>Affected By:</B> None.
|
|
<P>
|
|
<P><B>Exceptional Situations:</B> None.
|
|
<P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="f_adju_1.htm#adjustable-array-p"><B>adjustable-array-p</B></A>, <A REL=DEFINITION HREF="f_aref.htm#aref"><B>aref</B></A>, <A REL=DEFINITION HREF="f_arrayp.htm#arrayp"><B>arrayp</B></A>, <A REL=DEFINITION HREF="f_ar_ele.htm#array-element-type"><B>array-element-type</B></A>, <A REL=DEFINITION HREF="v_ar_ran.htm#array-rank-limit"><B>array-rank-limit</B></A>, <A REL=DEFINITION HREF="v_ar_dim.htm#array-dimension-limit"><B>array-dimension-limit</B></A>, <A REL=DEFINITION HREF="f_fill_p.htm#fill-pointer"><B>fill-pointer</B></A>, <A REL=DEFINITION HREF="f_upgr_1.htm#upgraded-array-element-type"><B>upgraded-array-element-type</B></A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
There is no specified way to create an <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> for which <A REL=DEFINITION HREF="f_adju_1.htm#adjustable-array-p"><B>adjustable-array-p</B></A> definitely returns <A REL=DEFINITION HREF="26_glo_f.htm#false"><I>false</I></A>. There is no specified way to create an <A REL=DEFINITION HREF="26_glo_a.htm#array"><I>array</I></A> that is not a <A REL=DEFINITION HREF="26_glo_s.htm#simple_array"><I>simple array</I></A>. <P>
|
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss005.htm">ADJUST-ARRAY-NOT-ADJUSTABLE:IMPLICIT-COPY</A><LI> <A REL=CHILD HREF="../Issues/iss355.htm">UNINITIALIZED-ELEMENTS:CONSEQUENCES-UNDEFINED</A><P></UL><HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|