简体   繁体   English

在2d数组上首先运行x与y首先运行的结构

[英]Structure to run x first vs y first on 2d array

I want to have a flag passed to a function that runs an algorithm by either col-scanning or row-scanning: 我希望将一个标志传递给一个通过col-scanning或row-scanning运行算法的函数:

if run-on-x
  for 1..x
    for 1..y
      do something with ary[x][y]

else
  for 1..y
    for 1..x
      do something with ary[x][y]

But I don't want to duplicate all the loops and logic. 但我不想复制所有的循环和逻辑。

I've come up with this: 我想出来了:

let numPx = width * height;
for (let px = 0; px < numPx; px++) {
  let [x, y] = yAxis ? [px % width, 0 | px / width] : [0 | px / height, px % height];

But I think all the math is rather heavy, especially when I'm running it on fairly large arrays. 但我认为所有的数学都很重,特别是当我在相当大的数组上运行时。

Is there a better way to do this? 有一个更好的方法吗?

Perhaps by simply passing them in as parameters like so?: 也许只是简单地将它们作为参数传递出去?:

  function colRowScan(1stAxis,2ndAxis)
      for 1.. 1stAxis
        for 1.. 2ndAxis
          do something with ary[x][y]

Without seeing what the "do something" is I don't know if there is any unforeseen reasons why this couldn't work but given what you posted it should do the trick. 没有看到“做某事”是什么我不知道是否有任何不可预见的原因,为什么这不起作用,但鉴于你发布它应该做的伎俩。

I am not entirely sure what you are trying to do here: 我不完全确定你在这里要做什么:

let numPx = width * height;
for (let px = 0; px < numPx; px++) {
  let [x, y] = yAxis ? [px % width, 0 | px / width] : [0 | px / height, px % height];
function f(x, y, on_x) {
    var a, b;

    if (on_x) {
        a = x;
        b = y;
    } 
    else {
        a = y;
        b = x;
    }

    for (var ia = 0; ia < a.length; ia++) {
        for (var ib = 0; ib = b.length; ib++) {
            // ...
        }
    }
}

Keep the two sets of inner and outer loops, but change the body of the inner loop to a single function call. 保留两组内部和外部循环,但将内部循环的主体更改为单个函数调用。 Then there's not much code duplication. 然后没有太多的代码重复。

  for 1..x
    for 1..y {
      var a = run-on-x ? ary[x][y] : ary[y][x];
      do something with a
    }

Create helper functions for row major and column major iteration, taking the array and a function to apply to the array members. 为行主要和列主要迭代创建辅助函数,将数组和函数应用于数组成员。

var rowMajor = function (a, op) {
    var maxi = a.length;
    var maxj = a[0].length;
    for(var i = 0; i < maxi; ++i) {
        var row = a[i];
        for(var j = 0; j < maxj; ++j) 
            op(row[j],i,j);
            }
};
var colMajor = function (a, op) {
    var maxi = a.length;
    if(maxi === 0) return;
    var maxj = a[0].length;
    for(var j = 0; j < maxj; ++j) {
        for(var i = 0; i < maxi; ++i) {
            op(a[i][j],i,j);
        }
    }
};

// example use (with jQuery)
var array = [[11,12,13],[21,22,23]];
var div = $('<div></div>');
var append =  function(value) { 
    div.append($('<span></span>').text(value + ' '));
};

rowMajor(array,append);
div.append('<br/>');
colMajor(array, append);
$('body').append(div);

In your solution, 在你的解决方案中

let numPx = width * height;
for (let px = 0; px < numPx; px++) {
  let [x, y] = yAxis ? [px % width, 0 | px / width] : [0 | px / height, px % height];

Number of comparison is numPx times while earlier it was only once, leave out the heavy math involved. 比较的数量是numPx次,而早期只有一次,省去了繁重的数学。

I think the simple and best solution is to use a separate function. 我认为简单而最好的解决方案是使用单独的功能。

OR you can try this 或者你可以试试这个

var a, b, fAry;

if (run-on-x) {
    a = x;
    b = y;
    fAry = ary;
} else {
    a = y;
    b = x;
    fAry = transpose of(ary);  
}

for (var i = 0; i < a; i++) {
    for (var j = 0; j < b; j++) {
        do something with fAry[i][j];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM