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)
11 #ifndef _BOOST_UBLAS_SPARSE_VIEW_
12 #define _BOOST_UBLAS_SPARSE_VIEW_
14 #include <boost/numeric/ublas/matrix_expression.hpp>
15 #include <boost/numeric/ublas/detail/matrix_assign.hpp>
16 #if BOOST_UBLAS_TYPE_CHECK
17 #include <boost/numeric/ublas/matrix.hpp>
20 #include <boost/next_prior.hpp>
21 #include <boost/type_traits/remove_cv.hpp>
22 #include <boost/numeric/ublas/storage.hpp>
24 namespace boost { namespace numeric { namespace ublas {
26 // view a chunk of memory as ublas array
30 : public storage_array< c_array_view<T> > {
32 typedef c_array_view<T> self_type;
36 // TODO: think about a const pointer
37 typedef const pointer array_type;
39 typedef std::size_t size_type;
40 typedef std::ptrdiff_t difference_type;
43 typedef const T &const_reference;
44 typedef const T *const_pointer;
46 typedef const_pointer const_iterator;
47 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
50 // typedefs required by vector concept
53 typedef dense_tag storage_category;
54 typedef const vector_reference<const self_type> const_closure_type;
56 c_array_view(size_type size, array_type data) :
57 size_(size), data_(data)
64 // immutable methods of container concept
68 size_type size () const {
73 const_reference operator [] (size_type i) const {
74 BOOST_UBLAS_CHECK (i < size_, bad_index ());
79 const_iterator begin () const {
83 const_iterator end () const {
88 const_reverse_iterator rbegin () const {
89 return const_reverse_iterator (end ());
92 const_reverse_iterator rend () const {
93 return const_reverse_iterator (begin ());
102 /** \brief Present existing arrays as compressed array based
104 * This class provides CRS / CCS storage layout.
106 * see also http://www.netlib.org/utk/papers/templates/node90.html
108 * \param L layout type, either row_major or column_major
109 * \param IB index base, use 0 for C indexing and 1 for
110 * FORTRAN indexing of the internal index arrays. This
111 * does not affect the operator()(int,int) where the first
112 * row/column has always index 0.
113 * \param IA index array type, e.g., int[]
114 * \param TA value array type, e.g., double[]
116 template<class L, std::size_t IB, class IA, class JA, class TA>
117 class compressed_matrix_view:
118 public matrix_expression<compressed_matrix_view<L, IB, IA, JA, TA> > {
121 typedef typename vector_view_traits<TA>::value_type value_type;
124 typedef value_type &true_reference;
125 typedef value_type *pointer;
126 typedef const value_type *const_pointer;
127 typedef L layout_type;
128 typedef compressed_matrix_view<L, IB, IA, JA, TA> self_type;
131 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
132 using matrix_expression<self_type>::operator ();
134 // ISSUE require type consistency check
135 // is_convertable (IA::size_type, TA::size_type)
136 typedef typename boost::remove_cv<typename vector_view_traits<JA>::value_type>::type index_type;
137 // for compatibility, should be removed some day ...
138 typedef index_type size_type;
139 // size_type for the data arrays.
140 typedef typename vector_view_traits<JA>::size_type array_size_type;
141 typedef typename vector_view_traits<JA>::difference_type difference_type;
142 typedef const value_type & const_reference;
144 // do NOT define reference type, because class is read only
145 // typedef value_type & reference;
147 typedef IA rowptr_array_type;
148 typedef JA index_array_type;
149 typedef TA value_array_type;
150 typedef const matrix_reference<const self_type> const_closure_type;
151 typedef matrix_reference<self_type> closure_type;
153 // FIXME: define a corresponding temporary type
154 // typedef compressed_vector<T, IB, IA, TA> vector_temporary_type;
156 // FIXME: define a corresponding temporary type
157 // typedef self_type matrix_temporary_type;
159 typedef sparse_tag storage_category;
160 typedef typename L::orientation_category orientation_category;
163 // private types for internal use
167 typedef typename vector_view_traits<index_array_type>::const_iterator const_subiterator_type;
170 // Construction and destruction
173 /// private default constructor because data must be filled by caller
175 compressed_matrix_view () { }
179 compressed_matrix_view (index_type n_rows, index_type n_cols, array_size_type nnz
180 , const rowptr_array_type & iptr
181 , const index_array_type & jptr
182 , const value_array_type & values):
183 matrix_expression<self_type> (),
184 size1_ (n_rows), size2_ (n_cols),
188 value_data_ (values) {
189 storage_invariants ();
193 compressed_matrix_view(const compressed_matrix_view& o) :
194 size1_(o.size1_), size2_(o.size2_),
196 index1_data_(o.index1_data_),
197 index2_data_(o.index2_data_),
198 value_data_(o.value_data_)
202 // implement immutable iterator types
205 class const_iterator1 {};
206 class const_iterator2 {};
208 typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
209 typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
212 // implement all read only methods for the matrix expression concept
215 //! return the number of rows
216 index_type size1() const {
220 //! return the number of columns
221 index_type size2() const {
225 //! return value at position (i,j)
226 value_type operator()(index_type i, index_type j) const {
227 const_pointer p = find_element(i,j);
238 // private helper functions
241 const_pointer find_element (index_type i, index_type j) const {
242 index_type element1 (layout_type::index_M (i, j));
243 index_type element2 (layout_type::index_m (i, j));
245 const array_size_type itv = zero_based( index1_data_[element1] );
246 const array_size_type itv_next = zero_based( index1_data_[element1+1] );
248 const_subiterator_type it_start = boost::next(vector_view_traits<index_array_type>::begin(index2_data_),itv);
249 const_subiterator_type it_end = boost::next(vector_view_traits<index_array_type>::begin(index2_data_),itv_next);
250 const_subiterator_type it = find_index_in_row(it_start, it_end, element2) ;
252 if (it == it_end || *it != k_based (element2))
254 return &value_data_ [it - vector_view_traits<index_array_type>::begin(index2_data_)];
257 const_subiterator_type find_index_in_row(const_subiterator_type it_start
258 , const_subiterator_type it_end
259 , index_type index) const {
260 return std::lower_bound( it_start
267 void storage_invariants () const {
268 BOOST_UBLAS_CHECK (index1_data_ [layout_type::size_M (size1_, size2_)] == k_based (nnz_), external_logic ());
274 array_size_type nnz_;
276 const rowptr_array_type & index1_data_;
277 const index_array_type & index2_data_;
278 const value_array_type & value_data_;
280 static const value_type zero_;
283 static index_type zero_based (index_type k_based_index) {
284 return k_based_index - IB;
287 static index_type k_based (index_type zero_based_index) {
288 return zero_based_index + IB;
291 friend class iterator1;
292 friend class iterator2;
293 friend class const_iterator1;
294 friend class const_iterator2;
297 template<class L, std::size_t IB, class IA, class JA, class TA >
298 const typename compressed_matrix_view<L,IB,IA,JA,TA>::value_type
299 compressed_matrix_view<L,IB,IA,JA,TA>::zero_ = value_type/*zero*/();
302 template<class L, std::size_t IB, class IA, class JA, class TA >
303 compressed_matrix_view<L,IB,IA,JA,TA>
304 make_compressed_matrix_view(typename vector_view_traits<JA>::value_type n_rows
305 , typename vector_view_traits<JA>::value_type n_cols
306 , typename vector_view_traits<JA>::size_type nnz
311 return compressed_matrix_view<L,IB,IA,JA,TA>(n_rows, n_cols, nnz, ia, ja, ta);