Svante Carl v. Erichsen | 13 Sep 14:41 2011
Picon

Scratching an itch: copy-array


Hello!

I should like to propose a drop-in replacement for copy-array:

(defun copy-array (array &key
                   (element-type (array-element-type array))
                   (fill-pointer (and (array-has-fill-pointer-p array)
                                      (fill-pointer array)))
                   (adjustable (adjustable-array-p array)))
  "Returns an undisplaced copy of ARRAY, with same fill-pointer and
adjustability (if any) as the original, unless overridden by the keyword
arguments."
  (let* ((dimensions (array-dimensions array))
         (new-array (make-array dimensions
                                :element-type element-type
                                :adjustable adjustable
                                :fill-pointer fill-pointer)))
    (dotimes (i (array-total-size array))
      (setf (row-major-aref new-array i)
            (row-major-aref array i)))
    new-array))

My reasons can be seen from the following tests:

(deftest copy-array.3
    (let* ((orig (vector 1 2 3))
           (copy (copy-array orig)))
      (typep copy 'simple-array))
  t)

(deftest copy-array.4
    (let ((orig (make-array 21
                            :adjustable t
                            :fill-pointer 0)))
      (dotimes (n 42)
        (vector-push-extend n orig))
      (let ((copy (copy-array orig
                              :adjustable nil
                              :fill-pointer nil)))
        (typep copy 'simple-array)))
  t)

I believe that these tests should pass for compliance with the
specification of the type simple-array:

"The type of an array that is not displaced to another array, has no
fill pointer, and is not expressly adjustable is a subtype of type
simple-array."

In other words: I find it quite astonishing if the copy of a
simple-array is not a simple-array itself.

I am grateful for comments and, should you find this acceptable,
directions how to proceed.

Best wishes

Svante

P.S.: You can also reach me as 'serichsen' on #lisp at freenode IRC.

--
Svante Carl v. Erichsen
Wentorfer Str. 96
21029 Hamburg

+49-(0)40-34923721
+49-(0)160-6941474
Svante.v.Erichsen <at> web.de

Gmane