#include #include #include using namespace boost::numeric::ublas; template > class vector_generator; // Forward declaration template class vector_generator: public vector_container< vector_generator > { typedef F functor_type; typedef vector_generator 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_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 { 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(), it_(i) {} const_iterator(const self_type &v, size_type i ): container_const_reference(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_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 vg( tfun() ,12); vector vec( vg ); std::cout << vg*2 << std::endl; std::cout << vec << std::endl; std::cout << inner_prod(vg,vg) << std::endl; return 0; }