vector generator

View: New views
3 Messages — Rating Filter:   Alert me  

vector generator

by Bugzilla from matwey.kornilov@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi all,

I tried to make vector object that acts like generator. It means that if
there is rule that associates value for any index uniquely, it isn't
necessary to store that values in the memory. I wrote simple generator in
order to research uBLAS abilities(in attach). I would like to get comments
on the code. It works perfectly for me but it is possible that it can be
made more general and there are some uBLAS tips I've missed.


[vector_generator.cpp]

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

using namespace boost::numeric::ublas;

template<class F, class ALLOC = std::allocator<typename F::result_type> > class vector_generator; // Forward declaration

template<class F, class ALLOC>
class vector_generator:
        public vector_container< vector_generator<F, ALLOC> > {
               
        typedef F functor_type;
        typedef vector_generator<F, ALLOC> self_type;
public:
        typedef typename F::result_type value_type;
        typedef typename ALLOC::size_type size_type;
        typedef typename ALLOC::difference_type difference_type;
        typedef const value_type& const_reference;
        typedef value_type& reference;
        typedef sparse_tag storage_category;
        typedef const value_type* const_pointer;
        typedef const vector_reference<const self_type> const_closure_type;
public:
        vector_generator( functor_type functor, size_type size = 0 ):
                size_( size ), functor_( functor ) {
        }

        size_type size() const {
                return size_;
        }
        void resize( size_type size, bool /*preserve*/ = true ) {
                size_ = size;
        }
       
        const value_type operator()( size_type i ) const {
                return functor_( i );
        }
        //reference operator()(size_type i); no non-const access
        const value_type operator[]( size_type i ) const {
                return functor_( i );
        }
        //reference operator[](size_type i); no non-const access

        //Iterators
public:
        class const_iterator;

        const_iterator find( size_type i ) const {
                return const_iterator( (*this), i );
        }
       
        class const_iterator:
                public container_const_reference< self_type >,
                public bidirectional_iterator_base<sparse_bidirectional_iterator_tag, const_iterator, value_type> {
        public:
                typedef typename self_type::difference_type difference_type;
                typedef typename self_type::value_type value_type;
                typedef typename self_type::size_type size_type;
                typedef typename self_type::const_reference reference;
                typedef typename self_type::const_pointer pointer;

                // Construction and destruction
                const_iterator( size_type i = 0 ):
                        container_const_reference<self_type>(), it_(i) {}
                const_iterator(const self_type &v, size_type i ):
                        container_const_reference<self_type>(v), it_(i) {}

                const_iterator &operator++() {
                        ++it_;
                        return *this;
                }
                const_iterator &operator--() {
                        --it_;
                        return *this;
                }
                const_iterator &operator += (difference_type n) {
                        it_ += n;
                        return *this;
                }
                const_iterator &operator -= (difference_type n) {
                        it_ -= n;
                        return *this;
                }
                value_type operator*() const {
                        BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_, bad_index ());
                        return (*this)()( it_ );
                }
                value_type operator[](difference_type n) const {
                        return *(it_ + n);
                }
                size_type index() const {
                        BOOST_UBLAS_CHECK (it_ >= (*this)().begin ().it_ && it_ < (*this)().end ().it_, bad_index ());
                        return it_;
                }
                bool operator == (const const_iterator &it) const {
                        BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
                        return it_ == it.it_;
                }
                bool operator < (const const_iterator &it) const {
                        BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
                        return it_ < it.it_;
                }
        private:
                size_type it_;
        };

        typedef const_iterator iterator;
        typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
        const_iterator begin() const {
                return find( 0 );
        }
        const_iterator end() const {
                return find( size() );
        }
        const_reverse_iterator rbegin() const {
                return const_reverse_iterator( end() );
        }
        const_reverse_iterator rend() const {
                return const_reverse_iterator( begin() );
        }
       
private:
        size_type    size_;
        functor_type functor_;
};

struct tfun {
        typedef double result_type;

        result_type operator() ( unsigned int i ) const {
                return result_type(i);
        }
};

int main() {
        vector_generator<tfun> vg( tfun() ,12);
        vector<double> vec( vg );
        std::cout << vg*2 << std::endl;
        std::cout << vec << std::endl;
        std::cout << inner_prod(vg,vg) << std::endl;
        return 0;
}



_______________________________________________
ublas mailing list
ublas@...
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: lists@...

Re: vector generator

by Nasos Iliopoulos :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Nice and very useful.
Since I see no license statement on your code should I assume it is public domain?
Thanks!
Nasos




> To: ublas@...
> From: matwey.kornilov@...
> Date: Thu, 22 Oct 2009 21:48:40 +0400
> Subject: [ublas] vector generator
>
>
> Hi all,
>
> I tried to make vector object that acts like generator. It means that if
> there is rule that associates value for any index uniquely, it isn't
> necessary to store that values in the memory. I wrote simple generator in
> order to research uBLAS abilities(in attach). I would like to get comments
> on the code. It works perfectly for me but it is possible that it can be
> made more general and there are some uBLAS tips I've missed.
>


Bing brings you maps, menus, and reviews organized in one place. Try it now.
_______________________________________________
ublas mailing list
ublas@...
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: lists@...

Re: vector generator

by Bugzilla from matwey.kornilov@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I assumed the same conditions as for the boost.ublas.

Nasos Iliopoulos wrote:
>
> Nice and very useful.
> Since I see no license statement on your code should I assume it is public
> domain? Thanks!
> Nasos
>


_______________________________________________
ublas mailing list
ublas@...
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: lists@...