vector_sparse.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 1,787 行 · 第 1/5 页

HPP
1,787
字号
        typedef reverse_iterator_base<const_iterator> const_reverse_iterator;        typedef reverse_iterator_base<iterator> reverse_iterator;        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 ());        }        BOOST_UBLAS_INLINE        reverse_iterator rbegin () {            return reverse_iterator (end ());        }        BOOST_UBLAS_INLINE        reverse_iterator rend () {            return reverse_iterator (begin ());        }         // Serialization        template<class Archive>        void serialize(Archive & ar, const unsigned int /* file_version */){            serialization::collection_size_type s (size_);            ar & serialization::make_nvp("size",s);            if (Archive::is_loading::value) {                size_ = s;            }            ar & serialization::make_nvp("data", data_);        }    private:        size_type size_;        array_type data_;        static const value_type zero_;    };    template<class T, class A>    const typename mapped_vector<T, A>::value_type mapped_vector<T, A>::zero_ = value_type/*zero*/();    // Compressed 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_container<compressed_vector<T, IB, IA, TA> > {        typedef T &true_reference;        typedef T *pointer;        typedef const T *const_pointer;        typedef compressed_vector<T, IB, IA, TA> self_type;    public:#ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS        using vector_container<self_type>::operator ();#endif        // ISSUE require type consistency check        // is_convertable (IA::size_type, TA::size_type)        typedef typename IA::value_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<self_type> reference;#endif        typedef IA index_array_type;        typedef TA value_array_type;        typedef const vector_reference<const self_type> const_closure_type;        typedef vector_reference<self_type> closure_type;        typedef self_type vector_temporary_type;        typedef sparse_tag storage_category;        // Construction and destruction        BOOST_UBLAS_INLINE        compressed_vector ():            vector_container<self_type> (),            size_ (0), capacity_ (restrict_capacity (0)), filled_ (0),            index_data_ (capacity_), value_data_ (capacity_) {            storage_invariants ();        }        explicit BOOST_UBLAS_INLINE        compressed_vector (size_type size, size_type non_zeros = 0):            vector_container<self_type> (),            size_ (size), capacity_ (restrict_capacity (non_zeros)), filled_ (0),            index_data_ (capacity_), value_data_ (capacity_) {        storage_invariants ();        }        BOOST_UBLAS_INLINE        compressed_vector (const compressed_vector &v):            vector_container<self_type> (),            size_ (v.size_), capacity_ (v.capacity_), filled_ (v.filled_),            index_data_ (v.index_data_), value_data_ (v.value_data_) {            storage_invariants ();        }        template<class AE>        BOOST_UBLAS_INLINE        compressed_vector (const vector_expression<AE> &ae, size_type non_zeros = 0):            vector_container<self_type> (),            size_ (ae ().size ()), capacity_ (restrict_capacity (non_zeros)), filled_ (0),            index_data_ (capacity_), value_data_ (capacity_) {            storage_invariants ();            vector_assign<scalar_assign> (*this, ae);        }        // Accessors        BOOST_UBLAS_INLINE        size_type size () const {            return size_;        }        BOOST_UBLAS_INLINE        size_type nnz_capacity () const {            return capacity_;        }        BOOST_UBLAS_INLINE        size_type nnz () const {            return filled_;        }        // Storage accessors        BOOST_UBLAS_INLINE        static size_type index_base () {            return IB;        }        BOOST_UBLAS_INLINE        typename index_array_type::size_type filled () const {            return filled_;        }        BOOST_UBLAS_INLINE        const index_array_type &index_data () const {            return index_data_;        }        BOOST_UBLAS_INLINE        const value_array_type &value_data () const {            return value_data_;        }        BOOST_UBLAS_INLINE        void set_filled (const typename index_array_type::size_type & filled) {            filled_ = filled;            storage_invariants ();        }        BOOST_UBLAS_INLINE        index_array_type &index_data () {            return index_data_;        }        BOOST_UBLAS_INLINE        value_array_type &value_data () {            return value_data_;        }        // Resizing    private:        BOOST_UBLAS_INLINE        size_type restrict_capacity (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) {            size_ = size;            capacity_ = restrict_capacity (capacity_);            if (preserve) {                index_data_. resize (capacity_, size_type ());                value_data_. resize (capacity_, value_type ());                filled_ = (std::min) (capacity_, filled_);                while ((filled_ > 0) && (zero_based(index_data_[filled_ - 1]) >= size)) {                    --filled_;                }            }            else {                index_data_. resize (capacity_);                value_data_. resize (capacity_);                filled_ = 0;            }            storage_invariants ();        }        // Reserving        BOOST_UBLAS_INLINE        void reserve (size_type non_zeros, bool preserve = true) {            capacity_ = restrict_capacity (non_zeros);            if (preserve) {                index_data_. resize (capacity_, size_type ());                value_data_. resize (capacity_, value_type ());                filled_ = (std::min) (capacity_, filled_);            }            else {                index_data_. resize (capacity_);                value_data_. resize (capacity_);                filled_ = 0;            }            storage_invariants ();        }        // Element support        BOOST_UBLAS_INLINE        pointer find_element (size_type i) {            return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i));        }        BOOST_UBLAS_INLINE        const_pointer find_element (size_type i) const {            const_subiterator_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 ()];        }        // Element access        BOOST_UBLAS_INLINE        const_reference operator () (size_type i) const {            BOOST_UBLAS_CHECK (i < size_, bad_index ());            const_subiterator_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        true_reference ref (size_type i) {            BOOST_UBLAS_CHECK (i < size_, bad_index ());            subiterator_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 insert_element (i, value_type/*zero*/());            else                return value_data_ [it - index_data_.begin ()];        }        BOOST_UBLAS_INLINE        reference operator () (size_type i) {#ifndef BOOST_UBLAS_STRICT_VECTOR_SPARSE            return ref (i) ;#else            BOOST_UBLAS_CHECK (i < size_, bad_index ());            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);        }        // Element assignment        BOOST_UBLAS_INLINE        true_reference insert_element (size_type i, const_reference t) {            BOOST_UBLAS_CHECK (!find_element (i), bad_index ());        // duplicate element            if (filled_ >= capacity_)                reserve (2 * capacity_, true);            subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));            // ISSUE max_capacity limit due to difference_type            typename std::iterator_traits<subiterator_type>::difference_type n = it - index_data_.begin ();            BOOST_UBLAS_CHECK (filled_ == 0 || filled_ == typename index_array_type::size_type (n) || *it != k_based (i), internal_logic ());   // duplicate found by lower_bound            ++ 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;            storage_invariants ();            return *itt;        }        BOOST_UBLAS_INLINE        void erase_element (size_type i) {            subiterator_type it (detail::lower_bound (index_data_.begin (), index_data_.begin () + filled_, k_based (i), std::less<size_type> ()));            typename std::iterator_traits<subiterator_type>::difference_type  n = it - index_data_.begin ();            if (filled_ > typename index_array_type::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_;            }            storage_invariants ();        }                // Zeroing        BOOST_UBLAS_INLINE        void clear () {            filled_ = 0;            storage_invariants ();        }        // Assignment        BOOST_UBLAS_INLINE        compressed_vector &operator = (const compressed_vector &v) {            if (this != &v) {                size_ = v.size_;                capacity_ = v.capacity_;                filled_ = v.filled_;                index_data_ = v.index_data_;                value_data_ = v.value_data_;            }            storage_invariants ();            return *this;        }        template<class C>          // Container assignment without temporary        BOOST_UBLAS_INLINE        compressed_vector &operator = (const vector_container<C> &v) {            resize (v ().size (), false);            assign (v);            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) {            self_type temporary (ae, capacity_);            return assign_temporary (temporary);        }        template<class AE>        BOOST_UBLAS_INLINE        compressed_vector &assign (const vector_expression<AE> &ae) {            vector_assign<scalar_assign> (*this, ae);            return *this;        }        // Computed assignment        template<class AE>        BOOST_UBLAS_INLINE        compressed_vector &operator += (const vector_expression<AE> &ae) {            self_type temporary (*this + ae, capacity_);            return assign_temporary (temporary);        }        template<class C>          // Container assignment without temporary        BOOST_UBLAS_INLINE        compressed_vector &operator += (const vector_container<C> &v) {            plus_assign (v);            return *this;        }        template<class AE>        BOOST_UBLAS_INLINE        compressed_vector &plus_assign (const vector_expression<AE> &ae) {            vector_assign<scalar_plus_assign> (*this, ae);            return *this;        }        template<class AE>        BOOST_UBLAS_INLINE        compressed_vector &operator -= (const vector_expression<AE> &ae) {            self_type temporary (*this - ae, capacity_);            return assign_temporary (temporary);        }        template<class C>          // Container assignment without temporary        BOOST_UBLAS_INLINE        compressed_vector &operator -= (const vector_container<C> &v) {            minus_assign (v);            return *this;        }        template<class AE>        BOOST_UBLAS_INLINE        compressed_vector &minus_assign (const vector_expression<AE> &ae) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?