commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles Sadowski <gil...@harfang.homelinux.org>
Subject Re: [math] Read-only RealVector
Date Thu, 11 Aug 2011 11:44:41 GMT
On Thu, Aug 11, 2011 at 01:16:32PM +0200, Luc Maisonobe wrote:
> Le 11/08/2011 12:24, Sébastien Brisard a écrit :
> >>
> >>Well, in fact I would very much like to have immutable vectors too.
> >>Immutability is really a way to simplify implementations. Surprisingly it
> >>sometimes also decrease time and memory consumption, because defensive
> >>copies littering user code can be avoided.
> >>
> >Luc, I have a silly question. Why do you think immutable vectors would
> >prevent defensive copy ? Creating an immutable vector from an existing
> >mutable vector would require a defensive copy, wouldn't it?
> 
> Yes, of course. What I meant is that when users don't have the
> choice and are forced to use only mutable objects, then in many
> places they do defensive copying, just because they do not even know
> if some other code will attempt to modify the object in place after
> they have retrieved it. So you end up with "B b = a.getB().clone()"
> to protect the B instance.
> 
> When instances are known to be immutable, you don't copy them (and
> you often neither implement Cloneable nor set up copy constructors).
> 
> >Thats the reason why I'm talking about read-only vectors, and not
> >immutable vectors.
> >The solver would keep a reference to the underlying (mutable) vector,
> >so would be able to modify (indirectly) the read-only vector.
> >The observer would only have a reference to the read-only vector, so
> >would not be able to modify the underlying vector.
> 
> The problem with this approach is that the observer could not
> preserve the instance across several calls (for example to compare
> the instance from one previous call to the current one). If a
> specific observer wants to do that, then it needs to be able to copy
> the instance. If the observer knows the object is immutable, it will
> only preserve the reference.

If the vector must be mutable, because it is changed in place as the
algorithm iterates, and if the checker needs to keep older states, it
must copy the current solution passed to it. Thus you don't save
anything (a copy must be made either in the solver or in the checker).

With an immutable vector, you create a new intance at each iteration but you
can pass it directly (no copy) and the checker can keep it (also without
making a copy).


Gilles

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message