# Glas :

## Re: [glas] dense and sparse vectors

From: Karl Meerbergen (Karl.Meerbergen_at_[hidden])
Date: 2005-08-04 09:12:14

Hello All,

Which algorithms are going to use collections? If we answer this
question first, we can also decide which iterators are needed to
implement these algorithms? (We may conclude that iterators are not good
at all).

One example given by Toon is printing a vector.

Karl

>On Wed, 2005-08-03 at 22:20 +0200, Toon Knapen wrote:
>
>
>
>>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>
>>
>>

<snip>

>
>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, ...).
>