gpt4 book ai didi

vector - 如何保护矢量不被调整大小?

转载 作者:行者123 更新时间:2023-12-02 07:14:46 25 4
gpt4 key购买 nike

所以我再次学习 Practical Common Lisp,但我无法真正理解如何创建具有恒定大小的向量。

根据书中所述:

However, even a vector with a fill pointer isn't completely resizable. The vector *x* can hold at most five elements. To make an arbitrarily resizable vector, you need to pass MAKE-ARRAY another keyword argument: :adjustable.

但是,当我使用 (vector-push-extend) 时,即使我设置 :adjustable nil (或保留默认值),我也可以扩展初始向量。

(vector-push 'a *x*) ;; 0
(vector-push 'b *x*) ;; 1
(vector-push 'c *x*) ;; 2
(vector-push 'd *x*) ;; 3
(vector-push 'e *x*) ;; 4
*x* ;; #(A B C D E)
(vector-push 'f *x*) ;; NIL
*x* ;; #(A B C D E)
(vector-push-extend 'f *x*) ;; 5
*x* ;; #(A B C D E F)

我假设 (vector-push-extend) 无法调整不是 :adjustable 的数组大小?创建非动态(不可调整)数组的正确方法是什么?

最佳答案

该行为是特定于实现的。

Common Lisp 规范说:

There is no specified way to create an array for which adjustable-array-p definitely returns false.

即使 make-array:adjustable 参数指定为 nil,实现也可以使向量可调。

要查看数组对象是否实际上可调整,需要调用adjustable-array-p

Common Lisp 标准规定向量是可明确调整的(因此,如果要求的话,也实际上是可调整的。如果没有要求,则矢量仍然可以可调实际可调

所以 :adjustable nil arg 只是告诉 Lisp,如果可能的话,它可以使向量不可调整。

在 SBCL 中:

1) 法向量不可调整:

* (make-array 5)
#(0 0 0 0 0)
* (adjustable-array-p *)
NIL

2) 带有填充指针的向量在 SBCL 中实际上是可以调整的:

* (make-array 5 :fill-pointer 0)
#()
* (adjustable-array-p *)
T

3) 带有填充指针的向量在 SBCL 中实际上是可调整的,即使 :adjustable 参数被指定为 nil:

* (make-array 5 :fill-pointer 0 :adjustable nil)
#()
* (adjustable-array-p *)
T

这是在 SBCL 中。在 LispWorks 中,2) 和 3) 实际上是不可调整的。

关于vector - 如何保护矢量不被调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57603653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com