tags:

views:

89

answers:

2

Hi Guys,

a bit of a naive question, but nonetheless. I am trying to break up a for loop in which a 2d Matrix is being read. I would like to write a callback function to consume row-wise/col-wise chunks per row/col iteration. Any hints on how to tie up the callback function with iteration?

bool ReadMatrix(const int** array) {  

  .....  
    int columns;// array col  
    int rows; //array rows  

    for(int col=0; col<columns; ++col) {  
       for(int row=0; row<rows; ++row) {  
           ReadValue(row, col); //arbitrary function... which i would like to tie up with iterations  
      }  
    }  
    ....  

 }  
A: 

My first take would be for your callback to take the array, a row index, a row span, and column index and a column span.

class functor
{
public:
    void operator()(int data[][],
                    size_t row_idx,
                    size_t row_span,
                    size_t col_idx,
                    size_t col_span);
};

I'd write the iterator function as a template so it can easily take a functor, free function or class static function:

template <class CB_T>
void iterate(int data[][],
             size_t rows,
             size_t columns,
             CB_T cb);
R Samuel Klatchko
A: 
for(var y in m){
    rowCallback(m[y]);
    for(var x in m[y]){
        elemCallback(m[y][x]);
    }
}

???

nex