Chapel域有两组方法
domain.low, domain.high
Run Code Online (Sandbox Code Playgroud)
和
domain.first, domain.last
Run Code Online (Sandbox Code Playgroud)
什么是这些返回不同结果的各种情况(即什么时候domain.first != domain.low
和domain.last != domain.high
?
首先,请注意,这些查询不仅支持域,还支持范围(表示许多域及其域查询所基于的整数序列的简单类型).出于这个原因,在返回密集矩形域(使用每个维度的范围定义)之前,我的答案最初将集中于简单范围.
作为背景,first
并且last
在范围上设计用于指定迭代该范围时将获得的索引.相反,low
并high
指定定义范围的最小和最大索引.
对于一个简单的范围,比如1..10
,first
并且low
将是相同的,评估1
,同时last
和high
将评估10
在Chapel中以相反顺序迭代范围的方式是使用负步幅1..10 by -1
.对于该范围,low
并且high
仍将是1
和将10
分别但first
将是10
和last
将来,1
因为该范围表示整数10,9,8,...,1.
Chapel还支持非单位步幅,它们也可能导致差异.例如,对于范围1..10 by 2
,low
并且high
仍将是1
和将10
分别,并且first
仍将是1
但last
将是9
因为该范围仅表示1和10之间的奇数值.
以下程序演示了这些案例以及1..10 by -2
我将作为练习留给读者的案例(您也可以在线试用(TIO)):
proc printBounds(r) {
writeln("For range ", r, ":");
writeln(" first = ", r.first);
writeln(" last = ", r.last);
writeln(" low = ", r.low);
writeln(" high = ", r.high);
writeln();
}
printBounds(1..10);
printBounds(1..10 by -1);
printBounds(1..10 by 2);
printBounds(1..10 by -2);
Run Code Online (Sandbox Code Playgroud)
使用每维度的范围来定义密集的矩形域.类的查询low
,high
,first
,和last
对这样的结构域返回值,每一个尺寸的元组中,对应于查询在各自的范围的结果.例如,这是根据上述范围(TIO)定义的4D域:
const D = {1..10, 1..10 by -1, 1..10 by 2, 1..10 by -2};
writeln("low = ", D.low);
writeln("high = ", D.high);
writeln("first = ", D.first);
writeln("last = ", D.last);
Run Code Online (Sandbox Code Playgroud)