op_flip_meat.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup op_flip
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. op_flipud::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_flipud>& in)
  21. {
  22. arma_extra_debug_sigprint();
  23. const Proxy<T1> P(in.m);
  24. if(is_Mat<typename Proxy<T1>::stored_type>::value || P.is_alias(out))
  25. {
  26. const unwrap<typename Proxy<T1>::stored_type> U(P.Q);
  27. op_flipud::apply_direct(out, U.M);
  28. }
  29. else
  30. {
  31. op_flipud::apply_proxy_noalias(out, P);
  32. }
  33. }
  34. template<typename eT>
  35. inline
  36. void
  37. op_flipud::apply_direct(Mat<eT>& out, const Mat<eT>& X)
  38. {
  39. arma_extra_debug_sigprint();
  40. const uword X_n_rows = X.n_rows;
  41. const uword X_n_cols = X.n_cols;
  42. const uword X_n_rows_m1 = X_n_rows - 1;
  43. if(&out != &X)
  44. {
  45. out.set_size(X_n_rows, X_n_cols);
  46. if(X_n_cols == 1)
  47. {
  48. const eT* X_mem = X.memptr();
  49. eT* out_mem = out.memptr();
  50. for(uword row=0; row < X_n_rows; ++row)
  51. {
  52. out_mem[X_n_rows_m1 - row] = X_mem[row];
  53. }
  54. }
  55. else
  56. {
  57. for(uword col=0; col < X_n_cols; ++col)
  58. {
  59. const eT* X_colmem = X.colptr(col);
  60. eT* out_colmem = out.colptr(col);
  61. for(uword row=0; row < X_n_rows; ++row)
  62. {
  63. out_colmem[X_n_rows_m1 - row] = X_colmem[row];
  64. }
  65. }
  66. }
  67. }
  68. else // in-place operation
  69. {
  70. const uword N = X_n_rows / 2;
  71. if(X_n_cols == 1)
  72. {
  73. eT* out_mem = out.memptr();
  74. for(uword row=0; row < N; ++row)
  75. {
  76. std::swap(out_mem[X_n_rows_m1 - row], out_mem[row]);
  77. }
  78. }
  79. else
  80. {
  81. for(uword col=0; col < X_n_cols; ++col)
  82. {
  83. eT* out_colmem = out.colptr(col);
  84. for(uword row=0; row < N; ++row)
  85. {
  86. std::swap(out_colmem[X_n_rows_m1 - row], out_colmem[row]);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. template<typename T1>
  93. inline
  94. void
  95. op_flipud::apply_proxy_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P)
  96. {
  97. arma_extra_debug_sigprint();
  98. typedef typename T1::elem_type eT;
  99. const uword P_n_rows = P.get_n_rows();
  100. const uword P_n_cols = P.get_n_cols();
  101. const uword P_n_rows_m1 = P_n_rows - 1;
  102. out.set_size(P_n_rows, P_n_cols);
  103. if( ((T1::is_col) || (P_n_cols == 1)) && (Proxy<T1>::use_at == false) )
  104. {
  105. eT* out_mem = out.memptr();
  106. const typename Proxy<T1>::ea_type P_ea = P.get_ea();
  107. for(uword row=0; row < P_n_rows; ++row)
  108. {
  109. out_mem[P_n_rows_m1 - row] = P_ea[row];
  110. }
  111. }
  112. else
  113. {
  114. for(uword col=0; col < P_n_cols; ++col)
  115. {
  116. eT* out_colmem = out.colptr(col);
  117. for(uword row=0; row < P_n_rows; ++row)
  118. {
  119. out_colmem[P_n_rows_m1 - row] = P.at(row, col);
  120. }
  121. }
  122. }
  123. }
  124. //
  125. template<typename T1>
  126. inline
  127. void
  128. op_fliplr::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_fliplr>& in)
  129. {
  130. arma_extra_debug_sigprint();
  131. const Proxy<T1> P(in.m);
  132. if(is_Mat<typename Proxy<T1>::stored_type>::value || P.is_alias(out))
  133. {
  134. const unwrap<typename Proxy<T1>::stored_type> U(P.Q);
  135. op_fliplr::apply_direct(out, U.M);
  136. }
  137. else
  138. {
  139. op_fliplr::apply_proxy_noalias(out, P);
  140. }
  141. }
  142. template<typename eT>
  143. inline
  144. void
  145. op_fliplr::apply_direct(Mat<eT>& out, const Mat<eT>& X)
  146. {
  147. arma_extra_debug_sigprint();
  148. const uword X_n_rows = X.n_rows;
  149. const uword X_n_cols = X.n_cols;
  150. const uword X_n_cols_m1 = X_n_cols - 1;
  151. if(&out != &X)
  152. {
  153. out.set_size(X_n_rows, X_n_cols);
  154. if(X_n_rows == 1)
  155. {
  156. const eT* X_mem = X.memptr();
  157. eT* out_mem = out.memptr();
  158. for(uword col=0; col < X_n_cols; ++col)
  159. {
  160. out_mem[X_n_cols_m1 - col] = X_mem[col];
  161. }
  162. }
  163. else
  164. {
  165. for(uword col=0; col < X_n_cols; ++col)
  166. {
  167. out.col(X_n_cols_m1 - col) = X.col(col);
  168. }
  169. }
  170. }
  171. else // in-place operation
  172. {
  173. const uword N = X_n_cols / 2;
  174. if(X_n_rows == 1)
  175. {
  176. eT* out_mem = out.memptr();
  177. for(uword col=0; col < N; ++col)
  178. {
  179. std::swap(out_mem[X_n_cols_m1 - col], out_mem[col]);
  180. }
  181. }
  182. else
  183. {
  184. for(uword col=0; col < N; ++col)
  185. {
  186. out.swap_cols(X_n_cols_m1 - col, col);
  187. }
  188. }
  189. }
  190. }
  191. template<typename T1>
  192. inline
  193. void
  194. op_fliplr::apply_proxy_noalias(Mat<typename T1::elem_type>& out, const Proxy<T1>& P)
  195. {
  196. arma_extra_debug_sigprint();
  197. typedef typename T1::elem_type eT;
  198. const uword P_n_rows = P.get_n_rows();
  199. const uword P_n_cols = P.get_n_cols();
  200. const uword P_n_cols_m1 = P_n_cols - 1;
  201. out.set_size(P_n_rows, P_n_cols);
  202. if( ((T1::is_row) || (P_n_rows == 1)) && (Proxy<T1>::use_at == false) )
  203. {
  204. eT* out_mem = out.memptr();
  205. const typename Proxy<T1>::ea_type P_ea = P.get_ea();
  206. for(uword col=0; col < P_n_cols; ++col)
  207. {
  208. out_mem[P_n_cols_m1 - col] = P_ea[col];
  209. }
  210. }
  211. else
  212. {
  213. for(uword col=0; col < P_n_cols; ++col)
  214. {
  215. eT* out_colmem = out.colptr(P_n_cols_m1 - col);
  216. for(uword row=0; row < P_n_rows; ++row)
  217. {
  218. out_colmem[row] = P.at(row,col);
  219. }
  220. }
  221. }
  222. }
  223. //! @}