annotate DEPENDENCIES/generic/include/boost/numeric/ublas/experimental/sparse_view.hpp @ 133:4acb5d8d80b6 tip

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