Chris@16: // Chris@16: // Copyright (c) 2000-2002 Chris@16: // Joerg Walter, Mathias Koch Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // The authors gratefully acknowledge the support of Chris@16: // GeNeSys mbH & Co. KG in producing this work. Chris@16: // Chris@16: Chris@16: #ifndef _BOOST_UBLAS_VECTOR_PROXY_ Chris@16: #define _BOOST_UBLAS_VECTOR_PROXY_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // Iterators based on ideas of Jeremy Siek Chris@16: Chris@16: namespace boost { namespace numeric { namespace ublas { Chris@16: Chris@16: /** \brief A vector referencing a continuous subvector of elements of vector \c v containing all elements specified by \c range. Chris@16: * Chris@16: * A vector range can be used as a normal vector in any expression. Chris@16: * If the specified range falls outside that of the index range of the vector, then Chris@16: * the \c vector_range is not a well formed \i Vector \i Expression and access to an Chris@16: * element outside of index range of the vector is \b undefined. Chris@16: * Chris@16: * \tparam V the type of vector referenced (for example \c vector) Chris@16: */ Chris@16: template Chris@16: class vector_range: Chris@16: public vector_expression > { Chris@16: Chris@16: typedef vector_range self_type; Chris@16: public: Chris@16: #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS Chris@16: using vector_expression::operator (); Chris@16: #endif Chris@16: typedef const V const_vector_type; Chris@16: typedef V vector_type; Chris@16: typedef typename V::size_type size_type; Chris@16: typedef typename V::difference_type difference_type; Chris@16: typedef typename V::value_type value_type; Chris@16: typedef typename V::const_reference const_reference; Chris@16: typedef typename boost::mpl::if_, Chris@16: typename V::const_reference, Chris@16: typename V::reference>::type reference; Chris@16: typedef typename boost::mpl::if_, Chris@16: typename V::const_closure_type, Chris@16: typename V::closure_type>::type vector_closure_type; Chris@16: typedef basic_range range_type; Chris@16: typedef const self_type const_closure_type; Chris@16: typedef self_type closure_type; Chris@16: typedef typename storage_restrict_traits::storage_category storage_category; Chris@16: Chris@16: // Construction and destruction Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range (vector_type &data, const range_type &r): Chris@16: data_ (data), r_ (r.preprocess (data.size ())) { Chris@16: // Early checking of preconditions here. Chris@16: // BOOST_UBLAS_CHECK (r_.start () <= data_.size () && Chris@16: // r_.start () + r_.size () <= data_.size (), bad_index ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range (const vector_closure_type &data, const range_type &r, bool): Chris@16: data_ (data), r_ (r.preprocess (data.size ())) { Chris@16: // Early checking of preconditions here. Chris@16: // BOOST_UBLAS_CHECK (r_.start () <= data_.size () && Chris@16: // r_.start () + r_.size () <= data_.size (), bad_index ()); Chris@16: } Chris@16: Chris@16: // Accessors Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type start () const { Chris@16: return r_.start (); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type size () const { Chris@16: return r_.size (); Chris@16: } Chris@16: Chris@16: // Storage accessors Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_closure_type &data () const { Chris@16: return data_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_closure_type &data () { Chris@16: return data_; Chris@16: } Chris@16: Chris@16: // Element access Chris@16: #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator () (size_type i) const { Chris@16: return data_ (r_ (i)); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator () (size_type i) { Chris@16: return data_ (r_ (i)); Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator [] (size_type i) const { Chris@16: return (*this) (i); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator [] (size_type i) { Chris@16: return (*this) (i); Chris@16: } Chris@16: #else Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator () (size_type i) const { Chris@16: return data_ (r_ (i)); Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator [] (size_type i) const { Chris@16: return (*this) (i); Chris@16: } Chris@16: #endif Chris@16: Chris@16: // ISSUE can this be done in free project function? Chris@16: // Although a const function can create a non-const proxy to a non-const object Chris@16: // Critical is that vector_type and data_ (vector_closure_type) are const correct Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range project (const range_type &r) const { Chris@16: return vector_range (data_, r_.compose (r.preprocess (data_.size ())), false); Chris@16: } Chris@16: Chris@16: // Assignment Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range &operator = (const vector_range &vr) { Chris@16: // ISSUE need a temporary, proxy can be overlaping alias Chris@16: vector_assign (*this, typename vector_temporary_traits::type (vr)); Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range &assign_temporary (vector_range &vr) { Chris@16: // assign elements, proxied container remains the same Chris@16: vector_assign (*this, vr); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range &operator = (const vector_expression &ae) { Chris@16: vector_assign (*this, typename vector_temporary_traits::type (ae)); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range &assign (const vector_expression &ae) { Chris@16: vector_assign (*this, ae); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range &operator += (const vector_expression &ae) { Chris@16: vector_assign (*this, typename vector_temporary_traits::type (*this + ae)); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range &plus_assign (const vector_expression &ae) { Chris@16: vector_assign (*this, ae); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range &operator -= (const vector_expression &ae) { Chris@16: vector_assign (*this, typename vector_temporary_traits::type (*this - ae)); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range &minus_assign (const vector_expression &ae) { Chris@16: vector_assign (*this, ae); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range &operator *= (const AT &at) { Chris@16: vector_assign_scalar (*this, at); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range &operator /= (const AT &at) { Chris@16: vector_assign_scalar (*this, at); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Closure comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool same_closure (const vector_range &vr) const { Chris@16: return (*this).data_.same_closure (vr.data_); Chris@16: } Chris@16: Chris@16: // Comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator == (const vector_range &vr) const { Chris@16: return (*this).data_ == vr.data_ && r_ == vr.r_; Chris@16: } Chris@16: Chris@16: // Swapping Chris@16: BOOST_UBLAS_INLINE Chris@16: void swap (vector_range vr) { Chris@16: if (this != &vr) { Chris@16: BOOST_UBLAS_CHECK (size () == vr.size (), bad_size ()); Chris@16: // Sparse ranges may be nonconformant now. Chris@16: // std::swap_ranges (begin (), end (), vr.begin ()); Chris@16: vector_swap (*this, vr); Chris@16: } Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: friend void swap (vector_range vr1, vector_range vr2) { Chris@16: vr1.swap (vr2); Chris@16: } Chris@16: Chris@16: // Iterator types Chris@16: private: Chris@16: typedef typename V::const_iterator const_subiterator_type; Chris@16: typedef typename boost::mpl::if_, Chris@16: typename V::const_iterator, Chris@16: typename V::iterator>::type subiterator_type; Chris@16: Chris@16: public: Chris@16: #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: typedef indexed_iterator, Chris@16: typename subiterator_type::iterator_category> iterator; Chris@16: typedef indexed_const_iterator, Chris@16: typename const_subiterator_type::iterator_category> const_iterator; Chris@16: #else Chris@16: class const_iterator; Chris@16: class iterator; Chris@16: #endif Chris@16: Chris@16: // Element lookup Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator find (size_type i) const { Chris@16: const_subiterator_type it (data_.find (start () + i)); Chris@16: #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: return const_iterator (*this, it.index ()); Chris@16: #else Chris@16: return const_iterator (*this, it); Chris@16: #endif Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator find (size_type i) { Chris@16: subiterator_type it (data_.find (start () + i)); Chris@16: #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: return iterator (*this, it.index ()); Chris@16: #else Chris@16: return iterator (*this, it); Chris@16: #endif Chris@16: } Chris@16: Chris@16: #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: class const_iterator: Chris@16: public container_const_reference, Chris@16: public iterator_base_traits::template Chris@16: iterator_base::type { Chris@16: public: Chris@16: typedef typename const_subiterator_type::difference_type difference_type; Chris@16: typedef typename const_subiterator_type::value_type value_type; Chris@16: typedef typename const_subiterator_type::reference reference; Chris@16: typedef typename const_subiterator_type::pointer pointer; Chris@16: Chris@16: // Construction and destruction Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator (): Chris@16: container_const_reference (), it_ () {} Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator (const self_type &vr, const const_subiterator_type &it): Chris@16: container_const_reference (vr), it_ (it) {} Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here Chris@16: container_const_reference (it ()), it_ (it.it_) {} Chris@16: Chris@16: // Arithmetic Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator ++ () { Chris@16: ++ it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator -- () { Chris@16: -- it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator += (difference_type n) { Chris@16: it_ += n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator -= (difference_type n) { Chris@16: it_ -= n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: difference_type operator - (const const_iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ - it.it_; Chris@16: } Chris@16: Chris@16: // Dereference Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator * () const { Chris@16: BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ()); Chris@16: return *it_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator [] (difference_type n) const { Chris@16: return *(*this + n); Chris@16: } Chris@16: Chris@16: // Index Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type index () const { Chris@16: return it_.index () - (*this) ().start (); Chris@16: } Chris@16: Chris@16: // Assignment Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator = (const const_iterator &it) { Chris@16: container_const_reference::assign (&it ()); Chris@16: it_ = it.it_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator == (const const_iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ == it.it_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator < (const const_iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ < it.it_; Chris@16: } Chris@16: Chris@16: private: Chris@16: const_subiterator_type it_; Chris@16: }; Chris@16: #endif Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator begin () const { Chris@16: return find (0); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@101: const_iterator cbegin () const { Chris@101: return begin (); Chris@101: } Chris@101: BOOST_UBLAS_INLINE Chris@16: const_iterator end () const { Chris@16: return find (size ()); Chris@16: } Chris@101: BOOST_UBLAS_INLINE Chris@101: const_iterator cend () const { Chris@101: return end (); Chris@101: } Chris@16: Chris@16: #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: class iterator: Chris@16: public container_reference, Chris@16: public iterator_base_traits::template Chris@16: iterator_base::type { Chris@16: public: Chris@16: typedef typename subiterator_type::difference_type difference_type; Chris@16: typedef typename subiterator_type::value_type value_type; Chris@16: typedef typename subiterator_type::reference reference; Chris@16: typedef typename subiterator_type::pointer pointer; Chris@16: Chris@16: // Construction and destruction Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator (): Chris@16: container_reference (), it_ () {} Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator (self_type &vr, const subiterator_type &it): Chris@16: container_reference (vr), it_ (it) {} Chris@16: Chris@16: // Arithmetic Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator ++ () { Chris@16: ++ it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator -- () { Chris@16: -- it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator += (difference_type n) { Chris@16: it_ += n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator -= (difference_type n) { Chris@16: it_ -= n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: difference_type operator - (const iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ - it.it_; Chris@16: } Chris@16: Chris@16: // Dereference Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator * () const { Chris@16: BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ()); Chris@16: return *it_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator [] (difference_type n) const { Chris@16: return *(*this + n); Chris@16: } Chris@16: Chris@16: // Index Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type index () const { Chris@16: return it_.index () - (*this) ().start (); Chris@16: } Chris@16: Chris@16: // Assignment Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator = (const iterator &it) { Chris@16: container_reference::assign (&it ()); Chris@16: it_ = it.it_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator == (const iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ == it.it_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator < (const iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ < it.it_; Chris@16: } Chris@16: Chris@16: private: Chris@16: subiterator_type it_; Chris@16: Chris@16: friend class const_iterator; Chris@16: }; Chris@16: #endif Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator begin () { Chris@16: return find (0); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator end () { Chris@16: return find (size ()); Chris@16: } Chris@16: Chris@16: // Reverse iterator Chris@16: typedef reverse_iterator_base const_reverse_iterator; Chris@16: typedef reverse_iterator_base reverse_iterator; Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reverse_iterator rbegin () const { Chris@16: return const_reverse_iterator (end ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@101: const_reverse_iterator crbegin () const { Chris@101: return rbegin (); Chris@101: } Chris@101: BOOST_UBLAS_INLINE Chris@16: const_reverse_iterator rend () const { Chris@16: return const_reverse_iterator (begin ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@101: const_reverse_iterator crend () const { Chris@101: return rend (); Chris@101: } Chris@101: Chris@101: BOOST_UBLAS_INLINE Chris@16: reverse_iterator rbegin () { Chris@16: return reverse_iterator (end ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reverse_iterator rend () { Chris@16: return reverse_iterator (begin ()); Chris@16: } Chris@16: Chris@16: private: Chris@16: vector_closure_type data_; Chris@16: range_type r_; Chris@16: }; Chris@16: Chris@16: // ------------------ Chris@16: // Simple Projections Chris@16: // ------------------ Chris@16: Chris@16: /** \brief Return a \c vector_range on a specified vector, a start and stop index. Chris@16: * Return a \c vector_range on a specified vector, a start and stop index. The resulting \c vector_range can be manipulated like a normal vector. Chris@16: * If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed Chris@16: * Vector Expression and access to an element outside of index range of the vector is \b undefined. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range subrange (V &data, typename V::size_type start, typename V::size_type stop) { Chris@16: typedef basic_range range_type; Chris@16: return vector_range (data, range_type (start, stop)); Chris@16: } Chris@16: Chris@16: /** \brief Return a \c const \c vector_range on a specified vector, a start and stop index. Chris@16: * Return a \c const \c vector_range on a specified vector, a start and stop index. The resulting \c const \c vector_range can be manipulated like a normal vector. Chris@16: *If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed Chris@16: * Vector Expression and access to an element outside of index range of the vector is \b undefined. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range subrange (const V &data, typename V::size_type start, typename V::size_type stop) { Chris@16: typedef basic_range range_type; Chris@16: return vector_range (data, range_type (start, stop)); Chris@16: } Chris@16: Chris@16: // ------------------- Chris@16: // Generic Projections Chris@16: // ------------------- Chris@16: Chris@16: /** \brief Return a \c const \c vector_range on a specified vector and \c range Chris@16: * Return a \c const \c vector_range on a specified vector and \c range. The resulting \c vector_range can be manipulated like a normal vector. Chris@16: * If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed Chris@16: * Vector Expression and access to an element outside of index range of the vector is \b undefined. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range project (V &data, typename vector_range::range_type const &r) { Chris@16: return vector_range (data, r); Chris@16: } Chris@16: Chris@16: /** \brief Return a \c vector_range on a specified vector and \c range Chris@16: * Return a \c vector_range on a specified vector and \c range. The resulting \c vector_range can be manipulated like a normal vector. Chris@16: * If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed Chris@16: * Vector Expression and access to an element outside of index range of the vector is \b undefined. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_range project (const V &data, typename vector_range::range_type const &r) { Chris@16: // ISSUE was: return vector_range (const_cast (data), r); Chris@16: return vector_range (data, r); Chris@16: } Chris@16: Chris@16: /** \brief Return a \c const \c vector_range on a specified vector and const \c range Chris@16: * Return a \c const \c vector_range on a specified vector and const \c range. The resulting \c vector_range can be manipulated like a normal vector. Chris@16: * If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed Chris@16: * Vector Expression and access to an element outside of index range of the vector is \b undefined. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_range project (vector_range &data, const typename vector_range::range_type &r) { Chris@16: return data.project (r); Chris@16: } Chris@16: Chris@16: /** \brief Return a \c vector_range on a specified vector and const \c range Chris@16: * Return a \c vector_range on a specified vector and const \c range. The resulting \c vector_range can be manipulated like a normal vector. Chris@16: * If the specified range falls outside that of of the index range of the vector, then the resulting \c vector_range is not a well formed Chris@16: * Vector Expression and access to an element outside of index range of the vector is \b undefined. Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_range project (const vector_range &data, const typename vector_range::range_type &r) { Chris@16: return data.project (r); Chris@16: } Chris@16: Chris@16: // Specialization of temporary_traits Chris@16: template Chris@16: struct vector_temporary_traits< vector_range > Chris@16: : vector_temporary_traits< V > {} ; Chris@16: template Chris@16: struct vector_temporary_traits< const vector_range > Chris@16: : vector_temporary_traits< V > {} ; Chris@16: Chris@16: Chris@16: /** \brief A vector referencing a non continuous subvector of elements of vector v containing all elements specified by \c slice. Chris@16: * Chris@16: * A vector slice can be used as a normal vector in any expression. Chris@16: * If the specified slice falls outside that of the index slice of the vector, then Chris@16: * the \c vector_slice is not a well formed \i Vector \i Expression and access to an Chris@16: * element outside of index slice of the vector is \b undefined. Chris@16: * Chris@16: * A slice is a generalization of a range. In a range going from \f$a\f$ to \f$b\f$, Chris@16: * all elements belong to the range. In a slice, a \i \f$step\f$ can be specified meaning to Chris@16: * take one element over \f$step\f$ in the range specified from \f$a\f$ to \f$b\f$. Chris@16: * Obviously, a slice with a \f$step\f$ of 1 is equivalent to a range. Chris@16: * Chris@16: * \tparam V the type of vector referenced (for example \c vector) Chris@16: */ Chris@16: template Chris@16: class vector_slice: Chris@16: public vector_expression > { Chris@16: Chris@16: typedef vector_slice self_type; Chris@16: public: Chris@16: #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS Chris@16: using vector_expression::operator (); Chris@16: #endif Chris@16: typedef const V const_vector_type; Chris@16: typedef V vector_type; Chris@16: typedef typename V::size_type size_type; Chris@16: typedef typename V::difference_type difference_type; Chris@16: typedef typename V::value_type value_type; Chris@16: typedef typename V::const_reference const_reference; Chris@16: typedef typename boost::mpl::if_, Chris@16: typename V::const_reference, Chris@16: typename V::reference>::type reference; Chris@16: typedef typename boost::mpl::if_, Chris@16: typename V::const_closure_type, Chris@16: typename V::closure_type>::type vector_closure_type; Chris@16: typedef basic_range range_type; Chris@16: typedef basic_slice slice_type; Chris@16: typedef const self_type const_closure_type; Chris@16: typedef self_type closure_type; Chris@16: typedef typename storage_restrict_traits::storage_category storage_category; Chris@16: Chris@16: // Construction and destruction Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice (vector_type &data, const slice_type &s): Chris@16: data_ (data), s_ (s.preprocess (data.size ())) { Chris@16: // Early checking of preconditions here. Chris@16: // BOOST_UBLAS_CHECK (s_.start () <= data_.size () && Chris@16: // s_.start () + s_.stride () * (s_.size () - (s_.size () > 0)) <= data_.size (), bad_index ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice (const vector_closure_type &data, const slice_type &s, int): Chris@16: data_ (data), s_ (s.preprocess (data.size ())) { Chris@16: // Early checking of preconditions here. Chris@16: // BOOST_UBLAS_CHECK (s_.start () <= data_.size () && Chris@16: // s_.start () + s_.stride () * (s_.size () - (s_.size () > 0)) <= data_.size (), bad_index ()); Chris@16: } Chris@16: Chris@16: // Accessors Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type start () const { Chris@16: return s_.start (); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: difference_type stride () const { Chris@16: return s_.stride (); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type size () const { Chris@16: return s_.size (); Chris@16: } Chris@16: Chris@16: // Storage accessors Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_closure_type &data () const { Chris@16: return data_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_closure_type &data () { Chris@16: return data_; Chris@16: } Chris@16: Chris@16: // Element access Chris@16: #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator () (size_type i) const { Chris@16: return data_ (s_ (i)); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator () (size_type i) { Chris@16: return data_ (s_ (i)); Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator [] (size_type i) const { Chris@16: return (*this) (i); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator [] (size_type i) { Chris@16: return (*this) (i); Chris@16: } Chris@16: #else Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator () (size_type i) const { Chris@16: return data_ (s_ (i)); Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator [] (size_type i) const { Chris@16: return (*this) (i); Chris@16: } Chris@16: #endif Chris@16: Chris@16: // ISSUE can this be done in free project function? Chris@16: // Although a const function can create a non-const proxy to a non-const object Chris@16: // Critical is that vector_type and data_ (vector_closure_type) are const correct Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice project (const range_type &r) const { Chris@16: return vector_slice (data_, s_.compose (r.preprocess (data_.size ())), false); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice project (const slice_type &s) const { Chris@16: return vector_slice (data_, s_.compose (s.preprocess (data_.size ())), false); Chris@16: } Chris@16: Chris@16: // Assignment Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice &operator = (const vector_slice &vs) { Chris@16: // ISSUE need a temporary, proxy can be overlaping alias Chris@16: vector_assign (*this, typename vector_temporary_traits::type (vs)); Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice &assign_temporary (vector_slice &vs) { Chris@16: // assign elements, proxied container remains the same Chris@16: vector_assign (*this, vs); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice &operator = (const vector_expression &ae) { Chris@16: vector_assign (*this, typename vector_temporary_traits::type (ae)); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice &assign (const vector_expression &ae) { Chris@16: vector_assign (*this, ae); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice &operator += (const vector_expression &ae) { Chris@16: vector_assign (*this, typename vector_temporary_traits::type (*this + ae)); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice &plus_assign (const vector_expression &ae) { Chris@16: vector_assign (*this, ae); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice &operator -= (const vector_expression &ae) { Chris@16: vector_assign (*this, typename vector_temporary_traits::type (*this - ae)); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice &minus_assign (const vector_expression &ae) { Chris@16: vector_assign (*this, ae); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice &operator *= (const AT &at) { Chris@16: vector_assign_scalar (*this, at); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice &operator /= (const AT &at) { Chris@16: vector_assign_scalar (*this, at); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Closure comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool same_closure (const vector_slice &vr) const { Chris@16: return (*this).data_.same_closure (vr.data_); Chris@16: } Chris@16: Chris@16: // Comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator == (const vector_slice &vs) const { Chris@16: return (*this).data_ == vs.data_ && s_ == vs.s_; Chris@16: } Chris@16: Chris@16: // Swapping Chris@16: BOOST_UBLAS_INLINE Chris@16: void swap (vector_slice vs) { Chris@16: if (this != &vs) { Chris@16: BOOST_UBLAS_CHECK (size () == vs.size (), bad_size ()); Chris@16: // Sparse ranges may be nonconformant now. Chris@16: // std::swap_ranges (begin (), end (), vs.begin ()); Chris@16: vector_swap (*this, vs); Chris@16: } Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: friend void swap (vector_slice vs1, vector_slice vs2) { Chris@16: vs1.swap (vs2); Chris@16: } Chris@16: Chris@16: // Iterator types Chris@16: private: Chris@16: // Use slice as an index - FIXME this fails for packed assignment Chris@16: typedef typename slice_type::const_iterator const_subiterator_type; Chris@16: typedef typename slice_type::const_iterator subiterator_type; Chris@16: Chris@16: public: Chris@16: #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: typedef indexed_iterator, Chris@16: typename vector_type::iterator::iterator_category> iterator; Chris@16: typedef indexed_const_iterator, Chris@16: typename vector_type::const_iterator::iterator_category> const_iterator; Chris@16: #else Chris@16: class const_iterator; Chris@16: class iterator; Chris@16: #endif Chris@16: Chris@16: // Element lookup Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator find (size_type i) const { Chris@16: #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: return const_iterator (*this, i); Chris@16: #else Chris@16: return const_iterator (*this, s_.begin () + i); Chris@16: #endif Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator find (size_type i) { Chris@16: #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: return iterator (*this, i); Chris@16: #else Chris@16: return iterator (*this, s_.begin () + i); Chris@16: #endif Chris@16: } Chris@16: Chris@16: #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: class const_iterator: Chris@16: public container_const_reference, Chris@16: public iterator_base_traits::template Chris@16: iterator_base::type { Chris@16: public: Chris@16: typedef typename V::const_iterator::difference_type difference_type; Chris@16: typedef typename V::const_iterator::value_type value_type; Chris@16: typedef typename V::const_reference reference; //FIXME due to indexing access Chris@16: typedef typename V::const_iterator::pointer pointer; Chris@16: Chris@16: // Construction and destruction Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator (): Chris@16: container_const_reference (), it_ () {} Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator (const self_type &vs, const const_subiterator_type &it): Chris@16: container_const_reference (vs), it_ (it) {} Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here Chris@16: container_const_reference (it ()), it_ (it.it_) {} Chris@16: Chris@16: // Arithmetic Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator ++ () { Chris@16: ++ it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator -- () { Chris@16: -- it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator += (difference_type n) { Chris@16: it_ += n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator -= (difference_type n) { Chris@16: it_ -= n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: difference_type operator - (const const_iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ - it.it_; Chris@16: } Chris@16: Chris@16: // Dereference Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator * () const { Chris@16: // FIXME replace find with at_element Chris@16: BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ()); Chris@16: return (*this) ().data_ (*it_); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator [] (difference_type n) const { Chris@16: return *(*this + n); Chris@16: } Chris@16: Chris@16: // Index Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type index () const { Chris@16: return it_.index (); Chris@16: } Chris@16: Chris@16: // Assignment Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator = (const const_iterator &it) { Chris@16: container_const_reference::assign (&it ()); Chris@16: it_ = it.it_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator == (const const_iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ == it.it_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator < (const const_iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ < it.it_; Chris@16: } Chris@16: Chris@16: private: Chris@16: const_subiterator_type it_; Chris@16: }; Chris@16: #endif Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator begin () const { Chris@16: return find (0); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@101: const_iterator cbegin () const { Chris@101: return begin (); Chris@101: } Chris@101: BOOST_UBLAS_INLINE Chris@16: const_iterator end () const { Chris@16: return find (size ()); Chris@16: } Chris@101: BOOST_UBLAS_INLINE Chris@101: const_iterator cend () const { Chris@101: return end (); Chris@101: } Chris@16: Chris@16: #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: class iterator: Chris@16: public container_reference, Chris@16: public iterator_base_traits::template Chris@16: iterator_base::type { Chris@16: public: Chris@16: typedef typename V::iterator::difference_type difference_type; Chris@16: typedef typename V::iterator::value_type value_type; Chris@16: typedef typename V::reference reference; //FIXME due to indexing access Chris@16: typedef typename V::iterator::pointer pointer; Chris@16: Chris@16: // Construction and destruction Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator (): Chris@16: container_reference (), it_ () {} Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator (self_type &vs, const subiterator_type &it): Chris@16: container_reference (vs), it_ (it) {} Chris@16: Chris@16: // Arithmetic Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator ++ () { Chris@16: ++ it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator -- () { Chris@16: -- it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator += (difference_type n) { Chris@16: it_ += n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator -= (difference_type n) { Chris@16: it_ -= n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: difference_type operator - (const iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ - it.it_; Chris@16: } Chris@16: Chris@16: // Dereference Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator * () const { Chris@16: // FIXME replace find with at_element Chris@16: BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ()); Chris@16: return (*this) ().data_ (*it_); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator [] (difference_type n) const { Chris@16: return *(*this + n); Chris@16: } Chris@16: Chris@16: Chris@16: // Index Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type index () const { Chris@16: return it_.index (); Chris@16: } Chris@16: Chris@16: // Assignment Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator = (const iterator &it) { Chris@16: container_reference::assign (&it ()); Chris@16: it_ = it.it_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator == (const iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ == it.it_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator < (const iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ < it.it_; Chris@16: } Chris@16: Chris@16: private: Chris@16: subiterator_type it_; Chris@16: Chris@16: friend class const_iterator; Chris@16: }; Chris@16: #endif Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator begin () { Chris@16: return find (0); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator end () { Chris@16: return find (size ()); Chris@16: } Chris@16: Chris@16: // Reverse iterator Chris@16: typedef reverse_iterator_base const_reverse_iterator; Chris@16: typedef reverse_iterator_base reverse_iterator; Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reverse_iterator rbegin () const { Chris@16: return const_reverse_iterator (end ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@101: const_reverse_iterator crbegin () const { Chris@101: return rbegin (); Chris@101: } Chris@101: BOOST_UBLAS_INLINE Chris@16: const_reverse_iterator rend () const { Chris@16: return const_reverse_iterator (begin ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@101: const_reverse_iterator crend () const { Chris@101: return rend (); Chris@101: } Chris@101: BOOST_UBLAS_INLINE Chris@16: reverse_iterator rbegin () { Chris@16: return reverse_iterator (end ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reverse_iterator rend () { Chris@16: return reverse_iterator (begin ()); Chris@16: } Chris@16: Chris@16: private: Chris@16: vector_closure_type data_; Chris@16: slice_type s_; Chris@16: }; Chris@16: Chris@16: // Simple Projections Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice subslice (V &data, typename V::size_type start, typename V::difference_type stride, typename V::size_type size) { Chris@16: typedef basic_slice slice_type; Chris@16: return vector_slice (data, slice_type (start, stride, size)); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice subslice (const V &data, typename V::size_type start, typename V::difference_type stride, typename V::size_type size) { Chris@16: typedef basic_slice slice_type; Chris@16: return vector_slice (data, slice_type (start, stride, size)); Chris@16: } Chris@16: Chris@16: // Generic Projections Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice project (V &data, const typename vector_slice::slice_type &s) { Chris@16: return vector_slice (data, s); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_slice project (const V &data, const typename vector_slice::slice_type &s) { Chris@16: // ISSUE was: return vector_slice (const_cast (data), s); Chris@16: return vector_slice (data, s); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice project (vector_slice &data, const typename vector_slice::slice_type &s) { Chris@16: return data.project (s); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_slice project (const vector_slice &data, const typename vector_slice::slice_type &s) { Chris@16: return data.project (s); Chris@16: } Chris@16: // ISSUE in the following two functions it would be logical to use vector_slice::range_type but this confuses VC7.1 and 8.0 Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_slice project (vector_slice &data, const typename vector_range::range_type &r) { Chris@16: return data.project (r); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_slice project (const vector_slice &data, const typename vector_range::range_type &r) { Chris@16: return data.project (r); Chris@16: } Chris@16: Chris@16: // Specialization of temporary_traits Chris@16: template Chris@16: struct vector_temporary_traits< vector_slice > Chris@16: : vector_temporary_traits< V > {} ; Chris@16: template Chris@16: struct vector_temporary_traits< const vector_slice > Chris@16: : vector_temporary_traits< V > {} ; Chris@16: Chris@16: Chris@16: // Vector based indirection class Chris@16: // Contributed by Toon Knapen. Chris@16: // Extended and optimized by Kresimir Fresl. Chris@16: Chris@16: /** \brief A vector referencing a non continuous subvector of elements given another vector of indices. Chris@16: * Chris@16: * It is the most general version of any subvectors because it uses another vector of indices to reference Chris@16: * the subvector. Chris@16: * Chris@16: * The vector of indices can be of any type with the restriction that its elements must be Chris@16: * type-compatible with the size_type \c of the container. In practice, the following are good candidates: Chris@16: * - \c boost::numeric::ublas::indirect_array where \c A can be \c int, \c size_t, \c long, etc... Chris@16: * - \c std::vector where \c A can \c int, \c size_t, \c long, etc... Chris@16: * - \c boost::numeric::ublas::vector can work too (\c int can be replaced by another integer type) Chris@16: * - etc... Chris@16: * Chris@16: * An indirect vector can be used as a normal vector in any expression. If the specified indirect vector Chris@16: * falls outside that of the indices of the vector, then the \c vector_indirect is not a well formed Chris@16: * \i Vector \i Expression and access to an element outside of indices of the vector is \b undefined. Chris@16: * Chris@16: * \tparam V the type of vector referenced (for example \c vector) Chris@16: * \tparam IA the type of index vector. Default is \c ublas::indirect_array<> Chris@16: */ Chris@16: template Chris@16: class vector_indirect: Chris@16: public vector_expression > { Chris@16: Chris@16: typedef vector_indirect self_type; Chris@16: public: Chris@16: #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS Chris@16: using vector_expression::operator (); Chris@16: #endif Chris@16: typedef const V const_vector_type; Chris@16: typedef V vector_type; Chris@16: typedef const IA const_indirect_array_type; Chris@16: typedef IA indirect_array_type; Chris@16: typedef typename V::size_type size_type; Chris@16: typedef typename V::difference_type difference_type; Chris@16: typedef typename V::value_type value_type; Chris@16: typedef typename V::const_reference const_reference; Chris@16: typedef typename boost::mpl::if_, Chris@16: typename V::const_reference, Chris@16: typename V::reference>::type reference; Chris@16: typedef typename boost::mpl::if_, Chris@16: typename V::const_closure_type, Chris@16: typename V::closure_type>::type vector_closure_type; Chris@16: typedef basic_range range_type; Chris@16: typedef basic_slice slice_type; Chris@16: typedef const self_type const_closure_type; Chris@16: typedef self_type closure_type; Chris@16: typedef typename storage_restrict_traits::storage_category storage_category; Chris@16: Chris@16: // Construction and destruction Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect (vector_type &data, size_type size): Chris@16: data_ (data), ia_ (size) {} Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect (vector_type &data, const indirect_array_type &ia): Chris@16: data_ (data), ia_ (ia.preprocess (data.size ())) {} Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect (const vector_closure_type &data, const indirect_array_type &ia, int): Chris@16: data_ (data), ia_ (ia.preprocess (data.size ())) {} Chris@16: Chris@16: // Accessors Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type size () const { Chris@16: return ia_.size (); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_indirect_array_type &indirect () const { Chris@16: return ia_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: indirect_array_type &indirect () { Chris@16: return ia_; Chris@16: } Chris@16: Chris@16: // Storage accessors Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_closure_type &data () const { Chris@16: return data_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_closure_type &data () { Chris@16: return data_; Chris@16: } Chris@16: Chris@16: // Element access Chris@16: #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator () (size_type i) const { Chris@16: return data_ (ia_ (i)); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator () (size_type i) { Chris@16: return data_ (ia_ (i)); Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator [] (size_type i) const { Chris@16: return (*this) (i); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator [] (size_type i) { Chris@16: return (*this) (i); Chris@16: } Chris@16: #else Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator () (size_type i) const { Chris@16: return data_ (ia_ (i)); Chris@16: } Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator [] (size_type i) const { Chris@16: return (*this) (i); Chris@16: } Chris@16: #endif Chris@16: Chris@16: // ISSUE can this be done in free project function? Chris@16: // Although a const function can create a non-const proxy to a non-const object Chris@16: // Critical is that vector_type and data_ (vector_closure_type) are const correct Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect project (const range_type &r) const { Chris@16: return vector_indirect (data_, ia_.compose (r.preprocess (data_.size ())), 0); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect project (const slice_type &s) const { Chris@16: return vector_indirect (data_, ia_.compose (s.preprocess (data_.size ())), 0); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect project (const indirect_array_type &ia) const { Chris@16: return vector_indirect (data_, ia_.compose (ia.preprocess (data_.size ())), 0); Chris@16: } Chris@16: Chris@16: // Assignment Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect &operator = (const vector_indirect &vi) { Chris@16: // ISSUE need a temporary, proxy can be overlaping alias Chris@16: vector_assign (*this, typename vector_temporary_traits::type (vi)); Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect &assign_temporary (vector_indirect &vi) { Chris@16: // assign elements, proxied container remains the same Chris@16: vector_assign (*this, vi); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect &operator = (const vector_expression &ae) { Chris@16: vector_assign (*this, typename vector_temporary_traits::type (ae)); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect &assign (const vector_expression &ae) { Chris@16: vector_assign (*this, ae); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect &operator += (const vector_expression &ae) { Chris@16: vector_assign (*this, typename vector_temporary_traits::type (*this + ae)); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect &plus_assign (const vector_expression &ae) { Chris@16: vector_assign (*this, ae); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect &operator -= (const vector_expression &ae) { Chris@16: vector_assign (*this, typename vector_temporary_traits::type (*this - ae)); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect &minus_assign (const vector_expression &ae) { Chris@16: vector_assign (*this, ae); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect &operator *= (const AT &at) { Chris@16: vector_assign_scalar (*this, at); Chris@16: return *this; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect &operator /= (const AT &at) { Chris@16: vector_assign_scalar (*this, at); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Closure comparison Chris@16: BOOST_UBLAS_INLINE Chris@101: bool same_closure (const vector_indirect &/*vr*/) const { Chris@101: return true; Chris@16: } Chris@16: Chris@16: // Comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator == (const vector_indirect &vi) const { Chris@16: return (*this).data_ == vi.data_ && ia_ == vi.ia_; Chris@16: } Chris@16: Chris@16: // Swapping Chris@16: BOOST_UBLAS_INLINE Chris@16: void swap (vector_indirect vi) { Chris@16: if (this != &vi) { Chris@16: BOOST_UBLAS_CHECK (size () == vi.size (), bad_size ()); Chris@16: // Sparse ranges may be nonconformant now. Chris@16: // std::swap_ranges (begin (), end (), vi.begin ()); Chris@16: vector_swap (*this, vi); Chris@16: } Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: friend void swap (vector_indirect vi1, vector_indirect vi2) { Chris@16: vi1.swap (vi2); Chris@16: } Chris@16: Chris@16: // Iterator types Chris@16: private: Chris@16: // Use indirect array as an index - FIXME this fails for packed assignment Chris@16: typedef typename IA::const_iterator const_subiterator_type; Chris@16: typedef typename IA::const_iterator subiterator_type; Chris@16: Chris@16: public: Chris@16: #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: typedef indexed_iterator, Chris@16: typename vector_type::iterator::iterator_category> iterator; Chris@16: typedef indexed_const_iterator, Chris@16: typename vector_type::const_iterator::iterator_category> const_iterator; Chris@16: #else Chris@16: class const_iterator; Chris@16: class iterator; Chris@16: #endif Chris@16: // Element lookup Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator find (size_type i) const { Chris@16: #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: return const_iterator (*this, i); Chris@16: #else Chris@16: return const_iterator (*this, ia_.begin () + i); Chris@16: #endif Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator find (size_type i) { Chris@16: #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: return iterator (*this, i); Chris@16: #else Chris@16: return iterator (*this, ia_.begin () + i); Chris@16: #endif Chris@16: } Chris@16: Chris@16: // Iterators simply are indices. Chris@16: Chris@16: #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: class const_iterator: Chris@16: public container_const_reference, Chris@16: public iterator_base_traits::template Chris@16: iterator_base::type { Chris@16: public: Chris@16: typedef typename V::const_iterator::difference_type difference_type; Chris@16: typedef typename V::const_iterator::value_type value_type; Chris@16: typedef typename V::const_reference reference; //FIXME due to indexing access Chris@16: typedef typename V::const_iterator::pointer pointer; Chris@16: Chris@16: // Construction and destruction Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator (): Chris@16: container_const_reference (), it_ () {} Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator (const self_type &vi, const const_subiterator_type &it): Chris@16: container_const_reference (vi), it_ (it) {} Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here Chris@16: container_const_reference (it ()), it_ (it.it_) {} Chris@16: Chris@16: // Arithmetic Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator ++ () { Chris@16: ++ it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator -- () { Chris@16: -- it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator += (difference_type n) { Chris@16: it_ += n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator -= (difference_type n) { Chris@16: it_ -= n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: difference_type operator - (const const_iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ - it.it_; Chris@16: } Chris@16: Chris@16: // Dereference Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator * () const { Chris@16: // FIXME replace find with at_element Chris@16: BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ()); Chris@16: return (*this) ().data_ (*it_); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reference operator [] (difference_type n) const { Chris@16: return *(*this + n); Chris@16: } Chris@16: Chris@16: // Index Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type index () const { Chris@16: return it_.index (); Chris@16: } Chris@16: Chris@16: // Assignment Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator &operator = (const const_iterator &it) { Chris@16: container_const_reference::assign (&it ()); Chris@16: it_ = it.it_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator == (const const_iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ == it.it_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator < (const const_iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ < it.it_; Chris@16: } Chris@16: Chris@16: private: Chris@16: const_subiterator_type it_; Chris@16: }; Chris@16: #endif Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: const_iterator begin () const { Chris@16: return find (0); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@101: const_iterator cbegin () const { Chris@101: return begin (); Chris@101: } Chris@101: BOOST_UBLAS_INLINE Chris@16: const_iterator end () const { Chris@16: return find (size ()); Chris@16: } Chris@101: BOOST_UBLAS_INLINE Chris@101: const_iterator cend () const { Chris@101: return end (); Chris@101: } Chris@16: Chris@16: #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR Chris@16: class iterator: Chris@16: public container_reference, Chris@16: public iterator_base_traits::template Chris@16: iterator_base::type { Chris@16: public: Chris@16: typedef typename V::iterator::difference_type difference_type; Chris@16: typedef typename V::iterator::value_type value_type; Chris@16: typedef typename V::reference reference; //FIXME due to indexing access Chris@16: typedef typename V::iterator::pointer pointer; Chris@16: Chris@16: // Construction and destruction Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator (): Chris@16: container_reference (), it_ () {} Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator (self_type &vi, const subiterator_type &it): Chris@16: container_reference (vi), it_ (it) {} Chris@16: Chris@16: // Arithmetic Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator ++ () { Chris@16: ++ it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator -- () { Chris@16: -- it_; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator += (difference_type n) { Chris@16: it_ += n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator -= (difference_type n) { Chris@16: it_ -= n; Chris@16: return *this; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: difference_type operator - (const iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ - it.it_; Chris@16: } Chris@16: Chris@16: // Dereference Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator * () const { Chris@16: // FIXME replace find with at_element Chris@16: BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ()); Chris@16: return (*this) ().data_ (*it_); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reference operator [] (difference_type n) const { Chris@16: return *(*this + n); Chris@16: } Chris@16: Chris@16: // Index Chris@16: BOOST_UBLAS_INLINE Chris@16: size_type index () const { Chris@16: return it_.index (); Chris@16: } Chris@16: Chris@16: // Assignment Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator &operator = (const iterator &it) { Chris@16: container_reference::assign (&it ()); Chris@16: it_ = it.it_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Comparison Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator == (const iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ == it.it_; Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: bool operator < (const iterator &it) const { Chris@16: BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ()); Chris@16: return it_ < it.it_; Chris@16: } Chris@16: Chris@16: private: Chris@16: subiterator_type it_; Chris@16: Chris@16: friend class const_iterator; Chris@16: }; Chris@16: #endif Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator begin () { Chris@16: return find (0); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: iterator end () { Chris@16: return find (size ()); Chris@16: } Chris@16: Chris@16: // Reverse iterator Chris@16: typedef reverse_iterator_base const_reverse_iterator; Chris@16: typedef reverse_iterator_base reverse_iterator; Chris@16: Chris@16: BOOST_UBLAS_INLINE Chris@16: const_reverse_iterator rbegin () const { Chris@16: return const_reverse_iterator (end ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@101: const_reverse_iterator crbegin () const { Chris@101: return rbegin (); Chris@101: } Chris@101: BOOST_UBLAS_INLINE Chris@16: const_reverse_iterator rend () const { Chris@16: return const_reverse_iterator (begin ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@101: const_reverse_iterator crend () const { Chris@101: return rend (); Chris@101: } Chris@101: Chris@101: BOOST_UBLAS_INLINE Chris@16: reverse_iterator rbegin () { Chris@16: return reverse_iterator (end ()); Chris@16: } Chris@16: BOOST_UBLAS_INLINE Chris@16: reverse_iterator rend () { Chris@16: return reverse_iterator (begin ()); Chris@16: } Chris@16: Chris@16: private: Chris@16: vector_closure_type data_; Chris@16: indirect_array_type ia_; Chris@16: }; Chris@16: Chris@16: // Projections Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect > project (V &data, const indirect_array &ia) { Chris@16: return vector_indirect > (data, ia); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_indirect > project (const V &data, const indirect_array &ia) { Chris@16: // ISSUE was: return vector_indirect > (const_cast (data), ia) Chris@16: return vector_indirect > (data, ia); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect project (vector_indirect &data, const typename vector_indirect::range_type &r) { Chris@16: return data.project (r); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_indirect project (const vector_indirect &data, const typename vector_indirect::range_type &r) { Chris@16: return data.project (r); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect project (vector_indirect &data, const typename vector_indirect::slice_type &s) { Chris@16: return data.project (s); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_indirect project (const vector_indirect &data, const typename vector_indirect::slice_type &s) { Chris@16: return data.project (s); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: vector_indirect > project (vector_indirect > &data, const indirect_array &ia) { Chris@16: return data.project (ia); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: const vector_indirect > project (const vector_indirect > &data, const indirect_array &ia) { Chris@16: return data.project (ia); Chris@16: } Chris@16: Chris@16: // Specialization of temporary_traits Chris@16: template Chris@16: struct vector_temporary_traits< vector_indirect > Chris@16: : vector_temporary_traits< V > {} ; Chris@16: template Chris@16: struct vector_temporary_traits< const vector_indirect > Chris@16: : vector_temporary_traits< V > {} ; Chris@16: Chris@16: }}} Chris@16: Chris@16: #endif