数组的 PostScript 复制运算符
PostScript copy operator for arrays
我正在寻找有关 PostScript 中 复制运算符 与数组(或 dictionaries/strings/...)一起使用时的一些额外详细信息。
在 PostScript 语言参考手册中我发现:
array1 array2 copy subarray2
我在一个网站上找到了这个例子:http://www.linuxfocus.org/English/July1999/article100.html
GS>[1 2 3] [4 5 6 7 8] copy pstack
[1 2 3]
GS<1>/ar [4 5 6 7 8] def
GS<1>[1 2 3] ar copy
GS<2>ar pstack
[1 2 3 7 8]
[1 2 3]
[1 2 3]
GS<3>
但是这个例子让我感到困惑,因为 [1 2 3] 和 [1 2 3 7 8]。
现在复制运算符究竟是如何处理数组的?
如果我有这个堆栈:
---------------top-
copy
[4 5 6 7 8]
[1 2 3]
------------bottom-
复制操作后什么留在堆栈上?
只有子数组?:
---------------top-
[1 2 3]
------------bottom-
或子数组+数组2的一部分?:
---------------top-
[1 2 3 7 8]
------------bottom-
提前致谢。
来自 PostScript 语言参考手册,第 3 版,第 548 页,复制运算符:
'...In the other forms, copy copies all the elements of the first
composite object into the second. The composite object operands must
be of the same type, except that a packed array can be copied into an
array (and only into an array—copy cannot copy into packed arrays,
because they are read-only). This form of copy copies the value of a
composite object. This is quite different from dup and other operators
that copy only the objects themselves (see Section 3.3.1, “Simple and
Composite Objects”). However, copy performs only one level of copying.
It does not apply recursively to elements that are themselves
composite objects; instead, the values of those elements become
shared.
In the case of arrays or strings, the length of the second object must
be at least as great as the first; copy returns the initial subarray
or substring of the second operand into which the elements were
copied. Any remaining elements of array2 or string2 are unaffected.'
在您的情况下,您(我认为)被引用复合对象这一事实所困扰。您需要根据指针或引用来考虑堆栈上的复合对象,而不是物理内存块。
所以在第一个实例中,您创建了 2 个数组并将指向它们的指针放在堆栈上,这些都没有任何其他引用指向它们。然后执行复制。
复制运算符将第一个操作数指向的数组中的前3个元素复制到第二个操作数指向的数组中。然后它创建一个新的子数组,其中只包含修改过的元素。
然后它从堆栈中删除 2 个数组,如果您将它们视为指针,它只是从堆栈中删除指针。由于这些数组未在其他任何地方引用,因此内存将被释放。
最后它把指向新数组的指针放在堆栈上。 pstack 解析指向数组的指针并打印结果元素。
现在,在第二种情况下,您创建一个数组并从名称 /ar 引用它(它将存储在当前字典中)。然后创建第二个数组并将对它的引用(指针)放在操作数堆栈上,然后将对 'ar' 的引用放在堆栈上。
接下来调用copy。 Copy 将初始数组的前 3 个元素复制到 'ar' 引用的数组。它还创建了一个新的子数组。然后它从操作数堆栈中删除对操作数的引用。由于初始数组现在未被引用,因此它被释放。但是,当前字典包含一个键“/ar”,其值是对第二个数组的引用。所以我们没有释放与之关联的内存,它仍然被引用。最后它将新的子数组放入堆栈
然后您将另一个对由“/ar”键控的数组的引用放在堆栈上,然后再次调用 pstack。 pstack 解析数组引用并打印内容。
如您所见,复制更改了“/ar”引用的数组的前 3 个元素,还返回了一个包含复制元素的子数组。
NB 在原始示例中,第一个 'copy' 在堆栈上留下对包含 [1 2 3] 的数组的引用(由第一个 pstack 打印)。这就是为什么第二个 pstack(在第二次执行复制之后)打印两次显然是同一个数组,但事实并非如此,它打印引用到两个不同的子数组,每个子数组都由 'copy' 的执行之一返回.所以不是 :
我们可以简单地完成:
GS>[1 2 3] [4 5 6 7 8] copy pstack
[1 2 3]
GS<1>/ar [4 5 6 7 8] def
GS<1> ar copy
GS>ar pstack
[1 2 3 7 8]
[1 2 3]
GS<2>
也就是说,我们将第一个副本产生的 [1 2 3] 子数组用作第二个副本的初始操作数。
请注意 'GS' 提示中有数字表示操作数堆栈中的条目数。
所以,为了回答你原来的问题,没有任何东西留在堆栈上,两个操作数都被删除,并且创建了一个新的子数组,并将对它的引用放在堆栈上.
我正在寻找有关 PostScript 中 复制运算符 与数组(或 dictionaries/strings/...)一起使用时的一些额外详细信息。
在 PostScript 语言参考手册中我发现:
array1 array2 copy subarray2
我在一个网站上找到了这个例子:http://www.linuxfocus.org/English/July1999/article100.html
GS>[1 2 3] [4 5 6 7 8] copy pstack
[1 2 3]
GS<1>/ar [4 5 6 7 8] def
GS<1>[1 2 3] ar copy
GS<2>ar pstack
[1 2 3 7 8]
[1 2 3]
[1 2 3]
GS<3>
但是这个例子让我感到困惑,因为 [1 2 3] 和 [1 2 3 7 8]。
现在复制运算符究竟是如何处理数组的? 如果我有这个堆栈:
---------------top-
copy
[4 5 6 7 8]
[1 2 3]
------------bottom-
复制操作后什么留在堆栈上?
只有子数组?:
---------------top-
[1 2 3]
------------bottom-
或子数组+数组2的一部分?:
---------------top-
[1 2 3 7 8]
------------bottom-
提前致谢。
来自 PostScript 语言参考手册,第 3 版,第 548 页,复制运算符:
'...In the other forms, copy copies all the elements of the first composite object into the second. The composite object operands must be of the same type, except that a packed array can be copied into an array (and only into an array—copy cannot copy into packed arrays, because they are read-only). This form of copy copies the value of a composite object. This is quite different from dup and other operators that copy only the objects themselves (see Section 3.3.1, “Simple and Composite Objects”). However, copy performs only one level of copying. It does not apply recursively to elements that are themselves composite objects; instead, the values of those elements become shared.
In the case of arrays or strings, the length of the second object must be at least as great as the first; copy returns the initial subarray or substring of the second operand into which the elements were copied. Any remaining elements of array2 or string2 are unaffected.'
在您的情况下,您(我认为)被引用复合对象这一事实所困扰。您需要根据指针或引用来考虑堆栈上的复合对象,而不是物理内存块。
所以在第一个实例中,您创建了 2 个数组并将指向它们的指针放在堆栈上,这些都没有任何其他引用指向它们。然后执行复制。
复制运算符将第一个操作数指向的数组中的前3个元素复制到第二个操作数指向的数组中。然后它创建一个新的子数组,其中只包含修改过的元素。
然后它从堆栈中删除 2 个数组,如果您将它们视为指针,它只是从堆栈中删除指针。由于这些数组未在其他任何地方引用,因此内存将被释放。
最后它把指向新数组的指针放在堆栈上。 pstack 解析指向数组的指针并打印结果元素。
现在,在第二种情况下,您创建一个数组并从名称 /ar 引用它(它将存储在当前字典中)。然后创建第二个数组并将对它的引用(指针)放在操作数堆栈上,然后将对 'ar' 的引用放在堆栈上。
接下来调用copy。 Copy 将初始数组的前 3 个元素复制到 'ar' 引用的数组。它还创建了一个新的子数组。然后它从操作数堆栈中删除对操作数的引用。由于初始数组现在未被引用,因此它被释放。但是,当前字典包含一个键“/ar”,其值是对第二个数组的引用。所以我们没有释放与之关联的内存,它仍然被引用。最后它将新的子数组放入堆栈
然后您将另一个对由“/ar”键控的数组的引用放在堆栈上,然后再次调用 pstack。 pstack 解析数组引用并打印内容。
如您所见,复制更改了“/ar”引用的数组的前 3 个元素,还返回了一个包含复制元素的子数组。
NB 在原始示例中,第一个 'copy' 在堆栈上留下对包含 [1 2 3] 的数组的引用(由第一个 pstack 打印)。这就是为什么第二个 pstack(在第二次执行复制之后)打印两次显然是同一个数组,但事实并非如此,它打印引用到两个不同的子数组,每个子数组都由 'copy' 的执行之一返回.所以不是 :
我们可以简单地完成:
GS>[1 2 3] [4 5 6 7 8] copy pstack
[1 2 3]
GS<1>/ar [4 5 6 7 8] def
GS<1> ar copy
GS>ar pstack
[1 2 3 7 8]
[1 2 3]
GS<2>
也就是说,我们将第一个副本产生的 [1 2 3] 子数组用作第二个副本的初始操作数。
请注意 'GS' 提示中有数字表示操作数堆栈中的条目数。
所以,为了回答你原来的问题,没有任何东西留在堆栈上,两个操作数都被删除,并且创建了一个新的子数组,并将对它的引用放在堆栈上.