You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1068 lines
47 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2017 Yves Renard
  4. This file is a part of GetFEM++
  5. GetFEM++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /**@file gmm_interface.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date October 13, 2002.
  29. @brief gmm interface for STL vectors.
  30. */
  31. #ifndef GMM_INTERFACE_H__
  32. #define GMM_INTERFACE_H__
  33. #include "gmm_blas.h"
  34. #include "gmm_sub_index.h"
  35. namespace gmm {
  36. /* ********************************************************************* */
  37. /* */
  38. /* What is needed for a Vector type : */
  39. /* Vector v(n) defines a vector with n components. */
  40. /* v[i] allows to access to the ith component of v. */
  41. /* linalg_traits<Vector> should be filled with appropriate definitions */
  42. /* */
  43. /* for a dense vector : the minimum is two random iterators (begin and */
  44. /* end) and a pointer to a valid origin. */
  45. /* for a sparse vector : the minimum is two forward iterators, with */
  46. /* a method it.index() which gives the index of */
  47. /* a non zero element, an interface object */
  48. /* should describe the method to add new non */
  49. /* zero element, and a pointer to a valid */
  50. /* origin. */
  51. /* */
  52. /* What is needed for a Matrix type : */
  53. /* Matrix m(n, m) defines a matrix with n rows and m columns. */
  54. /* m(i, j) allows to access to the element at row i and column j. */
  55. /* linalg_traits<Matrix> should be filled with appropriate definitions */
  56. /* */
  57. /* What is needed for an iterator on dense vector */
  58. /* to be standard random access iterator */
  59. /* */
  60. /* What is needed for an iterator on a sparse vector */
  61. /* to be a standard bidirectional iterator */
  62. /* elt should be sorted with increasing indices. */
  63. /* it.index() gives the index of the non-zero element. */
  64. /* */
  65. /* Remark : If original iterators are not convenient, they could be */
  66. /* redefined and interfaced in linalg_traits<Vector> without changing */
  67. /* the original Vector type. */
  68. /* */
  69. /* ********************************************************************* */
  70. /* ********************************************************************* */
  71. /* Simple references on vectors */
  72. /* ********************************************************************* */
  73. template <typename PT> struct simple_vector_ref {
  74. typedef simple_vector_ref<PT> this_type;
  75. typedef typename std::iterator_traits<PT>::value_type V;
  76. typedef V * CPT;
  77. typedef typename std::iterator_traits<PT>::reference ref_V;
  78. typedef typename linalg_traits<this_type>::iterator iterator;
  79. typedef typename linalg_traits<this_type>::reference reference;
  80. typedef typename linalg_traits<this_type>::porigin_type porigin_type;
  81. iterator begin_, end_;
  82. porigin_type origin;
  83. size_type size_;
  84. simple_vector_ref(ref_V v) : begin_(vect_begin(const_cast<V&>(v))),
  85. end_(vect_end(const_cast<V&>(v))),
  86. origin(linalg_origin(const_cast<V&>(v))),
  87. size_(vect_size(v)) {}
  88. simple_vector_ref(const simple_vector_ref<CPT> &cr)
  89. : begin_(cr.begin_),end_(cr.end_),origin(cr.origin),size_(cr.size_) {}
  90. simple_vector_ref(void) {}
  91. reference operator[](size_type i) const
  92. { return linalg_traits<V>::access(origin, begin_, end_, i); }
  93. };
  94. template <typename IT, typename ORG, typename PT> inline
  95. void set_to_begin(IT &it, ORG o, simple_vector_ref<PT> *,linalg_modifiable) {
  96. typedef typename linalg_traits<simple_vector_ref<PT> >::V_reference ref_t;
  97. set_to_begin(it, o, PT(), ref_t());
  98. }
  99. template <typename IT, typename ORG, typename PT> inline
  100. void set_to_begin(IT &it, ORG o, const simple_vector_ref<PT> *,
  101. linalg_modifiable) {
  102. typedef typename linalg_traits<simple_vector_ref<PT> >::V_reference ref_t;
  103. set_to_begin(it, o, PT(), ref_t());
  104. }
  105. template <typename IT, typename ORG, typename PT> inline
  106. void set_to_end(IT &it, ORG o, simple_vector_ref<PT> *, linalg_modifiable) {
  107. typedef typename linalg_traits<simple_vector_ref<PT> >::V_reference ref_t;
  108. set_to_end(it, o, PT(), ref_t());
  109. }
  110. template <typename IT, typename ORG, typename PT> inline
  111. void set_to_end(IT &it, ORG o, const simple_vector_ref<PT> *,
  112. linalg_modifiable) {
  113. typedef typename linalg_traits<simple_vector_ref<PT> >::V_reference ref_t;
  114. set_to_end(it, o, PT(), ref_t());
  115. }
  116. template <typename PT> struct linalg_traits<simple_vector_ref<PT> > {
  117. typedef simple_vector_ref<PT> this_type;
  118. typedef this_type *pthis_type;
  119. typedef typename std::iterator_traits<PT>::value_type V;
  120. typedef typename linalg_traits<V>::origin_type origin_type;
  121. typedef V *pV;
  122. typedef typename linalg_traits<V>::is_reference V_reference;
  123. typedef typename which_reference<PT>::is_reference is_reference;
  124. typedef abstract_vector linalg_type;
  125. typedef typename linalg_traits<V>::value_type value_type;
  126. typedef typename select_ref<value_type, typename
  127. linalg_traits<V>::reference, PT>::ref_type reference;
  128. typedef typename select_ref<const origin_type *, origin_type *,
  129. PT>::ref_type porigin_type;
  130. typedef typename select_ref<typename linalg_traits<V>::const_iterator,
  131. typename linalg_traits<V>::iterator, PT>::ref_type iterator;
  132. typedef typename linalg_traits<V>::const_iterator const_iterator;
  133. typedef typename linalg_traits<V>::storage_type storage_type;
  134. typedef linalg_true index_sorted;
  135. static size_type size(const this_type &v) { return v.size_; }
  136. static inline iterator begin(this_type &v) {
  137. iterator it = v.begin_;
  138. set_to_begin(it, v.origin, pthis_type(), is_reference());
  139. return it;
  140. }
  141. static inline const_iterator begin(const this_type &v) {
  142. const_iterator it = v.begin_;
  143. set_to_begin(it, v.origin, pthis_type(), is_reference());
  144. return it;
  145. }
  146. static inline iterator end(this_type &v) {
  147. iterator it = v.end_;
  148. set_to_end(it, v.origin, pthis_type(), is_reference());
  149. return it;
  150. }
  151. static inline const_iterator end(const this_type &v) {
  152. const_iterator it = v.end_;
  153. set_to_end(it, v.origin, pthis_type(), is_reference());
  154. return it;
  155. }
  156. static origin_type* origin(this_type &v) { return v.origin; }
  157. static const origin_type* origin(const this_type &v) { return v.origin; }
  158. static void clear(origin_type* o, const iterator &it, const iterator &ite)
  159. { linalg_traits<V>::clear(o, it, ite); }
  160. static void do_clear(this_type &v) { clear(v.origin, v.begin_, v.end_); }
  161. static value_type access(const origin_type *o, const const_iterator &it,
  162. const const_iterator &ite, size_type i)
  163. { return linalg_traits<V>::access(o, it, ite, i); }
  164. static reference access(origin_type *o, const iterator &it,
  165. const iterator &ite, size_type i)
  166. { return linalg_traits<V>::access(o, it, ite, i); }
  167. };
  168. template <typename PT>
  169. std::ostream &operator << (std::ostream &o, const simple_vector_ref<PT>& v)
  170. { gmm::write(o,v); return o; }
  171. template <typename T, typename alloc>
  172. simple_vector_ref<const std::vector<T,alloc> *>
  173. vref(const std::vector<T, alloc> &vv)
  174. { return simple_vector_ref<const std::vector<T,alloc> *>(vv); }
  175. /* ********************************************************************* */
  176. /* */
  177. /* Traits for S.T.L. object */
  178. /* */
  179. /* ********************************************************************* */
  180. template <typename T, typename alloc>
  181. struct linalg_traits<std::vector<T, alloc> > {
  182. typedef std::vector<T, alloc> this_type;
  183. typedef this_type origin_type;
  184. typedef linalg_false is_reference;
  185. typedef abstract_vector linalg_type;
  186. typedef T value_type;
  187. typedef T& reference;
  188. typedef typename this_type::iterator iterator;
  189. typedef typename this_type::const_iterator const_iterator;
  190. typedef abstract_dense storage_type;
  191. typedef linalg_true index_sorted;
  192. static size_type size(const this_type &v) { return v.size(); }
  193. static iterator begin(this_type &v) { return v.begin(); }
  194. static const_iterator begin(const this_type &v) { return v.begin(); }
  195. static iterator end(this_type &v) { return v.end(); }
  196. static const_iterator end(const this_type &v) { return v.end(); }
  197. static origin_type* origin(this_type &v) { return &v; }
  198. static const origin_type* origin(const this_type &v) { return &v; }
  199. static void clear(origin_type*, const iterator &it, const iterator &ite)
  200. { std::fill(it, ite, value_type(0)); }
  201. static void do_clear(this_type &v) { std::fill(v.begin(), v.end(), T(0)); }
  202. static value_type access(const origin_type *, const const_iterator &it,
  203. const const_iterator &, size_type i)
  204. { return it[i]; }
  205. static reference access(origin_type *, const iterator &it,
  206. const iterator &, size_type i)
  207. { return it[i]; }
  208. static void resize(this_type &v, size_type n) { v.resize(n); }
  209. };
  210. template <typename T>
  211. inline size_type nnz(const std::vector<T>& l) { return l.size(); }
  212. /* ********************************************************************* */
  213. /* */
  214. /* Traits for ref objects */
  215. /* */
  216. /* ********************************************************************* */
  217. template <typename IT, typename V>
  218. struct tab_ref_with_origin : public gmm::tab_ref<IT> {
  219. typedef tab_ref_with_origin<IT, V> this_type;
  220. // next line replaced by the 4 following lines in order to please aCC
  221. //typedef typename linalg_traits<this_type>::porigin_type porigin_type;
  222. typedef typename linalg_traits<V>::origin_type origin_type;
  223. typedef typename std::iterator_traits<IT>::pointer PT;
  224. typedef typename select_ref<const origin_type *, origin_type *,
  225. PT>::ref_type porigin_type;
  226. porigin_type origin;
  227. tab_ref_with_origin(void) {}
  228. template <class PT> tab_ref_with_origin(const IT &b, const IT &e, PT p)
  229. : gmm::tab_ref<IT>(b,e), origin(porigin_type(p)) {}
  230. tab_ref_with_origin(const IT &b, const IT &e, porigin_type p)
  231. : gmm::tab_ref<IT>(b,e), origin(p) {}
  232. tab_ref_with_origin(const V &v, const sub_interval &si)
  233. : gmm::tab_ref<IT>(vect_begin(const_cast<V&>(v))+si.min,
  234. vect_begin(const_cast<V&>(v))+si.max),
  235. origin(linalg_origin(const_cast<V&>(v))) {}
  236. tab_ref_with_origin(V &v, const sub_interval &si)
  237. : gmm::tab_ref<IT>(vect_begin(const_cast<V&>(v))+si.min,
  238. vect_begin(const_cast<V&>(v))+si.max),
  239. origin(linalg_origin(const_cast<V&>(v))) {}
  240. };
  241. template <typename IT, typename V>
  242. struct linalg_traits<tab_ref_with_origin<IT, V> > {
  243. typedef typename std::iterator_traits<IT>::pointer PT;
  244. typedef typename linalg_traits<V>::origin_type origin_type;
  245. typedef tab_ref_with_origin<IT, V> this_type;
  246. typedef typename which_reference<PT>::is_reference is_reference;
  247. typedef abstract_vector linalg_type;
  248. typedef typename select_ref<const origin_type *, origin_type *,
  249. PT>::ref_type porigin_type;
  250. typedef typename std::iterator_traits<IT>::value_type value_type;
  251. typedef typename std::iterator_traits<IT>::reference reference;
  252. typedef typename this_type::iterator iterator;
  253. typedef typename this_type::iterator const_iterator;
  254. typedef abstract_dense storage_type;
  255. typedef linalg_true index_sorted;
  256. static size_type size(const this_type &v) { return v.size(); }
  257. static iterator begin(this_type &v) { return v.begin(); }
  258. static const_iterator begin(const this_type &v) { return v.begin(); }
  259. static iterator end(this_type &v) { return v.end(); }
  260. static const_iterator end(const this_type &v) { return v.end(); }
  261. static origin_type* origin(this_type &v) { return v.origin; }
  262. static const origin_type* origin(const this_type &v) { return v.origin; }
  263. static void clear(origin_type*, const iterator &it, const iterator &ite)
  264. { std::fill(it, ite, value_type(0)); }
  265. static inline void do_clear(this_type &v)
  266. { std::fill(v.begin(), v.end(), value_type(0)); }
  267. static value_type access(const origin_type *, const const_iterator &it,
  268. const const_iterator &, size_type i)
  269. { return it[i]; }
  270. static reference access(origin_type *, const iterator &it,
  271. const iterator &, size_type i)
  272. { return it[i]; }
  273. };
  274. template <typename IT, typename V> std::ostream &operator <<
  275. (std::ostream &o, const tab_ref_with_origin<IT, V>& m)
  276. { gmm::write(o,m); return o; }
  277. template <typename IT, typename V>
  278. struct tab_ref_reg_spaced_with_origin : public gmm::tab_ref_reg_spaced<IT> {
  279. typedef tab_ref_reg_spaced_with_origin<IT, V> this_type;
  280. typedef typename linalg_traits<this_type>::porigin_type porigin_type;
  281. porigin_type origin;
  282. tab_ref_reg_spaced_with_origin(void) {}
  283. tab_ref_reg_spaced_with_origin(const IT &b, size_type n, size_type s,
  284. const porigin_type p)
  285. : gmm::tab_ref_reg_spaced<IT>(b,n,s), origin(p) {}
  286. tab_ref_reg_spaced_with_origin(const V &v, const sub_slice &si)
  287. : gmm::tab_ref_reg_spaced<IT>(vect_begin(const_cast<V&>(v)) + si.min,
  288. si.N, (si.max - si.min)/si.N),
  289. origin(linalg_origin(const_cast<V&>(v))) {}
  290. tab_ref_reg_spaced_with_origin(V &v, const sub_slice &si)
  291. : gmm::tab_ref_reg_spaced<IT>(vect_begin(const_cast<V&>(v)) + si.min,
  292. si.N, (si.max - si.min)/si.N),
  293. origin(linalg_origin(const_cast<V&>(v))) {}
  294. };
  295. template <typename IT, typename V>
  296. struct linalg_traits<tab_ref_reg_spaced_with_origin<IT, V> > {
  297. typedef typename std::iterator_traits<IT>::pointer PT;
  298. typedef tab_ref_reg_spaced_with_origin<IT, V> this_type;
  299. typedef typename linalg_traits<V>::origin_type origin_type;
  300. typedef typename select_ref<const origin_type *, origin_type *,
  301. PT>::ref_type porigin_type;
  302. typedef typename which_reference<PT>::is_reference is_reference;
  303. typedef abstract_vector linalg_type;
  304. typedef typename std::iterator_traits<IT>::value_type value_type;
  305. typedef typename std::iterator_traits<IT>::reference reference;
  306. typedef typename this_type::iterator iterator;
  307. typedef typename this_type::iterator const_iterator;
  308. typedef abstract_dense storage_type;
  309. typedef linalg_true index_sorted;
  310. static size_type size(const this_type &v) { return v.size(); }
  311. static iterator begin(this_type &v) { return v.begin(); }
  312. static const_iterator begin(const this_type &v) { return v.begin(); }
  313. static iterator end(this_type &v) { return v.end(); }
  314. static const_iterator end(const this_type &v) { return v.end(); }
  315. static origin_type* origin(this_type &v) { return v.origin; }
  316. static const origin_type* origin(const this_type &v) { return v.origin; }
  317. static void clear(origin_type*, const iterator &it, const iterator &ite)
  318. { std::fill(it, ite, value_type(0)); }
  319. static void do_clear(this_type &v)
  320. { std::fill(v.begin(), v.end(), value_type(0)); }
  321. static value_type access(const origin_type *, const const_iterator &it,
  322. const const_iterator &, size_type i)
  323. { return it[i]; }
  324. static reference access(origin_type *, const iterator &it,
  325. const iterator &, size_type i)
  326. { return it[i]; }
  327. };
  328. template <typename IT, typename V> std::ostream &operator <<
  329. (std::ostream &o, const tab_ref_reg_spaced_with_origin<IT, V>& m)
  330. { gmm::write(o,m); return o; }
  331. template <typename IT, typename ITINDEX, typename V>
  332. struct tab_ref_index_ref_with_origin
  333. : public gmm::tab_ref_index_ref<IT, ITINDEX> {
  334. typedef tab_ref_index_ref_with_origin<IT, ITINDEX, V> this_type;
  335. typedef typename linalg_traits<this_type>::porigin_type porigin_type;
  336. porigin_type origin;
  337. tab_ref_index_ref_with_origin(void) {}
  338. tab_ref_index_ref_with_origin(const IT &b, const ITINDEX &bi,
  339. const ITINDEX &ei, porigin_type p)
  340. : gmm::tab_ref_index_ref<IT, ITINDEX>(b, bi, ei), origin(p) {}
  341. tab_ref_index_ref_with_origin(const V &v, const sub_index &si)
  342. : gmm::tab_ref_index_ref<IT, ITINDEX>(vect_begin(const_cast<V&>(v)),
  343. si.begin(), si.end()),
  344. origin(linalg_origin(const_cast<V&>(v))) {}
  345. tab_ref_index_ref_with_origin(V &v, const sub_index &si)
  346. : gmm::tab_ref_index_ref<IT, ITINDEX>(vect_begin(const_cast<V&>(v)),
  347. si.begin(), si.end()),
  348. origin(linalg_origin(const_cast<V&>(v))) {}
  349. };
  350. template <typename IT, typename ITINDEX, typename V>
  351. struct linalg_traits<tab_ref_index_ref_with_origin<IT, ITINDEX, V> > {
  352. typedef typename std::iterator_traits<IT>::pointer PT;
  353. typedef tab_ref_index_ref_with_origin<IT, ITINDEX, V> this_type;
  354. typedef typename linalg_traits<V>::origin_type origin_type;
  355. typedef typename select_ref<const origin_type *, origin_type *,
  356. PT>::ref_type porigin_type;
  357. typedef typename which_reference<PT>::is_reference is_reference;
  358. typedef abstract_vector linalg_type;
  359. typedef typename std::iterator_traits<IT>::value_type value_type;
  360. typedef typename std::iterator_traits<IT>::reference reference;
  361. typedef typename this_type::iterator iterator;
  362. typedef typename this_type::iterator const_iterator;
  363. typedef abstract_dense storage_type;
  364. typedef linalg_true index_sorted;
  365. static size_type size(const this_type &v) { return v.size(); }
  366. static iterator begin(this_type &v) { return v.begin(); }
  367. static const_iterator begin(const this_type &v) { return v.begin(); }
  368. static iterator end(this_type &v) { return v.end(); }
  369. static const_iterator end(const this_type &v) { return v.end(); }
  370. static origin_type* origin(this_type &v) { return v.origin; }
  371. static const origin_type* origin(const this_type &v) { return v.origin; }
  372. static void clear(origin_type*, const iterator &it, const iterator &ite)
  373. { std::fill(it, ite, value_type(0)); }
  374. static void do_clear(this_type &v)
  375. { std::fill(v.begin(), v.end(), value_type(0)); }
  376. static value_type access(const origin_type *, const const_iterator &it,
  377. const const_iterator &, size_type i)
  378. { return it[i]; }
  379. static reference access(origin_type *, const iterator &it,
  380. const iterator &, size_type i)
  381. { return it[i]; }
  382. };
  383. template <typename IT, typename ITINDEX, typename V>
  384. std::ostream &operator <<
  385. (std::ostream &o, const tab_ref_index_ref_with_origin<IT, ITINDEX, V>& m)
  386. { gmm::write(o,m); return o; }
  387. template<typename ITER, typename MIT, typename PT>
  388. struct dense_compressed_iterator {
  389. typedef ITER value_type;
  390. typedef ITER *pointer;
  391. typedef ITER &reference;
  392. typedef ptrdiff_t difference_type;
  393. typedef std::random_access_iterator_tag iterator_category;
  394. typedef size_t size_type;
  395. typedef dense_compressed_iterator<ITER, MIT, PT> iterator;
  396. typedef typename std::iterator_traits<PT>::value_type *MPT;
  397. ITER it;
  398. size_type N, nrows, ncols, i;
  399. PT origin;
  400. iterator operator ++(int) { iterator tmp = *this; i++; return tmp; }
  401. iterator operator --(int) { iterator tmp = *this; i--; return tmp; }
  402. iterator &operator ++() { ++i; return *this; }
  403. iterator &operator --() { --i; return *this; }
  404. iterator &operator +=(difference_type ii) { i += ii; return *this; }
  405. iterator &operator -=(difference_type ii) { i -= ii; return *this; }
  406. iterator operator +(difference_type ii) const
  407. { iterator itt = *this; return (itt += ii); }
  408. iterator operator -(difference_type ii) const
  409. { iterator itt = *this; return (itt -= ii); }
  410. difference_type operator -(const iterator &ii) const
  411. { return (N ? (it - ii.it) / N : 0) + i - ii.i; }
  412. ITER operator *() const { return it+i*N; }
  413. ITER operator [](int ii) const { return it + (i+ii) * N; }
  414. bool operator ==(const iterator &ii) const
  415. { return (*this - ii) == difference_type(0); }
  416. bool operator !=(const iterator &ii) const { return !(ii == *this); }
  417. bool operator < (const iterator &ii) const
  418. { return (*this - ii) < difference_type(0); }
  419. dense_compressed_iterator(void) {}
  420. dense_compressed_iterator(const dense_compressed_iterator<MIT,MIT,MPT> &ii)
  421. : it(ii.it), N(ii.N), nrows(ii.nrows), ncols(ii.ncols), i(ii.i),
  422. origin(ii.origin) {}
  423. dense_compressed_iterator(const ITER &iter, size_type n, size_type r,
  424. size_type c, size_type ii, PT o)
  425. : it(iter), N(n), nrows(r), ncols(c), i(ii), origin(o) { }
  426. };
  427. /* ******************************************************************** */
  428. /* Read only reference on a compressed sparse vector */
  429. /* ******************************************************************** */
  430. template <typename PT1, typename PT2, int shift = 0>
  431. struct cs_vector_ref_iterator {
  432. PT1 pr;
  433. PT2 ir;
  434. typedef typename std::iterator_traits<PT1>::value_type value_type;
  435. typedef PT1 pointer;
  436. typedef typename std::iterator_traits<PT1>::reference reference;
  437. typedef size_t size_type;
  438. typedef ptrdiff_t difference_type;
  439. typedef std::bidirectional_iterator_tag iterator_category;
  440. typedef cs_vector_ref_iterator<PT1, PT2, shift> iterator;
  441. cs_vector_ref_iterator(void) {}
  442. cs_vector_ref_iterator(PT1 p1, PT2 p2) : pr(p1), ir(p2) {}
  443. inline size_type index(void) const { return (*ir) - shift; }
  444. iterator &operator ++() { ++pr; ++ir; return *this; }
  445. iterator operator ++(int) { iterator tmp = *this; ++(*this); return tmp; }
  446. iterator &operator --() { --pr; --ir; return *this; }
  447. iterator operator --(int) { iterator tmp = *this; --(*this); return tmp; }
  448. reference operator *() const { return *pr; }
  449. pointer operator ->() const { return pr; }
  450. bool operator ==(const iterator &i) const { return (i.pr==pr);}
  451. bool operator !=(const iterator &i) const { return (i.pr!=pr);}
  452. };
  453. template <typename PT1, typename PT2, int shift = 0> struct cs_vector_ref {
  454. PT1 pr;
  455. PT2 ir;
  456. size_type n, size_;
  457. typedef cs_vector_ref<PT1, PT2, shift> this_type;
  458. typedef typename std::iterator_traits<PT1>::value_type value_type;
  459. typedef typename linalg_traits<this_type>::const_iterator const_iterator;
  460. cs_vector_ref(PT1 pt1, PT2 pt2, size_type nnz, size_type ns)
  461. : pr(pt1), ir(pt2), n(nnz), size_(ns) {}
  462. cs_vector_ref(void) {}
  463. size_type size(void) const { return size_; }
  464. const_iterator begin(void) const { return const_iterator(pr, ir); }
  465. const_iterator end(void) const { return const_iterator(pr+n, ir+n); }
  466. value_type operator[](size_type i) const
  467. { return linalg_traits<this_type>::access(pr, begin(), end(),i); }
  468. };
  469. template <typename PT1, typename PT2, int shift>
  470. struct linalg_traits<cs_vector_ref<PT1, PT2, shift> > {
  471. typedef cs_vector_ref<PT1, PT2, shift> this_type;
  472. typedef linalg_const is_reference;
  473. typedef abstract_vector linalg_type;
  474. typedef typename std::iterator_traits<PT1>::value_type value_type;
  475. typedef value_type origin_type;
  476. typedef typename std::iterator_traits<PT1>::value_type reference;
  477. typedef cs_vector_ref_iterator<typename const_pointer<PT1>::pointer,
  478. typename const_pointer<PT2>::pointer, shift> const_iterator;
  479. typedef abstract_null_type iterator;
  480. typedef abstract_sparse storage_type;
  481. typedef linalg_true index_sorted;
  482. static size_type size(const this_type &v) { return v.size(); }
  483. static iterator begin(this_type &v) { return v.begin(); }
  484. static const_iterator begin(const this_type &v) { return v.begin(); }
  485. static iterator end(this_type &v) { return v.end(); }
  486. static const_iterator end(const this_type &v) { return v.end(); }
  487. static const origin_type* origin(const this_type &v) { return v.pr; }
  488. static value_type access(const origin_type *, const const_iterator &b,
  489. const const_iterator &e, size_type i) {
  490. if (b.ir == e.ir) return value_type(0);
  491. PT2 p = std::lower_bound(b.ir, e.ir, i+shift);
  492. return (*p == i+shift && p != e.ir) ? b.pr[p-b.ir] : value_type(0);
  493. }
  494. };
  495. template <typename PT1, typename PT2, int shift>
  496. std::ostream &operator <<
  497. (std::ostream &o, const cs_vector_ref<PT1, PT2, shift>& m)
  498. { gmm::write(o,m); return o; }
  499. template <typename PT1, typename PT2, int shift>
  500. inline size_type nnz(const cs_vector_ref<PT1, PT2, shift>& l) { return l.n; }
  501. /* ******************************************************************** */
  502. /* Read only reference on a compressed sparse column matrix */
  503. /* ******************************************************************** */
  504. template <typename PT1, typename PT2, typename PT3, int shift = 0>
  505. struct sparse_compressed_iterator {
  506. typedef typename std::iterator_traits<PT1>::value_type value_type;
  507. typedef const value_type *pointer;
  508. typedef const value_type &reference;
  509. typedef ptrdiff_t difference_type;
  510. typedef size_t size_type;
  511. typedef std::random_access_iterator_tag iterator_category;
  512. typedef sparse_compressed_iterator<PT1, PT2, PT3, shift> iterator;
  513. PT1 pr;
  514. PT2 ir;
  515. PT3 jc;
  516. size_type n;
  517. const value_type *origin;
  518. iterator operator ++(int) { iterator tmp = *this; jc++; return tmp; }
  519. iterator operator --(int) { iterator tmp = *this; jc--; return tmp; }
  520. iterator &operator ++() { jc++; return *this; }
  521. iterator &operator --() { jc--; return *this; }
  522. iterator &operator +=(difference_type i) { jc += i; return *this; }
  523. iterator &operator -=(difference_type i) { jc -= i; return *this; }
  524. iterator operator +(difference_type i) const
  525. { iterator itt = *this; return (itt += i); }
  526. iterator operator -(difference_type i) const
  527. { iterator itt = *this; return (itt -= i); }
  528. difference_type operator -(const iterator &i) const { return jc - i.jc; }
  529. reference operator *() const { return pr + *jc - shift; }
  530. reference operator [](int ii) { return pr + *(jc+ii) - shift; }
  531. bool operator ==(const iterator &i) const { return (jc == i.jc); }
  532. bool operator !=(const iterator &i) const { return !(i == *this); }
  533. bool operator < (const iterator &i) const { return (jc < i.jc); }
  534. sparse_compressed_iterator(void) {}
  535. sparse_compressed_iterator(PT1 p1, PT2 p2, PT3 p3, size_type nn,
  536. const value_type *o)
  537. : pr(p1), ir(p2), jc(p3), n(nn), origin(o) { }
  538. };
  539. template <typename PT1, typename PT2, typename PT3, int shift = 0>
  540. struct csc_matrix_ref {
  541. PT1 pr; // values.
  542. PT2 ir; // row indexes.
  543. PT3 jc; // column repartition on pr and ir.
  544. size_type nc, nr;
  545. typedef typename std::iterator_traits<PT1>::value_type value_type;
  546. csc_matrix_ref(PT1 pt1, PT2 pt2, PT3 pt3, size_type nrr, size_type ncc)
  547. : pr(pt1), ir(pt2), jc(pt3), nc(ncc), nr(nrr) {}
  548. csc_matrix_ref(void) {}
  549. size_type nrows(void) const { return nr; }
  550. size_type ncols(void) const { return nc; }
  551. value_type operator()(size_type i, size_type j) const
  552. { return mat_col(*this, j)[i]; }
  553. };
  554. template <typename PT1, typename PT2, typename PT3, int shift>
  555. struct linalg_traits<csc_matrix_ref<PT1, PT2, PT3, shift> > {
  556. typedef csc_matrix_ref<PT1, PT2, PT3, shift> this_type;
  557. typedef linalg_const is_reference;
  558. typedef abstract_matrix linalg_type;
  559. typedef typename std::iterator_traits<PT1>::value_type value_type;
  560. typedef typename std::iterator_traits<PT1>::value_type reference;
  561. typedef value_type origin_type;
  562. typedef abstract_sparse storage_type;
  563. typedef abstract_null_type sub_row_type;
  564. typedef abstract_null_type const_sub_row_type;
  565. typedef abstract_null_type row_iterator;
  566. typedef abstract_null_type const_row_iterator;
  567. typedef abstract_null_type sub_col_type;
  568. typedef cs_vector_ref<typename const_pointer<PT1>::pointer,
  569. typename const_pointer<PT2>::pointer, shift> const_sub_col_type;
  570. typedef sparse_compressed_iterator<typename const_pointer<PT1>::pointer,
  571. typename const_pointer<PT2>::pointer,
  572. typename const_pointer<PT3>::pointer,
  573. shift> const_col_iterator;
  574. typedef abstract_null_type col_iterator;
  575. typedef col_major sub_orientation;
  576. typedef linalg_true index_sorted;
  577. static size_type nrows(const this_type &m) { return m.nrows(); }
  578. static size_type ncols(const this_type &m) { return m.ncols(); }
  579. static const_col_iterator col_begin(const this_type &m)
  580. { return const_col_iterator(m.pr, m.ir, m.jc, m.nr, m.pr); }
  581. static const_col_iterator col_end(const this_type &m)
  582. { return const_col_iterator(m.pr, m.ir, m.jc + m.nc, m.nr, m.pr); }
  583. static const_sub_col_type col(const const_col_iterator &it) {
  584. return const_sub_col_type(it.pr + *(it.jc) - shift,
  585. it.ir + *(it.jc) - shift, *(it.jc + 1) - *(it.jc), it.n);
  586. }
  587. static const origin_type* origin(const this_type &m) { return m.pr; }
  588. static value_type access(const const_col_iterator &itcol, size_type j)
  589. { return col(itcol)[j]; }
  590. };
  591. template <typename PT1, typename PT2, typename PT3, int shift>
  592. std::ostream &operator <<
  593. (std::ostream &o, const csc_matrix_ref<PT1, PT2, PT3, shift>& m)
  594. { gmm::write(o,m); return o; }
  595. /* ******************************************************************** */
  596. /* Read only reference on a compressed sparse row matrix */
  597. /* ******************************************************************** */
  598. template <typename PT1, typename PT2, typename PT3, int shift = 0>
  599. struct csr_matrix_ref {
  600. PT1 pr; // values.
  601. PT2 ir; // column indexes.
  602. PT3 jc; // row repartition on pr and ir.
  603. size_type nc, nr;
  604. typedef typename std::iterator_traits<PT1>::value_type value_type;
  605. csr_matrix_ref(PT1 pt1, PT2 pt2, PT3 pt3, size_type nrr, size_type ncc)
  606. : pr(pt1), ir(pt2), jc(pt3), nc(ncc), nr(nrr) {}
  607. csr_matrix_ref(void) {}
  608. size_type nrows(void) const { return nr; }
  609. size_type ncols(void) const { return nc; }
  610. value_type operator()(size_type i, size_type j) const
  611. { return mat_row(*this, i)[j]; }
  612. };
  613. template <typename PT1, typename PT2, typename PT3, int shift>
  614. struct linalg_traits<csr_matrix_ref<PT1, PT2, PT3, shift> > {
  615. typedef csr_matrix_ref<PT1, PT2, PT3, shift> this_type;
  616. typedef linalg_const is_reference;
  617. typedef abstract_matrix linalg_type;
  618. typedef typename std::iterator_traits<PT1>::value_type value_type;
  619. typedef typename std::iterator_traits<PT1>::value_type reference;
  620. typedef value_type origin_type;
  621. typedef abstract_sparse storage_type;
  622. typedef abstract_null_type sub_col_type;
  623. typedef abstract_null_type const_sub_col_type;
  624. typedef abstract_null_type col_iterator;
  625. typedef abstract_null_type const_col_iterator;
  626. typedef abstract_null_type sub_row_type;
  627. typedef cs_vector_ref<typename const_pointer<PT1>::pointer,
  628. typename const_pointer<PT2>::pointer, shift>
  629. const_sub_row_type;
  630. typedef sparse_compressed_iterator<typename const_pointer<PT1>::pointer,
  631. typename const_pointer<PT2>::pointer,
  632. typename const_pointer<PT3>::pointer,
  633. shift> const_row_iterator;
  634. typedef abstract_null_type row_iterator;
  635. typedef row_major sub_orientation;
  636. typedef linalg_true index_sorted;
  637. static size_type nrows(const this_type &m) { return m.nrows(); }
  638. static size_type ncols(const this_type &m) { return m.ncols(); }
  639. static const_row_iterator row_begin(const this_type &m)
  640. { return const_row_iterator(m.pr, m.ir, m.jc, m.nc, m.pr); }
  641. static const_row_iterator row_end(const this_type &m)
  642. { return const_row_iterator(m.pr, m.ir, m.jc + m.nr, m.nc, m.pr); }
  643. static const_sub_row_type row(const const_row_iterator &it) {
  644. return const_sub_row_type(it.pr + *(it.jc) - shift,
  645. it.ir + *(it.jc) - shift, *(it.jc + 1) - *(it.jc), it.n);
  646. }
  647. static const origin_type* origin(const this_type &m) { return m.pr; }
  648. static value_type access(const const_row_iterator &itrow, size_type j)
  649. { return row(itrow)[j]; }
  650. };
  651. template <typename PT1, typename PT2, typename PT3, int shift>
  652. std::ostream &operator <<
  653. (std::ostream &o, const csr_matrix_ref<PT1, PT2, PT3, shift>& m)
  654. { gmm::write(o,m); return o; }
  655. /* ********************************************************************* */
  656. /* */
  657. /* Simple interface for C arrays */
  658. /* */
  659. /* ********************************************************************* */
  660. template <class PT> struct array1D_reference {
  661. typedef typename std::iterator_traits<PT>::value_type value_type;
  662. PT begin, end;
  663. const value_type &operator[](size_type i) const { return *(begin+i); }
  664. value_type &operator[](size_type i) { return *(begin+i); }
  665. array1D_reference(PT begin_, size_type s) : begin(begin_), end(begin_+s) {}
  666. };
  667. template <typename PT>
  668. struct linalg_traits<array1D_reference<PT> > {
  669. typedef array1D_reference<PT> this_type;
  670. typedef this_type origin_type;
  671. typedef typename which_reference<PT>::is_reference is_reference;
  672. typedef abstract_vector linalg_type;
  673. typedef typename std::iterator_traits<PT>::value_type value_type;
  674. typedef typename std::iterator_traits<PT>::reference reference;
  675. typedef PT iterator;
  676. typedef PT const_iterator;
  677. typedef abstract_dense storage_type;
  678. typedef linalg_true index_sorted;
  679. static size_type size(const this_type &v) { return v.end - v.begin; }
  680. static iterator begin(this_type &v) { return v.begin; }
  681. static const_iterator begin(const this_type &v) { return v.begin; }
  682. static iterator end(this_type &v) { return v.end; }
  683. static const_iterator end(const this_type &v) { return v.end; }
  684. static origin_type* origin(this_type &v) { return &v; }
  685. static const origin_type* origin(const this_type &v) { return &v; }
  686. static void clear(origin_type*, const iterator &it, const iterator &ite)
  687. { std::fill(it, ite, value_type(0)); }
  688. static void do_clear(this_type &v)
  689. { std::fill(v.begin, v.end, value_type(0)); }
  690. static value_type access(const origin_type *, const const_iterator &it,
  691. const const_iterator &, size_type i)
  692. { return it[i]; }
  693. static reference access(origin_type *, const iterator &it,
  694. const iterator &, size_type i)
  695. { return it[i]; }
  696. static void resize(this_type &, size_type )
  697. { GMM_ASSERT1(false, "Not resizable vector"); }
  698. };
  699. template<typename PT> std::ostream &operator <<
  700. (std::ostream &o, const array1D_reference<PT>& v)
  701. { gmm::write(o,v); return o; }
  702. template <class PT> struct array2D_col_reference {
  703. typedef typename std::iterator_traits<PT>::value_type T;
  704. typedef typename std::iterator_traits<PT>::reference reference;
  705. typedef typename const_reference<reference>::reference const_reference;
  706. typedef PT iterator;
  707. typedef typename const_pointer<PT>::pointer const_iterator;
  708. PT begin_;
  709. size_type nbl, nbc;
  710. inline const_reference operator ()(size_type l, size_type c) const {
  711. GMM_ASSERT2(l < nbl && c < nbc, "out of range");
  712. return *(begin_ + c*nbl+l);
  713. }
  714. inline reference operator ()(size_type l, size_type c) {
  715. GMM_ASSERT2(l < nbl && c < nbc, "out of range");
  716. return *(begin_ + c*nbl+l);
  717. }
  718. void resize(size_type, size_type);
  719. void reshape(size_type m, size_type n) {
  720. GMM_ASSERT2(n*m == nbl*nbc, "dimensions mismatch");
  721. nbl = m; nbc = n;
  722. }
  723. void fill(T a, T b = T(0)) {
  724. std::fill(begin_, begin_+nbc*nbl, b);
  725. iterator p = begin_, e = begin_+nbc*nbl;
  726. while (p < e) { *p = a; p += nbl+1; }
  727. }
  728. inline size_type nrows(void) const { return nbl; }
  729. inline size_type ncols(void) const { return nbc; }
  730. iterator begin(void) { return begin_; }
  731. const_iterator begin(void) const { return begin_; }
  732. iterator end(void) { return begin_+nbl*nbc; }
  733. const_iterator end(void) const { return begin_+nbl*nbc; }
  734. array2D_col_reference(PT begin__, size_type nrows_, size_type ncols_)
  735. : begin_(begin__), nbl(nrows_), nbc(ncols_) {}
  736. };
  737. template <typename PT> struct linalg_traits<array2D_col_reference<PT> > {
  738. typedef array2D_col_reference<PT> this_type;
  739. typedef this_type origin_type;
  740. typedef typename which_reference<PT>::is_reference is_reference;
  741. typedef abstract_matrix linalg_type;
  742. typedef typename std::iterator_traits<PT>::value_type value_type;
  743. typedef typename std::iterator_traits<PT>::reference reference;
  744. typedef abstract_dense storage_type;
  745. typedef tab_ref_reg_spaced_with_origin<typename this_type::iterator,
  746. this_type> sub_row_type;
  747. typedef tab_ref_reg_spaced_with_origin<typename this_type::const_iterator,
  748. this_type> const_sub_row_type;
  749. typedef dense_compressed_iterator<typename this_type::iterator,
  750. typename this_type::iterator,
  751. this_type *> row_iterator;
  752. typedef dense_compressed_iterator<typename this_type::const_iterator,
  753. typename this_type::iterator,
  754. const this_type *> const_row_iterator;
  755. typedef tab_ref_with_origin<typename this_type::iterator,
  756. this_type> sub_col_type;
  757. typedef tab_ref_with_origin<typename this_type::const_iterator,
  758. this_type> const_sub_col_type;
  759. typedef dense_compressed_iterator<typename this_type::iterator,
  760. typename this_type::iterator,
  761. this_type *> col_iterator;
  762. typedef dense_compressed_iterator<typename this_type::const_iterator,
  763. typename this_type::iterator,
  764. const this_type *> const_col_iterator;
  765. typedef col_and_row sub_orientation;
  766. typedef linalg_true index_sorted;
  767. static size_type nrows(const this_type &m) { return m.nrows(); }
  768. static size_type ncols(const this_type &m) { return m.ncols(); }
  769. static const_sub_row_type row(const const_row_iterator &it)
  770. { return const_sub_row_type(*it, it.nrows, it.ncols, it.origin); }
  771. static const_sub_col_type col(const const_col_iterator &it)
  772. { return const_sub_col_type(*it, *it + it.nrows, it.origin); }
  773. static sub_row_type row(const row_iterator &it)
  774. { return sub_row_type(*it, it.nrows, it.ncols, it.origin); }
  775. static sub_col_type col(const col_iterator &it)
  776. { return sub_col_type(*it, *it + it.nrows, it.origin); }
  777. static row_iterator row_begin(this_type &m)
  778. { return row_iterator(m.begin(), 1, m.nrows(), m.ncols(), 0, &m); }
  779. static row_iterator row_end(this_type &m)
  780. { return row_iterator(m.begin(), 1, m.nrows(), m.ncols(), m.nrows(), &m); }
  781. static const_row_iterator row_begin(const this_type &m)
  782. { return const_row_iterator(m.begin(), 1, m.nrows(), m.ncols(), 0, &m); }
  783. static const_row_iterator row_end(const this_type &m) {
  784. return const_row_iterator(m.begin(), 1, m.nrows(),
  785. m.ncols(), m.nrows(), &m);
  786. }
  787. static col_iterator col_begin(this_type &m)
  788. { return col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(), 0, &m); }
  789. static col_iterator col_end(this_type &m) {
  790. return col_iterator(m.begin(), m.nrows(), m.nrows(), m.ncols(),
  791. m.ncols(), &m);
  792. }
  793. static const_col_iterator col_begin(const this_type &m) {
  794. return const_col_iterator(m.begin(), m.nrows(), m.nrows(),
  795. m.ncols(), 0, &m);
  796. }
  797. static const_col_iterator col_end(const this_type &m) {
  798. return const_col_iterator(m.begin(), m.nrows(),m.nrows(),m.ncols(),
  799. m.ncols(), &m);
  800. }
  801. static origin_type* origin(this_type &m) { return &m; }
  802. static const origin_type* origin(const this_type &m) { return &m; }
  803. static void do_clear(this_type &m) { m.fill(value_type(0)); }
  804. static value_type access(const const_col_iterator &itcol, size_type j)
  805. { return (*itcol)[j]; }
  806. static reference access(const col_iterator &itcol, size_type j)
  807. { return (*itcol)[j]; }
  808. static void resize(this_type &v, size_type m, size_type n)
  809. { v.resize(m,n); }
  810. static void reshape(this_type &v, size_type m, size_type n)
  811. { v.reshape(m, n); }
  812. };
  813. template<typename PT> std::ostream &operator <<
  814. (std::ostream &o, const array2D_col_reference<PT>& m)
  815. { gmm::write(o,m); return o; }
  816. template <class PT> struct array2D_row_reference {
  817. typedef typename std::iterator_traits<PT>::value_type T;
  818. typedef typename std::iterator_traits<PT>::reference reference;
  819. typedef typename const_reference<reference>::reference const_reference;
  820. typedef PT iterator;
  821. typedef typename const_pointer<PT>::pointer const_iterator;
  822. PT begin_;
  823. size_type nbl, nbc;
  824. inline const_reference operator ()(size_type l, size_type c) const {
  825. GMM_ASSERT2(l < nbl && c < nbc, "out of range");
  826. return *(begin_ + l*nbc+c);
  827. }
  828. inline reference operator ()(size_type l, size_type c) {
  829. GMM_ASSERT2(l < nbl && c < nbc, "out of range");
  830. return *(begin_ + l*nbc+c);
  831. }
  832. void resize(size_type, size_type);
  833. void reshape(size_type m, size_type n) {
  834. GMM_ASSERT2(n*m == nbl*nbc, "dimensions mismatch");
  835. nbl = m; nbc = n;
  836. }
  837. void fill(T a, T b = T(0)) {
  838. std::fill(begin_, begin_+nbc*nbl, b);
  839. iterator p = begin_, e = begin_+nbc*nbl;
  840. while (p < e) { *p = a; p += nbc+1; }
  841. }
  842. inline size_type nrows(void) const { return nbl; }
  843. inline size_type ncols(void) const { return nbc; }
  844. iterator begin(void) { return begin_; }
  845. const_iterator begin(void) const { return begin_; }
  846. iterator end(void) { return begin_+nbl*nbc; }
  847. const_iterator end(void) const { return begin_+nbl*nbc; }
  848. array2D_row_reference(PT begin__, size_type nrows_, size_type ncols_)
  849. : begin_(begin__), nbl(nrows_), nbc(ncols_) {}
  850. };
  851. template <typename PT> struct linalg_traits<array2D_row_reference<PT> > {
  852. typedef array2D_row_reference<PT> this_type;
  853. typedef this_type origin_type;
  854. typedef typename which_reference<PT>::is_reference is_reference;
  855. typedef abstract_matrix linalg_type;
  856. typedef typename std::iterator_traits<PT>::value_type value_type;
  857. typedef typename std::iterator_traits<PT>::reference reference;
  858. typedef abstract_dense storage_type;
  859. typedef tab_ref_reg_spaced_with_origin<typename this_type::iterator,
  860. this_type> sub_col_type;
  861. typedef tab_ref_reg_spaced_with_origin<typename this_type::const_iterator,
  862. this_type> const_sub_col_type;
  863. typedef dense_compressed_iterator<typename this_type::iterator,
  864. typename this_type::iterator,
  865. this_type *> col_iterator;
  866. typedef dense_compressed_iterator<typename this_type::const_iterator,
  867. typename this_type::iterator,
  868. const this_type *> const_col_iterator;
  869. typedef tab_ref_with_origin<typename this_type::iterator,
  870. this_type> sub_row_type;
  871. typedef tab_ref_with_origin<typename this_type::const_iterator,
  872. this_type> const_sub_row_type;
  873. typedef dense_compressed_iterator<typename this_type::iterator,
  874. typename this_type::iterator,
  875. this_type *> row_iterator;
  876. typedef dense_compressed_iterator<typename this_type::const_iterator,
  877. typename this_type::iterator,
  878. const this_type *> const_row_iterator;
  879. typedef col_and_row sub_orientation;
  880. typedef linalg_true index_sorted;
  881. static size_type ncols(const this_type &m) { return m.ncols(); }
  882. static size_type nrows(const this_type &m) { return m.nrows(); }
  883. static const_sub_col_type col(const const_col_iterator &it)
  884. { return const_sub_col_type(*it, it.ncols, it.nrows, it.origin); }
  885. static const_sub_row_type row(const const_row_iterator &it)
  886. { return const_sub_row_type(*it, *it + it.ncols, it.origin); }
  887. static sub_col_type col(const col_iterator &it)
  888. { return sub_col_type(*it, *it, it.ncols, it.nrows, it.origin); }
  889. static sub_row_type row(const row_iterator &it)
  890. { return sub_row_type(*it, *it + it.ncols, it.origin); }
  891. static col_iterator col_begin(this_type &m)
  892. { return col_iterator(m.begin(), 1, m.ncols(), m.nrows(), 0, &m); }
  893. static col_iterator col_end(this_type &m)
  894. { return col_iterator(m.begin(), 1, m.ncols(), m.nrows(), m.ncols(), &m); }
  895. static const_col_iterator col_begin(const this_type &m)
  896. { return const_col_iterator(m.begin(), 1, m.ncols(), m.nrows(), 0, &m); }
  897. static const_col_iterator col_end(const this_type &m) {
  898. return const_col_iterator(m.begin(), 1, m.ncols(),
  899. m.nrows(), m.ncols(), &m);
  900. }
  901. static row_iterator row_begin(this_type &m)
  902. { return row_iterator(m.begin(), m.ncols(), m.ncols(), m.nrows(), 0, &m); }
  903. static row_iterator row_end(this_type &m) {
  904. return row_iterator(m.begin(), m.ncols(), m.ncols(), m.nrows(),
  905. m.nrows(), &m);
  906. }
  907. static const_row_iterator row_begin(const this_type &m) {
  908. return const_row_iterator(m.begin(), m.ncols(), m.ncols(), m.nrows(),
  909. 0, &m);
  910. }
  911. static const_row_iterator row_end(const this_type &m) {
  912. return const_row_iterator(m.begin(), m.ncols(), m.ncols(), m.nrows(),
  913. m.nrows(), &m);
  914. }
  915. static origin_type* origin(this_type &m) { return &m; }
  916. static const origin_type* origin(const this_type &m) { return &m; }
  917. static void do_clear(this_type &m) { m.fill(value_type(0)); }
  918. static value_type access(const const_row_iterator &itrow, size_type j)
  919. { return (*itrow)[j]; }
  920. static reference access(const row_iterator &itrow, size_type j)
  921. { return (*itrow)[j]; }
  922. static void resize(this_type &v, size_type m, size_type n)
  923. { v.resize(m,n); }
  924. static void reshape(this_type &v, size_type m, size_type n)
  925. { v.reshape(m, n); }
  926. };
  927. template<typename PT> std::ostream &operator <<
  928. (std::ostream &o, const array2D_row_reference<PT>& m)
  929. { gmm::write(o,m); return o; }
  930. }
  931. #endif // GMM_INTERFACE_H__