home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!darwin.sura.net!spool.mu.edu!olivea!mintaka.lcs.mit.edu!gateway
- From: SWM@stony-brook.scrc.symbolics.com (Scott McKay)
- Newsgroups: comp.lang.lisp
- Subject: Re: copy-array?
- Message-ID: <19920915145558.5.SWM@SUMMER.SCRC.Symbolics.COM>
- Date: 15 Sep 92 14:55:32 GMT
- References: <1935kvINNi3f@early-bird.think.com>
- Sender: news@mintaka.lcs.mit.edu
- Organization: LCS news/mail gateway
- Lines: 57
- X-Unparseable-Date: Tue
-
-
- Date: Mon, 14 Sep 1992 19:01 EDT
- From: Barry Margolin <barmar@think.com>
-
- In article <9209142212.AA24213@brownie.cs.wisc.edu> so@CS.WISC.EDU (Bryan S. So) writes:
- >This is almost the first time I seriously use arrays
- >in Common Lisp. Now, how come there is no intrinsic
- >function to make a copy of an array?
-
- There was a proposal in X3J13 to extend some of the sequence functions to
- work on multidimensional arrays, but it was voted down. That would have
- enabled COPY-SEQ to do what you want.
-
- It's kind of a shame that a multidimensional array can't be used as the
- :INITIAL-CONTENTS argument for MAKE-ARRAY, even when it matches the
- dimensionality of the array being built. Then you could do:
-
- A "shame" is an understatement. I think that, at the very least, this
- correction to MAKE-ARRAY should be proposed during the dpANS public
- review period. (Note that I do not consider this a true correction of
- an error of omission, not simply an extension.)
-
- (make-array ... :initial-contents old-array)
-
- >The only algorithm I have is to make a new array then
- >assign the elements one by one. Is there an easier/
- >more efficient method to do this?
-
- You can do it using displaced arrays and MAP-INTO:
-
- (defun copy-array (old-array)
- (let* ((type (array-element-type old-array))
- (size (array-total-size old-array))
- (new-array (make-array (array-dimensions old-array)
- :element-type type))
- (old-vector (make-array size :element-type type :displaced-to old-array))
- (new-vector (make-array size :element-type type :displaced-to new-array)))
- (copy-into new-vector #'identity old-vector)
- new-array))
-
- A possibly more efficient way to implement this is:
-
- (defun copy-array (old-array)
- (let* ((type (array-element-type old-array))
- (size (array-total-size old-array))
- (old-vector (make-array size :element-type type :displaced-to old-array))
- (new-vector (copy-seq old-vector)))
- (make-array (array-dimensions old-array) :element-type type
- :displaced-to new-vector)))
-
- However, this has the misfeature that the resulting array is likely to be
- more expensive to access, because it's displaced.
- --
- Barry Margolin
- System Manager, Thinking Machines Corp.
-
- barmar@think.com {uunet,harvard}!think!barmar
-