@@ -49,7 +49,7 @@ Mat<eT>::Mat()
4949 , n_alloc(0 )
5050 , vec_state(0 )
5151 , mem_state(0 )
52- , mem()
52+ , mem(nullptr )
5353 {
5454 arma_debug_sigprint_this (this );
5555 }
@@ -312,15 +312,10 @@ Mat<eT>::init_cold()
312312 const char * error_message = " Mat::init(): requested size is too large; suggest to enable ARMA_64BIT_WORD" ;
313313 #endif
314314
315- arma_conform_check
316- (
317- (
318- ( (n_rows > ARMA_MAX_UHWORD) || (n_cols > ARMA_MAX_UHWORD) )
319- ? ( (double (n_rows) * double (n_cols)) > double (ARMA_MAX_UWORD) )
320- : false
321- ),
322- error_message
323- );
315+ if ( (n_rows > ARMA_MAX_UHWORD) || (n_cols > ARMA_MAX_UHWORD) )
316+ {
317+ arma_conform_check ( ( (double (n_rows) * double (n_cols)) > double (ARMA_MAX_UWORD) ), error_message );
318+ }
324319
325320 if (n_elem <= arma_config::mat_prealloc)
326321 {
@@ -383,17 +378,10 @@ Mat<eT>::init_warm(uword in_n_rows, uword in_n_cols)
383378 const char * error_message_4 = " Mat::init(): requested size is too large; suggest to enable ARMA_64BIT_WORD" ;
384379 #endif
385380
386- arma_conform_set_error
387- (
388- err_state,
389- err_msg,
390- (
391- ( (in_n_rows > ARMA_MAX_UHWORD) || (in_n_cols > ARMA_MAX_UHWORD) )
392- ? ( (double (in_n_rows) * double (in_n_cols)) > double (ARMA_MAX_UWORD) )
393- : false
394- ),
395- error_message_4
396- );
381+ if ( (in_n_rows > ARMA_MAX_UHWORD) || (in_n_cols > ARMA_MAX_UHWORD) )
382+ {
383+ arma_conform_set_error ( err_state, err_msg, ( (double (in_n_rows) * double (in_n_cols)) > double (ARMA_MAX_UWORD) ), error_message_4 );
384+ }
397385
398386 arma_conform_check (err_state, err_msg);
399387
@@ -7580,10 +7568,40 @@ Mat<eT>::resize(const uword new_n_elem)
75807568 {
75817569 arma_debug_sigprint ();
75827570
7583- const uword new_n_rows = (vec_state == 2 ) ? uword (1 ) : uword (new_n_elem);
7584- const uword new_n_cols = (vec_state == 2 ) ? uword (new_n_elem) : uword (1 );
7571+ const bool reuse_mem =
7572+ ( is_vec () && (mem_state == 0 ) )
7573+ &&
7574+ (
7575+ ( (new_n_elem <= arma_config::mat_prealloc) && (n_elem <= arma_config::mat_prealloc) && ( n_elem > 0 ) )
7576+ || ( (new_n_elem > arma_config::mat_prealloc) && (n_elem > arma_config::mat_prealloc) && (new_n_elem <= n_alloc) )
7577+ );
7578+
7579+ if (reuse_mem)
7580+ {
7581+ arma_debug_print (" Mat::resize(): reusing memory" );
7582+
7583+ if (new_n_elem > n_elem)
7584+ {
7585+ arma_debug_print (" Mat::resize(): zeroing memory" );
7586+
7587+ eT* t_mem = (*this ).memptr (); // the (n_elem > 0) check above ensures that (*this).memptr() is a valid pointer
7588+
7589+ for (uword ii = n_elem; ii < new_n_elem; ++ii) { t_mem[ii] = eT (0 ); }
7590+ }
7591+
7592+ access::rw (n_rows) = (vec_state == 2 ) ? uword (1 ) : uword (new_n_elem);
7593+ access::rw (n_cols) = (vec_state == 2 ) ? uword (new_n_elem) : uword (1 );
7594+ access::rw (n_elem) = new_n_elem;
7595+ }
7596+ else
7597+ {
7598+ const uword new_n_rows = (vec_state == 2 ) ? uword (1 ) : uword (new_n_elem);
7599+ const uword new_n_cols = (vec_state == 2 ) ? uword (new_n_elem) : uword (1 );
7600+
7601+ (*this ).resize (new_n_rows, new_n_cols);
7602+ }
75857603
7586- return (*this ). resize (new_n_rows, new_n_cols) ;
7604+ return (*this );
75877605 }
75887606
75897607
0 commit comments