title: iterating through TWO sparse matrices
body: <p>I'm using boost sparse matrices holding bool's and trying to write a comparison function for storing them in a map. It is a very simple comparison function. Basically, the idea is to look at the matrix as a binary number (after being flattened into a vector) and sorting based on the value of that number. This can be accomplished in this way:</p>
  
  <pre><code>for(unsigned int j = 0; j &lt; maxJ; j++)
  {
    for(unsigned int i = 0; i &lt; maxI; i++)
    {
      if(matrix1(i,j) &lt; matrix2(i,j) return true;
      else if(matrix1(i,j) &gt; matrix2(i,j) return false;
    }
  }
  return false;
  </code></pre>
  
  <p>However, this is inefficient because of the sparseness of the matrices and I'd like to use iterators for the same result. The algorithm using iterators seems straightforward, i.e.
  1) grab the first nonzero cell in each matrix, 2) compare j*maxJ+i for both, 3) if equal, grab the next nonzero cells in each matrix and repeat. Unfortunately, in code this is extremely tedious and I'm worried about errors.</p>
  
  <p>What I'm wondering is (a) is there a better way to do this and (b) is there a simple way to get the "next nonzero cell" for both matrices? Obviously, I can't use nested for loops like one would to iterate through one sparse matrix. </p>
  
  <p>Thanks for your help.</p>
  
  <p>-- </p>
  
  <p>Since it seems that the algorithm I proposed above may be the best solution in my particular application, I figured I should post the code I developed for the tricky part, getting the next nonzero cells in the two sparse matrices. This code is not ideal and not very clear, but I'm not sure how to improve it. If anyone spots a bug or knows how to improve it, I would appreciate some comments. Otherwise, I hope this is useful to someone else.</p>
  
  <pre><code>typedef boost::numeric::ublas::mapped_matrix&lt;bool&gt;::const_iterator1 iter1;
  typedef boost::numeric::ublas::mapped_matrix&lt;bool&gt;::const_iterator2 iter2;
  
  // Grabs the next nonzero cell in a sparse matrix after the cell pointed to by i1, i2.
  std::pair&lt;iter1, iter2&gt; next_cell(iter1 i1, iter2 i2, iter1 end) const
  {
      if(i2 == i1.end())
      {
          if (i1 == end)
              return std::pair&lt;iter1, iter2&gt;(i1, i2);
          ++i1;
          i2 = i1.begin();
      }
      else
      {
          ++i2;
      }
  
      for(; i1 != end;)
      {
          for(; i2 != i1.end(); ++i2)
          {
              return std::pair&lt;iter1, iter2&gt;(i1,i2);
          }
          ++i1;
          if(i1 != end) i2 = i1.begin();
      }
      return std::pair&lt;iter1, iter2&gt;(i1, i2);
  }
  </code></pre>