Fix three out of bounds errors in CompressedRowSparseMatrix.

All three errors are of the form where, the vector storing
the columns array for a CompressedRowSparseMatrix is being
accessed out of bounds. But in each case, the access is innocuous
because there are other tests which prevent it from being used.

However, if one compiles with debug mode on, the bounds checking
in the underlying std::vector triggers an error.

More details:

1. FromTripletSparseMatrix tried accessing the col vector of
   a matrix with zero rows, but the following code was a no-op
   because there was nothing to copy.

   Adding an early return which deals with this case fixes this.

2. RightMultiply and SquaredColumnNorm had the following loop

   while (r > cols_[idx] && idx < idx_end) {
        ++idx;
   }

   where cols_ was being accessed out of bounds, before idx < idx_end
   was being checked. Swapping the order of the comparisons fixes this
   error.

@richmattes reported this error.

https://github.com/ceres-solver/ceres-solver/issues/383

Change-Id: I2dbc90ef24ecaffc6f46f1622339d6053b43db45
diff --git a/internal/ceres/compressed_row_sparse_matrix.cc b/internal/ceres/compressed_row_sparse_matrix.cc
index 8b57956..bdb0b7a 100644
--- a/internal/ceres/compressed_row_sparse_matrix.cc
+++ b/internal/ceres/compressed_row_sparse_matrix.cc
@@ -216,6 +216,11 @@
   CompressedRowSparseMatrix* output =
       new CompressedRowSparseMatrix(num_rows, num_cols, input.num_nonzeros());
 
+  if (num_rows == 0) {
+    // No data to copy.
+    return output;
+  }
+
   // Copy the contents of the cols and values array in the order given
   // by index and count the number of entries in each row.
   int* output_rows = output->mutable_rows();
@@ -291,7 +296,7 @@
 
       // For upper triangular matrices r <= c, so skip entries with r
       // > c.
-      while (r > cols_[idx] && idx < idx_end) {
+      while (idx < idx_end && r > cols_[idx]) {
         ++idx;
       }
 
@@ -364,7 +369,7 @@
 
       // For upper triangular matrices r <= c, so skip entries with r
       // > c.
-      while (r > cols_[idx] && idx < idx_end) {
+      while (idx < idx_end && r > cols_[idx]) {
         ++idx;
       }