On 1/28/2010 5:00 PM, Richard Harter wrote:
> On 28 Jan 2010 20:57:50 +0100, (Ersek,
> Laszlo) wrote:
>> [...]
>> I think returning a deep copy of the contained object is overkill.
>
> That depends on the application and the data. As a general
> observation, people tend to overestimate the cost of deep copies.
> One deep copy of data that is going to be operated on is
> effectively free - the operation cost dominates the copy cost.
> The cost arises when copying is daisy chained.
There's also the implementation difficulty: Unless the
container is purpose-built for the data type, it doesn't know
how to make a deep copy! The objects in a general-purpose
container are just opaque bags of bytes, whose inner nature
is known only to the functions that hash them, compare them,
and so on.
Of course, you could always endow the container with a
copy function that knows how to copy deeply -- but what does
that copy function receive? (What, for that matter, do hash
and comparison functions receive?) Pointers, right, give a
cigar to that man, that nice man who doesn't like to pass
pointers around because he thinks they threaten security
I suppose your container could make a temporary copy of
each bag of bytes before calling one of the user-supplied type-
aware functions -- but this is starting to get expensive, and
it still doesn't solve the problem of pointers (or other links)
embedded inside the contained objects.
IMHO, a container should not copy objects at all: It should
just keep track of pointers to the objects the caller supplies,
and leave the management of their storage to the caller. This
avoids a lot of copying, makes it possible for one object to
reside in several different containers simultaneously, and allows
me to update a contained object without the subterfuge of deleting
it and re-inserting the modified version.
>> With
>> the exception of foo_close() and foo_delete(), all foo_*() operations
>> should try not to invalidate any existing iterator. foo_delete() should
>> take an iterator and try not to invalidate any iterator pointing
>> elsewhere.
>
> Sossman's iterator objects seem appropriate.
Three points: First, concrete iterator objects are by no
means original with me. Second, I think you'll find it difficult
to make an iterator behave sensibly if the container is modified
while an iteration is in progress (think of a hash table that
gets re-hashed when an insertion makes it too crowded). Third
(and most important), there are only two S'es in "Sosman."
--
Eric Sosman
lid