From 32808202dcd39b494dcdf84eb6429c8190235416 Mon Sep 17 00:00:00 2001 From: Sameer Agarwal Date: Thu, 14 Jun 2012 13:47:43 -0700 Subject: [PATCH] Do not dereference an iterator when it point to the end. Thanks to Markus Moll for catching my broken fix from a previous change. Change-Id: I37d0185cfc6f86d0c31f580ca988577abc168ab5 --- internal/ceres/suitesparse.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/ceres/suitesparse.cc b/internal/ceres/suitesparse.cc index 96c1d6cee..cf3c48f84 100644 --- a/internal/ceres/suitesparse.cc +++ b/internal/ceres/suitesparse.cc @@ -224,9 +224,16 @@ void SuiteSparse::ScalarMatrixToBlockMatrix(const cholmod_sparse* A, vector::const_iterator it = lower_bound(row_block_starts.begin(), row_block_starts.end(), scalar_rows[idx]); - - // Only consider the first row of each row block. - if (*it != scalar_rows[idx]) { + // Since we are using lower_bound, it will return the row id + // where the row block starts. For everything but the first row + // of the block, where these values will be the same, we can + // skip, as we only need the first row to detect the presence of + // the block. + // + // For rows all but the first row in the last row block, + // lower_bound will return row_block_starts.end(), but those can + // be skipped like the rows in other row blocks too. + if (it == row_block_starts.end() || *it != scalar_rows[idx]) { continue; }