From 73e22653ca61072db87110c0c4e347c07047a275 Mon Sep 17 00:00:00 2001 From: conrad Date: Sat, 14 Jan 2023 02:35:34 +1000 Subject: [PATCH] simplifications --- include/armadillo_bits/fn_shift.hpp | 30 ++++++++-- include/armadillo_bits/op_shift_bones.hpp | 6 -- include/armadillo_bits/op_shift_meat.hpp | 70 +++++------------------ 3 files changed, 37 insertions(+), 69 deletions(-) diff --git a/include/armadillo_bits/fn_shift.hpp b/include/armadillo_bits/fn_shift.hpp index 05299b76..e246562a 100644 --- a/include/armadillo_bits/fn_shift.hpp +++ b/include/armadillo_bits/fn_shift.hpp @@ -48,12 +48,12 @@ shift template arma_warn_unused -arma_inline +inline typename enable_if2 < is_arma_type::value && resolves_to_vector::no, - const Op + Mat >::result shift ( @@ -63,22 +63,30 @@ shift { arma_extra_debug_sigprint(); + typedef typename T1::elem_type eT; + const uword len = (N < 0) ? uword(-N) : uword(N); const uword neg = (N < 0) ? uword( 1) : uword(0); - return Op(X, len, neg, uword(0), 'j'); + quasi_unwrap U(X); + + Mat out; + + op_shift::apply_noalias(out, U.M, len, neg, 0); + + return out; } template arma_warn_unused -arma_inline +inline typename enable_if2 < (is_arma_type::value), - const Op + Mat >::result shift ( @@ -89,10 +97,20 @@ shift { arma_extra_debug_sigprint(); + typedef typename T1::elem_type eT; + + arma_debug_check( (dim > 1), "vectorise(): parameter 'dim' must be 0 or 1" ); + const uword len = (N < 0) ? uword(-N) : uword(N); const uword neg = (N < 0) ? uword( 1) : uword(0); - return Op(X, len, neg, dim, 'j'); + quasi_unwrap U(X); + + Mat out; + + op_shift::apply_noalias(out, U.M, len, neg, dim); + + return out; } diff --git a/include/armadillo_bits/op_shift_bones.hpp b/include/armadillo_bits/op_shift_bones.hpp index 41d3ec2e..74e49025 100644 --- a/include/armadillo_bits/op_shift_bones.hpp +++ b/include/armadillo_bits/op_shift_bones.hpp @@ -37,13 +37,7 @@ class op_shift { public: - template inline static void apply(Mat& out, const Op& in); - - template inline static void apply_direct(Mat& out, const Mat& X, const uword len, const uword neg, const uword dim); - template inline static void apply_noalias(Mat& out, const Mat& X, const uword len, const uword neg, const uword dim); - - template inline static void apply_alias(Mat& out, const uword len, const uword neg, const uword dim); }; diff --git a/include/armadillo_bits/op_shift_meat.hpp b/include/armadillo_bits/op_shift_meat.hpp index 370fd5e9..b369b5d3 100644 --- a/include/armadillo_bits/op_shift_meat.hpp +++ b/include/armadillo_bits/op_shift_meat.hpp @@ -29,55 +29,26 @@ op_shift_vec::apply(Mat& out, const Op& { arma_extra_debug_sigprint(); - const unwrap U(in.m); + typedef typename T1::elem_type eT; + + const quasi_unwrap U(in.m); const uword len = in.aux_uword_a; const uword neg = in.aux_uword_b; const uword dim = (T1::is_xvec) ? uword(U.M.is_rowvec() ? 1 : 0) : uword((T1::is_row) ? 1 : 0); - op_shift::apply_direct(out, U.M, len, neg, dim); - } - - - -template -inline -void -op_shift::apply(Mat& out, const Op& in) - { - arma_extra_debug_sigprint(); - - const unwrap U(in.m); - - const uword len = in.aux_uword_a; - const uword neg = in.aux_uword_b; - const uword dim = in.aux_uword_c; - - arma_debug_check( (dim > 1), "shift(): parameter 'dim' must be 0 or 1" ); - - op_shift::apply_direct(out, U.M, len, neg, dim); - } - - - -template -inline -void -op_shift::apply_direct(Mat& out, const Mat& X, const uword len, const uword neg, const uword dim) - { - arma_extra_debug_sigprint(); - - arma_debug_check_bounds( ((dim == 0) && (len >= X.n_rows)), "shift(): shift amount out of bounds" ); - arma_debug_check_bounds( ((dim == 1) && (len >= X.n_cols)), "shift(): shift amount out of bounds" ); - - if(&out == &X) + if(U.is_alias(out)) { - op_shift::apply_alias(out, len, neg, dim); + Mat tmp; + + op_shift::apply_noalias(tmp, U.M, len, neg, dim); + + out.steal_mem(tmp); } else { - op_shift::apply_noalias(out, X, len, neg, dim); + op_shift::apply_noalias(out, U.M, len, neg, dim); } } @@ -90,6 +61,9 @@ op_shift::apply_noalias(Mat& out, const Mat& X, const uword len, const u { arma_extra_debug_sigprint(); + arma_debug_check_bounds( ((dim == 0) && (len >= X.n_rows)), "shift(): shift amount out of bounds" ); + arma_debug_check_bounds( ((dim == 1) && (len >= X.n_cols)), "shift(): shift amount out of bounds" ); + out.copy_size(X); const uword X_n_rows = X.n_rows; @@ -204,22 +178,4 @@ op_shift::apply_noalias(Mat& out, const Mat& X, const uword len, const u -template -inline -void -op_shift::apply_alias(Mat& X, const uword len, const uword neg, const uword dim) - { - arma_extra_debug_sigprint(); - - // TODO: replace with better implementation - - Mat tmp; - - op_shift::apply_noalias(tmp, X, len, neg, dim); - - X.steal_mem(tmp); - } - - - //! @}