vector - 可扩展向量类型

标签 vector common-lisp adjustable-array

如何正确指定可扩展向量的 common-lisp 类型(即,vector-push-extend 可接受),以便可以复制它。例如,如果一个向量定义为:

(defparameter v (make-array 2
                            :initial-contents '((a (b)) (c (d) e))
                            :adjustable t
                            :fill-pointer t))

我复制它的简单(不正确)方法是:

(map 'array #'copy-tree v)

但这会在 sbcl 中产生类型错误。一个适当的序列类型规范可以做到这一点吗?

最佳答案

简单复制

你可以这样做:

(map (type-of v) #'copy-tree v)

什么类型?

CL-USER> (type-of v)
(VECTOR T 2)

以下内容就足够了:

(map 'vector #'copy-tree v)

This graph有助于记住类型层次结构,尤其是数组和向量。

可调,填充指针,...

但是,生成的向量是不可调整的。也许这样的事情会有所帮助:

(defun my-copy (vector)
  (map-into (make-array (array-dimensions vector)
                        :adjustable (adjustable-array-p vector)
                        :fill-pointer (fill-pointer vector))
            #'copy-tree
            vector))

关于vector - 可扩展向量类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41771165/

相关文章:

c++ - 函数 std::make_shared<vector> 的参数太多

performance - 提高字符串操作的速度

string - 如何在 Common Lisp 中使用填充指针初始化字符串?

types - 如何正确指定可调向量的元素类型

c++ - 如何将 vector 转换为 typedef 类型的数组

C++,什么时候应该返回引用?

c++ - & 在这种情况下做什么?

multithreading - 常见的Lisp : Run function in the background

lisp - 我的 LISP 函数如何成为未绑定(bind)变量?