📄 vector_sparse.hpp
字号:
data () = v.data (); } return *this; } BOOST_UBLAS_INLINE sparse_vector &assign_temporary (sparse_vector &v) { swap (v); return *this; } template<class AE> BOOST_UBLAS_INLINE sparse_vector &operator = (const vector_expression<AE> &ae) { self_type temporary (ae, detail::map_capacity (data())); return assign_temporary (temporary); } template<class AE> BOOST_UBLAS_INLINE sparse_vector &assign (const vector_expression<AE> &ae) { vector_assign (scalar_assign<true_reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae); return *this; } template<class AE> BOOST_UBLAS_INLINE sparse_vector &operator += (const vector_expression<AE> &ae) { self_type temporary (*this + ae, detail::map_capacity (data())); return assign_temporary (temporary); } template<class AE> BOOST_UBLAS_INLINE sparse_vector &plus_assign (const vector_expression<AE> &ae) { vector_assign (scalar_plus_assign<true_reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae); return *this; } template<class AE> BOOST_UBLAS_INLINE sparse_vector &operator -= (const vector_expression<AE> &ae) { self_type temporary (*this - ae, detail::map_capacity (data())); return assign_temporary (temporary); } template<class AE> BOOST_UBLAS_INLINE sparse_vector &minus_assign (const vector_expression<AE> &ae) { vector_assign (scalar_minus_assign<true_reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae); return *this; } template<class AT> BOOST_UBLAS_INLINE sparse_vector &operator *= (const AT &at) { vector_assign_scalar (scalar_multiplies_assign<true_reference, AT> (), *this, at); return *this; } template<class AT> BOOST_UBLAS_INLINE sparse_vector &operator /= (const AT &at) { vector_assign_scalar (scalar_divides_assign<true_reference, AT> (), *this, at); return *this; } // Swapping BOOST_UBLAS_INLINE void swap (sparse_vector &v) { if (this != &v) { std::swap (size_, v.size_); data ().swap (v.data ()); } }#ifndef BOOST_UBLAS_NO_MEMBER_FRIENDS BOOST_UBLAS_INLINE friend void swap (sparse_vector &v1, sparse_vector &v2) { v1.swap (v2); }#endif // Element insertion and erasure BOOST_UBLAS_INLINE void insert (size_type i, const_reference t) { BOOST_UBLAS_CHECK (data ().find (i) == data ().end (), bad_index ()); data ().insert (data ().end (), BOOST_UBLAS_TYPENAME array_type::value_type (i, t)); } BOOST_UBLAS_INLINE void erase (size_type i) { // FIXME: shouldn't we use const_iterator_type here? iterator_type it = data ().find (i); if (it == data ().end ()) return; data ().erase (it); } BOOST_UBLAS_INLINE void clear () { data ().clear (); } // Iterator types private: // Use storage iterator typedef typename A::const_iterator const_iterator_type; typedef typename A::iterator iterator_type; public: class const_iterator; class iterator; // Element lookup // This function seems to be big. So we do not let the compiler inline it. // BOOST_UBLAS_INLINE const_iterator find (size_type i) const { return const_iterator (*this, data ().lower_bound (i)); } // This function seems to be big. So we do not let the compiler inline it. // BOOST_UBLAS_INLINE iterator find (size_type i) { return iterator (*this, data ().lower_bound (i)); } class const_iterator: public container_const_reference<sparse_vector>, public bidirectional_iterator_base<sparse_bidirectional_iterator_tag, const_iterator, value_type> { public: typedef sparse_bidirectional_iterator_tag iterator_category;#ifdef BOOST_MSVC_STD_ITERATOR typedef const_reference reference;#else typedef typename sparse_vector::value_type value_type; typedef typename sparse_vector::difference_type difference_type; typedef typename sparse_vector::const_reference reference; typedef const typename sparse_vector::pointer pointer;#endif // Construction and destruction BOOST_UBLAS_INLINE const_iterator (): container_const_reference<self_type> (), it_ () {} BOOST_UBLAS_INLINE const_iterator (const self_type &v, const const_iterator_type &it): container_const_reference<self_type> (v), it_ (it) {}#ifndef BOOST_UBLAS_QUALIFIED_TYPENAME BOOST_UBLAS_INLINE const_iterator (const iterator &it): container_const_reference<self_type> (it ()), it_ (it.it_) {}#else BOOST_UBLAS_INLINE const_iterator (const typename self_type::iterator &it): container_const_reference<self_type> (it ()), it_ (it.it_) {}#endif // Arithmetic BOOST_UBLAS_INLINE const_iterator &operator ++ () { ++ it_; return *this; } BOOST_UBLAS_INLINE const_iterator &operator -- () { -- it_; return *this; } // Dereference BOOST_UBLAS_INLINE const_reference operator * () const { BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ()); return (*it_).second; } // Index BOOST_UBLAS_INLINE size_type index () const { BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ()); BOOST_UBLAS_CHECK ((*it_).first < (*this) ().size (), bad_index ()); return (*it_).first; } // Assignment BOOST_UBLAS_INLINE const_iterator &operator = (const const_iterator &it) { container_const_reference<self_type>::assign (&it ()); it_ = it.it_; return *this; } // Comparison BOOST_UBLAS_INLINE bool operator == (const const_iterator &it) const { BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); return it_ == it.it_; } private: const_iterator_type it_; }; BOOST_UBLAS_INLINE const_iterator begin () const { return const_iterator (*this, data ().begin ()); } BOOST_UBLAS_INLINE const_iterator end () const { return const_iterator (*this, data ().end ()); } class iterator: public container_reference<sparse_vector>, public bidirectional_iterator_base<sparse_bidirectional_iterator_tag, iterator, value_type> { public: typedef sparse_bidirectional_iterator_tag iterator_category;#ifndef BOOST_MSVC_STD_ITERATOR typedef typename sparse_vector::value_type value_type; typedef typename sparse_vector::difference_type difference_type; typedef typename sparse_vector::true_reference reference; typedef typename sparse_vector::pointer pointer;#endif // Construction and destruction BOOST_UBLAS_INLINE iterator (): container_reference<self_type> (), it_ () {} BOOST_UBLAS_INLINE iterator (self_type &v, const iterator_type &it): container_reference<self_type> (v), it_ (it) {} // Arithmetic BOOST_UBLAS_INLINE iterator &operator ++ () { ++ it_; return *this; } BOOST_UBLAS_INLINE iterator &operator -- () { -- it_; return *this; } // Dereference BOOST_UBLAS_INLINE reference operator * () const { BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ()); return (*it_).second; } // Index BOOST_UBLAS_INLINE size_type index () const { BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ()); BOOST_UBLAS_CHECK ((*it_).first < (*this) ().size (), bad_index ()); return (*it_).first; } // Assignment BOOST_UBLAS_INLINE iterator &operator = (const iterator &it) { container_reference<self_type>::assign (&it ()); it_ = it.it_; return *this; } // Comparison BOOST_UBLAS_INLINE bool operator == (const iterator &it) const { BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); return it_ == it.it_; } private: iterator_type it_; friend class const_iterator; }; BOOST_UBLAS_INLINE iterator begin () { return iterator (*this, data ().begin ()); } BOOST_UBLAS_INLINE iterator end () { return iterator (*this, data ().end ()); } // Reverse iterator#ifdef BOOST_MSVC_STD_ITERATOR typedef reverse_iterator_base<const_iterator, value_type, const_reference> const_reverse_iterator;#else typedef reverse_iterator_base<const_iterator> const_reverse_iterator;#endif BOOST_UBLAS_INLINE const_reverse_iterator rbegin () const { return const_reverse_iterator (end ()); } BOOST_UBLAS_INLINE const_reverse_iterator rend () const { return const_reverse_iterator (begin ()); }#ifdef BOOST_MSVC_STD_ITERATOR typedef reverse_iterator_base<iterator, value_type, reference> reverse_iterator;#else typedef reverse_iterator_base<iterator> reverse_iterator;#endif BOOST_UBLAS_INLINE reverse_iterator rbegin () { return reverse_iterator (end ()); } BOOST_UBLAS_INLINE reverse_iterator rend () { return reverse_iterator (begin ()); } private: size_type size_; array_type data_; static const value_type zero_; }; template<class T, class A> const typename sparse_vector<T, A>::value_type sparse_vector<T, A>::zero_#ifdef BOOST_UBLAS_STATIC_OLD_INIT = BOOST_UBLAS_TYPENAME sparse_vector<T, A>::value_type#endif (0); // Array based sparse vector class // Thanks to Kresimir Fresl for extending this to cover different index bases. template<class T, std::size_t IB, class IA, class TA> class compressed_vector: public vector_expression<compressed_vector<T, IB, IA, TA> > { public:#ifndef BOOST_UBLAS_NO_PROXY_SHORTCUTS BOOST_UBLAS_USING vector_expression<compressed_vector<T, IB, IA, TA> >::operator ();#endif // ISSUE require type consistency check for IA TA and IA::value_type typedef typename IA::size_type size_type; typedef typename IA::difference_type difference_type; typedef T value_type; typedef const T &const_reference;#ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE typedef T &reference;#else typedef sparse_vector_element<compressed_vector<T, IB, IA, TA> > reference;#endif typedef IA index_array_type; typedef TA value_array_type; private: typedef T &true_reference; typedef T *pointer; typedef compressed_vector<T, IB, IA, TA> self_type; public:#ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS typedef const vector_const_reference<const self_type> const_closure_type;#else typedef const vector_reference<const self_type> const_closure_type;#endif typedef vector_reference<self_type> closure_type; typedef self_type vector_temporary_type;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -