ProxyCube.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 ProxyCube
  16. //! @{
  17. template<typename T1>
  18. class ProxyCube
  19. {
  20. public:
  21. inline ProxyCube(const T1&)
  22. {
  23. arma_type_check(( is_arma_cube_type<T1>::value == false ));
  24. }
  25. };
  26. // ea_type is the "element accessor" type,
  27. // which can provide access to elements via operator[]
  28. template<typename eT>
  29. class ProxyCube< Cube<eT> >
  30. {
  31. public:
  32. typedef eT elem_type;
  33. typedef typename get_pod_type<elem_type>::result pod_type;
  34. typedef Cube<eT> stored_type;
  35. typedef const eT* ea_type;
  36. typedef const Cube<eT>& aligned_ea_type;
  37. static const bool use_at = false;
  38. static const bool use_mp = false;
  39. static const bool has_subview = false;
  40. arma_aligned const Cube<eT>& Q;
  41. inline explicit ProxyCube(const Cube<eT>& A)
  42. : Q(A)
  43. {
  44. arma_extra_debug_sigprint();
  45. }
  46. arma_inline uword get_n_rows() const { return Q.n_rows; }
  47. arma_inline uword get_n_cols() const { return Q.n_cols; }
  48. arma_inline uword get_n_elem_slice() const { return Q.n_elem_slice; }
  49. arma_inline uword get_n_slices() const { return Q.n_slices; }
  50. arma_inline uword get_n_elem() const { return Q.n_elem; }
  51. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  52. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  53. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  54. arma_inline ea_type get_ea() const { return Q.memptr(); }
  55. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  56. template<typename eT2>
  57. arma_inline bool is_alias(const Cube<eT2>& X) const { return (void_ptr(&Q) == void_ptr(&X)); }
  58. arma_inline bool is_aligned() const { return memory::is_aligned(Q.memptr()); }
  59. };
  60. template<typename eT, typename gen_type>
  61. class ProxyCube< GenCube<eT, gen_type> >
  62. {
  63. public:
  64. typedef eT elem_type;
  65. typedef typename get_pod_type<elem_type>::result pod_type;
  66. typedef GenCube<eT, gen_type> stored_type;
  67. typedef const GenCube<eT, gen_type>& ea_type;
  68. typedef const GenCube<eT, gen_type>& aligned_ea_type;
  69. static const bool use_at = false;
  70. static const bool use_mp = false;
  71. static const bool has_subview = false;
  72. arma_aligned const GenCube<eT, gen_type>& Q;
  73. inline explicit ProxyCube(const GenCube<eT, gen_type>& A)
  74. : Q(A)
  75. {
  76. arma_extra_debug_sigprint();
  77. }
  78. arma_inline uword get_n_rows() const { return Q.n_rows; }
  79. arma_inline uword get_n_cols() const { return Q.n_cols; }
  80. arma_inline uword get_n_elem_slice() const { return Q.n_rows*Q.n_cols; }
  81. arma_inline uword get_n_slices() const { return Q.n_slices; }
  82. arma_inline uword get_n_elem() const { return Q.n_rows*Q.n_cols*Q.n_slices; }
  83. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  84. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  85. arma_inline elem_type at_alt (const uword i) const { return Q[i]; }
  86. arma_inline ea_type get_ea() const { return Q; }
  87. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  88. template<typename eT2>
  89. arma_inline bool is_alias(const Cube<eT2>&) const { return false; }
  90. arma_inline bool is_aligned() const { return GenCube<eT, gen_type>::is_simple; }
  91. };
  92. template<typename eT>
  93. class ProxyCube< GenCube<eT, gen_randu> >
  94. {
  95. public:
  96. typedef eT elem_type;
  97. typedef typename get_pod_type<elem_type>::result pod_type;
  98. typedef Cube<eT> stored_type;
  99. typedef const eT* ea_type;
  100. typedef const Cube<eT>& aligned_ea_type;
  101. static const bool use_at = false;
  102. static const bool use_mp = false;
  103. static const bool has_subview = false;
  104. arma_aligned const Cube<eT> Q;
  105. inline explicit ProxyCube(const GenCube<eT, gen_randu>& A)
  106. : Q(A)
  107. {
  108. arma_extra_debug_sigprint();
  109. }
  110. arma_inline uword get_n_rows() const { return Q.n_rows; }
  111. arma_inline uword get_n_cols() const { return Q.n_cols; }
  112. arma_inline uword get_n_elem_slice() const { return Q.n_elem_slice; }
  113. arma_inline uword get_n_slices() const { return Q.n_slices; }
  114. arma_inline uword get_n_elem() const { return Q.n_elem; }
  115. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  116. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  117. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  118. arma_inline ea_type get_ea() const { return Q.memptr(); }
  119. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  120. template<typename eT2>
  121. arma_inline bool is_alias(const Cube<eT2>&) const { return false; }
  122. arma_inline bool is_aligned() const { return memory::is_aligned(Q.memptr()); }
  123. };
  124. template<typename eT>
  125. class ProxyCube< GenCube<eT, gen_randn> >
  126. {
  127. public:
  128. typedef eT elem_type;
  129. typedef typename get_pod_type<elem_type>::result pod_type;
  130. typedef Cube<eT> stored_type;
  131. typedef const eT* ea_type;
  132. typedef const Cube<eT>& aligned_ea_type;
  133. static const bool use_at = false;
  134. static const bool use_mp = false;
  135. static const bool has_subview = false;
  136. arma_aligned const Cube<eT> Q;
  137. inline explicit ProxyCube(const GenCube<eT, gen_randn>& A)
  138. : Q(A)
  139. {
  140. arma_extra_debug_sigprint();
  141. }
  142. arma_inline uword get_n_rows() const { return Q.n_rows; }
  143. arma_inline uword get_n_cols() const { return Q.n_cols; }
  144. arma_inline uword get_n_elem_slice() const { return Q.n_elem_slice; }
  145. arma_inline uword get_n_slices() const { return Q.n_slices; }
  146. arma_inline uword get_n_elem() const { return Q.n_elem; }
  147. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  148. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  149. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  150. arma_inline ea_type get_ea() const { return Q.memptr(); }
  151. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  152. template<typename eT2>
  153. arma_inline bool is_alias(const Cube<eT2>&) const { return false; }
  154. arma_inline bool is_aligned() const { return memory::is_aligned(Q.memptr()); }
  155. };
  156. template<typename T1, typename op_type>
  157. class ProxyCube< OpCube<T1, op_type> >
  158. {
  159. public:
  160. typedef typename T1::elem_type elem_type;
  161. typedef typename get_pod_type<elem_type>::result pod_type;
  162. typedef Cube<elem_type> stored_type;
  163. typedef const elem_type* ea_type;
  164. typedef const Cube<elem_type>& aligned_ea_type;
  165. static const bool use_at = false;
  166. static const bool use_mp = false;
  167. static const bool has_subview = false;
  168. arma_aligned const Cube<elem_type> Q;
  169. inline explicit ProxyCube(const OpCube<T1, op_type>& A)
  170. : Q(A)
  171. {
  172. arma_extra_debug_sigprint();
  173. }
  174. arma_inline uword get_n_rows() const { return Q.n_rows; }
  175. arma_inline uword get_n_cols() const { return Q.n_cols; }
  176. arma_inline uword get_n_elem_slice() const { return Q.n_elem_slice; }
  177. arma_inline uword get_n_slices() const { return Q.n_slices; }
  178. arma_inline uword get_n_elem() const { return Q.n_elem; }
  179. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  180. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  181. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  182. arma_inline ea_type get_ea() const { return Q.memptr(); }
  183. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  184. template<typename eT2>
  185. arma_inline bool is_alias(const Cube<eT2>&) const { return false; }
  186. arma_inline bool is_aligned() const { return memory::is_aligned(Q.memptr()); }
  187. };
  188. template<typename T1, typename T2, typename glue_type>
  189. class ProxyCube< GlueCube<T1, T2, glue_type> >
  190. {
  191. public:
  192. typedef typename T1::elem_type elem_type;
  193. typedef typename get_pod_type<elem_type>::result pod_type;
  194. typedef Cube<elem_type> stored_type;
  195. typedef const elem_type* ea_type;
  196. typedef const Cube<elem_type>& aligned_ea_type;
  197. static const bool use_at = false;
  198. static const bool use_mp = false;
  199. static const bool has_subview = false;
  200. arma_aligned const Cube<elem_type> Q;
  201. inline explicit ProxyCube(const GlueCube<T1, T2, glue_type>& A)
  202. : Q(A)
  203. {
  204. arma_extra_debug_sigprint();
  205. }
  206. arma_inline uword get_n_rows() const { return Q.n_rows; }
  207. arma_inline uword get_n_cols() const { return Q.n_cols; }
  208. arma_inline uword get_n_elem_slice() const { return Q.n_elem_slice; }
  209. arma_inline uword get_n_slices() const { return Q.n_slices; }
  210. arma_inline uword get_n_elem() const { return Q.n_elem; }
  211. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  212. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  213. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  214. arma_inline ea_type get_ea() const { return Q.memptr(); }
  215. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  216. template<typename eT2>
  217. arma_inline bool is_alias(const Cube<eT2>&) const { return false; }
  218. arma_inline bool is_aligned() const { return memory::is_aligned(Q.memptr()); }
  219. };
  220. template<typename eT>
  221. class ProxyCube< subview_cube<eT> >
  222. {
  223. public:
  224. typedef eT elem_type;
  225. typedef typename get_pod_type<elem_type>::result pod_type;
  226. typedef subview_cube<eT> stored_type;
  227. typedef const subview_cube<eT>& ea_type;
  228. typedef const subview_cube<eT>& aligned_ea_type;
  229. static const bool use_at = true;
  230. static const bool use_mp = false;
  231. static const bool has_subview = true;
  232. arma_aligned const subview_cube<eT>& Q;
  233. inline explicit ProxyCube(const subview_cube<eT>& A)
  234. : Q(A)
  235. {
  236. arma_extra_debug_sigprint();
  237. }
  238. arma_inline uword get_n_rows() const { return Q.n_rows; }
  239. arma_inline uword get_n_cols() const { return Q.n_cols; }
  240. arma_inline uword get_n_elem_slice() const { return Q.n_elem_slice; }
  241. arma_inline uword get_n_slices() const { return Q.n_slices; }
  242. arma_inline uword get_n_elem() const { return Q.n_elem; }
  243. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  244. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  245. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  246. arma_inline ea_type get_ea() const { return Q; }
  247. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  248. template<typename eT2>
  249. arma_inline bool is_alias(const Cube<eT2>& X) const { return (void_ptr(&(Q.m)) == void_ptr(&X)); }
  250. arma_inline bool is_aligned() const { return false; }
  251. };
  252. template<typename eT, typename T1>
  253. class ProxyCube< subview_cube_slices<eT,T1> >
  254. {
  255. public:
  256. typedef eT elem_type;
  257. typedef typename get_pod_type<elem_type>::result pod_type;
  258. typedef Cube<eT> stored_type;
  259. typedef const eT* ea_type;
  260. typedef const Cube<eT>& aligned_ea_type;
  261. static const bool use_at = false;
  262. static const bool use_mp = false;
  263. static const bool has_subview = false;
  264. arma_aligned const Cube<eT> Q;
  265. inline explicit ProxyCube(const subview_cube_slices<eT,T1>& A)
  266. : Q(A)
  267. {
  268. arma_extra_debug_sigprint();
  269. }
  270. arma_inline uword get_n_rows() const { return Q.n_rows; }
  271. arma_inline uword get_n_cols() const { return Q.n_cols; }
  272. arma_inline uword get_n_elem_slice() const { return Q.n_elem_slice; }
  273. arma_inline uword get_n_slices() const { return Q.n_slices; }
  274. arma_inline uword get_n_elem() const { return Q.n_elem; }
  275. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  276. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  277. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  278. arma_inline ea_type get_ea() const { return Q.memptr(); }
  279. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  280. template<typename eT2>
  281. arma_inline bool is_alias(const Cube<eT2>&) const { return false; }
  282. arma_inline bool is_aligned() const { return memory::is_aligned(Q.memptr()); }
  283. };
  284. template<typename T1, typename eop_type>
  285. class ProxyCube< eOpCube<T1, eop_type > >
  286. {
  287. public:
  288. typedef typename T1::elem_type elem_type;
  289. typedef typename get_pod_type<elem_type>::result pod_type;
  290. typedef eOpCube<T1, eop_type> stored_type;
  291. typedef const eOpCube<T1, eop_type>& ea_type;
  292. typedef const eOpCube<T1, eop_type>& aligned_ea_type;
  293. static const bool use_at = eOpCube<T1, eop_type>::use_at;
  294. static const bool use_mp = eOpCube<T1, eop_type>::use_mp;
  295. static const bool has_subview = eOpCube<T1, eop_type>::has_subview;
  296. arma_aligned const eOpCube<T1, eop_type>& Q;
  297. inline explicit ProxyCube(const eOpCube<T1, eop_type>& A)
  298. : Q(A)
  299. {
  300. arma_extra_debug_sigprint();
  301. }
  302. arma_inline uword get_n_rows() const { return Q.get_n_rows(); }
  303. arma_inline uword get_n_cols() const { return Q.get_n_cols(); }
  304. arma_inline uword get_n_elem_slice() const { return Q.get_n_elem_slice(); }
  305. arma_inline uword get_n_slices() const { return Q.get_n_slices(); }
  306. arma_inline uword get_n_elem() const { return Q.get_n_elem(); }
  307. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  308. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  309. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  310. arma_inline ea_type get_ea() const { return Q; }
  311. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  312. template<typename eT2>
  313. arma_inline bool is_alias(const Cube<eT2>& X) const { return Q.P.is_alias(X); }
  314. arma_inline bool is_aligned() const { return Q.P.is_aligned(); }
  315. };
  316. template<typename T1, typename T2, typename eglue_type>
  317. class ProxyCube< eGlueCube<T1, T2, eglue_type > >
  318. {
  319. public:
  320. typedef typename T1::elem_type elem_type;
  321. typedef typename get_pod_type<elem_type>::result pod_type;
  322. typedef eGlueCube<T1, T2, eglue_type> stored_type;
  323. typedef const eGlueCube<T1, T2, eglue_type>& ea_type;
  324. typedef const eGlueCube<T1, T2, eglue_type>& aligned_ea_type;
  325. static const bool use_at = eGlueCube<T1, T2, eglue_type>::use_at;
  326. static const bool use_mp = eGlueCube<T1, T2, eglue_type>::use_mp;
  327. static const bool has_subview = eGlueCube<T1, T2, eglue_type>::has_subview;
  328. arma_aligned const eGlueCube<T1, T2, eglue_type>& Q;
  329. inline explicit ProxyCube(const eGlueCube<T1, T2, eglue_type>& A)
  330. : Q(A)
  331. {
  332. arma_extra_debug_sigprint();
  333. }
  334. arma_inline uword get_n_rows() const { return Q.get_n_rows(); }
  335. arma_inline uword get_n_cols() const { return Q.get_n_cols(); }
  336. arma_inline uword get_n_elem_slice() const { return Q.get_n_elem_slice(); }
  337. arma_inline uword get_n_slices() const { return Q.get_n_slices(); }
  338. arma_inline uword get_n_elem() const { return Q.get_n_elem(); }
  339. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  340. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  341. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  342. arma_inline ea_type get_ea() const { return Q; }
  343. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  344. template<typename eT2>
  345. arma_inline bool is_alias(const Cube<eT2>& X) const { return (Q.P1.is_alias(X) || Q.P2.is_alias(X)); }
  346. arma_inline bool is_aligned() const { return Q.P1.is_aligned() && Q.P2.is_aligned(); }
  347. };
  348. template<typename out_eT, typename T1, typename op_type>
  349. class ProxyCube< mtOpCube<out_eT, T1, op_type> >
  350. {
  351. public:
  352. typedef out_eT elem_type;
  353. typedef typename get_pod_type<out_eT>::result pod_type;
  354. typedef Cube<out_eT> stored_type;
  355. typedef const elem_type* ea_type;
  356. typedef const Cube<out_eT>& aligned_ea_type;
  357. static const bool use_at = false;
  358. static const bool use_mp = false;
  359. static const bool has_subview = false;
  360. arma_aligned const Cube<out_eT> Q;
  361. inline explicit ProxyCube(const mtOpCube<out_eT, T1, op_type>& A)
  362. : Q(A)
  363. {
  364. arma_extra_debug_sigprint();
  365. }
  366. arma_inline uword get_n_rows() const { return Q.n_rows; }
  367. arma_inline uword get_n_cols() const { return Q.n_cols; }
  368. arma_inline uword get_n_elem_slice() const { return Q.n_elem_slice; }
  369. arma_inline uword get_n_slices() const { return Q.n_slices; }
  370. arma_inline uword get_n_elem() const { return Q.n_elem; }
  371. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  372. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  373. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  374. arma_inline ea_type get_ea() const { return Q.memptr(); }
  375. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  376. template<typename eT2>
  377. arma_inline bool is_alias(const Cube<eT2>&) const { return false; }
  378. arma_inline bool is_aligned() const { return memory::is_aligned(Q.memptr()); }
  379. };
  380. template<typename out_eT, typename T1, typename T2, typename glue_type>
  381. class ProxyCube< mtGlueCube<out_eT, T1, T2, glue_type > >
  382. {
  383. public:
  384. typedef out_eT elem_type;
  385. typedef typename get_pod_type<out_eT>::result pod_type;
  386. typedef Cube<out_eT> stored_type;
  387. typedef const elem_type* ea_type;
  388. typedef const Cube<out_eT>& aligned_ea_type;
  389. static const bool use_at = false;
  390. static const bool use_mp = false;
  391. static const bool has_subview = false;
  392. arma_aligned const Cube<out_eT> Q;
  393. inline explicit ProxyCube(const mtGlueCube<out_eT, T1, T2, glue_type>& A)
  394. : Q(A)
  395. {
  396. arma_extra_debug_sigprint();
  397. }
  398. arma_inline uword get_n_rows() const { return Q.n_rows; }
  399. arma_inline uword get_n_cols() const { return Q.n_cols; }
  400. arma_inline uword get_n_elem_slice() const { return Q.n_elem_slice; }
  401. arma_inline uword get_n_slices() const { return Q.n_slices; }
  402. arma_inline uword get_n_elem() const { return Q.n_elem; }
  403. arma_inline elem_type operator[] (const uword i) const { return Q[i]; }
  404. arma_inline elem_type at (const uword row, const uword col, const uword slice) const { return Q.at(row, col, slice); }
  405. arma_inline elem_type at_alt (const uword i) const { return Q.at_alt(i); }
  406. arma_inline ea_type get_ea() const { return Q.memptr(); }
  407. arma_inline aligned_ea_type get_aligned_ea() const { return Q; }
  408. template<typename eT2>
  409. arma_inline bool is_alias(const Cube<eT2>&) const { return false; }
  410. arma_inline bool is_aligned() const { return memory::is_aligned(Q.memptr()); }
  411. };
  412. //! @}