【发布时间】:2019-07-17 15:34:12
【问题描述】:
我有两个带有填充指针的向量。我需要merge 这些向量,因此有一个新的向量,它仍然有一个填充指针。
(defparameter *a* (make-array 3 :fill-pointer 3
:initial-contents '(1 3 5)))
(defparameter *b* (make-array 3 :fill-pointer 3
:initial-contents '(0 2 4)))
(type-of *a*)
;;=> (VECTOR T 6)
;; Pushing new elements works as intended.
(vector-push-extend 7 *a*)
(vector-push-extend 6 *b*)
;; Now we create a new vector by merging *a* and *b*.
(defparameter *c* (merge 'vector *a* *b* #'<))
;;=> #(0 1 2 3 4 5 6 7)
(type-of *c*)
;;=> (SIMPLE-VECTOR 8)
;; The type of this new vector does not allow pushing elements.
(vector-push-extend 8 *c*)
;; The value
;; #(0 1 2 3 4 5 6 7)
;; is not of type
;; (AND VECTOR (NOT SIMPLE-ARRAY))
;; [Condition of type TYPE-ERROR]
我似乎找不到要指定给merge 的类型,因此结果将具有填充指针。我想明显的解决方法是:
- 自己编写一个
merge函数,声明一个新向量并以正确的顺序执行插入。 - 使用填充指针将结果复制到另一个向量中。
当然,如果有办法使用标准中的merge 来做到这一点,那么这两种解决方法都非常不令人满意。
【问题讨论】:
-
(defparameter *d* (make-array (length *c*) :fill-pointer (length *c*) :initial-contents *c*))有什么问题?(fill-pointer *d*) => 8 -
我没想到。它是否涉及对数组中所有单元格的额外复制操作?我正在测试这个。
-
@ThomasHoullier:是的,这会创建一个新向量并在那里复制数据。
-
好吧,我写了一个快速测试,确实声明这个新向量的操作的执行时间与数组的大小成线性关系。
标签: vector merge common-lisp fill-pointer