📄 storage_sparse.hpp
字号:
//// Copyright (c) 2000-2002// Joerg Walter, Mathias Koch//// Distributed under the Boost Software License, Version 1.0. (See// accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)//// The authors gratefully acknowledge the support of// GeNeSys mbH & Co. KG in producing this work.//#ifndef _BOOST_UBLAS_STORAGE_SPARSE_#define _BOOST_UBLAS_STORAGE_SPARSE_#include <map>#include <boost/serialization/collection_size_type.hpp>#include <boost/serialization/nvp.hpp>#include <boost/serialization/array.hpp>#include <boost/serialization/map.hpp>#include <boost/serialization/base_object.hpp>#include <boost/numeric/ublas/storage.hpp>namespace boost { namespace numeric { namespace ublas { namespace detail { template<class I, class T, class C> BOOST_UBLAS_INLINE I lower_bound (const I &begin, const I &end, const T &t, C compare) { // t <= *begin <=> ! (*begin < t) if (begin == end || ! compare (*begin, t)) return begin; if (compare (*(end - 1), t)) return end; return std::lower_bound (begin, end, t, compare); } template<class I, class T, class C> BOOST_UBLAS_INLINE I upper_bound (const I &begin, const I &end, const T &t, C compare) { if (begin == end || compare (t, *begin)) return begin; // (*end - 1) <= t <=> ! (t < *end) if (! compare (t, *(end - 1))) return end; return std::upper_bound (begin, end, t, compare); } template<class P> struct less_pair { BOOST_UBLAS_INLINE bool operator () (const P &p1, const P &p2) { return p1.first < p2.first; } }; template<class T> struct less_triple { BOOST_UBLAS_INLINE bool operator () (const T &t1, const T &t2) { return t1.first.first < t2.first.first || (t1.first.first == t2.first.first && t1.first.second < t2.first.second); } }; }#ifdef BOOST_UBLAS_STRICT_MAP_ARRAY template<class A> class sparse_storage_element: public container_reference<A> { public: typedef A array_type; typedef typename A::key_type index_type; typedef typename A::mapped_type data_value_type; // typedef const data_value_type &data_const_reference; typedef typename type_traits<data_value_type>::const_reference data_const_reference; typedef data_value_type &data_reference; typedef typename A::value_type value_type; typedef value_type *pointer; // Construction and destruction BOOST_UBLAS_INLINE sparse_storage_element (array_type &a, pointer it): container_reference<array_type> (a), it_ (it), i_ (it->first), d_ (it->second), dirty_ (false) {} BOOST_UBLAS_INLINE sparse_storage_element (array_type &a, index_type i): container_reference<array_type> (a), it_ (), i_ (i), d_ (), dirty_ (false) { pointer it = (*this) ().find (i_); if (it == (*this) ().end ()) it = (*this) ().insert ((*this) ().end (), value_type (i_, d_)); d_ = it->second; } BOOST_UBLAS_INLINE ~sparse_storage_element () { if (dirty_) { if (! it_) it_ = (*this) ().find (i_); BOOST_UBLAS_CHECK (it_ != (*this) ().end (), internal_logic ()); it_->second = d_; } } // Element access - only if data_const_reference is defined BOOST_UBLAS_INLINE typename data_value_type::data_const_reference operator [] (index_type i) const { return d_ [i]; } // Assignment BOOST_UBLAS_INLINE sparse_storage_element &operator = (const sparse_storage_element &p) { // Overide the implict copy assignment d_ = p.d_; dirty_ = true; return *this; } template<class D> BOOST_UBLAS_INLINE sparse_storage_element &operator = (const D &d) { d_ = d; dirty_ = true; return *this; } template<class D> BOOST_UBLAS_INLINE sparse_storage_element &operator += (const D &d) { d_ += d; dirty_ = true; return *this; } template<class D> BOOST_UBLAS_INLINE sparse_storage_element &operator -= (const D &d) { d_ -= d; dirty_ = true; return *this; } template<class D> BOOST_UBLAS_INLINE sparse_storage_element &operator *= (const D &d) { d_ *= d; dirty_ = true; return *this; } template<class D> BOOST_UBLAS_INLINE sparse_storage_element &operator /= (const D &d) { d_ /= d; dirty_ = true; return *this; } // Comparison template<class D> BOOST_UBLAS_INLINE bool operator == (const D &d) const { return d_ == d; } template<class D> BOOST_UBLAS_INLINE bool operator != (const D &d) const { return d_ != d; } // Conversion BOOST_UBLAS_INLINE operator data_const_reference () const { return d_; } // Swapping BOOST_UBLAS_INLINE void swap (sparse_storage_element p) { if (this != &p) { dirty_ = true; p.dirty_ = true; std::swap (d_, p.d_); } } BOOST_UBLAS_INLINE friend void swap (sparse_storage_element p1, sparse_storage_element p2) { p1.swap (p2); } private: pointer it_; index_type i_; data_value_type d_; bool dirty_; };#endif // Default map type is simply forwarded to std::map // FIXME should use ALLOC for map but std::allocator of std::pair<const I, T> and std::pair<I,T> fail to compile template<class I, class T, class ALLOC> class map_std : public std::map<I, T /*, ALLOC */> { public: // Serialization template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */){ ar & serialization::make_nvp("base", boost::serialization::base_object< std::map<I, T /*, ALLOC */> >(*this)); } }; // Map array // Implementation requires pair<I, T> allocator definition (without const) template<class I, class T, class ALLOC> class map_array { public: typedef ALLOC allocator_type; typedef typename ALLOC::size_type size_type; typedef typename ALLOC::difference_type difference_type; typedef std::pair<I,T> value_type; typedef I key_type; typedef T mapped_type; typedef const value_type &const_reference; typedef value_type &reference; typedef const value_type *const_pointer; typedef value_type *pointer; // Iterators simply are pointers. typedef const_pointer const_iterator; typedef pointer iterator; typedef const T &data_const_reference;#ifndef BOOST_UBLAS_STRICT_MAP_ARRAY typedef T &data_reference;#else typedef sparse_storage_element<map_array> data_reference;#endif // Construction and destruction BOOST_UBLAS_INLINE map_array (const ALLOC &a = ALLOC()): alloc_(a), capacity_ (0), size_ (0) { data_ = 0; } BOOST_UBLAS_INLINE map_array (const map_array &c): alloc_ (c.alloc_), capacity_ (c.size_), size_ (c.size_) { if (capacity_) { data_ = alloc_.allocate (capacity_); std::uninitialized_copy (data_, data_ + capacity_, c.data_); // capacity != size_ requires uninitialized_fill (size_ to capacity_) } else data_ = 0; } BOOST_UBLAS_INLINE ~map_array () { if (capacity_) { std::for_each (data_, data_ + capacity_, static_destroy); alloc_.deallocate (data_, capacity_); } } private: // Resizing - implicitly exposses uninitialized (but default constructed) mapped_type BOOST_UBLAS_INLINE void resize (size_type size) { BOOST_UBLAS_CHECK (size_ <= capacity_, internal_logic ()); if (size > capacity_) { const size_type capacity = size << 1; BOOST_UBLAS_CHECK (capacity, internal_logic ()); pointer data = alloc_.allocate (capacity); std::uninitialized_copy (data_, data_ + (std::min) (size, size_), data); std::uninitialized_fill (data + (std::min) (size, size_), data + capacity, value_type ()); if (capacity_) { std::for_each (data_, data_ + capacity_, static_destroy); alloc_.deallocate (data_, capacity_); } capacity_ = capacity; data_ = data; } size_ = size;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -