你如何复制Common Lisp中的数组? [英] How do you copy an array in common lisp?

查看:185
本文介绍了你如何复制Common Lisp中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使我的二维数组,这感觉就像是不错的,功能性的,非破坏性的处理方式阵列的副本。什么是这样做的lispy方式?

I'd like to make copies of my 2D array, which feels like the nice, functional, nondestructive way of handling arrays. What is the lispy way of doing this?

推荐答案

我用下面的,我相信这是比亚历山大更好的版本:

I use the following, which I believe is better than the alexandria version:

(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))

与亚历山大的版本问题是,调整阵列
黑客导致的结果(至少在SBCL)永远不会成为一个
简单阵列,其中一些其他库(例如opticl)的期望。该
以上版本还对我更快。

The problem with the alexandria version is that the adjust-array hack causes the result (at least on SBCL) to never be a simple-array, which some other libraries (e.g. opticl) expect. The above version also was faster for me.

别人在不同出版了一本非常类似的版本
图书馆,但我忘了的人都和库的名称。

Someone else has published a very similar version in a different library, but I forgot the names of both person and library.

这篇关于你如何复制Common Lisp中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