# Vector Proxies

## Vector Range

#### Description

The templated class `vector_range<V>` allows addressing a range of a vector.

#### Example

    #include <boost/numeric/ublas/vector.hpp>
    #include <boost/numeric/ublas/io.hpp>

    int main () {
        using namespace boost::numeric::ublas;
        vector<double> v (3);
        vector_range<vector<double> > vr (v, range (0, 3));
        for (unsigned i = 0; i < vr.size (); ++ i)
            vr (i) = i;
        std::cout << vr << std::endl;
    }

#### Definition

Defined in the header vector_proxy.hpp.

#### Template parameters

Parameter | Description | Default
--- | --- | ---
`V` | The type of vector referenced. |

#### Model of

Vector Expression .

#### Type requirements

None, except for those imposed by the requirements of Vector Expression .

#### Public base classes

`vector_expression<vector_range<V> >`

#### Members

Member | Description
--- | ---
`vector_range (vector_type &data, const range &r)` | Constructs a sub vector.
`size_type start () const` | Returns the start of the sub vector.
`size_type size () const` | Returns the size of the sub vector.
`const_reference operator () (size_type i) const` | Returns the value of the `i`-th element.
`reference operator () (size_type i)` | Returns a reference of the `i`-th element.
`const_reference operator [] (size_type i) const` | Returns the value of the `i`-th element.
`reference operator [] (size_type i)` | Returns a reference of the `i`-th element.
`vector_range &operator = (const vector_range &vr)` | The assignment operator.
`vector_range &assign_temporary (vector_range &vr)` | Assigns a temporary. May change the vector range `vr` .
`template<class AE> vector_range &operator = (const vector_expression<AE> &ae)` | The extended assignment operator.
`template<class AE> vector_range &assign (const vector_expression<AE> &ae)` | Assigns a vector expression to the sub vector. Left and right hand side of the assignment should be independent.
`template<class AE> vector_range &operator += (const vector_expression<AE> &ae)` | A computed assignment operator. Adds the vector expression to the sub vector.
`template<class AE> vector_range &plus_assign (const vector_expression<AE> &ae)` | Adds a vector expression to the sub vector. Left and right hand side of the assignment should be independent.
`template<class AE> vector_range &operator -= (const vector_expression<AE> &ae)` | A computed assignment operator. Subtracts the vector expression from the sub vector.
`template<class AE> vector_range &minus_assign (const vector_expression<AE> &ae)` | Subtracts a vector expression from the sub vector. Left and right hand side of the assignment should be independent.
`template<class AT> vector_range &operator *= (const AT &at)` | A computed assignment operator. Multiplies the sub vector with a scalar.
`template<class AT> vector_range &operator /= (const AT &at)` | A computed assignment operator. Divides the sub vector through a scalar.
`void swap (vector_range &vr)` | Swaps the contents of the sub vectors.
`const_iterator begin () const` | Returns a `const_iterator` pointing to the beginning of the `vector_range`.
`const_iterator end () const` | Returns a `const_iterator` pointing to the end of the `vector_range`.
`iterator begin ()` | Returns a `iterator` pointing to the beginning of the `vector_range`.
`iterator end ()` | Returns a `iterator` pointing to the end of the `vector_range`.
`const_reverse_iterator rbegin () const` | Returns a `const_reverse_iterator` pointing to the beginning of the reversed `vector_range`.
`const_reverse_iterator rend () const` | Returns a `const_reverse_iterator` pointing to the end of the reversed `vector_range`.
`reverse_iterator rbegin ()` | Returns a `reverse_iterator` pointing to the beginning of the reversed `vector_range`.
`reverse_iterator rend ()` | Returns a `reverse_iterator` pointing to the end of the reversed `vector_range`.

