Chapel 域:`low/high` 和 `first/last` 方法之间的差异

Chapel domains : differences between `low/high` and `first/last` methods

教堂域有两套方法

domain.low, domain.high

domain.first, domain.last

这些 return 不同结果的各种情况是什么(即什么时候 domain.first != domain.lowdomain.last != domain.high

首先,请注意,这些查询不仅在域上受支持,而且在范围上也受支持(一种更简单的类型,表示许多域及其域查询所基于的整数序列)。出于这个原因,为了简单起见,我的回答最初将集中在范围上,然后 return 进入密集的矩形域(使用每个维度的范围定义)。

作为背景,范围上的 firstlast 旨在指定迭代该范围时将获得的索引。相反,lowhigh 指定定义范围的最小和最大索引。

  • 对于一个简单的范围,如1..10firstlow将是相同的,评估为1,而lasthigh 都将计算为 10

  • 在 Chapel 中以相反顺序遍历范围的方式是使用像 1..10 by -1 这样的负步幅。对于此范围,lowhigh 仍将分别是 110,但 first 将是 10last将是 1,因为范围表示整数 10、9、8、...、1。

  • Chapel 也支持非单位步长,它们也会产生差异。例如,对于范围 1..10 by 2lowhigh 仍将分别是 110,而 first 仍将是 1 但是 last 将是 9 因为这个范围只代表 1 到 10 之间的奇数值。

下面的程序演示了这些情况以及 1..10 by -2,我将把它留作 reader 的练习(您也可以 try it online (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);

密集矩形域是使用每个维度的范围定义的。 lowhighfirstlast 等域上的查询 return 值的元组,每个维度一个,对应于查询的结果在各自的范围内。例如,这是根据上述范围 (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);