home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / lisp / 2435 < prev    next >
Encoding:
Internet Message Format  |  1992-09-15  |  2.3 KB

  1. Path: sparky!uunet!gatech!asuvax!farallon!danny.farallon.com!user
  2. From: danny@farallon.com (Danny Brewer)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: copy-array?
  5. Message-ID: <danny-150992090432@danny.farallon.com>
  6. Date: 15 Sep 92 14:00:36 GMT
  7. References: <9209142212.AA24213@brownie.cs.wisc.edu>
  8. Sender: news@farallon.farallon.com
  9. Followup-To: comp.lang.lisp
  10. Organization: Farallon Computing, Inc.
  11. Lines: 54
  12. Nntp-Posting-Host: danny
  13.  
  14. In article <9209142212.AA24213@brownie.cs.wisc.edu>, so@CS.WISC.EDU (Bryan
  15. S. So) wrote:
  16. > The only algorithm I have is to make a new array then
  17. > assign the elements one by one.  Is there an easier/
  18. > more efficient method to do this?
  19.  
  20. That's what I concluded myself.  I wrote the following
  21. function which makes a new array and copies the elements
  22. of the old array into it.  I use two temporary vectors
  23. displaced to the arrays so that I treat the copy like
  24. a sequence.
  25.  
  26. About a year ago this question of copying arrays came
  27. up and I posted this code.  Someone suggested that I
  28. use REPLACE instead of DOTIMES because it might be
  29. more efficient.  I tried this which resulted in more
  30. than doubling the speed.  If you pass in a copier
  31. function, then the DOTIMES method is used so that
  32. the copier function can process each element.
  33.  
  34.  
  35. (DEFUN Copy-Array (array &KEY copierFn)
  36.   "Make a new array which is a copy of the original."
  37.   (LET* (
  38.          (arrayType (ARRAY-ELEMENT-TYPE array))
  39.          (arraySize (ARRAY-TOTAL-SIZE array))
  40.          (newArray (MAKE-ARRAY (ARRAY-DIMENSIONS array)
  41.                                :ELEMENT-TYPE arrayType
  42.                                :ADJUSTABLE (ADJUSTABLE-ARRAY-P array)))
  43.          (source (MAKE-ARRAY arraySize
  44.                              :ELEMENT-TYPE arrayType
  45.                              :DISPLACED-TO array))
  46.          (dest (MAKE-ARRAY arraySize
  47.                            :ELEMENT-TYPE arrayType
  48.                            :DISPLACED-TO newArray))
  49.          )
  50.     (IF copierFn
  51.       (DOTIMES (i arraySize)
  52.         (SETF (ELT dest i) (FUNCALL copierFn (ELT source i))))
  53.  
  54.       ;; If no copierFn was supplied, then use REPLACE
  55.       ;;  because the compiler might generate much more
  56.       ;;  efficient code for it.
  57.       (REPLACE dest source))
  58.     newArray))
  59.  
  60.  
  61. Sorry I didn't use dashes in the variable names, but I
  62. wrote this a long time ago.
  63.  
  64. Danny Brewer
  65. danny@farallon.com
  66.