#### Interface

        // Vector based range class
        template<class V>
        class vector_range:
            public vector_expression<vector_range<V> > {
        public:
            typedef const V const_vector_type;
            typedef V vector_type;
            typedef typename V::size_type size_type;
            typedef typename V::difference_type difference_type;
            typedef typename V::value_type value_type;
            typedef typename V::const_reference const_reference;
            typedef typename V::reference reference;
            typedef typename V::const_pointer const_pointer;
            typedef typename V::pointer pointer;
            typedef const vector_const_reference<const vector_range<vector_type> > const_closure_type;
            typedef vector_reference<vector_range<vector_type> > closure_type;
            typedef typename V::const_iterator const_iterator_type;
            typedef typename V::iterator iterator_type;
            typedef typename storage_restrict_traits<typename V::storage_category,
                                                     dense_proxy_tag>::storage_category storage_category;

            // Construction and destruction
            vector_range ();
            vector_range (vector_type &data, const range &r);

            // Accessors
            size_type start () const;
            size_type size () const;
            const_vector_type &data () const;
            vector_type &data ();

            // Element access
            const_reference operator () (size_type i) const;
            reference operator () (size_type i);

            const_reference operator [] (size_type i) const;
            reference operator [] (size_type i);

            vector_range<vector_type> project (const range &r) const;

            // Assignment
            vector_range &operator = (const vector_range &vr);
            vector_range &assign_temporary (vector_range &vr);
            template<class AE>
            vector_range &operator = (const vector_expression<AE> &ae);
            template<class AE>
            vector_range &assign (const vector_expression<AE> &ae);
            template<class AE>
            vector_range &operator += (const vector_expression<AE> &ae);
            template<class AE>
            vector_range &plus_assign (const vector_expression<AE> &ae);
            template<class AE>
            vector_range &operator -= (const vector_expression<AE> &ae);
            template<class AE>
            vector_range &minus_assign (const vector_expression<AE> &ae);
            template<class AT>
            vector_range &operator *= (const AT &at);
            template<class AT>
            vector_range &operator /= (const AT &at);

            // Swapping
            void swap (vector_range &vr);
            friend void swap (vector_range &vr1, vector_range &vr2);

            class const_iterator;
            class iterator;

            // Element lookup
            const_iterator find_first (size_type i) const;
            iterator find_first (size_type i);
            const_iterator find_last (size_type i) const;
            iterator find_last (size_type i);

            // Iterators simply are pointers.

            class const_iterator:
                public container_const_reference<vector_range>,
                public random_access_iterator_base<const_iterator, value_type> {
            public:
                typedef typename const_iterator_type::iterator_category iterator_category;
                typedef typename const_iterator_type::difference_type difference_type;
                typedef typename const_iterator_type::value_type value_type;
                typedef typename const_iterator_type::reference reference;
                typedef typename const_iterator_type::pointer pointer;

                // Construction and destruction
                const_iterator ();
                const_iterator (const vector_range &vr, const const_iterator_type &it);
                const_iterator (const iterator &it);

                // Arithmetic
                const_iterator &operator ++ ();
                const_iterator &operator -- ();
                const_iterator &operator += (difference_type n);
                const_iterator &operator -= (difference_type n);
                difference_type operator - (const const_iterator &it) const;

                // Dereference
                reference operator * () const;

                // Index
                size_type index () const;

                // Assignment
                const_iterator &operator = (const const_iterator &it);

                // Comparison
                bool operator == (const const_iterator &it) const;
                bool operator <(const const_iterator &it) const;
            };

            const_iterator begin () const;
            const_iterator end () const;

            class iterator:
                public container_reference<vector_range>,
                public random_access_iterator_base<iterator, value_type> {
            public:
                typedef typename iterator_type::iterator_category iterator_category;
                typedef typename iterator_type::difference_type difference_type;
                typedef typename iterator_type::value_type value_type;
                typedef typename iterator_type::reference reference;
                typedef typename iterator_type::pointer pointer;

                // Construction and destruction
                iterator ();
                iterator (vector_range &vr, const iterator_type &it);

                // Arithmetic
                iterator &operator ++ ();
                iterator &operator -- ();
                iterator &operator += (difference_type n);
                iterator &operator -= (difference_type n);
                difference_type operator - (const iterator &it) const;

                // Dereference
                reference operator * () const;

                // Index
                size_type index () const;

                // Assignment
                iterator &operator = (const iterator &it);

                // Comparison
                bool operator == (const iterator &it) const;
                bool operator <(const iterator &it) const;
            };

            iterator begin ();
            iterator end ();

            // Reverse iterator

            typedef reverse_iterator_base<const_iterator> const_reverse_iterator;

            const_reverse_iterator rbegin () const;
            const_reverse_iterator rend () const;

            typedef reverse_iterator_base<iterator> reverse_iterator;

            reverse_iterator rbegin ();
            reverse_iterator rend ();
        };

