podarray_meat.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 podarray
  16. //! @{
  17. template<typename eT>
  18. arma_hot
  19. inline
  20. podarray<eT>::~podarray()
  21. {
  22. arma_extra_debug_sigprint_this(this);
  23. if(n_elem > podarray_prealloc_n_elem::val )
  24. {
  25. memory::release( mem );
  26. }
  27. }
  28. template<typename eT>
  29. inline
  30. podarray<eT>::podarray()
  31. : n_elem(0)
  32. , mem (0)
  33. {
  34. arma_extra_debug_sigprint_this(this);
  35. }
  36. template<typename eT>
  37. inline
  38. podarray<eT>::podarray(const podarray& x)
  39. : n_elem(x.n_elem)
  40. {
  41. arma_extra_debug_sigprint();
  42. const uword x_n_elem = x.n_elem;
  43. init_cold(x_n_elem);
  44. arrayops::copy( memptr(), x.memptr(), x_n_elem );
  45. }
  46. template<typename eT>
  47. inline
  48. const podarray<eT>&
  49. podarray<eT>::operator=(const podarray& x)
  50. {
  51. arma_extra_debug_sigprint();
  52. if(this != &x)
  53. {
  54. const uword x_n_elem = x.n_elem;
  55. init_warm(x_n_elem);
  56. arrayops::copy( memptr(), x.memptr(), x_n_elem );
  57. }
  58. return *this;
  59. }
  60. template<typename eT>
  61. arma_hot
  62. arma_inline
  63. podarray<eT>::podarray(const uword new_n_elem)
  64. : n_elem(new_n_elem)
  65. {
  66. arma_extra_debug_sigprint_this(this);
  67. init_cold(new_n_elem);
  68. }
  69. template<typename eT>
  70. arma_inline
  71. podarray<eT>::podarray(const eT* X, const uword new_n_elem)
  72. : n_elem(new_n_elem)
  73. {
  74. arma_extra_debug_sigprint_this(this);
  75. init_cold(new_n_elem);
  76. arrayops::copy( memptr(), X, new_n_elem );
  77. }
  78. // template<typename eT>
  79. // template<typename T1>
  80. // inline
  81. // podarray<eT>::podarray(const Proxy<T1>& P)
  82. // : n_elem(P.get_n_elem())
  83. // {
  84. // arma_extra_debug_sigprint_this(this);
  85. //
  86. // const uword P_n_elem = P.get_n_elem();
  87. //
  88. // init_cold(P_n_elem);
  89. //
  90. // eT* out_mem = (*this).memptr();
  91. //
  92. // if(Proxy<T1>::use_at == false)
  93. // {
  94. // typename Proxy<T1>::ea_type A = P.get_ea();
  95. //
  96. // uword i,j;
  97. // for(i=0, j=1; j < P_n_elem; i+=2, j+=2)
  98. // {
  99. // const eT val_i = A[i];
  100. // const eT val_j = A[j];
  101. //
  102. // out_mem[i] = val_i;
  103. // out_mem[j] = val_j;
  104. // }
  105. //
  106. // if(i < P_n_elem)
  107. // {
  108. // out_mem[i] = A[i];
  109. // }
  110. // }
  111. // else
  112. // {
  113. // const uword P_n_rows = P.get_n_rows();
  114. // const uword P_n_cols = P.get_n_cols();
  115. //
  116. // if(P_n_rows != 1)
  117. // {
  118. // uword count = 0;
  119. //
  120. // for(uword col=0; col < P_n_cols; ++col)
  121. // for(uword row=0; row < P_n_rows; ++row, ++count)
  122. // {
  123. // out_mem[count] = P.at(row,col);
  124. // }
  125. // }
  126. // else
  127. // {
  128. // for(uword col=0; col < P_n_cols; ++col)
  129. // {
  130. // out_mem[col] = P.at(0,col);
  131. // }
  132. // }
  133. // }
  134. // }
  135. template<typename eT>
  136. arma_inline
  137. eT
  138. podarray<eT>::operator[] (const uword i) const
  139. {
  140. return mem[i];
  141. }
  142. template<typename eT>
  143. arma_inline
  144. eT&
  145. podarray<eT>::operator[] (const uword i)
  146. {
  147. return access::rw(mem[i]);
  148. }
  149. template<typename eT>
  150. arma_inline
  151. eT
  152. podarray<eT>::operator() (const uword i) const
  153. {
  154. arma_debug_check( (i >= n_elem), "podarray::operator(): index out of bounds");
  155. return mem[i];
  156. }
  157. template<typename eT>
  158. arma_inline
  159. eT&
  160. podarray<eT>::operator() (const uword i)
  161. {
  162. arma_debug_check( (i >= n_elem), "podarray::operator(): index out of bounds");
  163. return access::rw(mem[i]);
  164. }
  165. template<typename eT>
  166. inline
  167. void
  168. podarray<eT>::set_min_size(const uword min_n_elem)
  169. {
  170. arma_extra_debug_sigprint();
  171. if(min_n_elem > n_elem)
  172. {
  173. init_warm(min_n_elem);
  174. }
  175. }
  176. template<typename eT>
  177. inline
  178. void
  179. podarray<eT>::set_size(const uword new_n_elem)
  180. {
  181. arma_extra_debug_sigprint();
  182. init_warm(new_n_elem);
  183. }
  184. template<typename eT>
  185. inline
  186. void
  187. podarray<eT>::reset()
  188. {
  189. arma_extra_debug_sigprint();
  190. init_warm(0);
  191. }
  192. template<typename eT>
  193. inline
  194. void
  195. podarray<eT>::fill(const eT val)
  196. {
  197. arma_extra_debug_sigprint();
  198. arrayops::inplace_set(memptr(), val, n_elem);
  199. }
  200. template<typename eT>
  201. inline
  202. void
  203. podarray<eT>::zeros()
  204. {
  205. arma_extra_debug_sigprint();
  206. arrayops::fill_zeros(memptr(), n_elem);
  207. }
  208. template<typename eT>
  209. inline
  210. void
  211. podarray<eT>::zeros(const uword new_n_elem)
  212. {
  213. arma_extra_debug_sigprint();
  214. init_warm(new_n_elem);
  215. arrayops::fill_zeros(memptr(), n_elem);
  216. }
  217. template<typename eT>
  218. arma_inline
  219. eT*
  220. podarray<eT>::memptr()
  221. {
  222. return mem;
  223. }
  224. template<typename eT>
  225. arma_inline
  226. const eT*
  227. podarray<eT>::memptr() const
  228. {
  229. return mem;
  230. }
  231. template<typename eT>
  232. arma_hot
  233. inline
  234. void
  235. podarray<eT>::copy_row(const Mat<eT>& A, const uword row)
  236. {
  237. const uword cols = A.n_cols;
  238. // note: this function assumes that the podarray has been set to the correct size beforehand
  239. eT* out = memptr();
  240. switch(cols)
  241. {
  242. default:
  243. {
  244. uword i,j;
  245. for(i=0, j=1; j < cols; i+=2, j+=2)
  246. {
  247. const eT tmp_i = A.at(row, i);
  248. const eT tmp_j = A.at(row, j);
  249. out[i] = tmp_i;
  250. out[j] = tmp_j;
  251. }
  252. if(i < cols)
  253. {
  254. out[i] = A.at(row, i);
  255. }
  256. }
  257. break;
  258. case 8: out[7] = A.at(row, 7);
  259. // fallthrough
  260. case 7: out[6] = A.at(row, 6);
  261. // fallthrough
  262. case 6: out[5] = A.at(row, 5);
  263. // fallthrough
  264. case 5: out[4] = A.at(row, 4);
  265. // fallthrough
  266. case 4: out[3] = A.at(row, 3);
  267. // fallthrough
  268. case 3: out[2] = A.at(row, 2);
  269. // fallthrough
  270. case 2: out[1] = A.at(row, 1);
  271. // fallthrough
  272. case 1: out[0] = A.at(row, 0);
  273. // fallthrough
  274. case 0: ;
  275. // fallthrough
  276. }
  277. }
  278. template<typename eT>
  279. inline
  280. void
  281. podarray<eT>::init_cold(const uword new_n_elem)
  282. {
  283. arma_extra_debug_sigprint();
  284. if(new_n_elem <= podarray_prealloc_n_elem::val )
  285. {
  286. mem = mem_local;
  287. }
  288. else
  289. {
  290. mem = memory::acquire<eT>(new_n_elem);
  291. }
  292. }
  293. template<typename eT>
  294. inline
  295. void
  296. podarray<eT>::init_warm(const uword new_n_elem)
  297. {
  298. arma_extra_debug_sigprint();
  299. if(n_elem == new_n_elem)
  300. {
  301. return;
  302. }
  303. if(n_elem > podarray_prealloc_n_elem::val )
  304. {
  305. memory::release( mem );
  306. }
  307. if(new_n_elem <= podarray_prealloc_n_elem::val )
  308. {
  309. mem = mem_local;
  310. }
  311. else
  312. {
  313. mem = memory::acquire<eT>(new_n_elem);
  314. }
  315. access::rw(n_elem) = new_n_elem;
  316. }
  317. //! @}