2 // Copyright (c) 2000-2002
3 // Joerg Walter, Mathias Koch
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // The authors gratefully acknowledge the support of
10 // GeNeSys mbH & Co. KG in producing this work.
13 #ifndef _BOOST_UBLAS_VECTOR_PROXY_
14 #define _BOOST_UBLAS_VECTOR_PROXY_
16 #include <boost/numeric/ublas/vector_expression.hpp>
17 #include <boost/numeric/ublas/detail/vector_assign.hpp>
18 #include <boost/numeric/ublas/detail/temporary.hpp>
20 // Iterators based on ideas of Jeremy Siek
22 namespace boost { namespace numeric { namespace ublas {
24 /** \brief A vector referencing a continuous subvector of elements of vector \c v containing all elements specified by \c range.
26 * A vector range can be used as a normal vector in any expression.
27 * If the specified range falls outside that of the index range of the vector, then
28 * the \c vector_range is not a well formed \i Vector \i Expression and access to an
29 * element outside of index range of the vector is \b undefined.
31 * \tparam V the type of vector referenced (for example \c vector<double>)
35 public vector_expression<vector_range<V> > {
37 typedef vector_range<V> self_type;
39 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
40 using vector_expression<self_type>::operator ();
42 typedef const V const_vector_type;
43 typedef V vector_type;
44 typedef typename V::size_type size_type;
45 typedef typename V::difference_type difference_type;
46 typedef typename V::value_type value_type;
47 typedef typename V::const_reference const_reference;
48 typedef typename boost::mpl::if_<boost::is_const<V>,
49 typename V::const_reference,
50 typename V::reference>::type reference;
51 typedef typename boost::mpl::if_<boost::is_const<V>,
52 typename V::const_closure_type,
53 typename V::closure_type>::type vector_closure_type;
54 typedef basic_range<size_type, difference_type> range_type;
55 typedef const self_type const_closure_type;
56 typedef self_type closure_type;
57 typedef typename storage_restrict_traits<typename V::storage_category,
58 dense_proxy_tag>::storage_category storage_category;
60 // Construction and destruction
62 vector_range (vector_type &data, const range_type &r):
63 data_ (data), r_ (r.preprocess (data.size ())) {
64 // Early checking of preconditions here.
65 // BOOST_UBLAS_CHECK (r_.start () <= data_.size () &&
66 // r_.start () + r_.size () <= data_.size (), bad_index ());
69 vector_range (const vector_closure_type &data, const range_type &r, bool):
70 data_ (data), r_ (r.preprocess (data.size ())) {
71 // Early checking of preconditions here.
72 // BOOST_UBLAS_CHECK (r_.start () <= data_.size () &&
73 // r_.start () + r_.size () <= data_.size (), bad_index ());
78 size_type start () const {
82 size_type size () const {
88 const vector_closure_type &data () const {
92 vector_closure_type &data () {
97 #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
99 const_reference operator () (size_type i) const {
100 return data_ (r_ (i));
103 reference operator () (size_type i) {
104 return data_ (r_ (i));
108 const_reference operator [] (size_type i) const {
112 reference operator [] (size_type i) {
117 reference operator () (size_type i) const {
118 return data_ (r_ (i));
122 reference operator [] (size_type i) const {
127 // ISSUE can this be done in free project function?
128 // Although a const function can create a non-const proxy to a non-const object
129 // Critical is that vector_type and data_ (vector_closure_type) are const correct
131 vector_range<vector_type> project (const range_type &r) const {
132 return vector_range<vector_type> (data_, r_.compose (r.preprocess (data_.size ())), false);
137 vector_range &operator = (const vector_range &vr) {
138 // ISSUE need a temporary, proxy can be overlaping alias
139 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (vr));
143 vector_range &assign_temporary (vector_range &vr) {
144 // assign elements, proxied container remains the same
145 vector_assign<scalar_assign> (*this, vr);
150 vector_range &operator = (const vector_expression<AE> &ae) {
151 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (ae));
156 vector_range &assign (const vector_expression<AE> &ae) {
157 vector_assign<scalar_assign> (*this, ae);
162 vector_range &operator += (const vector_expression<AE> &ae) {
163 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this + ae));
168 vector_range &plus_assign (const vector_expression<AE> &ae) {
169 vector_assign<scalar_plus_assign> (*this, ae);
174 vector_range &operator -= (const vector_expression<AE> &ae) {
175 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this - ae));
180 vector_range &minus_assign (const vector_expression<AE> &ae) {
181 vector_assign<scalar_minus_assign> (*this, ae);
186 vector_range &operator *= (const AT &at) {
187 vector_assign_scalar<scalar_multiplies_assign> (*this, at);
192 vector_range &operator /= (const AT &at) {
193 vector_assign_scalar<scalar_divides_assign> (*this, at);
197 // Closure comparison
199 bool same_closure (const vector_range &vr) const {
200 return (*this).data_.same_closure (vr.data_);
205 bool operator == (const vector_range &vr) const {
206 return (*this).data_ == vr.data_ && r_ == vr.r_;
211 void swap (vector_range vr) {
213 BOOST_UBLAS_CHECK (size () == vr.size (), bad_size ());
214 // Sparse ranges may be nonconformant now.
215 // std::swap_ranges (begin (), end (), vr.begin ());
216 vector_swap<scalar_swap> (*this, vr);
220 friend void swap (vector_range vr1, vector_range vr2) {
226 typedef typename V::const_iterator const_subiterator_type;
227 typedef typename boost::mpl::if_<boost::is_const<V>,
228 typename V::const_iterator,
229 typename V::iterator>::type subiterator_type;
232 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
233 typedef indexed_iterator<vector_range<vector_type>,
234 typename subiterator_type::iterator_category> iterator;
235 typedef indexed_const_iterator<vector_range<vector_type>,
236 typename const_subiterator_type::iterator_category> const_iterator;
238 class const_iterator;
244 const_iterator find (size_type i) const {
245 const_subiterator_type it (data_.find (start () + i));
246 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
247 return const_iterator (*this, it.index ());
249 return const_iterator (*this, it);
253 iterator find (size_type i) {
254 subiterator_type it (data_.find (start () + i));
255 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
256 return iterator (*this, it.index ());
258 return iterator (*this, it);
262 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
263 class const_iterator:
264 public container_const_reference<vector_range>,
265 public iterator_base_traits<typename const_subiterator_type::iterator_category>::template
266 iterator_base<const_iterator, value_type>::type {
268 typedef typename const_subiterator_type::difference_type difference_type;
269 typedef typename const_subiterator_type::value_type value_type;
270 typedef typename const_subiterator_type::reference reference;
271 typedef typename const_subiterator_type::pointer pointer;
273 // Construction and destruction
276 container_const_reference<self_type> (), it_ () {}
278 const_iterator (const self_type &vr, const const_subiterator_type &it):
279 container_const_reference<self_type> (vr), it_ (it) {}
281 const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
282 container_const_reference<self_type> (it ()), it_ (it.it_) {}
286 const_iterator &operator ++ () {
291 const_iterator &operator -- () {
296 const_iterator &operator += (difference_type n) {
301 const_iterator &operator -= (difference_type n) {
306 difference_type operator - (const const_iterator &it) const {
307 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
313 const_reference operator * () const {
314 BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
318 const_reference operator [] (difference_type n) const {
324 size_type index () const {
325 return it_.index () - (*this) ().start ();
330 const_iterator &operator = (const const_iterator &it) {
331 container_const_reference<self_type>::assign (&it ());
338 bool operator == (const const_iterator &it) const {
339 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
340 return it_ == it.it_;
343 bool operator < (const const_iterator &it) const {
344 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
349 const_subiterator_type it_;
354 const_iterator begin () const {
358 const_iterator cbegin () const {
362 const_iterator end () const {
363 return find (size ());
366 const_iterator cend () const {
370 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
372 public container_reference<vector_range>,
373 public iterator_base_traits<typename subiterator_type::iterator_category>::template
374 iterator_base<iterator, value_type>::type {
376 typedef typename subiterator_type::difference_type difference_type;
377 typedef typename subiterator_type::value_type value_type;
378 typedef typename subiterator_type::reference reference;
379 typedef typename subiterator_type::pointer pointer;
381 // Construction and destruction
384 container_reference<self_type> (), it_ () {}
386 iterator (self_type &vr, const subiterator_type &it):
387 container_reference<self_type> (vr), it_ (it) {}
391 iterator &operator ++ () {
396 iterator &operator -- () {
401 iterator &operator += (difference_type n) {
406 iterator &operator -= (difference_type n) {
411 difference_type operator - (const iterator &it) const {
412 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
418 reference operator * () const {
419 BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
423 reference operator [] (difference_type n) const {
429 size_type index () const {
430 return it_.index () - (*this) ().start ();
435 iterator &operator = (const iterator &it) {
436 container_reference<self_type>::assign (&it ());
443 bool operator == (const iterator &it) const {
444 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
445 return it_ == it.it_;
448 bool operator < (const iterator &it) const {
449 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
454 subiterator_type it_;
456 friend class const_iterator;
466 return find (size ());
470 typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
471 typedef reverse_iterator_base<iterator> reverse_iterator;
474 const_reverse_iterator rbegin () const {
475 return const_reverse_iterator (end ());
478 const_reverse_iterator crbegin () const {
482 const_reverse_iterator rend () const {
483 return const_reverse_iterator (begin ());
486 const_reverse_iterator crend () const {
491 reverse_iterator rbegin () {
492 return reverse_iterator (end ());
495 reverse_iterator rend () {
496 return reverse_iterator (begin ());
500 vector_closure_type data_;
504 // ------------------
505 // Simple Projections
506 // ------------------
508 /** \brief Return a \c vector_range on a specified vector, a start and stop index.
509 * 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.
510 * 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
511 * Vector Expression and access to an element outside of index range of the vector is \b undefined.
515 vector_range<V> subrange (V &data, typename V::size_type start, typename V::size_type stop) {
516 typedef basic_range<typename V::size_type, typename V::difference_type> range_type;
517 return vector_range<V> (data, range_type (start, stop));
520 /** \brief Return a \c const \c vector_range on a specified vector, a start and stop index.
521 * 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.
522 *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
523 * Vector Expression and access to an element outside of index range of the vector is \b undefined.
527 vector_range<const V> subrange (const V &data, typename V::size_type start, typename V::size_type stop) {
528 typedef basic_range<typename V::size_type, typename V::difference_type> range_type;
529 return vector_range<const V> (data, range_type (start, stop));
532 // -------------------
533 // Generic Projections
534 // -------------------
536 /** \brief Return a \c const \c vector_range on a specified vector and \c range
537 * 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.
538 * 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
539 * Vector Expression and access to an element outside of index range of the vector is \b undefined.
543 vector_range<V> project (V &data, typename vector_range<V>::range_type const &r) {
544 return vector_range<V> (data, r);
547 /** \brief Return a \c vector_range on a specified vector and \c range
548 * Return a \c vector_range on a specified vector and \c range. The resulting \c vector_range can be manipulated like a normal vector.
549 * 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
550 * Vector Expression and access to an element outside of index range of the vector is \b undefined.
554 const vector_range<const V> project (const V &data, typename vector_range<V>::range_type const &r) {
555 // ISSUE was: return vector_range<V> (const_cast<V &> (data), r);
556 return vector_range<const V> (data, r);
559 /** \brief Return a \c const \c vector_range on a specified vector and const \c range
560 * 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.
561 * 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
562 * Vector Expression and access to an element outside of index range of the vector is \b undefined.
566 vector_range<V> project (vector_range<V> &data, const typename vector_range<V>::range_type &r) {
567 return data.project (r);
570 /** \brief Return a \c vector_range on a specified vector and const \c range
571 * 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.
572 * 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
573 * Vector Expression and access to an element outside of index range of the vector is \b undefined.
577 const vector_range<V> project (const vector_range<V> &data, const typename vector_range<V>::range_type &r) {
578 return data.project (r);
581 // Specialization of temporary_traits
583 struct vector_temporary_traits< vector_range<V> >
584 : vector_temporary_traits< V > {} ;
586 struct vector_temporary_traits< const vector_range<V> >
587 : vector_temporary_traits< V > {} ;
590 /** \brief A vector referencing a non continuous subvector of elements of vector v containing all elements specified by \c slice.
592 * A vector slice can be used as a normal vector in any expression.
593 * If the specified slice falls outside that of the index slice of the vector, then
594 * the \c vector_slice is not a well formed \i Vector \i Expression and access to an
595 * element outside of index slice of the vector is \b undefined.
597 * A slice is a generalization of a range. In a range going from \f$a\f$ to \f$b\f$,
598 * all elements belong to the range. In a slice, a \i \f$step\f$ can be specified meaning to
599 * take one element over \f$step\f$ in the range specified from \f$a\f$ to \f$b\f$.
600 * Obviously, a slice with a \f$step\f$ of 1 is equivalent to a range.
602 * \tparam V the type of vector referenced (for example \c vector<double>)
606 public vector_expression<vector_slice<V> > {
608 typedef vector_slice<V> self_type;
610 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
611 using vector_expression<self_type>::operator ();
613 typedef const V const_vector_type;
614 typedef V vector_type;
615 typedef typename V::size_type size_type;
616 typedef typename V::difference_type difference_type;
617 typedef typename V::value_type value_type;
618 typedef typename V::const_reference const_reference;
619 typedef typename boost::mpl::if_<boost::is_const<V>,
620 typename V::const_reference,
621 typename V::reference>::type reference;
622 typedef typename boost::mpl::if_<boost::is_const<V>,
623 typename V::const_closure_type,
624 typename V::closure_type>::type vector_closure_type;
625 typedef basic_range<size_type, difference_type> range_type;
626 typedef basic_slice<size_type, difference_type> slice_type;
627 typedef const self_type const_closure_type;
628 typedef self_type closure_type;
629 typedef typename storage_restrict_traits<typename V::storage_category,
630 dense_proxy_tag>::storage_category storage_category;
632 // Construction and destruction
634 vector_slice (vector_type &data, const slice_type &s):
635 data_ (data), s_ (s.preprocess (data.size ())) {
636 // Early checking of preconditions here.
637 // BOOST_UBLAS_CHECK (s_.start () <= data_.size () &&
638 // s_.start () + s_.stride () * (s_.size () - (s_.size () > 0)) <= data_.size (), bad_index ());
641 vector_slice (const vector_closure_type &data, const slice_type &s, int):
642 data_ (data), s_ (s.preprocess (data.size ())) {
643 // Early checking of preconditions here.
644 // BOOST_UBLAS_CHECK (s_.start () <= data_.size () &&
645 // s_.start () + s_.stride () * (s_.size () - (s_.size () > 0)) <= data_.size (), bad_index ());
650 size_type start () const {
654 difference_type stride () const {
658 size_type size () const {
664 const vector_closure_type &data () const {
668 vector_closure_type &data () {
673 #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
675 const_reference operator () (size_type i) const {
676 return data_ (s_ (i));
679 reference operator () (size_type i) {
680 return data_ (s_ (i));
684 const_reference operator [] (size_type i) const {
688 reference operator [] (size_type i) {
693 reference operator () (size_type i) const {
694 return data_ (s_ (i));
698 reference operator [] (size_type i) const {
703 // ISSUE can this be done in free project function?
704 // Although a const function can create a non-const proxy to a non-const object
705 // Critical is that vector_type and data_ (vector_closure_type) are const correct
707 vector_slice<vector_type> project (const range_type &r) const {
708 return vector_slice<vector_type> (data_, s_.compose (r.preprocess (data_.size ())), false);
711 vector_slice<vector_type> project (const slice_type &s) const {
712 return vector_slice<vector_type> (data_, s_.compose (s.preprocess (data_.size ())), false);
717 vector_slice &operator = (const vector_slice &vs) {
718 // ISSUE need a temporary, proxy can be overlaping alias
719 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (vs));
723 vector_slice &assign_temporary (vector_slice &vs) {
724 // assign elements, proxied container remains the same
725 vector_assign<scalar_assign> (*this, vs);
730 vector_slice &operator = (const vector_expression<AE> &ae) {
731 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (ae));
736 vector_slice &assign (const vector_expression<AE> &ae) {
737 vector_assign<scalar_assign> (*this, ae);
742 vector_slice &operator += (const vector_expression<AE> &ae) {
743 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this + ae));
748 vector_slice &plus_assign (const vector_expression<AE> &ae) {
749 vector_assign<scalar_plus_assign> (*this, ae);
754 vector_slice &operator -= (const vector_expression<AE> &ae) {
755 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this - ae));
760 vector_slice &minus_assign (const vector_expression<AE> &ae) {
761 vector_assign<scalar_minus_assign> (*this, ae);
766 vector_slice &operator *= (const AT &at) {
767 vector_assign_scalar<scalar_multiplies_assign> (*this, at);
772 vector_slice &operator /= (const AT &at) {
773 vector_assign_scalar<scalar_divides_assign> (*this, at);
777 // Closure comparison
779 bool same_closure (const vector_slice &vr) const {
780 return (*this).data_.same_closure (vr.data_);
785 bool operator == (const vector_slice &vs) const {
786 return (*this).data_ == vs.data_ && s_ == vs.s_;
791 void swap (vector_slice vs) {
793 BOOST_UBLAS_CHECK (size () == vs.size (), bad_size ());
794 // Sparse ranges may be nonconformant now.
795 // std::swap_ranges (begin (), end (), vs.begin ());
796 vector_swap<scalar_swap> (*this, vs);
800 friend void swap (vector_slice vs1, vector_slice vs2) {
806 // Use slice as an index - FIXME this fails for packed assignment
807 typedef typename slice_type::const_iterator const_subiterator_type;
808 typedef typename slice_type::const_iterator subiterator_type;
811 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
812 typedef indexed_iterator<vector_slice<vector_type>,
813 typename vector_type::iterator::iterator_category> iterator;
814 typedef indexed_const_iterator<vector_slice<vector_type>,
815 typename vector_type::const_iterator::iterator_category> const_iterator;
817 class const_iterator;
823 const_iterator find (size_type i) const {
824 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
825 return const_iterator (*this, i);
827 return const_iterator (*this, s_.begin () + i);
831 iterator find (size_type i) {
832 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
833 return iterator (*this, i);
835 return iterator (*this, s_.begin () + i);
839 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
840 class const_iterator:
841 public container_const_reference<vector_slice>,
842 public iterator_base_traits<typename V::const_iterator::iterator_category>::template
843 iterator_base<const_iterator, value_type>::type {
845 typedef typename V::const_iterator::difference_type difference_type;
846 typedef typename V::const_iterator::value_type value_type;
847 typedef typename V::const_reference reference; //FIXME due to indexing access
848 typedef typename V::const_iterator::pointer pointer;
850 // Construction and destruction
853 container_const_reference<self_type> (), it_ () {}
855 const_iterator (const self_type &vs, const const_subiterator_type &it):
856 container_const_reference<self_type> (vs), it_ (it) {}
858 const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
859 container_const_reference<self_type> (it ()), it_ (it.it_) {}
863 const_iterator &operator ++ () {
868 const_iterator &operator -- () {
873 const_iterator &operator += (difference_type n) {
878 const_iterator &operator -= (difference_type n) {
883 difference_type operator - (const const_iterator &it) const {
884 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
890 const_reference operator * () const {
891 // FIXME replace find with at_element
892 BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
893 return (*this) ().data_ (*it_);
896 const_reference operator [] (difference_type n) const {
902 size_type index () const {
908 const_iterator &operator = (const const_iterator &it) {
909 container_const_reference<self_type>::assign (&it ());
916 bool operator == (const const_iterator &it) const {
917 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
918 return it_ == it.it_;
921 bool operator < (const const_iterator &it) const {
922 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
927 const_subiterator_type it_;
932 const_iterator begin () const {
936 const_iterator cbegin () const {
940 const_iterator end () const {
941 return find (size ());
944 const_iterator cend () const {
948 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
950 public container_reference<vector_slice>,
951 public iterator_base_traits<typename V::iterator::iterator_category>::template
952 iterator_base<iterator, value_type>::type {
954 typedef typename V::iterator::difference_type difference_type;
955 typedef typename V::iterator::value_type value_type;
956 typedef typename V::reference reference; //FIXME due to indexing access
957 typedef typename V::iterator::pointer pointer;
959 // Construction and destruction
962 container_reference<self_type> (), it_ () {}
964 iterator (self_type &vs, const subiterator_type &it):
965 container_reference<self_type> (vs), it_ (it) {}
969 iterator &operator ++ () {
974 iterator &operator -- () {
979 iterator &operator += (difference_type n) {
984 iterator &operator -= (difference_type n) {
989 difference_type operator - (const iterator &it) const {
990 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
996 reference operator * () const {
997 // FIXME replace find with at_element
998 BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
999 return (*this) ().data_ (*it_);
1002 reference operator [] (difference_type n) const {
1003 return *(*this + n);
1009 size_type index () const {
1010 return it_.index ();
1015 iterator &operator = (const iterator &it) {
1016 container_reference<self_type>::assign (&it ());
1023 bool operator == (const iterator &it) const {
1024 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
1025 return it_ == it.it_;
1028 bool operator < (const iterator &it) const {
1029 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
1030 return it_ < it.it_;
1034 subiterator_type it_;
1036 friend class const_iterator;
1046 return find (size ());
1050 typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
1051 typedef reverse_iterator_base<iterator> reverse_iterator;
1054 const_reverse_iterator rbegin () const {
1055 return const_reverse_iterator (end ());
1058 const_reverse_iterator crbegin () const {
1062 const_reverse_iterator rend () const {
1063 return const_reverse_iterator (begin ());
1066 const_reverse_iterator crend () const {
1070 reverse_iterator rbegin () {
1071 return reverse_iterator (end ());
1074 reverse_iterator rend () {
1075 return reverse_iterator (begin ());
1079 vector_closure_type data_;
1083 // Simple Projections
1086 vector_slice<V> subslice (V &data, typename V::size_type start, typename V::difference_type stride, typename V::size_type size) {
1087 typedef basic_slice<typename V::size_type, typename V::difference_type> slice_type;
1088 return vector_slice<V> (data, slice_type (start, stride, size));
1092 vector_slice<const V> subslice (const V &data, typename V::size_type start, typename V::difference_type stride, typename V::size_type size) {
1093 typedef basic_slice<typename V::size_type, typename V::difference_type> slice_type;
1094 return vector_slice<const V> (data, slice_type (start, stride, size));
1097 // Generic Projections
1100 vector_slice<V> project (V &data, const typename vector_slice<V>::slice_type &s) {
1101 return vector_slice<V> (data, s);
1105 const vector_slice<const V> project (const V &data, const typename vector_slice<V>::slice_type &s) {
1106 // ISSUE was: return vector_slice<V> (const_cast<V &> (data), s);
1107 return vector_slice<const V> (data, s);
1111 vector_slice<V> project (vector_slice<V> &data, const typename vector_slice<V>::slice_type &s) {
1112 return data.project (s);
1116 const vector_slice<V> project (const vector_slice<V> &data, const typename vector_slice<V>::slice_type &s) {
1117 return data.project (s);
1119 // ISSUE in the following two functions it would be logical to use vector_slice<V>::range_type but this confuses VC7.1 and 8.0
1122 vector_slice<V> project (vector_slice<V> &data, const typename vector_range<V>::range_type &r) {
1123 return data.project (r);
1127 const vector_slice<V> project (const vector_slice<V> &data, const typename vector_range<V>::range_type &r) {
1128 return data.project (r);
1131 // Specialization of temporary_traits
1133 struct vector_temporary_traits< vector_slice<V> >
1134 : vector_temporary_traits< V > {} ;
1136 struct vector_temporary_traits< const vector_slice<V> >
1137 : vector_temporary_traits< V > {} ;
1140 // Vector based indirection class
1141 // Contributed by Toon Knapen.
1142 // Extended and optimized by Kresimir Fresl.
1144 /** \brief A vector referencing a non continuous subvector of elements given another vector of indices.
1146 * It is the most general version of any subvectors because it uses another vector of indices to reference
1149 * The vector of indices can be of any type with the restriction that its elements must be
1150 * type-compatible with the size_type \c of the container. In practice, the following are good candidates:
1151 * - \c boost::numeric::ublas::indirect_array<A> where \c A can be \c int, \c size_t, \c long, etc...
1152 * - \c std::vector<A> where \c A can \c int, \c size_t, \c long, etc...
1153 * - \c boost::numeric::ublas::vector<int> can work too (\c int can be replaced by another integer type)
1156 * An indirect vector can be used as a normal vector in any expression. If the specified indirect vector
1157 * falls outside that of the indices of the vector, then the \c vector_indirect is not a well formed
1158 * \i Vector \i Expression and access to an element outside of indices of the vector is \b undefined.
1160 * \tparam V the type of vector referenced (for example \c vector<double>)
1161 * \tparam IA the type of index vector. Default is \c ublas::indirect_array<>
1163 template<class V, class IA>
1164 class vector_indirect:
1165 public vector_expression<vector_indirect<V, IA> > {
1167 typedef vector_indirect<V, IA> self_type;
1169 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
1170 using vector_expression<self_type>::operator ();
1172 typedef const V const_vector_type;
1173 typedef V vector_type;
1174 typedef const IA const_indirect_array_type;
1175 typedef IA indirect_array_type;
1176 typedef typename V::size_type size_type;
1177 typedef typename V::difference_type difference_type;
1178 typedef typename V::value_type value_type;
1179 typedef typename V::const_reference const_reference;
1180 typedef typename boost::mpl::if_<boost::is_const<V>,
1181 typename V::const_reference,
1182 typename V::reference>::type reference;
1183 typedef typename boost::mpl::if_<boost::is_const<V>,
1184 typename V::const_closure_type,
1185 typename V::closure_type>::type vector_closure_type;
1186 typedef basic_range<size_type, difference_type> range_type;
1187 typedef basic_slice<size_type, difference_type> slice_type;
1188 typedef const self_type const_closure_type;
1189 typedef self_type closure_type;
1190 typedef typename storage_restrict_traits<typename V::storage_category,
1191 dense_proxy_tag>::storage_category storage_category;
1193 // Construction and destruction
1195 vector_indirect (vector_type &data, size_type size):
1196 data_ (data), ia_ (size) {}
1198 vector_indirect (vector_type &data, const indirect_array_type &ia):
1199 data_ (data), ia_ (ia.preprocess (data.size ())) {}
1201 vector_indirect (const vector_closure_type &data, const indirect_array_type &ia, int):
1202 data_ (data), ia_ (ia.preprocess (data.size ())) {}
1206 size_type size () const {
1210 const_indirect_array_type &indirect () const {
1214 indirect_array_type &indirect () {
1218 // Storage accessors
1220 const vector_closure_type &data () const {
1224 vector_closure_type &data () {
1229 #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
1231 const_reference operator () (size_type i) const {
1232 return data_ (ia_ (i));
1235 reference operator () (size_type i) {
1236 return data_ (ia_ (i));
1240 const_reference operator [] (size_type i) const {
1244 reference operator [] (size_type i) {
1249 reference operator () (size_type i) const {
1250 return data_ (ia_ (i));
1254 reference operator [] (size_type i) const {
1259 // ISSUE can this be done in free project function?
1260 // Although a const function can create a non-const proxy to a non-const object
1261 // Critical is that vector_type and data_ (vector_closure_type) are const correct
1263 vector_indirect<vector_type, indirect_array_type> project (const range_type &r) const {
1264 return vector_indirect<vector_type, indirect_array_type> (data_, ia_.compose (r.preprocess (data_.size ())), 0);
1267 vector_indirect<vector_type, indirect_array_type> project (const slice_type &s) const {
1268 return vector_indirect<vector_type, indirect_array_type> (data_, ia_.compose (s.preprocess (data_.size ())), 0);
1271 vector_indirect<vector_type, indirect_array_type> project (const indirect_array_type &ia) const {
1272 return vector_indirect<vector_type, indirect_array_type> (data_, ia_.compose (ia.preprocess (data_.size ())), 0);
1277 vector_indirect &operator = (const vector_indirect &vi) {
1278 // ISSUE need a temporary, proxy can be overlaping alias
1279 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (vi));
1283 vector_indirect &assign_temporary (vector_indirect &vi) {
1284 // assign elements, proxied container remains the same
1285 vector_assign<scalar_assign> (*this, vi);
1290 vector_indirect &operator = (const vector_expression<AE> &ae) {
1291 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (ae));
1296 vector_indirect &assign (const vector_expression<AE> &ae) {
1297 vector_assign<scalar_assign> (*this, ae);
1302 vector_indirect &operator += (const vector_expression<AE> &ae) {
1303 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this + ae));
1308 vector_indirect &plus_assign (const vector_expression<AE> &ae) {
1309 vector_assign<scalar_plus_assign> (*this, ae);
1314 vector_indirect &operator -= (const vector_expression<AE> &ae) {
1315 vector_assign<scalar_assign> (*this, typename vector_temporary_traits<V>::type (*this - ae));
1320 vector_indirect &minus_assign (const vector_expression<AE> &ae) {
1321 vector_assign<scalar_minus_assign> (*this, ae);
1326 vector_indirect &operator *= (const AT &at) {
1327 vector_assign_scalar<scalar_multiplies_assign> (*this, at);
1332 vector_indirect &operator /= (const AT &at) {
1333 vector_assign_scalar<scalar_divides_assign> (*this, at);
1337 // Closure comparison
1339 bool same_closure (const vector_indirect &/*vr*/) const {
1345 bool operator == (const vector_indirect &vi) const {
1346 return (*this).data_ == vi.data_ && ia_ == vi.ia_;
1351 void swap (vector_indirect vi) {
1353 BOOST_UBLAS_CHECK (size () == vi.size (), bad_size ());
1354 // Sparse ranges may be nonconformant now.
1355 // std::swap_ranges (begin (), end (), vi.begin ());
1356 vector_swap<scalar_swap> (*this, vi);
1360 friend void swap (vector_indirect vi1, vector_indirect vi2) {
1366 // Use indirect array as an index - FIXME this fails for packed assignment
1367 typedef typename IA::const_iterator const_subiterator_type;
1368 typedef typename IA::const_iterator subiterator_type;
1371 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1372 typedef indexed_iterator<vector_indirect<vector_type, indirect_array_type>,
1373 typename vector_type::iterator::iterator_category> iterator;
1374 typedef indexed_const_iterator<vector_indirect<vector_type, indirect_array_type>,
1375 typename vector_type::const_iterator::iterator_category> const_iterator;
1377 class const_iterator;
1382 const_iterator find (size_type i) const {
1383 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1384 return const_iterator (*this, i);
1386 return const_iterator (*this, ia_.begin () + i);
1390 iterator find (size_type i) {
1391 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1392 return iterator (*this, i);
1394 return iterator (*this, ia_.begin () + i);
1398 // Iterators simply are indices.
1400 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
1401 class const_iterator:
1402 public container_const_reference<vector_indirect>,
1403 public iterator_base_traits<typename V::const_iterator::iterator_category>::template
1404 iterator_base<const_iterator, value_type>::type {
1406 typedef typename V::const_iterator::difference_type difference_type;
1407 typedef typename V::const_iterator::value_type value_type;
1408 typedef typename V::const_reference reference; //FIXME due to indexing access
1409 typedef typename V::const_iterator::pointer pointer;
1411 // Construction and destruction
1414 container_const_reference<self_type> (), it_ () {}
1416 const_iterator (const self_type &vi, const const_subiterator_type &it):
1417 container_const_reference<self_type> (vi), it_ (it) {}
1419 const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
1420 container_const_reference<self_type> (it ()), it_ (it.it_) {}
1424 const_iterator &operator ++ () {
1429 const_iterator &operator -- () {
1434 const_iterator &operator += (difference_type n) {
1439 const_iterator &operator -= (difference_type n) {
1444 difference_type operator - (const const_iterator &it) const {
1445 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
1446 return it_ - it.it_;
1451 const_reference operator * () const {
1452 // FIXME replace find with at_element
1453 BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
1454 return (*this) ().data_ (*it_);
1457 const_reference operator [] (difference_type n) const {
1458 return *(*this + n);
1463 size_type index () const {
1464 return it_.index ();
1469 const_iterator &operator = (const const_iterator &it) {
1470 container_const_reference<self_type>::assign (&it ());
1477 bool operator == (const const_iterator &it) const {
1478 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
1479 return it_ == it.it_;
1482 bool operator < (const const_iterator &it) const {
1483 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
1484 return it_ < it.it_;
1488 const_subiterator_type it_;
1493 const_iterator begin () const {
1497 const_iterator cbegin () const {
1501 const_iterator end () const {
1502 return find (size ());
1505 const_iterator cend () const {
1509 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
1511 public container_reference<vector_indirect>,
1512 public iterator_base_traits<typename V::iterator::iterator_category>::template
1513 iterator_base<iterator, value_type>::type {
1515 typedef typename V::iterator::difference_type difference_type;
1516 typedef typename V::iterator::value_type value_type;
1517 typedef typename V::reference reference; //FIXME due to indexing access
1518 typedef typename V::iterator::pointer pointer;
1520 // Construction and destruction
1523 container_reference<self_type> (), it_ () {}
1525 iterator (self_type &vi, const subiterator_type &it):
1526 container_reference<self_type> (vi), it_ (it) {}
1530 iterator &operator ++ () {
1535 iterator &operator -- () {
1540 iterator &operator += (difference_type n) {
1545 iterator &operator -= (difference_type n) {
1550 difference_type operator - (const iterator &it) const {
1551 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
1552 return it_ - it.it_;
1557 reference operator * () const {
1558 // FIXME replace find with at_element
1559 BOOST_UBLAS_CHECK (index () < (*this) ().size (), bad_index ());
1560 return (*this) ().data_ (*it_);
1563 reference operator [] (difference_type n) const {
1564 return *(*this + n);
1569 size_type index () const {
1570 return it_.index ();
1575 iterator &operator = (const iterator &it) {
1576 container_reference<self_type>::assign (&it ());
1583 bool operator == (const iterator &it) const {
1584 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
1585 return it_ == it.it_;
1588 bool operator < (const iterator &it) const {
1589 BOOST_UBLAS_CHECK ((*this) ().same_closure (it ()), external_logic ());
1590 return it_ < it.it_;
1594 subiterator_type it_;
1596 friend class const_iterator;
1606 return find (size ());
1610 typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
1611 typedef reverse_iterator_base<iterator> reverse_iterator;
1614 const_reverse_iterator rbegin () const {
1615 return const_reverse_iterator (end ());
1618 const_reverse_iterator crbegin () const {
1622 const_reverse_iterator rend () const {
1623 return const_reverse_iterator (begin ());
1626 const_reverse_iterator crend () const {
1631 reverse_iterator rbegin () {
1632 return reverse_iterator (end ());
1635 reverse_iterator rend () {
1636 return reverse_iterator (begin ());
1640 vector_closure_type data_;
1641 indirect_array_type ia_;
1645 template<class V, class A>
1647 vector_indirect<V, indirect_array<A> > project (V &data, const indirect_array<A> &ia) {
1648 return vector_indirect<V, indirect_array<A> > (data, ia);
1650 template<class V, class A>
1652 const vector_indirect<const V, indirect_array<A> > project (const V &data, const indirect_array<A> &ia) {
1653 // ISSUE was: return vector_indirect<V, indirect_array<A> > (const_cast<V &> (data), ia)
1654 return vector_indirect<const V, indirect_array<A> > (data, ia);
1656 template<class V, class IA>
1658 vector_indirect<V, IA> project (vector_indirect<V, IA> &data, const typename vector_indirect<V, IA>::range_type &r) {
1659 return data.project (r);
1661 template<class V, class IA>
1663 const vector_indirect<V, IA> project (const vector_indirect<V, IA> &data, const typename vector_indirect<V, IA>::range_type &r) {
1664 return data.project (r);
1666 template<class V, class IA>
1668 vector_indirect<V, IA> project (vector_indirect<V, IA> &data, const typename vector_indirect<V, IA>::slice_type &s) {
1669 return data.project (s);
1671 template<class V, class IA>
1673 const vector_indirect<V, IA> project (const vector_indirect<V, IA> &data, const typename vector_indirect<V, IA>::slice_type &s) {
1674 return data.project (s);
1676 template<class V, class A>
1678 vector_indirect<V, indirect_array<A> > project (vector_indirect<V, indirect_array<A> > &data, const indirect_array<A> &ia) {
1679 return data.project (ia);
1681 template<class V, class A>
1683 const vector_indirect<V, indirect_array<A> > project (const vector_indirect<V, indirect_array<A> > &data, const indirect_array<A> &ia) {
1684 return data.project (ia);
1687 // Specialization of temporary_traits
1689 struct vector_temporary_traits< vector_indirect<V> >
1690 : vector_temporary_traits< V > {} ;
1692 struct vector_temporary_traits< const vector_indirect<V> >
1693 : vector_temporary_traits< V > {} ;