Skip to content

Commit 763b89f

Browse files
committed
More robust implementation of sorting mode 1
1 parent a346aa1 commit 763b89f

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

include/graphblas/reference/matrix.hpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -412,16 +412,18 @@ namespace grb {
412412
);
413413
#pragma omp parallel for num_threads( nThreads )
414414
for( size_t i = 0; i < A.n; ++i ) {
415-
const auto index_it = std::find(
416-
ccs.row_index + ccs.col_start[ i ],
417-
ccs.row_index + ccs.col_start[ i + 1 ],
418-
i
419-
);
420-
if( !std::is_void< InputType >::value ) {
421-
const size_t index = std::distance( ccs.row_index, index_it );
422-
std::swap( ccs.values[ index ], ccs.values[ ccs.col_start[ i ] ] );
415+
const auto start = ccs.row_index + ccs.col_start[ i ];
416+
const auto end = ccs.row_index + ccs.col_start[ i + 1 ];
417+
const auto index_it = std::find( start, end, i );
418+
if( index_it == end ) {
419+
throw std::runtime_error( "No diagonal element present" );
420+
} else if( index_it != start ) {
421+
if( !std::is_void< InputType >::value ) {
422+
const size_t index = std::distance( ccs.row_index, index_it );
423+
std::swap( ccs.values[ index ], ccs.values[ ccs.col_start[ i ] ] );
424+
}
425+
std::swap( *index_it, ccs.row_index[ ccs.col_start[ i ] ] );
423426
}
424-
std::swap( *index_it, ccs.row_index[ ccs.col_start[ i ] ] );
425427
}
426428
}
427429
} else {

0 commit comments

Comments
 (0)