home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / lisp / 2436 < prev    next >
Encoding:
Text File  |  1992-09-15  |  2.7 KB  |  70 lines

  1. Path: sparky!uunet!gatech!darwin.sura.net!spool.mu.edu!olivea!mintaka.lcs.mit.edu!gateway
  2. From: SWM@stony-brook.scrc.symbolics.com (Scott McKay)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: copy-array?
  5. Message-ID: <19920915145558.5.SWM@SUMMER.SCRC.Symbolics.COM>
  6. Date: 15 Sep 92 14:55:32 GMT
  7. References: <1935kvINNi3f@early-bird.think.com>
  8. Sender: news@mintaka.lcs.mit.edu
  9. Organization: LCS news/mail gateway
  10. Lines: 57
  11. X-Unparseable-Date: Tue
  12.  
  13.  
  14.     Date: Mon, 14 Sep 1992 19:01 EDT
  15.     From: Barry Margolin <barmar@think.com>
  16.  
  17.     In article <9209142212.AA24213@brownie.cs.wisc.edu> so@CS.WISC.EDU (Bryan S. So) writes:
  18.     >This is almost the first time I seriously use arrays
  19.     >in Common Lisp.  Now, how come there is no intrinsic
  20.     >function to make a copy of an array?
  21.  
  22.     There was a proposal in X3J13 to extend some of the sequence functions to
  23.     work on multidimensional arrays, but it was voted down.  That would have
  24.     enabled COPY-SEQ to do what you want.
  25.  
  26.     It's kind of a shame that a multidimensional array can't be used as the
  27.     :INITIAL-CONTENTS argument for MAKE-ARRAY, even when it matches the
  28.     dimensionality of the array being built.  Then you could do:
  29.  
  30. A "shame" is an understatement.  I think that, at the very least, this
  31. correction to MAKE-ARRAY should be proposed during the dpANS public
  32. review period.  (Note that I do not consider this a true correction of
  33. an error of omission, not simply an extension.)
  34.  
  35.     (make-array ... :initial-contents old-array)
  36.  
  37.     >The only algorithm I have is to make a new array then
  38.     >assign the elements one by one.  Is there an easier/
  39.     >more efficient method to do this?
  40.  
  41.     You can do it using displaced arrays and MAP-INTO:
  42.  
  43.     (defun copy-array (old-array)
  44.       (let* ((type (array-element-type old-array))
  45.          (size (array-total-size old-array))
  46.          (new-array (make-array (array-dimensions old-array)
  47.                 :element-type type))
  48.          (old-vector (make-array size :element-type type :displaced-to old-array))
  49.          (new-vector (make-array size :element-type type :displaced-to new-array)))
  50.     (copy-into new-vector #'identity old-vector)
  51.     new-array))
  52.  
  53.     A possibly more efficient way to implement this is:
  54.  
  55.     (defun copy-array (old-array)
  56.       (let* ((type (array-element-type old-array))
  57.          (size (array-total-size old-array))
  58.          (old-vector (make-array size :element-type type :displaced-to old-array))
  59.          (new-vector (copy-seq old-vector)))
  60.     (make-array (array-dimensions old-array) :element-type type
  61.         :displaced-to new-vector)))
  62.  
  63.     However, this has the misfeature that the resulting array is likely to be
  64.     more expensive to access, because it's displaced.
  65.     -- 
  66.     Barry Margolin
  67.     System Manager, Thinking Machines Corp.
  68.  
  69.     barmar@think.com          {uunet,harvard}!think!barmar
  70.