swarm-support
[Top][All Lists]
Advanced

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

Re: Dropping and copying


From: Benedikt Stefansson
Subject: Re: Dropping and copying
Date: Sat, 31 Oct 1998 19:35:27 -0800

Kerimcan Ozcan wrote:

> Hi folks,
>
> I know, some of you will call me miserable for staying in on a Halloween
> night and having nothing better to do than programming in Swarm, but
>

Rest assured, you are not the only one...

> I have two Map objects, mapOld and mapNew respectively.
> Members of these map objects are swarmobjects themselves (with several
> variables and methods). Each time period, I use members in mapOld
> to update member in mapNew (mapOld members are not modified meanwhile).
> At the end of each time period, I would like to make mapOld an exact
> replica of mapNew. Next time period, I will start with members of this
> new mapOld object and update members of mapNew, and so on.  In other
> words, I have something similar to a double buffer operation here. I tried
>   [mapOld deleteAll];
>   [mapOld drop];
>   mapOld = [mapNew copy: [self getZone]];
> However, this didn't work. Is there a clean-cut way of doing what I want
> to achieve?
>

The copy: methods in Map, Set and List are actually very deceptive - I wonder
if they shouldn't be taken out altogether. If you look at the code it doesn't
actually create a new instance of the members, but simply copies pointers
over. So when you issue the [mapOld deleteAll] you are probably also deleting
the members in mapNew, which you then try to copy over to mapOld. Notice there
is a big difference between -dropAll and -deleteAll, the first only removes
from the list, the second also kills the object being removed.

If you only want to move the members over from one list/map to the other but
only deal with the same instances over and over, then you can actually take
out the [mapOld deleteAll] statement and you'll end up with the members of
mapNew in mapOld after each round. You don't provide enough context to see if
this is what you want.

Since you didn't provide the context I wrote a short test and it fails on the
second round (the method code is at the bottom of this message). I verified
that this fails for Lists and Maps (in the second case there is also the
problem of copying the keys. If you check the source code for Map you'll see
that in copy: the keys are not being copied over. Another reason why the use
of copy should be deprecated).

If you remove the line [mapOld deleteAll] the code doesn't fail. To make a
deep copy of each member from the new list to the old list, you'll have to
subsitute a few lines that iterate over mapNew and for each member
    1) Create a completely new object instance
    2) Read values for var1,...,varN out of the member of mapNew
    3) Set the value for var1,...,varN in the new object instance
    4) Put the new object instance on mapOld
You can also create a copy: o method in each object that does steps 2) and 3),
e.g.

    -copy: o {
        MyClass * obj;
        obj=o;

        var1=obj->var1;
        var2=obj->var2;
        ...
        varN=obj->varN;
        return self;
    }

If you skip the "MyClass *obj;obj=o" step GCC will complain that you are
statically accessing o, although the code seems to work in practice.

Here is the code example I mentioned before that tries to replicate the
problem that you described.

-newGeneration {
  id index;
  id <HolderObject> member;

  printf("%ld Members in mapOld:\n",[[self getActivity] getCurrentTime]);
  [mapOld forEach:M(print)];

  printf("%ld Members in mapNew:\n",[[self getActivity] getCurrentTime]);
  [mapNew forEach:M(print)];

  index=[mapNew begin: self];
  while((member=[index next])) {
    [member setHolderValue: [member getHolderValue]+100];
  }
  [index drop];

  [mapOld deleteAll];
  [mapOld drop];
  mapOld = [mapNew copy: self];

  printf("%ld (After copy) Members in mapOld:\n",[[self getActivity]
getCurrentTime]);
  [mapOld forEach:M(print)];

  printf("%ld (After copy) Members in mapNew:\n",[[self getActivity]
getCurrentTime]);
  [mapNew forEach:M(print)];

return self;
}

The full test application is in the attachement. Line 98 makes it crash.

Regards,
-Benedikt

--
----------------
Benedikt Stefansson                 address@hidden
Department of Economics, UCLA       Fax. (310) 825-9528
Los Angeles, CA 90095-1477          Tel. (310) 825-1777

Attachment: testmap.tar.gz
Description: application/unknown-content-type-winzip


reply via email to

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