### Projections

#### Prototypes

        template<class V>
        vector_range<V> project (V &data, const range &r);
        template<class V>
        const vector_range<const V> project (const V &data, const range &r);
        template<class V>
        vector_range<V> project (const vector_range<V> &data, const range &r);

#### Description

The free `project` functions support the construction of vector ranges.

#### Definition

Defined in the header vector_proxy.hpp.

#### Type requirements

- `V` is a model of Vector Expression .

#### Preconditions

- `r.start () + r.size () <= data.size ()`

#### Complexity

Linear depending from the size of the range.

#### Examples

    #include <boost/numeric/ublas/vector.hpp>
    #include <boost/numeric/ublas/io.hpp>

    int main () {
        using namespace boost::numeric::ublas;
        vector<double> v (3);
        for (int i = 0; i < 3; ++ i)
            project (v, range (0, 3)) (i) = i;
        std::cout << project (v, range (0, 3)) << std::endl;
    }

## Vector Slice

#### Description

The templated class `vector_slice<V>` allows addressing a slice of a vector.

#### Example

    #include <boost/numeric/ublas/vector.hpp>
    #include <boost/numeric/ublas/io.hpp>

    int main () {
        using namespace boost::numeric::ublas;
        vector<double> v (3);
        vector_slice<vector<double> > vs (v, slice (0, 1, 3));
        for (unsigned i = 0; i < vs.size (); ++ i)
            vs (i) = i;
        std::cout << vs << std::endl;
    }

#### Definition

Defined in the header vector_proxy.hpp.

#### Template parameters

Parameter | Description | Default
--- | --- | ---
`V` | The type of vector referenced. |

#### Model of

Vector Expression .

#### Type requirements

None, except for those imposed by the requirements of Vector Expression .

#### Public base classes

`vector_expression<vector_slice<V> >`

#### Members

