sparse_view.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // Copyright (c) 2009
  3. // Gunter Winkler
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. //
  10. #ifndef _BOOST_UBLAS_SPARSE_VIEW_
  11. #define _BOOST_UBLAS_SPARSE_VIEW_
  12. #include <boost/numeric/ublas/matrix_expression.hpp>
  13. #include <boost/numeric/ublas/detail/matrix_assign.hpp>
  14. #if BOOST_UBLAS_TYPE_CHECK
  15. #include <boost/numeric/ublas/matrix.hpp>
  16. #endif
  17. #include <boost/next_prior.hpp>
  18. #include <boost/type_traits/remove_cv.hpp>
  19. #include <boost/numeric/ublas/storage.hpp>
  20. namespace boost { namespace numeric { namespace ublas {
  21. // view a chunk of memory as ublas array
  22. template < class T >
  23. class c_array_view
  24. : public storage_array< c_array_view<T> > {
  25. private:
  26. typedef c_array_view<T> self_type;
  27. typedef T * pointer;
  28. public:
  29. // TODO: think about a const pointer
  30. typedef const pointer array_type;
  31. typedef std::size_t size_type;
  32. typedef std::ptrdiff_t difference_type;
  33. typedef T value_type;
  34. typedef const T &const_reference;
  35. typedef const T *const_pointer;
  36. typedef const_pointer const_iterator;
  37. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  38. //
  39. // typedefs required by vector concept
  40. //
  41. typedef dense_tag storage_category;
  42. typedef const vector_reference<const self_type> const_closure_type;
  43. c_array_view(size_type size, array_type data) :
  44. size_(size), data_(data)
  45. {}
  46. ~c_array_view()
  47. {}
  48. //
  49. // immutable methods of container concept
  50. //
  51. BOOST_UBLAS_INLINE
  52. size_type size () const {
  53. return size_;
  54. }
  55. BOOST_UBLAS_INLINE
  56. const_reference operator [] (size_type i) const {
  57. BOOST_UBLAS_CHECK (i < size_, bad_index ());
  58. return data_ [i];
  59. }
  60. BOOST_UBLAS_INLINE
  61. const_iterator begin () const {
  62. return data_;
  63. }
  64. BOOST_UBLAS_INLINE
  65. const_iterator end () const {
  66. return data_ + size_;
  67. }
  68. BOOST_UBLAS_INLINE
  69. const_reverse_iterator rbegin () const {
  70. return const_reverse_iterator (end ());
  71. }
  72. BOOST_UBLAS_INLINE
  73. const_reverse_iterator rend () const {
  74. return const_reverse_iterator (begin ());
  75. }
  76. private:
  77. size_type size_;
  78. array_type data_;
  79. };
  80. /** \brief Present existing arrays as compressed array based
  81. * sparse matrix.
  82. * This class provides CRS / CCS storage layout.
  83. *
  84. * see also http://www.netlib.org/utk/papers/templates/node90.html
  85. *
  86. * \param L layout type, either row_major or column_major
  87. * \param IB index base, use 0 for C indexing and 1 for
  88. * FORTRAN indexing of the internal index arrays. This
  89. * does not affect the operator()(int,int) where the first
  90. * row/column has always index 0.
  91. * \param IA index array type, e.g., int[]
  92. * \param TA value array type, e.g., double[]
  93. */
  94. template<class L, std::size_t IB, class IA, class JA, class TA>
  95. class compressed_matrix_view:
  96. public matrix_expression<compressed_matrix_view<L, IB, IA, JA, TA> > {
  97. public:
  98. typedef typename vector_view_traits<TA>::value_type value_type;
  99. private:
  100. typedef value_type &true_reference;
  101. typedef value_type *pointer;
  102. typedef const value_type *const_pointer;
  103. typedef L layout_type;
  104. typedef compressed_matrix_view<L, IB, IA, JA, TA> self_type;
  105. public:
  106. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  107. using matrix_expression<self_type>::operator ();
  108. #endif
  109. // ISSUE require type consistency check
  110. // is_convertable (IA::size_type, TA::size_type)
  111. typedef typename boost::remove_cv<typename vector_view_traits<JA>::value_type>::type index_type;
  112. // for compatibility, should be removed some day ...
  113. typedef index_type size_type;
  114. // size_type for the data arrays.
  115. typedef typename vector_view_traits<JA>::size_type array_size_type;
  116. typedef typename vector_view_traits<JA>::difference_type difference_type;
  117. typedef const value_type & const_reference;
  118. // do NOT define reference type, because class is read only
  119. // typedef value_type & reference;
  120. typedef IA rowptr_array_type;
  121. typedef JA index_array_type;
  122. typedef TA value_array_type;
  123. typedef const matrix_reference<const self_type> const_closure_type;
  124. typedef matrix_reference<self_type> closure_type;
  125. // FIXME: define a corresponding temporary type
  126. // typedef compressed_vector<T, IB, IA, TA> vector_temporary_type;
  127. // FIXME: define a corresponding temporary type
  128. // typedef self_type matrix_temporary_type;
  129. typedef sparse_tag storage_category;
  130. typedef typename L::orientation_category orientation_category;
  131. //
  132. // private types for internal use
  133. //
  134. private:
  135. typedef typename vector_view_traits<index_array_type>::const_iterator const_subiterator_type;
  136. //
  137. // Construction and destruction
  138. //
  139. private:
  140. /// private default constructor because data must be filled by caller
  141. BOOST_UBLAS_INLINE
  142. compressed_matrix_view () { }
  143. public:
  144. BOOST_UBLAS_INLINE
  145. compressed_matrix_view (index_type n_rows, index_type n_cols, array_size_type nnz
  146. , const rowptr_array_type & iptr
  147. , const index_array_type & jptr
  148. , const value_array_type & values):
  149. matrix_expression<self_type> (),
  150. size1_ (n_rows), size2_ (n_cols),
  151. nnz_ (nnz),
  152. index1_data_ (iptr),
  153. index2_data_ (jptr),
  154. value_data_ (values) {
  155. storage_invariants ();
  156. }
  157. BOOST_UBLAS_INLINE
  158. compressed_matrix_view(const compressed_matrix_view& o) :
  159. size1_(o.size1_), size2_(o.size2_),
  160. nnz_(o.nnz_),
  161. index1_data_(o.index1_data_),
  162. index2_data_(o.index2_data_),
  163. value_data_(o.value_data_)
  164. {}
  165. //
  166. // implement immutable iterator types
  167. //
  168. class const_iterator1 {};
  169. class const_iterator2 {};
  170. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  171. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  172. //
  173. // implement all read only methods for the matrix expression concept
  174. //
  175. //! return the number of rows
  176. index_type size1() const {
  177. return size1_;
  178. }
  179. //! return the number of columns
  180. index_type size2() const {
  181. return size2_;
  182. }
  183. //! return value at position (i,j)
  184. value_type operator()(index_type i, index_type j) const {
  185. const_pointer p = find_element(i,j);
  186. if (!p) {
  187. return zero_;
  188. } else {
  189. return *p;
  190. }
  191. }
  192. private:
  193. //
  194. // private helper functions
  195. //
  196. const_pointer find_element (index_type i, index_type j) const {
  197. index_type element1 (layout_type::index_M (i, j));
  198. index_type element2 (layout_type::index_m (i, j));
  199. const array_size_type itv = zero_based( index1_data_[element1] );
  200. const array_size_type itv_next = zero_based( index1_data_[element1+1] );
  201. const_subiterator_type it_start = boost::next(vector_view_traits<index_array_type>::begin(index2_data_),itv);
  202. const_subiterator_type it_end = boost::next(vector_view_traits<index_array_type>::begin(index2_data_),itv_next);
  203. const_subiterator_type it = find_index_in_row(it_start, it_end, element2) ;
  204. if (it == it_end || *it != k_based (element2))
  205. return 0;
  206. return &value_data_ [it - vector_view_traits<index_array_type>::begin(index2_data_)];
  207. }
  208. const_subiterator_type find_index_in_row(const_subiterator_type it_start
  209. , const_subiterator_type it_end
  210. , index_type index) const {
  211. return std::lower_bound( it_start
  212. , it_end
  213. , k_based (index) );
  214. }
  215. private:
  216. void storage_invariants () const {
  217. BOOST_UBLAS_CHECK (index1_data_ [layout_type::size_M (size1_, size2_)] == k_based (nnz_), external_logic ());
  218. }
  219. index_type size1_;
  220. index_type size2_;
  221. array_size_type nnz_;
  222. const rowptr_array_type & index1_data_;
  223. const index_array_type & index2_data_;
  224. const value_array_type & value_data_;
  225. static const value_type zero_;
  226. BOOST_UBLAS_INLINE
  227. static index_type zero_based (index_type k_based_index) {
  228. return k_based_index - IB;
  229. }
  230. BOOST_UBLAS_INLINE
  231. static index_type k_based (index_type zero_based_index) {
  232. return zero_based_index + IB;
  233. }
  234. friend class iterator1;
  235. friend class iterator2;
  236. friend class const_iterator1;
  237. friend class const_iterator2;
  238. };
  239. template<class L, std::size_t IB, class IA, class JA, class TA >
  240. const typename compressed_matrix_view<L,IB,IA,JA,TA>::value_type
  241. compressed_matrix_view<L,IB,IA,JA,TA>::zero_ = value_type/*zero*/();
  242. template<class L, std::size_t IB, class IA, class JA, class TA >
  243. compressed_matrix_view<L,IB,IA,JA,TA>
  244. make_compressed_matrix_view(typename vector_view_traits<JA>::value_type n_rows
  245. , typename vector_view_traits<JA>::value_type n_cols
  246. , typename vector_view_traits<JA>::size_type nnz
  247. , const IA & ia
  248. , const JA & ja
  249. , const TA & ta) {
  250. return compressed_matrix_view<L,IB,IA,JA,TA>(n_rows, n_cols, nnz, ia, ja, ta);
  251. }
  252. }}}
  253. #endif