⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vector_sparse.hpp

📁 boost库提供标准的C++ API 配合dev c++使用,功能更加强大
💻 HPP
📖 第 1 页 / 共 5 页
字号:
#else
        typedef sparse_vector_element<coordinate_vector<T, IB, IA, TA> > reference;
#endif
        typedef const T *const_pointer;
        typedef T *pointer;
        typedef IA index_array_type;
        typedef TA value_array_type;
        typedef const coordinate_vector<T, IB, IA, TA> const_self_type;
        typedef coordinate_vector<T, IB, IA, TA> self_type;
#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 typename IA::const_iterator const_iterator_type;
        typedef typename IA::iterator iterator_type;
        typedef sparse_tag storage_category;

        // Construction and destruction
        BOOST_UBLAS_INLINE
        coordinate_vector ():
            vector_expression<self_type> (),
            size_ (0), non_zeros_ (0), filled_ (0),
            sorted_ (true), index_data_ (), value_data_ () {}
        BOOST_UBLAS_INLINE
        coordinate_vector (size_type size, size_type non_zeros = 0):
            vector_expression<self_type> (),
            size_ (size), non_zeros_ (non_zeros), filled_ (0),
            sorted_ (true), index_data_ (non_zeros), value_data_ (non_zeros) {
            reserve (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_ (non_zeros), filled_ (0),
            sorted_ (true), index_data_ (non_zeros), value_data_ (non_zeros) {
            reserve (non_zeros_, false);
            vector_assign (scalar_assign<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 index_base_;
        }
        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
        BOOST_UBLAS_INLINE
        void resize (size_type size, size_type non_zeros = 0, bool preserve = true) {
            size_ = size;
            non_zeros_ = std::max (non_zeros, size_type (1));
            // FIX: coordinate_vector may contain duplicate elements.
            // non_zeros_ = std::min (non_zeros_, size_);
            filled_ = 0;
            detail::resize (index_data (), non_zeros_, preserve);
            detail::resize (value_data (), non_zeros_, preserve);
        }

        // Reserving
        BOOST_UBLAS_INLINE
        void reserve (size_type non_zeros = 0, bool preserve = true) {
            non_zeros_ = std::max (non_zeros, size_type (1));
            // FIX: coordinate_vector may contain duplicate elements.
            // non_zeros_ = std::min (non_zeros_, size_);
            detail::resize (index_data (), non_zeros_, preserve);
            detail::resize (value_data (), non_zeros_, preserve);
        }

        // 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 ());
                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);
        }

        // Assignment
        BOOST_UBLAS_INLINE
        coordinate_vector &operator = (const coordinate_vector &v) {
            // Too unusual semantic.
            // BOOST_UBLAS_CHECK (this != &v, external_logic ());
            if (this != &v) {
                // Precondition for container relaxed as requested during review.
                // BOOST_UBLAS_CHECK (size_ == v.size_, bad_size ());
                size_ = v.size_;
                non_zeros_ = v.non_zeros_;
                filled_ = v.filled_;
                sorted_ = v.sorted_;
                detail::resize (index_data (), non_zeros_, false);
                detail::resize (value_data (), non_zeros_, false);
                index_data () = v.index_data ();
                value_data () = v.value_data ();
            }
            return *this;
        }
        BOOST_UBLAS_INLINE
        coordinate_vector &assign_temporary (coordinate_vector &v) {
            swap (v);
            return *this;
        }
        template<class AE>
        BOOST_UBLAS_INLINE
        coordinate_vector &operator = (const vector_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
        coordinate_vector &reset (const vector_expression<AE> &ae) {
            self_type temporary (ae, non_zeros_);
            resize (temporary.size (), non_zeros_, false);
            return assign_temporary (temporary);
        }
        template<class AE>
        BOOST_UBLAS_INLINE
        coordinate_vector &assign (const vector_expression<AE> &ae) {
            vector_assign (scalar_assign<reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae);
            return *this;
        }
        template<class AE>
        BOOST_UBLAS_INLINE
        coordinate_vector &operator += (const vector_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
        coordinate_vector &plus_assign (const vector_expression<AE> &ae) {
            vector_assign (scalar_plus_assign<reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae);
            return *this;
        }
        template<class AE>
        BOOST_UBLAS_INLINE
        coordinate_vector &operator -= (const vector_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
        coordinate_vector &minus_assign (const vector_expression<AE> &ae) {
            vector_assign (scalar_minus_assign<reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae);
            return *this;
        }
        template<class AT>
        BOOST_UBLAS_INLINE
        coordinate_vector &operator *= (const AT &at) {
            vector_assign_scalar (scalar_multiplies_assign<reference, AT> (), *this, at);
            return *this;
        }
        template<class AT>
        BOOST_UBLAS_INLINE
        coordinate_vector &operator /= (const AT &at) {
            vector_assign_scalar (scalar_divides_assign<reference, AT> (), *this, at);
            return *this;
        }

        // Swapping
        BOOST_UBLAS_INLINE
        void swap (coordinate_vector &v) {
            // Too unusual semantic.
            // BOOST_UBLAS_CHECK (this != &v, external_logic ());
            if (this != &v) {
                // Precondition for container relaxed as requested during review.
                // BOOST_UBLAS_CHECK (size_ == v.size_, bad_size ());
                // BOOST_UBLAS_CHECK (non_zeros_ == v.non_zeros_, bad_size ());
                std::swap (size_, v.size_);
                std::swap (non_zeros_, v.non_zeros_);
                std::swap (filled_, v.filled_);
                std::swap (sorted_, v.sorted_);
                index_data ().swap (v.index_data ());
                value_data ().swap (v.value_data ());
            }
        }
#ifndef BOOST_UBLAS_NO_MEMBER_FRIENDS
        BOOST_UBLAS_INLINE
        friend void swap (coordinate_vector &v1, coordinate_vector &v2) {
            v1.swap (v2);
        }
#endif

        // Sorting
        BOOST_UBLAS_INLINE
        void sort () const {
            if (! sorted_ && filled_ > 0) {
                index_pair_array<index_array_type, value_array_type>
                    ipa (filled_, index_data_, value_data_);
                std::sort (ipa.begin (), ipa.end ());
                // FIX: check for duplicates
                size_type filled = 1;
                for (size_type i = 1; i < filled_; ++ i) {
                    if (index_data_ [filled - 1] != index_data_ [i]) {
                        ++ filled;
                        if (filled - 1 != i) {
                            index_data_ [filled - 1] = index_data_ [i];
                            value_data_ [filled - 1] = value_data_ [i];
                        }
                    } else {
                        value_data_ [filled - 1] += value_data_ [i];
                    }
                }
                filled_ = filled;
                sorted_ = true;
            }
        }

        // Element insertion and erasure
        BOOST_UBLAS_INLINE
        void push_back (size_type i, const_reference t) {
            if (filled_ >= non_zeros_)
                reserve (2 * non_zeros_);
            if (filled_ == 0 || index_data () [filled_ - 1] < k_based (i)) {
                ++ filled_;
                index_data () [filled_ - 1] = k_based (i);
                value_data () [filled_ - 1] = t;
                return;
            }
            // Raising exceptions abstracted as requested during review.
            // throw external_logic ();
            external_logic ().raise ();
        }
        BOOST_UBLAS_INLINE
        void insert (size_type i, const_reference t) {
            if (filled_ >= non_zeros_)
                reserve (2 * non_zeros_);
            ++ filled_;
            index_data () [filled_ - 1] = k_based (i);
            value_data () [filled_ - 1] = t;
            sorted_ = false;
        }
        BOOST_UBLAS_INLINE
        void pop_back () {
            BOOST_UBLAS_CHECK (filled_ > 0, external_logic ());
            -- filled_;
        }
        BOOST_UBLAS_INLINE
        void erase (size_type i) {
            sort ();
            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 () {
        

⌨️ 快捷键说明

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