Boost logo

Ublas :

From: Vardan Akopian (vakopian_at_[hidden])
Date: 2006-02-13 17:56:24


Hi Gunter,

I think readonly_array_adaptor and shallow_array_adaptor<const double>
are very similar in functionality. Of course readonly_array_adaptor is
more lightweight (e.g. avoids reference counting and the locking
associated with it). But on the other hand readonly_array_adaptor is
not really a model of Storage as defined in the Storage concept
(http://www.boost.org/libs/numeric/ublas/doc/storage_concept.htm). For
example it does not (and cannot) implement the X(n,t) constructor and
resize(n, t) method (not that it makes it any less usefull !).
Therefore I don't think it should be in storage.hpp, but rather in a
separate header file, since classes in storage.hpp are assumed to be
models of Storage (?).

Notice that in the discussion referred above
(http://lists.boost.org/MailArchives/ublas/2005/08/0678.php) I was
advocating for the carray_adaptor to become what you have in your
readonly_array_adaptor, and to put it in a separate header file. So
I'm all for it.

Thanks.
-Vardan

On 2/13/06, Gunter Winkler <guwi17_at_[hidden]> wrote:
> On Friday 10 February 2006 13:17, Gunter Winkler wrote:
> > Hello,
> >
> > I have the following problem:
> >
> > I have a pointer to a double array: const double *data;
> > and an integer size
> >
> > I would like to use this as a vector in ublas routines.
> >
> > I expected that
> >
> > carray_adaptor<const double> shared(size, data);
> > vector<const double, carray_adaptor<const double> > v(size, shared);
> >
> > does the job. Unfortunately it does not compile because the copy
> > constructor of carray_adaptor is private. But this make the adaptor quite
> > useless, because I am not able to convert it into a vector_expression
> > (without copying the data).
> >
> > What is the rigth way to solve my problem?
> >
> > Should we support a syntax like
> >
> > vector<double> v(size);
> > matrix<double> m(size, size);
> > const double *external_data;
> >
> > v = prod(m, external_vector_adaptor(size, external_data));
> >
> > this would be very convenient.
>
> I solved the above problem by modifying carray_adaptor. Should it be added
> to storage.hpp and documented or should I provide a short sample program an put
> it on webpage?
>
> template <class T>
> boost::numeric::ublas::vector<const T, boost::numeric::ublas::readonly_array_adaptor<T> >
> make_vector(const size_t dim, const T *data)
> {
> typedef boost::numeric::ublas::readonly_array_adaptor<T> a_t;
> typedef boost::numeric::ublas::vector<const T, a_t> v_t;
> a_t pa(dim, data);
> v_t v(dim, pa);
> return v;
> }
>
>
>
> /** \brief gives read only access to a chunk of memory.
> *
> * example:
> * <code>
> * T data[dim];
> * typedef readonly_array_adaptor<T> a_t;
> * typedef vector<const T, a_t> v_t;
> * a_t pa(dim, &(data[0]));
> * v_t v(dim, pa);
> * </code>
> */
> template<class T>
> class readonly_array_adaptor:
> public storage_array<readonly_array_adaptor<T> > {
>
> typedef readonly_array_adaptor<T> self_type;
> public:
> typedef std::size_t size_type;
> typedef std::ptrdiff_t difference_type;
> typedef T value_type;
> typedef const T &const_reference;
> typedef const T *const_pointer;
> public:
>
> // Construction and destruction
> BOOST_UBLAS_INLINE
> readonly_array_adaptor ():
> size_ (0), data_ (0) {
> }
> BOOST_UBLAS_INLINE
> readonly_array_adaptor (size_type size, const_pointer data):
> size_ (size), data_ (data) {
> }
> BOOST_UBLAS_INLINE
> ~readonly_array_adaptor () {
> }
>
> readonly_array_adaptor (const readonly_array_adaptor& rhs)
> : size_(rhs.size_), data_(rhs.data_)
> { }
>
> // Resizing
> BOOST_UBLAS_INLINE
> void resize (size_type size) {
> size_ = size;
> }
> BOOST_UBLAS_INLINE
> void resize (size_type size, const_pointer data) {
> size_ = size;
> data_ = data;
> }
>
> // Random Access Container
> BOOST_UBLAS_INLINE
> size_type max_size () const {
> return std::numeric_limits<size_type>::max ();
> }
>
> BOOST_UBLAS_INLINE
> bool empty () const {
> return size_ == 0;
> }
>
> BOOST_UBLAS_INLINE
> size_type size () const {
> return size_;
> }
>
> // Element access
> BOOST_UBLAS_INLINE
> const_reference operator [] (size_type i) const {
> BOOST_UBLAS_CHECK (i < size_, bad_index ());
> return data_ [i];
> }
>
> // Iterators simply are pointers.
> typedef const_pointer const_iterator;
>
> BOOST_UBLAS_INLINE
> const_iterator begin () const {
> return data_;
> }
> BOOST_UBLAS_INLINE
> const_iterator end () const {
> return data_ + size_;
> }
>
> // this typedef is used by vector and matrix classes
> typedef const_pointer iterator;
>
> // Reverse iterators
> typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
> typedef std::reverse_iterator<iterator> reverse_iterator;
>
> BOOST_UBLAS_INLINE
> const_reverse_iterator rbegin () const {
> return const_reverse_iterator (end ());
> }
> BOOST_UBLAS_INLINE
> const_reverse_iterator rend () const {
> return const_reverse_iterator (begin ());
> }
>
> private:
> size_type size_;
> const_pointer data_;
> };
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
>