injector_meat.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 injector
  16. //! @{
  17. template<typename eT>
  18. inline
  19. mat_injector_row<eT>::mat_injector_row()
  20. : n_cols(0)
  21. {
  22. arma_extra_debug_sigprint();
  23. A.set_size( podarray_prealloc_n_elem::val );
  24. }
  25. template<typename eT>
  26. inline
  27. void
  28. mat_injector_row<eT>::insert(const eT val) const
  29. {
  30. arma_extra_debug_sigprint();
  31. if(n_cols < A.n_elem)
  32. {
  33. A[n_cols] = val;
  34. ++n_cols;
  35. }
  36. else
  37. {
  38. B.set_size(2 * A.n_elem);
  39. arrayops::copy(B.memptr(), A.memptr(), n_cols);
  40. B[n_cols] = val;
  41. ++n_cols;
  42. std::swap( access::rw(A.mem), access::rw(B.mem) );
  43. std::swap( access::rw(A.n_elem), access::rw(B.n_elem) );
  44. }
  45. }
  46. //
  47. //
  48. //
  49. template<typename T1>
  50. inline
  51. mat_injector<T1>::mat_injector(T1& in_X, const typename mat_injector<T1>::elem_type val)
  52. : X(in_X)
  53. , n_rows(1)
  54. {
  55. arma_extra_debug_sigprint();
  56. typedef typename mat_injector<T1>::elem_type eT;
  57. AA = new podarray< mat_injector_row<eT>* >;
  58. BB = new podarray< mat_injector_row<eT>* >;
  59. podarray< mat_injector_row<eT>* >& A = *AA;
  60. A.set_size(n_rows);
  61. for(uword row=0; row<n_rows; ++row)
  62. {
  63. A[row] = new mat_injector_row<eT>;
  64. }
  65. (*(A[0])).insert(val);
  66. }
  67. template<typename T1>
  68. inline
  69. mat_injector<T1>::mat_injector(T1& in_X, const injector_end_of_row<>& x)
  70. : X(in_X)
  71. , n_rows(1)
  72. {
  73. arma_extra_debug_sigprint();
  74. arma_ignore(x);
  75. typedef typename mat_injector<T1>::elem_type eT;
  76. AA = new podarray< mat_injector_row<eT>* >;
  77. BB = new podarray< mat_injector_row<eT>* >;
  78. podarray< mat_injector_row<eT>* >& A = *AA;
  79. A.set_size(n_rows);
  80. for(uword row=0; row<n_rows; ++row)
  81. {
  82. A[row] = new mat_injector_row<eT>;
  83. }
  84. (*this).end_of_row();
  85. }
  86. template<typename T1>
  87. inline
  88. mat_injector<T1>::~mat_injector()
  89. {
  90. arma_extra_debug_sigprint();
  91. typedef typename mat_injector<T1>::elem_type eT;
  92. podarray< mat_injector_row<eT>* >& A = *AA;
  93. if(n_rows > 0)
  94. {
  95. uword max_n_cols = (*(A[0])).n_cols;
  96. for(uword row=1; row<n_rows; ++row)
  97. {
  98. const uword n_cols = (*(A[row])).n_cols;
  99. if(max_n_cols < n_cols)
  100. {
  101. max_n_cols = n_cols;
  102. }
  103. }
  104. const uword max_n_rows = ((*(A[n_rows-1])).n_cols == 0) ? n_rows-1 : n_rows;
  105. if(is_Mat_only<T1>::value == true)
  106. {
  107. X.set_size(max_n_rows, max_n_cols);
  108. for(uword row=0; row<max_n_rows; ++row)
  109. {
  110. const uword n_cols = (*(A[row])).n_cols;
  111. for(uword col=0; col<n_cols; ++col)
  112. {
  113. X.at(row,col) = (*(A[row])).A[col];
  114. }
  115. for(uword col=n_cols; col<max_n_cols; ++col)
  116. {
  117. X.at(row,col) = eT(0);
  118. }
  119. }
  120. }
  121. else
  122. if(is_Row<T1>::value == true)
  123. {
  124. arma_debug_check( (max_n_rows > 1), "matrix initialisation: incompatible dimensions" );
  125. const uword n_cols = (*(A[0])).n_cols;
  126. X.set_size(1, n_cols);
  127. arrayops::copy( X.memptr(), (*(A[0])).A.memptr(), n_cols );
  128. }
  129. else
  130. if(is_Col<T1>::value == true)
  131. {
  132. const bool is_vec = ( (max_n_rows == 1) || (max_n_cols == 1) );
  133. arma_debug_check( (is_vec == false), "matrix initialisation: incompatible dimensions" );
  134. const uword n_elem = (std::max)(max_n_rows, max_n_cols);
  135. X.set_size(n_elem, 1);
  136. uword i = 0;
  137. for(uword row=0; row<max_n_rows; ++row)
  138. {
  139. const uword n_cols = (*(A[0])).n_cols;
  140. for(uword col=0; col<n_cols; ++col)
  141. {
  142. X[i] = (*(A[row])).A[col];
  143. ++i;
  144. }
  145. for(uword col=n_cols; col<max_n_cols; ++col)
  146. {
  147. X[i] = eT(0);
  148. ++i;
  149. }
  150. }
  151. }
  152. }
  153. for(uword row=0; row<n_rows; ++row)
  154. {
  155. delete A[row];
  156. }
  157. delete AA;
  158. delete BB;
  159. }
  160. template<typename T1>
  161. inline
  162. void
  163. mat_injector<T1>::insert(const typename mat_injector<T1>::elem_type val) const
  164. {
  165. arma_extra_debug_sigprint();
  166. typedef typename mat_injector<T1>::elem_type eT;
  167. podarray< mat_injector_row<eT>* >& A = *AA;
  168. (*(A[n_rows-1])).insert(val);
  169. }
  170. template<typename T1>
  171. inline
  172. void
  173. mat_injector<T1>::end_of_row() const
  174. {
  175. arma_extra_debug_sigprint();
  176. typedef typename mat_injector<T1>::elem_type eT;
  177. podarray< mat_injector_row<eT>* >& A = *AA;
  178. podarray< mat_injector_row<eT>* >& B = *BB;
  179. B.set_size( n_rows+1 );
  180. arrayops::copy(B.memptr(), A.memptr(), n_rows);
  181. for(uword row=n_rows; row<(n_rows+1); ++row)
  182. {
  183. B[row] = new mat_injector_row<eT>;
  184. }
  185. std::swap(AA, BB);
  186. n_rows += 1;
  187. }
  188. template<typename T1>
  189. arma_inline
  190. const mat_injector<T1>&
  191. operator<<(const mat_injector<T1>& ref, const typename mat_injector<T1>::elem_type val)
  192. {
  193. arma_extra_debug_sigprint();
  194. ref.insert(val);
  195. return ref;
  196. }
  197. template<typename T1>
  198. arma_inline
  199. const mat_injector<T1>&
  200. operator<<(const mat_injector<T1>& ref, const injector_end_of_row<>& x)
  201. {
  202. arma_extra_debug_sigprint();
  203. arma_ignore(x);
  204. ref.end_of_row();
  205. return ref;
  206. }
  207. //// using a mixture of operator << and , doesn't work yet
  208. //// e.g. A << 1, 2, 3 << endr
  209. //// in the above "3 << endr" requires special handling.
  210. //// similarly, special handling is necessary for "endr << 3"
  211. ////
  212. // template<typename T1>
  213. // arma_inline
  214. // const mat_injector<T1>&
  215. // operator,(const mat_injector<T1>& ref, const typename mat_injector<T1>::elem_type val)
  216. // {
  217. // arma_extra_debug_sigprint();
  218. //
  219. // ref.insert(val);
  220. //
  221. // return ref;
  222. // }
  223. // template<typename T1>
  224. // arma_inline
  225. // const mat_injector<T1>&
  226. // operator,(const mat_injector<T1>& ref, const injector_end_of_row<>& x)
  227. // {
  228. // arma_extra_debug_sigprint();
  229. // arma_ignore(x);
  230. //
  231. // ref.end_of_row();
  232. //
  233. // return ref;
  234. // }
  235. //
  236. //
  237. //
  238. template<typename oT>
  239. inline
  240. field_injector_row<oT>::field_injector_row()
  241. : n_cols(0)
  242. {
  243. arma_extra_debug_sigprint();
  244. AA = new field<oT>;
  245. BB = new field<oT>;
  246. field<oT>& A = *AA;
  247. A.set_size( field_prealloc_n_elem::val );
  248. }
  249. template<typename oT>
  250. inline
  251. field_injector_row<oT>::~field_injector_row()
  252. {
  253. arma_extra_debug_sigprint();
  254. delete AA;
  255. delete BB;
  256. }
  257. template<typename oT>
  258. inline
  259. void
  260. field_injector_row<oT>::insert(const oT& val) const
  261. {
  262. arma_extra_debug_sigprint();
  263. field<oT>& A = *AA;
  264. field<oT>& B = *BB;
  265. if(n_cols < A.n_elem)
  266. {
  267. A[n_cols] = val;
  268. ++n_cols;
  269. }
  270. else
  271. {
  272. B.set_size(2 * A.n_elem);
  273. for(uword i=0; i<n_cols; ++i)
  274. {
  275. B[i] = A[i];
  276. }
  277. B[n_cols] = val;
  278. ++n_cols;
  279. std::swap(AA, BB);
  280. }
  281. }
  282. //
  283. //
  284. //
  285. template<typename T1>
  286. inline
  287. field_injector<T1>::field_injector(T1& in_X, const typename field_injector<T1>::object_type& val)
  288. : X(in_X)
  289. , n_rows(1)
  290. {
  291. arma_extra_debug_sigprint();
  292. typedef typename field_injector<T1>::object_type oT;
  293. AA = new podarray< field_injector_row<oT>* >;
  294. BB = new podarray< field_injector_row<oT>* >;
  295. podarray< field_injector_row<oT>* >& A = *AA;
  296. A.set_size(n_rows);
  297. for(uword row=0; row<n_rows; ++row)
  298. {
  299. A[row] = new field_injector_row<oT>;
  300. }
  301. (*(A[0])).insert(val);
  302. }
  303. template<typename T1>
  304. inline
  305. field_injector<T1>::field_injector(T1& in_X, const injector_end_of_row<>& x)
  306. : X(in_X)
  307. , n_rows(1)
  308. {
  309. arma_extra_debug_sigprint();
  310. arma_ignore(x);
  311. typedef typename field_injector<T1>::object_type oT;
  312. AA = new podarray< field_injector_row<oT>* >;
  313. BB = new podarray< field_injector_row<oT>* >;
  314. podarray< field_injector_row<oT>* >& A = *AA;
  315. A.set_size(n_rows);
  316. for(uword row=0; row<n_rows; ++row)
  317. {
  318. A[row] = new field_injector_row<oT>;
  319. }
  320. (*this).end_of_row();
  321. }
  322. template<typename T1>
  323. inline
  324. field_injector<T1>::~field_injector()
  325. {
  326. arma_extra_debug_sigprint();
  327. typedef typename field_injector<T1>::object_type oT;
  328. podarray< field_injector_row<oT>* >& A = *AA;
  329. if(n_rows > 0)
  330. {
  331. uword max_n_cols = (*(A[0])).n_cols;
  332. for(uword row=1; row<n_rows; ++row)
  333. {
  334. const uword n_cols = (*(A[row])).n_cols;
  335. if(max_n_cols < n_cols)
  336. {
  337. max_n_cols = n_cols;
  338. }
  339. }
  340. const uword max_n_rows = ((*(A[n_rows-1])).n_cols == 0) ? n_rows-1 : n_rows;
  341. X.set_size(max_n_rows, max_n_cols);
  342. for(uword row=0; row<max_n_rows; ++row)
  343. {
  344. const uword n_cols = (*(A[row])).n_cols;
  345. for(uword col=0; col<n_cols; ++col)
  346. {
  347. const field<oT>& tmp = *((*(A[row])).AA);
  348. X.at(row,col) = tmp[col];
  349. }
  350. for(uword col=n_cols; col<max_n_cols; ++col)
  351. {
  352. X.at(row,col) = oT();
  353. }
  354. }
  355. }
  356. for(uword row=0; row<n_rows; ++row)
  357. {
  358. delete A[row];
  359. }
  360. delete AA;
  361. delete BB;
  362. }
  363. template<typename T1>
  364. inline
  365. void
  366. field_injector<T1>::insert(const typename field_injector<T1>::object_type& val) const
  367. {
  368. arma_extra_debug_sigprint();
  369. typedef typename field_injector<T1>::object_type oT;
  370. podarray< field_injector_row<oT>* >& A = *AA;
  371. (*(A[n_rows-1])).insert(val);
  372. }
  373. template<typename T1>
  374. inline
  375. void
  376. field_injector<T1>::end_of_row() const
  377. {
  378. arma_extra_debug_sigprint();
  379. typedef typename field_injector<T1>::object_type oT;
  380. podarray< field_injector_row<oT>* >& A = *AA;
  381. podarray< field_injector_row<oT>* >& B = *BB;
  382. B.set_size( n_rows+1 );
  383. for(uword row=0; row<n_rows; ++row)
  384. {
  385. B[row] = A[row];
  386. }
  387. for(uword row=n_rows; row<(n_rows+1); ++row)
  388. {
  389. B[row] = new field_injector_row<oT>;
  390. }
  391. std::swap(AA, BB);
  392. n_rows += 1;
  393. }
  394. template<typename T1>
  395. arma_inline
  396. const field_injector<T1>&
  397. operator<<(const field_injector<T1>& ref, const typename field_injector<T1>::object_type& val)
  398. {
  399. arma_extra_debug_sigprint();
  400. ref.insert(val);
  401. return ref;
  402. }
  403. template<typename T1>
  404. arma_inline
  405. const field_injector<T1>&
  406. operator<<(const field_injector<T1>& ref, const injector_end_of_row<>& x)
  407. {
  408. arma_extra_debug_sigprint();
  409. arma_ignore(x);
  410. ref.end_of_row();
  411. return ref;
  412. }
  413. //! @}