Structure to run x first vs y first on a 2d array

I want the flag to be passed to a function that runs the algorithm using a col-scan or row-scan scan:

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 do not want to duplicate all the loops and logic.

I came 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 that all the math is pretty hard, especially when I run it on rather large arrays.

Is there a better way to do this?

+6
source share
6 answers

Perhaps just passing them as such parameters ?:

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

Not seeing what β€œsomething” is doing, I don’t know if there are unforeseen reasons why this may not work, but considering what you posted, this should do the trick.

I'm not quite 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]; 
+2
source
 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++) { // ... } } } 
+1
source

Save two sets of inner and outer loops, but change the body of the inner loop to one function call. Then there is not much code duplication.

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

Create helper functions for the main and main iterations of the strings using an array and a function to apply to the elements of the array.

 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); 
0
source

In your decision

 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]; 

The comparison number is numPx times, and before it was only once, disregard the heavy math.

I think the simplest 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]; } } 
0
source

All Articles