@@ -2971,12 +2971,11 @@ SpMat<eT>::swap_cols(const uword in_col1, const uword in_col2)
29712971
29722972 // TODO: this is a rudimentary implementation
29732973
2974- SpMat<eT> tmp = (*this );
2974+ const SpMat<eT> tmp1 = (*this ).col (in_col1);
2975+ const SpMat<eT> tmp2 = (*this ).col (in_col2);
29752976
2976- tmp.col (in_col1) = (*this ).col (in_col2);
2977- tmp.col (in_col2) = (*this ).col (in_col1);
2978-
2979- steal_mem (tmp);
2977+ (*this ).col (in_col2) = tmp1;
2978+ (*this ).col (in_col1) = tmp2;
29802979
29812980 // for(uword lrow = 0; lrow < n_rows; ++lrow)
29822981 // {
@@ -4099,9 +4098,14 @@ SpMat<eT>::zeros()
40994098 {
41004099 arma_extra_debug_sigprint ();
41014100
4102- const bool already_done = ( (sync_state != 1 ) && (n_nonzero == 0 ) );
4103-
4104- if (already_done == false ) { init (n_rows, n_cols); }
4101+ if ((n_nonzero == 0 ) && (values != nullptr ))
4102+ {
4103+ invalidate_cache ();
4104+ }
4105+ else
4106+ {
4107+ init (n_rows, n_cols);
4108+ }
41054109
41064110 return *this ;
41074111 }
@@ -4136,9 +4140,14 @@ SpMat<eT>::zeros(const uword in_rows, const uword in_cols)
41364140 {
41374141 arma_extra_debug_sigprint ();
41384142
4139- const bool already_done = ( (sync_state != 1 ) && (n_nonzero == 0 ) && (n_rows == in_rows) && (n_cols == in_cols) );
4140-
4141- if (already_done == false ) { init (in_rows, in_cols); }
4143+ if ((n_nonzero == 0 ) && (n_rows == in_rows) && (n_cols == in_cols) && (values != nullptr ))
4144+ {
4145+ invalidate_cache ();
4146+ }
4147+ else
4148+ {
4149+ init (in_rows, in_cols);
4150+ }
41424151
41434152 return *this ;
41444153 }
@@ -5197,11 +5206,21 @@ SpMat<eT>::init_simple(const SpMat<eT>& x)
51975206
51985207 if (this == &x) { return ; }
51995208
5200- init (x.n_rows , x.n_cols , x.n_nonzero );
5209+ if ((x.n_nonzero == 0 ) && (n_nonzero == 0 ) && (n_rows == x.n_rows ) && (n_cols == x.n_cols ) && (values != nullptr ))
5210+ {
5211+ invalidate_cache ();
5212+ }
5213+ else
5214+ {
5215+ init (x.n_rows , x.n_cols , x.n_nonzero );
5216+ }
52015217
5202- if (x.values ) { arrayops::copy (access::rwp (values), x.values , x.n_nonzero + 1 ); }
5203- if (x.row_indices ) { arrayops::copy (access::rwp (row_indices), x.row_indices , x.n_nonzero + 1 ); }
5204- if (x.col_ptrs ) { arrayops::copy (access::rwp (col_ptrs), x.col_ptrs , x.n_cols + 1 ); }
5218+ if (x.n_nonzero != 0 )
5219+ {
5220+ if (x.values ) { arrayops::copy (access::rwp (values), x.values , x.n_nonzero + 1 ); }
5221+ if (x.row_indices ) { arrayops::copy (access::rwp (row_indices), x.row_indices , x.n_nonzero + 1 ); }
5222+ if (x.col_ptrs ) { arrayops::copy (access::rwp (col_ptrs), x.col_ptrs , x.n_cols + 1 ); }
5223+ }
52055224 }
52065225
52075226
0 commit comments