Boost logo

Boost :

From: Toon Knapen (toon.knapen_at_[hidden])
Date: 2006-04-27 16:05:44


Spencer Collyer wrote:

[snip]

>
> I have two versions of this function in my sparse_array class:
>
> reference operator[](size_type sub);
> const_reference operator[](size_type sub) const;
>
> The one that returns a non-const reference has to handle the case where there is no element existing at the given subscript. Because it can be used in expressions like
>
> sa[sub]++;
>
> I opted to always create a default-valued element if one did not exist. After reading Meyers' _More Effective C++_ item 30 on proxy classes, I decided not to go down the proxy root as we can't know in advance what our stored values are.
>
> The function that returns a const_reference has an easier time if there is no element at the subscript given, as it can just return a const& to the default value, without having to store anything in the sparse_array.
>
> Because we are sometimes forced to create default values in the array there is a 'normalise()' function which goes through and removes such values. Note also that assignment and merging to sparse_arrays does not copy default values if the values policy says not to.
>

but this means also that if I have a sparse-vector and I loop over all
indices to print the corresponding values on the screen (doing <code>
for( i=0 ; i < size ; ++i ) std::cout << my_spare_vector[i] </code>), I
will totally fill up the sparse structure!

toon


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