Boost logo

Glas :

Re: [glas] dense and sparse vectors

From: Theodore Papadopoulo (Theodore.Papadopoulo_at_[hidden])
Date: 2005-08-04 05:22:23

On Wed, 2005-08-03 at 22:20 +0200, Toon Knapen wrote:

> Thinking about it again, your approach gave me following idea which
> probably is more generic: The begin and end functions just take a 'tag'
> as argument that specifies if the iterator iterates over all elements or
> only all structural non-zero's. So soth like:
> <code>
> struct all{} ;
> struct nz{} ;
> class some_sparse_vector {
> public:
> iterator<all> begin(all) ;
> iterator<nz> begin(nz) ;
> iterator<all> end(all) ;
> iterator<nz> end(nz) ;
> } ;
> // so implementing a function that prints the elements
> // in a vector might be implemented to print all elements
> // or only the non-zero's
> template < class Tag >
> void print(const some_sparse_vector& v)
> {
> std::copy( v.begin(Tag()), v.end(Tag()),
> std::ostream_iterator(std::cout, ',')) ;
> }
> </code>

Funny, actually this was another comment that I wanted to do, and
because I felt it would be more complicated to do, restrained from doing
it. (And actually, you seem to follow the same path of thinking I did
when I introduced those).

The proposal of templated iterators (combined with my other suggestion)
is exactly what I'm doing in my various multidimensionnal data
structures (matrices, meshes or images...).

for (Image2D::const_iterator<row> i=im.begin();i!=im.end();++i) { ... }

And you can replace Image2D by Mesh,Matrix,... and row by (column,
elemt, pixel, ...).

In practise, I find this to be the best combination (fully stl
compatible, nice notation and somewhat expandable). The only drawback I
can find is a slight complexity in implementing it and (eventually if it
gets accepted in the standard), some incompatibility with the auto
feature (that let's the iterator type to be auto detected...).

Maybe having both versions: begin(XXX) and begin() would make sense.