swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: remove from a list


From: Roger M. Burkhart
Subject: Re: remove from a list
Date: Thu, 8 Feb 1996 07:22:21 -0600

> 1) Can I get rid of a member of a "(List) alist" without knowing the 
> index of that member within the list? I can write a function that does 
> this but I just wondered if something like this was planned or already 
> exists. I guess it would go something like [aList remove: aMember] where 
> aMember is an id.

The remove: function is already supported on all collections.  It's
defined in collections.h on the Collection supertype of List, Set, Map,
etc.  The message is defined as:

- remove: aMember;

It returns the member just removed, or nil if the member is not contained
in the collection.  If the underlying collection is a List or Map, the
meaning of message is to start at the beginning of the collection and
search for the first matching member, and then remove that entry in the
collection.  If the underlying collection is a Set, the meaning is to
remove the one member if it's contained in the collection.  Depending on
the implementation of the set, this may avoid the need for a linear search
for a matching member.  The current default Set just relies on a List
underneath so gives no advantage.

There's also a collection currently called Sequence, soon to be renamed
OrderedSet, which places the link of a list inside each member (at an
offset location you must specify) which results in fast direct removal of
the member without searching.  It's less safe to use, however, because it
doesn't currently check that the object even is a member of the collection
before removing it (you have to be sure when you make the request).  If
you want to build fast, tree-like structures, this gives the fastest
performance on single member operations, and we will be providing debug
options to check the validity of add/remove operations.

> 2) What happens in the background when a member is removed from a list? 
> Are the indices of the other members in the list updated accordingly?
> Does this have anything to do with difference between "drop" and "remove"?
> What do these 2 methods do differently? 

drop is a method on a single object which deallocates the object and makes
it invalid for any further use, regardless of whether it's contained in
any collection.  Once it is dropped, the id of an object may never be used
again; typically it causes a crash if you do.

remove just removes a member from a collection, but does not deallocate the
object formerly contained as a member.  The same object may be contained
in multiple collections; it may be added and removed from different
collections without affecting the validity of an id.  The effect of remove
is not on the object removed from the collection, which remains intact,
but only on the collection, which sees its count of members reduced by one.

When a member is removed from a list, any other indexes into the
collection, if you have them active, are *not* automatically updated if
they would be affected by the change.  However, the only case you really
have to worry about is if there's an index positioned directly on the
member which was just removed.  Normally, indexes are just created for
an immediate use and dropped when use is done, so the problem of other
existing indexes doesn't arise very often.  If you do have another index,
you should take care to reposition the index off a member being removed
before removing the member, or just drop the other index and recreate it.

We've had options before to automatically update indexes when they're
affected by other activity on a list, but they're temporarily not
implemented until a more complete coverage can be provided.  But
eventually you'll have this option, too, which would save you the extra
responsibility noted above, at some extra cost in time and cost of
collection operations.

Roger


reply via email to

[Prev in Thread] Current Thread [Next in Thread]