home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!asuvax!farallon!danny.farallon.com!user
- From: danny@farallon.com (Danny Brewer)
- Newsgroups: comp.lang.lisp
- Subject: Re: copy-array?
- Message-ID: <danny-150992090432@danny.farallon.com>
- Date: 15 Sep 92 14:00:36 GMT
- References: <9209142212.AA24213@brownie.cs.wisc.edu>
- Sender: news@farallon.farallon.com
- Followup-To: comp.lang.lisp
- Organization: Farallon Computing, Inc.
- Lines: 54
- Nntp-Posting-Host: danny
-
- In article <9209142212.AA24213@brownie.cs.wisc.edu>, so@CS.WISC.EDU (Bryan
- S. So) wrote:
- >
- > 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?
- >
-
- That's what I concluded myself. I wrote the following
- function which makes a new array and copies the elements
- of the old array into it. I use two temporary vectors
- displaced to the arrays so that I treat the copy like
- a sequence.
-
- About a year ago this question of copying arrays came
- up and I posted this code. Someone suggested that I
- use REPLACE instead of DOTIMES because it might be
- more efficient. I tried this which resulted in more
- than doubling the speed. If you pass in a copier
- function, then the DOTIMES method is used so that
- the copier function can process each element.
-
-
- (DEFUN Copy-Array (array &KEY copierFn)
- "Make a new array which is a copy of the original."
- (LET* (
- (arrayType (ARRAY-ELEMENT-TYPE array))
- (arraySize (ARRAY-TOTAL-SIZE array))
- (newArray (MAKE-ARRAY (ARRAY-DIMENSIONS array)
- :ELEMENT-TYPE arrayType
- :ADJUSTABLE (ADJUSTABLE-ARRAY-P array)))
- (source (MAKE-ARRAY arraySize
- :ELEMENT-TYPE arrayType
- :DISPLACED-TO array))
- (dest (MAKE-ARRAY arraySize
- :ELEMENT-TYPE arrayType
- :DISPLACED-TO newArray))
- )
- (IF copierFn
- (DOTIMES (i arraySize)
- (SETF (ELT dest i) (FUNCALL copierFn (ELT source i))))
-
- ;; If no copierFn was supplied, then use REPLACE
- ;; because the compiler might generate much more
- ;; efficient code for it.
- (REPLACE dest source))
- newArray))
-
-
- Sorry I didn't use dashes in the variable names, but I
- wrote this a long time ago.
-
- Danny Brewer
- danny@farallon.com
-