Boost logo

Glas :

Re: [glas] return type of operator[](size_type) for sparse vectors?

From: Guntram Berti (berti_at_[hidden])
Date: 2006-04-28 03:20:53


On Tue, Apr 25, 2006 at 09:22:23PM +0200, Toon Knapen wrote:
> Given a sparse vector v of size 10, what would v[0] return? Should it
> return a reference to the element at position 0 in the sparse vector.
> What if that element is a structural-zero? It clearly is not as
> straightforward as with dense vectors.
>
> The options AFAICT are:
> 1) provide no 'operator[](size_type)' to avoid confusion
>
> 2) return the value at the corresponding position or if the position
> corresponds to a structural-zero return zero. Thus in that case
> operator[] will not affect/change the structure and operator[] will not
> allow to change the value at the corresponding position.
>
> 3) return a proxy. If the proxy is being assigned to, the structure will
> be changed in case the position corresponded with a structural-zero.
>
> What would you find most intuitive? (of course other methods allow to
> query if a specific position corresponds to a non-zero or not so
> operator[] is not the only way to access elements). Also your feedback
> on you experience with other libraries can be usefull to make the right
> decision here.

In the GrAL library, I use both operator[] (for read/write access)
and operator() (for read-only access). For the equivalent of sparse
arrays in GrAL, operator() returns a copy of the value or the default
value, resp.
The convention of using both ops [] and () permits to choose at the call site
whether write access is needed, and thus to choose the potentially more
efficient operator() if possible. (I think that's not possible by just
providing operator[] in const and non-const form, as the non-const variant
seems to be preferred by the compiler - any language expert's comment on this?)

Of course, to make use of this idea within glas practical, one would have to
support operator() for all vectors and probably also matrices.

--guntram

-- 
============================================================
Dr. Guntram Berti -- NEC C&C Research Laboratories   O__  ==
Rathausallee 10, D-53757 St. Augustin, Germany      c/ /'_ == 
++49 +2241 92 52  -32(voice) -99(fax)              (*) \(*) ==
                                                  ------------ 
http://www.ccrl-nece.de/~berti - berti_at_[hidden]
============================================================