📄 matrix_sparse.hpp
字号:
BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
// BOOST_UBLAS_CHECK (rank_ == it.rank_, internal_logic ());
if (rank_ == 1 || it.rank_ == 1) {
return it_ == it.it_;
} else {
return i_ == it.i_ && j_ == it.j_;
}
}
private:
int rank_;
size_type i_;
size_type j_;
iterator_type it_;
friend class const_iterator2;
};
BOOST_UBLAS_INLINE
iterator2 begin2 () {
return find2 (0, 0, 0);
}
BOOST_UBLAS_INLINE
iterator2 end2 () {
return find2 (0, 0, size2_);
}
// Reverse iterators
BOOST_UBLAS_INLINE
const_reverse_iterator1 rbegin1 () const {
return const_reverse_iterator1 (end1 ());
}
BOOST_UBLAS_INLINE
const_reverse_iterator1 rend1 () const {
return const_reverse_iterator1 (begin1 ());
}
BOOST_UBLAS_INLINE
reverse_iterator1 rbegin1 () {
return reverse_iterator1 (end1 ());
}
BOOST_UBLAS_INLINE
reverse_iterator1 rend1 () {
return reverse_iterator1 (begin1 ());
}
BOOST_UBLAS_INLINE
const_reverse_iterator2 rbegin2 () const {
return const_reverse_iterator2 (end2 ());
}
BOOST_UBLAS_INLINE
const_reverse_iterator2 rend2 () const {
return const_reverse_iterator2 (begin2 ());
}
BOOST_UBLAS_INLINE
reverse_iterator2 rbegin2 () {
return reverse_iterator2 (end2 ());
}
BOOST_UBLAS_INLINE
reverse_iterator2 rend2 () {
return reverse_iterator2 (begin2 ());
}
private:
size_type size1_;
size_type size2_;
size_type non_zeros_;
array_type data_;
static value_type zero_;
};
template<class T, class F, class A>
typename sparse_matrix<T, F, A>::value_type sparse_matrix<T, F, A>::zero_ =
sparse_matrix<T, F, A>::value_type ();
// Array based sparse matrix class
template<class T, class F, class A>
class sparse_vector_of_sparse_vector:
public matrix_expression<sparse_vector_of_sparse_vector<T, F, A> > {
public:
#ifndef BOOST_UBLAS_NO_PROXY_SHORTCUTS
BOOST_UBLAS_USING matrix_expression<sparse_vector_of_sparse_vector<T, F, A> >::operator ();
#endif
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T value_type;
// typedef const T &const_reference;
typedef typename type_traits<T>::const_reference const_reference;
#if ! defined (BOOST_UBLAS_STRICT_STORAGE_SPARSE) && ! defined (BOOST_UBLAS_STRICT_MATRIX_SPARSE)
typedef T &reference;
#elif defined (BOOST_UBLAS_STRICT_MATRIX_SPARSE)
typedef sparse_matrix_element<sparse_vector_of_sparse_vector<T, F, A> > reference;
#elif defined (BOOST_UBLAS_STRICT_STORAGE_SPARSE)
typedef typename detail::map_traits<typename A::data_value_type>::reference reference;
#endif
typedef const T *const_pointer;
typedef T *pointer;
typedef A array_type;
typedef const A const_array_type;
typedef F functor_type;
typedef const sparse_vector_of_sparse_vector<T, F, A> const_self_type;
typedef sparse_vector_of_sparse_vector<T, F, A> self_type;
#ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS
typedef const matrix_const_reference<const_self_type> const_closure_type;
#else
typedef const matrix_reference<const_self_type> const_closure_type;
#endif
typedef matrix_reference<self_type> closure_type;
typedef typename A::value_type::second_type vector_data_value_type;
typedef typename A::const_iterator vector_const_iterator_type;
typedef typename A::iterator vector_iterator_type;
typedef typename A::value_type::second_type::const_iterator const_iterator_type;
typedef typename A::value_type::second_type::iterator iterator_type;
typedef sparse_tag storage_category;
typedef typename F::orientation_category orientation_category;
// Construction and destruction
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector ():
matrix_expression<self_type> (),
size1_ (0), size2_ (0), non_zeros_ (0), data_ () {
data_ [functor_type::size1 (size1_, size2_)] = vector_data_value_type ();
}
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector (size_type size1, size_type size2, size_type non_zeros = 0):
matrix_expression<self_type> (),
size1_ (size1), size2_ (size2), non_zeros_ (non_zeros), data_ () {
data_ [functor_type::size1 (size1_, size2_)] = vector_data_value_type ();
}
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector (const sparse_vector_of_sparse_vector &m):
matrix_expression<self_type> (),
size1_ (m.size1_), size2_ (m.size2_), non_zeros_ (m.non_zeros_), data_ (m.data_) {}
template<class AE>
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector (const matrix_expression<AE> &ae, size_type non_zeros = 0):
matrix_expression<self_type> (),
size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), non_zeros_ (non_zeros), data_ () {
data_ [functor_type::size1 (size1_, size2_)] = vector_data_value_type ();
matrix_assign (scalar_assign<reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae);
}
// Accessors
BOOST_UBLAS_INLINE
size_type size1 () const {
return size1_;
}
BOOST_UBLAS_INLINE
size_type size2 () const {
return size2_;
}
BOOST_UBLAS_INLINE
size_type non_zeros () const {
size_type non_zeros = 0;
for (vector_const_iterator_type itv = data_ ().begin (); itv != data_ ().end (); ++ itv)
non_zeros += (*itv).size ();
return non_zeros;
}
BOOST_UBLAS_INLINE
const_array_type &data () const {
return data_;
}
BOOST_UBLAS_INLINE
array_type &data () {
return data_;
}
// Resizing
BOOST_UBLAS_INLINE
void resize (size_type size1, size_type size2, size_type non_zeros = 0) {
size1_ = size1;
size2_ = size2;
non_zeros_ = non_zeros;
data ().clear ();
data () [functor_type::size1 (size1_, size2_)] = vector_data_value_type ();
}
// Proxy support
#ifdef BOOST_UBLAS_STRICT_MATRIX_SPARSE
pointer find_element (size_type i, size_type j) {
vector_iterator_type itv (data ().find (functor_type::element1 (i, size1_, j, size2_)));
if (itv == data ().end () || (*itv).first != functor_type::element1 (i, size1_, j, size2_))
return 0;
iterator_type it ((*itv).second.find (functor_type::element2 (i, size1_, j, size2_)));
if (it == (*itv).second.end () || (*it).first != functor_type::element2 (i, size1_, j, size2_))
return 0;
return &(*it).second;
}
#endif
// Element access
BOOST_UBLAS_INLINE
const_reference operator () (size_type i, size_type j) const {
vector_const_iterator_type itv (data ().find (functor_type::element1 (i, size1_, j, size2_)));
if (itv == data ().end () || (*itv).first != functor_type::element1 (i, size1_, j, size2_))
return zero_;
const_iterator_type it ((*itv).second.find (functor_type::element2 (i, size1_, j, size2_)));
if (it == (*itv).second.end () || (*it).first != functor_type::element2 (i, size1_, j, size2_))
return zero_;
return (*it).second;
}
BOOST_UBLAS_INLINE
reference operator () (size_type i, size_type j) {
#ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE
return data () [functor_type::element1 (i, size1_, j, size2_)] [functor_type::element2 (i, size1_, j, size2_)];
#else
return reference (*this, i, j);
#endif
}
// Assignment
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector &operator = (const sparse_vector_of_sparse_vector &m) {
// Too unusual semantic.
// BOOST_UBLAS_CHECK (this != &m, external_logic ());
if (this != &m) {
// Precondition for container relaxed as requested during review.
// BOOST_UBLAS_CHECK (size1_ == m.size1_, bad_size ());
// BOOST_UBLAS_CHECK (size2_ == m.size2_, bad_size ());
size1_ = m.size1_;
size2_ = m.size2_;
non_zeros_ = m.non_zeros_;
data () = m.data ();
}
return *this;
}
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector &assign_temporary (sparse_vector_of_sparse_vector &m) {
swap (m);
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector &operator = (const matrix_expression<AE> &ae) {
#ifdef BOOST_UBLAS_MUTABLE_TEMPORARY
return assign_temporary (self_type (ae, non_zeros_));
#else
// return assign (self_type (ae, non_zeros_));
self_type temporary (ae, non_zeros_);
return assign_temporary (temporary);
#endif
}
template<class AE>
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector &reset (const matrix_expression<AE> &ae) {
self_type temporary (ae, non_zeros_);
resize (temporary.size1 (), temporary.size2 (), non_zeros_);
return assign_temporary (temporary);
}
template<class AE>
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector &assign (const matrix_expression<AE> &ae) {
matrix_assign (scalar_assign<reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae);
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector& operator += (const matrix_expression<AE> &ae) {
#ifdef BOOST_UBLAS_MUTABLE_TEMPORARY
return assign_temporary (self_type (*this + ae, non_zeros_));
#else
// return assign (self_type (*this + ae, non_zeros_));
self_type temporary (*this + ae, non_zeros_);
return assign_temporary (temporary);
#endif
}
template<class AE>
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector &plus_assign (const matrix_expression<AE> &ae) {
matrix_assign (scalar_plus_assign<reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae);
return *this;
}
template<class AE>
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector& operator -= (const matrix_expression<AE> &ae) {
#ifdef BOOST_UBLAS_MUTABLE_TEMPORARY
return assign_temporary (self_type (*this - ae, non_zeros_));
#else
// return assign (self_type (*this - ae, non_zeros_));
self_type temporary (*this - ae, non_zeros_);
return assign_temporary (temporary);
#endif
}
template<class AE>
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector &minus_assign (const matrix_expression<AE> &ae) {
matrix_assign (scalar_minus_assign<reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae);
return *this;
}
template<class AT>
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector& operator *= (const AT &at) {
matrix_assign_scalar (scalar_multiplies_assign<reference, AT> (), *this, at);
return *this;
}
template<class AT>
BOOST_UBLAS_INLINE
sparse_vector_of_sparse_vector& operator /= (const AT &at) {
matrix_assign_scalar (scalar_divides_assign<reference, AT> (), *this, at);
return *this;
}
// Swapping
BOOST_UBLAS_INLINE
void swap (sparse_vector_of_sparse_vector &m) {
// Too unusual semantic.
// BOOST_UBLAS_CHECK (this != &m, external_logic ());
if (this != &m) {
// Precondition for container relaxed as requested during review.
// BOOST_UBLAS_CHECK (size1_ == m.size1_, bad_size ());
// BOOST_UBLAS_CHECK (size2_ == m.size2_, bad_size ());
// BOOST_UBLAS_CHECK (non_zeros_ == m.non_zeros_, bad_size ());
std::swap (size1_, m.size1_);
std::swap (size2_, m.size2_);
std::swap (non_zeros_, m.non_zeros_);
data ().swap (m.data ());
}
}
#ifndef BOOST_UBLAS_NO_MEMBER_FRIENDS
BOOST_UBLAS_INLINE
friend void swap (sparse_vector_of_sparse_vector &m1, sparse_vector_of_sparse_vector &m2) {
m1.swap (m2);
}
#endif
// Element insertion and erasure
BOOST_UBLAS_INLINE
void insert (size_type i, size_type j, const_reference t) {
vector_iterator_type itv (data ().find (f
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -