📄 vector_sparse.hpp
字号:
} // 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 find (0); } BOOST_UBLAS_INLINE const_iterator end () const { return find (size_); } class iterator: public container_reference<compressed_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 compressed_vector::value_type value_type; typedef typename compressed_vector::difference_type difference_type; typedef typename compressed_vector::true_reference reference; typedef typename compressed_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 (*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_); } // 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 find (0); } BOOST_UBLAS_INLINE iterator end () { return find (size_); } // 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_; size_type non_zeros_; size_type filled_; index_array_type index_data_; value_array_type value_data_; static const value_type zero_; BOOST_UBLAS_INLINE static size_type zero_based (size_type k_based_index) { return k_based_index - IB; } BOOST_UBLAS_INLINE static size_type k_based (size_type zero_based_index) { return zero_based_index + IB; } friend class iterator; friend class const_iterator; }; template<class T, std::size_t IB, class IA, class TA> const typename compressed_vector<T, IB, IA, TA>::value_type compressed_vector<T, IB, IA, TA>::zero_#ifdef BOOST_UBLAS_STATIC_OLD_INIT = BOOST_UBLAS_TYPENAME compressed_vector<T, IB, IA, TA>::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 coordinate_vector: public vector_expression<coordinate_vector<T, IB, IA, TA> > { public:#ifndef BOOST_UBLAS_NO_PROXY_SHORTCUTS BOOST_UBLAS_USING vector_expression<coordinate_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<coordinate_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 coordinate_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; typedef sparse_tag storage_category; // Construction and destruction BOOST_UBLAS_INLINE coordinate_vector (): vector_expression<self_type> (), size_ (0), non_zeros_ (restrict_nz (0)), filled_ (0), sorted_ (true), index_data_ (non_zeros_), value_data_ (non_zeros_) {} explicit BOOST_UBLAS_INLINE coordinate_vector (size_type size, size_type non_zeros = 0): vector_expression<self_type> (), size_ (size), non_zeros_ (restrict_nz (non_zeros)), filled_ (0), sorted_ (true), index_data_ (non_zeros_), value_data_ (non_zeros_) { } BOOST_UBLAS_INLINE coordinate_vector (const coordinate_vector &v): vector_expression<self_type> (), size_ (v.size_), non_zeros_ (v.non_zeros_), filled_ (v.filled_), sorted_ (v.sorted_), index_data_ (v.index_data_), value_data_ (v.value_data_) {} template<class AE> BOOST_UBLAS_INLINE coordinate_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), sorted_ (true), 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 { // minimum non_zeros non_zeros = (std::max) (non_zeros, size_type (1)); // ISSUE no maximum as coordinate may contain inserted duplicates return non_zeros; } public: BOOST_UBLAS_INLINE void resize (size_type size, bool preserve = true) { if (preserve) sort (); // remove duplicate elements. 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; } size_ = size; BOOST_UBLAS_CHECK (filled_ <= non_zeros_, internal_logic ()); } // Reserving BOOST_UBLAS_INLINE void reserve (size_type non_zeros, bool preserve = true) { if (preserve) sort (); // remove duplicate elements. 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; } BOOST_UBLAS_CHECK (filled_ <= non_zeros_, internal_logic ()); } // Proxy support#ifdef BOOST_UBLAS_STRICT_VECTOR_SPARSE pointer find_element (size_type i) { sort (); 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 ()); sort (); 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 sort (); 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)); sort (); 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); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -