Boost logo

Boost :

From: Joerg Walter (jhr.walter_at_[hidden])
Date: 2002-03-23 07:11:16


--- In boost_at_y..., Kresimir Fresl <fresl_at_g...> wrote:
>
> jhrwalter wrote:
>
> [...]
> > I imagine, that we could introduce here some (array_type_?)traits
> > classes, which define the iterators for valarray via partial
> > specialization.
> [...]
> > Ok, if all agree, that it would be useful to have std::valarray as
> > another array_type option, I'll try to add some traits logic to
> > support it at least under the more standard conforming compilers.
>
> Some time ago I wrote a patch that extends Dietmar Kuehl's
> `boost/array_traits.hpp' for `std::valarray<>'. Here are relevant
> lines (diff format):
>
> --------------------------------------------------------------------------
-----------
> 20a21,22
> > // Feb 23, 2001 Added std::valarray<T> support. --K. F.
> >
> 30a33,36
> > #ifndef NO_VALARRAY_SUPPORT
> > # include <valarray>
> > #endif
> >
> 98a105,150
> >
> > #ifndef NO_VALARRAY_SUPPORT
> >
> > // --- a special version for non-const valarray<T>s
> -----------------------
> >
> > template <typename T>
> > struct array_traits< std::valarray<T> >
> > {
> > typedef T* iterator;
> > typedef iterator iter_type; // just for backward compatibility
> > typedef T value_type;
> > typedef value_type& reference;
> > typedef std::size_t size_type;
> > static iterator begin (std::valarray<T>& va) { return &va[0]; }
> > static iterator end (std::valarray<T>& va) {
> > return &va[0] + va.size();
> > }
> > static size_type size (std::valarray<T>& va) { return
va.size(); }
> > };
> >
> > // --- a special version for const valarray<T>s ------------------
> >
> > template <typename T>
> > struct array_traits<std::valarray<T> const>
> > {
> > typedef T const* iterator;
> > typedef iterator iter_type; // just for backward compatibility
> > typedef std::size_t size_type;
> > typedef T const value_type;
> > typedef value_type& reference;
> > typedef value_type* pointer;
> > static iterator begin (std::valarray<T> const& cva) {
> > std::valarray<T>& va = const_cast<std::valarray<T>&> (cva);
> > return &va[0];
> > }
> > static iterator end (std::valarray<T> const& cva) {
> > std::valarray<T>& va = const_cast<std::valarray<T>&> (cva);
> > return &va[0] + va.size();
> > }
> > static size_type size (std::valarray<T> const& cva) {
> > return cva.size();
> > }
> > };
> >
> > #endif // NO_VALARRAY_SUPPORT
> --------------------------------------------------------------------------
------------
>
> `const_cast<std::valarray<T>&>()' in const version is needed
> because `valarray<>::operator[] const' returns `T' and not
> `T const&'. I hope that this code is legal -- it works with
> g++ 3.0.4 and Comeau 4.2.45.2.

Interesting on its own.

> Now, with following changes to `ublas/vector.h':
>
> --------------------------------------------------------------------------
------------
> 24a25,26
> > #include <boost/array_traits.hpp>
> >
> 351,352c353,357
> < typedef typename A::const_iterator const_iterator_type;
> < typedef typename A::iterator iterator_type;
> ---
> > #ifndef NUMERICS_USE_INDEXED_ITERATOR
> > typedef typename boost::array_traits<A const>::iterator
> > const_iterator_type;
> > typedef typename boost::array_traits<A>::iterator
iterator_type;
> > #endif
> 520c525
> < return const_iterator (*this, data_.begin () + i);
> ---
> > return const_iterator (*this, boost::begin (data_) + i);
> 528c533
> < return iterator (*this, data_.begin () + i);
> ---
> > return iterator (*this, boost::begin (data_) + i);
> --------------------------------------------------------------------------
-------------------
>
> `std::valarray<>' can be used as array_type in `numerics::vector<>'
> without NUMERICS_USE_INDEXED_ITERATOR defined
> (at least with g++ 3.0.4 && at least in some simple examples ;o).
>
> BTW, `std::valarray<>' hasn't `insert()', `erase()' and `clear()' member
> functions, but if corresponding functions in `numerics::vector<>' are
> not used, this shouldn't be a problem.

Under certain circumstances, these members should be called. If a sparse
matrix result is assigned to a dense matrix, the assignment operation should
be dispatched to the sparse matrix logic: clear the dense matrix and insert
the rhs elements at the corresponding positions.

In the last days I had another idea. To get the CLAPACK tests working, we
needed to adapt raw T* pointers. We therefore recently introduced a class
array_adaptor. Now another way to solve valarray adaption could be something
like:

valarray<double> va (size);
array_adaptor<double> aa (size, &va [0]);
vector<double, array_adaptor<double> > v (size, aa);

What do you think?

Best regards

Joerg


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk