sparse_view.hpp 10 KB

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