Member | Description
--- | ---
`vector_slice (vector_type &data, const slice &s)` | Constructs a sub vector.
`size_type size () const` | Returns the size of the sub vector.
`const_reference operator () (size_type i) const` | Returns the value of the `i`-th element.
`reference operator () (size_type i)` | Returns a reference of the `i`-th element.
`const_reference operator [] (size_type i) const` | Returns the value of the `i`-th element.
`reference operator [] (size_type i)` | Returns a reference of the `i`-th element.
`vector_slice &operator = (const vector_slice &vs)` | The assignment operator.
`vector_slice &assign_temporary (vector_slice &vs)` | Assigns a temporary. May change the vector slice `vs` .
`template<class AE> vector_slice &operator = (const vector_expression<AE> &ae)` | The extended assignment operator.
`template<class AE> vector_slice &assign (const vector_expression<AE> &ae)` | Assigns a vector expression to the sub vector. Left and right hand side of the assignment should be independent.
`template<class AE> vector_slice &operator += (const vector_expression<AE> &ae)` | A computed assignment operator. Adds the vector expression to the sub vector.
`template<class AE> vector_slice &plus_assign (const vector_expression<AE> &ae)` | Adds a vector expression to the sub vector. Left and right hand side of the assignment should be independent.
`template<class AE> vector_slice &operator -= (const vector_expression<AE> &ae)` | A computed assignment operator. Subtracts the vector expression from the sub vector.
`template<class AE> vector_slice &minus_assign (const vector_expression<AE> &ae)` | Subtracts a vector expression from the sub vector. Left and right hand side of the assignment should be independent.
`template<class AT> vector_slice &operator *= (const AT &at)` | A computed assignment operator. Multiplies the sub vector with a scalar.
`template<class AT> vector_slice &operator /= (const AT &at)` | A computed assignment operator. Divides the sub vector through a scalar.
`void swap (vector_slice &vs)` | Swaps the contents of the sub vectors.
`const_iterator begin () const` | Returns a `const_iterator` pointing to the beginning of the `vector_slice`.
`const_iterator end () const` | Returns a `const_iterator` pointing to the end of the `vector_slice`.
`iterator begin ()` | Returns a `iterator` pointing to the beginning of the `vector_slice`.
`iterator end ()` | Returns a `iterator` pointing to the end of the `vector_slice`.
`const_reverse_iterator rbegin () const` | Returns a `const_reverse_iterator` pointing to the beginning of the reversed `vector_slice`.
`const_reverse_iterator rend () const` | Returns a `const_reverse_iterator` pointing to the end of the reversed `vector_slice`.
`reverse_iterator rbegin ()` | Returns a `reverse_iterator` pointing to the beginning of the reversed `vector_slice`.
`reverse_iterator rend ()` | Returns a `reverse_iterator` pointing to the end of the reversed `vector_slice`.

