Re: copying arrays



I think this might be something you want. I forgot where does it come
from, so please add the reference.


(defun copy-array (array &key (undisplace nil))
"Shallow copies the contents of any array into another array with
equivalent properties. If array is displaced, then this function will
normally create another displaced array with similar properties,
unless UNDISPLACE is non-NIL, in which case the contents of the array
will be copied into a completely new, not displaced, array."
(declare (type array array))
(let ((copy (%make-array-with-same-properties array undisplace)))
(unless (array-displacement copy)
(dotimes (n (array-total-size copy))
(setf (row-major-aref copy n) (row-major-aref array n))))
copy))

(defun %make-array-with-same-properties (array undisplace)
"Make an array with the same properties (size, adjustability, etc.)
as another array, optionally undisplacing the array."
(apply #'make-array
(list* (array-dimensions array)
:element-type (array-element-type array)
:adjustable (adjustable-array-p array)
:fill-pointer (when (array-has-fill-pointer-p array)
(fill-pointer array))
(multiple-value-bind (displacement offset)
(array-displacement array)
(when (and displacement (not undisplace))
(list :displaced-to displacement
:displaced-index-offset offset))))))

.



Relevant Pages

  • Re: arrays + ffi
    ... (reduce function (make-array (array-total-size array) ... (if displacement ... (defun find-original-array (array) ...
    (comp.lang.lisp)
  • Re: Arrays
    ... Copying an array in CL raises similar issues ... > the copy from the original array (or arrays, since displacement ... they are not relevant in response to ...
    (comp.lang.lisp)
  • Re: arrays + ffi
    ... (reduce function (make-array (array-total-size array) ... (if displacement ...
    (comp.lang.lisp)
  • arrays + ffi
    ... When I first encountered Lisp a couple of months ago, ... (defun array-reduce (function array) ... But when it was created flat using displacement, ...
    (comp.lang.lisp)
  • Re: Two question about ADJUST-ARRAY and array displacement
    ... > array or create and return a new one. ... array will "inherit" the implicit displacement of ADJUST-ARRAY. ... ADJUST-ARRAY returns C to which A is displaced, ... > references to A disappear, the GC could merge them into a single ...
    (comp.lang.lisp)