SpCol_meat.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 SpCol
  16. //! @{
  17. template<typename eT>
  18. inline
  19. SpCol<eT>::SpCol()
  20. : SpMat<eT>(arma_vec_indicator(), 1)
  21. {
  22. arma_extra_debug_sigprint();
  23. }
  24. template<typename eT>
  25. inline
  26. SpCol<eT>::SpCol(const uword in_n_elem)
  27. : SpMat<eT>(arma_vec_indicator(), in_n_elem, 1, 1)
  28. {
  29. arma_extra_debug_sigprint();
  30. }
  31. template<typename eT>
  32. inline
  33. SpCol<eT>::SpCol(const uword in_n_rows, const uword in_n_cols)
  34. : SpMat<eT>(arma_vec_indicator(), in_n_rows, in_n_cols, 1)
  35. {
  36. arma_extra_debug_sigprint();
  37. }
  38. template<typename eT>
  39. inline
  40. SpCol<eT>::SpCol(const SizeMat& s)
  41. : SpMat<eT>(arma_vec_indicator(), 0, 0, 1)
  42. {
  43. arma_extra_debug_sigprint();
  44. SpMat<eT>::init(s.n_rows, s.n_cols);
  45. }
  46. template<typename eT>
  47. inline
  48. SpCol<eT>::SpCol(const char* text)
  49. : SpMat<eT>(arma_vec_indicator(), 1)
  50. {
  51. arma_extra_debug_sigprint();
  52. SpMat<eT>::init(std::string(text));
  53. }
  54. template<typename eT>
  55. inline
  56. SpCol<eT>&
  57. SpCol<eT>::operator=(const char* text)
  58. {
  59. arma_extra_debug_sigprint();
  60. SpMat<eT>::init(std::string(text));
  61. return *this;
  62. }
  63. template<typename eT>
  64. inline
  65. SpCol<eT>::SpCol(const std::string& text)
  66. : SpMat<eT>(arma_vec_indicator(), 1)
  67. {
  68. arma_extra_debug_sigprint();
  69. SpMat<eT>::init(text);
  70. }
  71. template<typename eT>
  72. inline
  73. SpCol<eT>&
  74. SpCol<eT>::operator=(const std::string& text)
  75. {
  76. arma_extra_debug_sigprint();
  77. SpMat<eT>::init(text);
  78. return *this;
  79. }
  80. template<typename eT>
  81. inline
  82. SpCol<eT>&
  83. SpCol<eT>::operator=(const eT val)
  84. {
  85. arma_extra_debug_sigprint();
  86. SpMat<eT>::operator=(val);
  87. return *this;
  88. }
  89. template<typename eT>
  90. template<typename T1>
  91. inline
  92. SpCol<eT>::SpCol(const Base<eT,T1>& X)
  93. : SpMat<eT>(arma_vec_indicator(), 1)
  94. {
  95. arma_extra_debug_sigprint();
  96. SpMat<eT>::operator=(X.get_ref());
  97. }
  98. template<typename eT>
  99. template<typename T1>
  100. inline
  101. SpCol<eT>&
  102. SpCol<eT>::operator=(const Base<eT,T1>& X)
  103. {
  104. arma_extra_debug_sigprint();
  105. SpMat<eT>::operator=(X.get_ref());
  106. return *this;
  107. }
  108. template<typename eT>
  109. template<typename T1>
  110. inline
  111. SpCol<eT>::SpCol(const SpBase<eT,T1>& X)
  112. : SpMat<eT>(arma_vec_indicator(), 1)
  113. {
  114. arma_extra_debug_sigprint();
  115. SpMat<eT>::operator=(X.get_ref());
  116. }
  117. template<typename eT>
  118. template<typename T1>
  119. inline
  120. SpCol<eT>&
  121. SpCol<eT>::operator=(const SpBase<eT,T1>& X)
  122. {
  123. arma_extra_debug_sigprint();
  124. SpMat<eT>::operator=(X.get_ref());
  125. return *this;
  126. }
  127. template<typename eT>
  128. template<typename T1, typename T2>
  129. inline
  130. SpCol<eT>::SpCol
  131. (
  132. const SpBase<typename SpCol<eT>::pod_type, T1>& A,
  133. const SpBase<typename SpCol<eT>::pod_type, T2>& B
  134. )
  135. : SpMat<eT>(arma_vec_indicator(), 1)
  136. {
  137. arma_extra_debug_sigprint();
  138. SpMat<eT>::init(A,B);
  139. }
  140. template<typename eT>
  141. inline
  142. const SpOp<SpCol<eT>,spop_htrans>
  143. SpCol<eT>::t() const
  144. {
  145. return SpOp<SpCol<eT>,spop_htrans>(*this);
  146. }
  147. template<typename eT>
  148. inline
  149. const SpOp<SpCol<eT>,spop_htrans>
  150. SpCol<eT>::ht() const
  151. {
  152. return SpOp<SpCol<eT>,spop_htrans>(*this);
  153. }
  154. template<typename eT>
  155. inline
  156. const SpOp<SpCol<eT>,spop_strans>
  157. SpCol<eT>::st() const
  158. {
  159. return SpOp<SpCol<eT>,spop_strans>(*this);
  160. }
  161. //! remove specified row
  162. template<typename eT>
  163. inline
  164. void
  165. SpCol<eT>::shed_row(const uword row_num)
  166. {
  167. arma_extra_debug_sigprint();
  168. arma_debug_check( row_num >= SpMat<eT>::n_rows, "SpCol::shed_row(): out of bounds");
  169. shed_rows(row_num, row_num);
  170. }
  171. //! remove specified rows
  172. template<typename eT>
  173. inline
  174. void
  175. SpCol<eT>::shed_rows(const uword in_row1, const uword in_row2)
  176. {
  177. arma_extra_debug_sigprint();
  178. arma_debug_check
  179. (
  180. (in_row1 > in_row2) || (in_row2 >= SpMat<eT>::n_rows),
  181. "SpCol::shed_rows(): indices out of bounds or incorrectly used"
  182. );
  183. SpMat<eT>::sync_csc();
  184. const uword diff = (in_row2 - in_row1 + 1);
  185. // This is easy because everything is in one column.
  186. uword start = 0, end = 0;
  187. bool start_found = false, end_found = false;
  188. for(uword i = 0; i < SpMat<eT>::n_nonzero; ++i)
  189. {
  190. // Start position found?
  191. if (SpMat<eT>::row_indices[i] >= in_row1 && !start_found)
  192. {
  193. start = i;
  194. start_found = true;
  195. }
  196. // End position found?
  197. if (SpMat<eT>::row_indices[i] > in_row2)
  198. {
  199. end = i;
  200. end_found = true;
  201. break;
  202. }
  203. }
  204. if (!end_found)
  205. {
  206. end = SpMat<eT>::n_nonzero;
  207. }
  208. // Now we can make the copy.
  209. if (start != end)
  210. {
  211. const uword elem_diff = end - start;
  212. eT* new_values = memory::acquire<eT> (SpMat<eT>::n_nonzero - elem_diff);
  213. uword* new_row_indices = memory::acquire<uword>(SpMat<eT>::n_nonzero - elem_diff);
  214. // Copy before the section we are dropping (if it exists).
  215. if (start > 0)
  216. {
  217. arrayops::copy(new_values, SpMat<eT>::values, start);
  218. arrayops::copy(new_row_indices, SpMat<eT>::row_indices, start);
  219. }
  220. // Copy after the section we are dropping (if it exists).
  221. if (end != SpMat<eT>::n_nonzero)
  222. {
  223. arrayops::copy(new_values + start, SpMat<eT>::values + end, (SpMat<eT>::n_nonzero - end));
  224. arrayops::copy(new_row_indices + start, SpMat<eT>::row_indices + end, (SpMat<eT>::n_nonzero - end));
  225. arrayops::inplace_minus(new_row_indices + start, diff, (SpMat<eT>::n_nonzero - end));
  226. }
  227. memory::release(SpMat<eT>::values);
  228. memory::release(SpMat<eT>::row_indices);
  229. access::rw(SpMat<eT>::values) = new_values;
  230. access::rw(SpMat<eT>::row_indices) = new_row_indices;
  231. access::rw(SpMat<eT>::n_nonzero) -= elem_diff;
  232. access::rw(SpMat<eT>::col_ptrs[1]) -= elem_diff;
  233. }
  234. access::rw(SpMat<eT>::n_rows) -= diff;
  235. access::rw(SpMat<eT>::n_elem) -= diff;
  236. SpMat<eT>::invalidate_cache();
  237. }
  238. // //! insert N rows at the specified row position,
  239. // //! optionally setting the elements of the inserted rows to zero
  240. // template<typename eT>
  241. // inline
  242. // void
  243. // SpCol<eT>::insert_rows(const uword row_num, const uword N, const bool set_to_zero)
  244. // {
  245. // arma_extra_debug_sigprint();
  246. //
  247. // arma_debug_check(set_to_zero == false, "SpCol::insert_rows(): cannot set nonzero values");
  248. //
  249. // arma_debug_check((row_num > SpMat<eT>::n_rows), "SpCol::insert_rows(): out of bounds");
  250. //
  251. // for(uword row = 0; row < SpMat<eT>::n_rows; ++row)
  252. // {
  253. // if (SpMat<eT>::row_indices[row] >= row_num)
  254. // {
  255. // access::rw(SpMat<eT>::row_indices[row]) += N;
  256. // }
  257. // }
  258. //
  259. // access::rw(SpMat<eT>::n_rows) += N;
  260. // access::rw(SpMat<eT>::n_elem) += N;
  261. // }
  262. template<typename eT>
  263. inline
  264. typename SpCol<eT>::row_iterator
  265. SpCol<eT>::begin_row(const uword row_num)
  266. {
  267. arma_extra_debug_sigprint();
  268. arma_debug_check( (row_num >= SpMat<eT>::n_rows), "SpCol::begin_row(): index out of bounds");
  269. SpMat<eT>::sync_csc();
  270. return row_iterator(*this, row_num, 0);
  271. }
  272. template<typename eT>
  273. inline
  274. typename SpCol<eT>::const_row_iterator
  275. SpCol<eT>::begin_row(const uword row_num) const
  276. {
  277. arma_extra_debug_sigprint();
  278. arma_debug_check( (row_num >= SpMat<eT>::n_rows), "SpCol::begin_row(): index out of bounds");
  279. SpMat<eT>::sync_csc();
  280. return const_row_iterator(*this, row_num, 0);
  281. }
  282. template<typename eT>
  283. inline
  284. typename SpCol<eT>::row_iterator
  285. SpCol<eT>::end_row(const uword row_num)
  286. {
  287. arma_extra_debug_sigprint();
  288. arma_debug_check( (row_num >= SpMat<eT>::n_rows), "SpCol::end_row(): index out of bounds");
  289. SpMat<eT>::sync_csc();
  290. return row_iterator(*this, row_num + 1, 0);
  291. }
  292. template<typename eT>
  293. inline
  294. typename SpCol<eT>::const_row_iterator
  295. SpCol<eT>::end_row(const uword row_num) const
  296. {
  297. arma_extra_debug_sigprint();
  298. arma_debug_check( (row_num >= SpMat<eT>::n_rows), "SpCol::end_row(): index out of bounds");
  299. SpMat<eT>::sync_csc();
  300. return const_row_iterator(*this, row_num + 1, 0);
  301. }
  302. #ifdef ARMA_EXTRA_SPCOL_MEAT
  303. #include ARMA_INCFILE_WRAP(ARMA_EXTRA_SPCOL_MEAT)
  304. #endif
  305. //! @}