#### Interface

        // Vector based slice class
        template<class V>
        class vector_slice:
            public vector_expression<vector_slice<V> > {
        public:
            typedef const V const_vector_type;
            typedef V vector_type;
            typedef typename V::size_type size_type;
            typedef typename V::difference_type difference_type;
            typedef typename V::value_type value_type;
            typedef typename V::const_reference const_reference;
            typedef typename V::reference reference;
            typedef typename V::const_pointer const_pointer;
            typedef typename V::pointer pointer;
            typedef const vector_const_reference<const vector_slice<vector_type> > const_closure_type;
            typedef vector_reference<vector_slice<vector_type> > closure_type;
            typedef slice::const_iterator const_iterator_type;
            typedef slice::const_iterator iterator_type;
            typedef typename storage_restrict_traits<typename V::storage_category,
                                                     dense_proxy_tag>::storage_category storage_category;

            // Construction and destruction
            vector_slice ();
            vector_slice (vector_type &data, const slice &s);

            // Accessors
            size_type start () const;
            size_type stride () const;
            size_type size () const;
            const_vector_type &data () const;
            vector_type &data ();

            // Element access
            const_reference operator () (size_type i) const;
            reference operator () (size_type i);

            const_reference operator [] (size_type i) const;
            reference operator [] (size_type i);

            vector_slice<vector_type> project (const range &r) const;
            vector_slice<vector_type> project (const slice &s) const;

            // Assignment
            vector_slice &operator = (const vector_slice &vs);
            vector_slice &assign_temporary (vector_slice &vs);
            template<class AE>
            vector_slice &operator = (const vector_expression<AE> &ae);
            template<class AE>
            vector_slice &assign (const vector_expression<AE> &ae);
            template<class AE>
            vector_slice &operator += (const vector_expression<AE> &ae);
            template<class AE>
            vector_slice &plus_assign (const vector_expression<AE> &ae);
            template<class AE>
            vector_slice &operator -= (const vector_expression<AE> &ae);
            template<class AE>
            vector_slice &minus_assign (const vector_expression<AE> &ae);
            template<class AT>
            vector_slice &operator *= (const AT &at);
            template<class AT>
            vector_slice &operator /= (const AT &at);

            // Swapping
            void swap (vector_slice &vs);
            friend void swap (vector_slice &vs1, vector_slice &vs2);

            class const_iterator;
            class iterator;

            // Element lookup
            const_iterator find_first (size_type i) const;
            iterator find_first (size_type i);
            const_iterator find_last (size_type i) const;
            iterator find_last (size_type i);

            // Iterators simply are indices.

            class const_iterator:
                public container_const_reference<vector_slice>,
                public random_access_iterator_base<const_iterator, value_type> {
            public:
                typedef typename V::const_iterator::iterator_category iterator_category;
                typedef typename V::const_iterator::difference_type difference_type;
                typedef typename V::const_iterator::value_type value_type;
                typedef typename V::const_iterator::reference reference;
                typedef typename V::const_iterator::pointer pointer;

                // Construction and destruction
                const_iterator ();
                const_iterator (const vector_slice &vs, const const_iterator_type &it);
                const_iterator (const iterator &it);

                // Arithmetic
                const_iterator &operator ++ ();
                const_iterator &operator -- ();
                const_iterator &operator += (difference_type n);
                const_iterator &operator -= (difference_type n);
                difference_type operator - (const const_iterator &it) const;

                // Dereference
                reference operator * () const;

                // Index
                size_type index () const;

                // Assignment
                const_iterator &operator = (const const_iterator &it);

                // Comparison
                bool operator == (const const_iterator &it) const;
                bool operator <(const const_iterator &it) const;
            };

            const_iterator begin () const;
            const_iterator end () const;

            class iterator:
                public container_reference<vector_slice>,
                public random_access_iterator_base<iterator, value_type> {
            public:
                typedef typename V::iterator::iterator_category iterator_category;
                typedef typename V::iterator::difference_type difference_type;
                typedef typename V::iterator::value_type value_type;
                typedef typename V::iterator::reference reference;
                typedef typename V::iterator::pointer pointer;

                // Construction and destruction
                iterator ();
                iterator (vector_slice &vs, const iterator_type &it);

                // Arithmetic
                iterator &operator ++ ();
                iterator &operator -- ();
                iterator &operator += (difference_type n);
                iterator &operator -= (difference_type n);
                difference_type operator - (const iterator &it) const;

                // Dereference
                reference operator * () const;

                // Index
                size_type index () const;

                // Assignment
                iterator &operator = (const iterator &it);

                // Comparison
                bool operator == (const iterator &it) const;
                bool operator <(const iterator &it) const;
            };

            iterator begin ();
            iterator end ();

            // Reverse iterator

            typedef reverse_iterator_base<const_iterator> const_reverse_iterator;

            const_reverse_iterator rbegin () const;
            const_reverse_iterator rend () const;

            typedef reverse_iterator_base<iterator> reverse_iterator;

            reverse_iterator rbegin ();
            reverse_iterator rend ();
        };

### Projections

#### Prototypes

        template<class V>
        vector_slice<V> project (const vector_slice<V> &data, const range &r);
        template<class V>
        vector_slice<V> project (V &data, const slice &s);
        template<class V>
        const vector_slice<const V> project (const V &data, const slice &s);
        template<class V>
        vector_slice<V> project (const vector_slice<V> &data, const slice &s);

#### Description

The free `project` functions support the construction of vector slices.

#### Definition

Defined in the header vector_proxy.hpp.

#### Type requirements

- `V` is a model of Vector Expression .

#### Preconditions

- `s.start () + s.stride () * s.size () <= data.size ()`

#### Complexity

Linear depending from the size of the slice.

#### Examples

    #include <boost/numeric/ublas/vector.hpp>
    #include <boost/numeric/ublas/io.hpp>

    int main () {
        using namespace boost::numeric::ublas;
        vector<double> v (3);
        for (int i = 0; i < 3; ++ i)
            project (v, slice (0, 1, 3)) (i) = i;
        std::cout << project (v, slice (0, 1, 3)) << std::endl;
    }

Copyright (©) 2000-2002 Joerg Walter, Mathias Koch
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided ``as is'' without express or implied warranty, and with no claim as to its suitability for any purpose.

Last revised: 1/15/2003