📄 vector_sparse.hpp
字号:
typedef sparse_tag storage_category; // Construction and destruction BOOST_UBLAS_INLINE compressed_vector (): vector_expression<self_type> (), size_ (0), non_zeros_ (restrict_nz (0)), filled_ (0), index_data_ (non_zeros_), value_data_ (non_zeros_) {} explicit BOOST_UBLAS_INLINE compressed_vector (size_type size, size_type non_zeros = 0): vector_expression<self_type> (), size_ (size), non_zeros_ (restrict_nz (non_zeros)), filled_ (0), index_data_ (non_zeros_), value_data_ (non_zeros_) { } BOOST_UBLAS_INLINE compressed_vector (const compressed_vector &v): vector_expression<self_type> (), size_ (v.size_), non_zeros_ (v.non_zeros_), filled_ (v.filled_), index_data_ (v.index_data_), value_data_ (v.value_data_) {} template<class AE> BOOST_UBLAS_INLINE compressed_vector (const vector_expression<AE> &ae, size_type non_zeros = 0): vector_expression<self_type> (), size_ (ae ().size ()), non_zeros_ (restrict_nz (non_zeros)), filled_ (0), index_data_ (non_zeros_), value_data_ (non_zeros_) { vector_assign (scalar_assign<true_reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae); } // Accessors BOOST_UBLAS_INLINE size_type size () const { return size_; } BOOST_UBLAS_INLINE size_type non_zeros () const { return non_zeros_; } BOOST_UBLAS_INLINE size_type filled () const { return filled_; } BOOST_UBLAS_INLINE static size_type index_base () { return IB; } BOOST_UBLAS_INLINE const index_array_type &index_data () const { return index_data_; } BOOST_UBLAS_INLINE index_array_type &index_data () { return index_data_; } BOOST_UBLAS_INLINE const value_array_type &value_data () const { return value_data_; } BOOST_UBLAS_INLINE value_array_type &value_data () { return value_data_; } // Resizing private: BOOST_UBLAS_INLINE size_type restrict_nz (size_type non_zeros) const { non_zeros = (std::max) (non_zeros, size_type (1)); non_zeros = (std::min) (non_zeros, size_); return non_zeros; } public: BOOST_UBLAS_INLINE void resize (size_type size, bool preserve = true) { // FIXME preserve unimplemented BOOST_UBLAS_CHECK (!preserve, internal_logic ()); size_ = size; non_zeros_ = restrict_nz (non_zeros_); index_data (). resize (non_zeros_); value_data (). resize (non_zeros_); filled_ = 0; } // Reserving BOOST_UBLAS_INLINE void reserve (size_type non_zeros, bool preserve = true) { non_zeros_ = restrict_nz (non_zeros); if (preserve) { index_data (). resize (non_zeros_, size_type ()); value_data (). resize (non_zeros_, value_type ()); filled_ = (std::min) (non_zeros_, filled_); } else { index_data (). resize (non_zeros_); value_data (). resize (non_zeros_); filled_ = 0; } } // Proxy support#ifdef BOOST_UBLAS_STRICT_VECTOR_SPARSE pointer find_element (size_type i) { iterator_type it (detail::lower_bound (index_data ().begin (), index_data ().begin () + filled_, k_based (i), std::less<size_type> ())); if (it == index_data ().begin () + filled_ || *it != k_based (i)) return 0; return &value_data () [it - index_data ().begin ()]; }#endif // Element access BOOST_UBLAS_INLINE const_reference operator () (size_type i) const { BOOST_UBLAS_CHECK (i < size_, bad_index ()); const_iterator_type it (detail::lower_bound (index_data ().begin (), index_data ().begin () + filled_, k_based (i), std::less<size_type> ())); if (it == index_data ().begin () + filled_ || *it != k_based (i)) return zero_; return value_data () [it - index_data ().begin ()]; } BOOST_UBLAS_INLINE reference operator () (size_type i) { BOOST_UBLAS_CHECK (i < size_, bad_index ());#ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE iterator_type it (detail::lower_bound (index_data ().begin (), index_data ().begin () + filled_, k_based (i), std::less<size_type> ())); if (it == index_data ().begin () + filled_ || *it != k_based (i)) { insert (i, value_type (0)); it = detail::lower_bound (index_data ().begin (), index_data ().begin () + filled_, k_based (i), std::less<size_type> ()); } return value_data () [it - index_data ().begin ()];#else return reference (*this, i);#endif } BOOST_UBLAS_INLINE const_reference operator [] (size_type i) const { return (*this) (i); } BOOST_UBLAS_INLINE reference operator [] (size_type i) { return (*this) (i); } // Assignment BOOST_UBLAS_INLINE compressed_vector &operator = (const compressed_vector &v) { if (this != &v) { size_ = v.size_; non_zeros_ = v.non_zeros_; filled_ = v.filled_; index_data () = v.index_data (); value_data () = v.value_data (); BOOST_UBLAS_CHECK (non_zeros_ == index_data ().size (), internal_logic ()); BOOST_UBLAS_CHECK (non_zeros_ == value_data ().size (), internal_logic ()); } return *this; } BOOST_UBLAS_INLINE compressed_vector &assign_temporary (compressed_vector &v) { swap (v); return *this; } template<class AE> BOOST_UBLAS_INLINE compressed_vector &operator = (const vector_expression<AE> &ae) { // return assign (self_type (ae, non_zeros_)); self_type temporary (ae, non_zeros_); return assign_temporary (temporary); } template<class AE> BOOST_UBLAS_INLINE compressed_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 compressed_vector &operator += (const vector_expression<AE> &ae) { // return assign (self_type (*this + ae, non_zeros_)); self_type temporary (*this + ae, non_zeros_); return assign_temporary (temporary); } template<class AE> BOOST_UBLAS_INLINE compressed_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 compressed_vector &operator -= (const vector_expression<AE> &ae) { // return assign (self_type (*this - ae, non_zeros_)); self_type temporary (*this - ae, non_zeros_); return assign_temporary (temporary); } template<class AE> BOOST_UBLAS_INLINE compressed_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 compressed_vector &operator *= (const AT &at) { vector_assign_scalar (scalar_multiplies_assign<true_reference, AT> (), *this, at); return *this; } template<class AT> BOOST_UBLAS_INLINE compressed_vector &operator /= (const AT &at) { vector_assign_scalar (scalar_divides_assign<true_reference, AT> (), *this, at); return *this; } // Swapping BOOST_UBLAS_INLINE void swap (compressed_vector &v) { if (this != &v) { std::swap (size_, v.size_); std::swap (non_zeros_, v.non_zeros_); std::swap (filled_, v.filled_); index_data ().swap (v.index_data ()); value_data ().swap (v.value_data ()); } }#ifndef BOOST_UBLAS_NO_MEMBER_FRIENDS BOOST_UBLAS_INLINE friend void swap (compressed_vector &v1, compressed_vector &v2) { v1.swap (v2); }#endif // Element insertion and erasure BOOST_UBLAS_INLINE void push_back (size_type i, const_reference t) { if (filled_ >= non_zeros_) reserve (2 * non_zeros_, true); if (filled_ == 0 || index_data () [filled_ - 1] < k_based (i)) { ++ filled_; index_data () [filled_ - 1] = k_based (i); value_data () [filled_ - 1] = t; return; } external_logic ().raise (); } BOOST_UBLAS_INLINE void insert (size_type i, const_reference t) { if (filled_ >= non_zeros_) reserve (2 * non_zeros_, true); iterator_type it (detail::lower_bound (index_data ().begin (), index_data ().begin () + filled_, k_based (i), std::less<size_type> ())); difference_type n = it - index_data ().begin (); BOOST_UBLAS_CHECK (filled_ == 0 || filled_ == size_type (n) || *it != k_based (i), external_logic ()); ++ filled_; it = index_data ().begin () + n; std::copy_backward (it, index_data ().begin () + filled_ - 1, index_data ().begin () + filled_); *it = k_based (i); typename value_array_type::iterator itt (value_data ().begin () + n); std::copy_backward (itt, value_data ().begin () + filled_ - 1, value_data ().begin () + filled_); *itt = t; } BOOST_UBLAS_INLINE void pop_back () { BOOST_UBLAS_CHECK (filled_ > 0, external_logic ()); -- filled_; } BOOST_UBLAS_INLINE void erase (size_type i) { iterator_type it (detail::lower_bound (index_data ().begin (), index_data ().begin () + filled_, k_based (i), std::less<size_type> ())); difference_type n = it - index_data ().begin (); if (filled_ > size_type (n) && *it == k_based (i)) { std::copy (it + 1, index_data ().begin () + filled_, it); typename value_array_type::iterator itt (value_data ().begin () + n); std::copy (itt + 1, value_data ().begin () + filled_, itt); -- filled_; } } BOOST_UBLAS_INLINE void clear () { filled_ = 0; } // Iterator types private: // Use index array iterator typedef typename IA::const_iterator const_iterator_type; typedef typename IA::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, detail::lower_bound (index_data ().begin (), index_data ().begin () + filled_, k_based (i), std::less<size_type> ())); } // 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, detail::lower_bound (index_data ().begin (), index_data ().begin () + filled_, k_based (i), std::less<size_type> ())); } class const_iterator: public container_const_reference<compressed_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 compressed_vector::value_type value_type; typedef typename compressed_vector::difference_type difference_type; typedef typename compressed_vector::const_reference reference; typedef const typename compressed_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 (*this) ().value_data () [it_ - (*this) ().index_data ().begin ()]; } // Index BOOST_UBLAS_INLINE size_type index () const { BOOST_UBLAS_CHECK (*this != (*this) ().end (), bad_index ()); BOOST_UBLAS_CHECK ((*this) ().zero_based (*it_) < (*this) ().size (), bad_index ()); return (*this) ().zero_based (*it_);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -