IdentityDictionary reset

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

IdentityDictionary reset

dario trussardi
Ciao,

        in the Pharo 4.0 image i have a IdentityDictionary with ten items.

        Now when i reset the dictionary the system behaves as follows:

        Version A)  resetAllLocks
       
                                locks keysAndValuesDo:[:k :v | locks removeKey: k ifAbsent: [self halt]]


                Not all items are removed fro the dictionary.


        Version B) resetAllLocks
       
                                locks keys do:[:k | locks removeKey: k ifAbsent: [self halt]]

                All the items are always removed.

       
        Considerations about it?

        Thanks,
                       
                Dario

Reply | Threaded
Open this post in threaded view
|

Re: IdentityDictionary reset

alistairgrant
Hi Dario,

On 9 April 2018 at 10:00, Trussardi Dario Romano
<[hidden email]> wrote:

> Ciao,
>
>         in the Pharo 4.0 image i have a IdentityDictionary with ten items.
>
>         Now when i reset the dictionary the system behaves as follows:
>
>         Version A)  resetAllLocks
>
>                                 locks keysAndValuesDo:[:k :v | locks removeKey: k ifAbsent: [self halt]]
>
>
>                 Not all items are removed fro the dictionary.
>
>
>         Version B) resetAllLocks
>
>                                 locks keys do:[:k | locks removeKey: k ifAbsent: [self halt]]
>
>                 All the items are always removed.
>
>
>         Considerations about it?

The first version modifies the collection while iterating over it -
generally a bad thing to do.

The second version creates a new collection of keys and then uses it
to remove the items - much safer.

If you set a breakpoint in the methods and step into each message send
you'll be able to see the difference.

Cheers,
Alistair

Reply | Threaded
Open this post in threaded view
|

Re: IdentityDictionary reset

Sven Van Caekenberghe-2
In reply to this post by dario trussardi
There is #removeAll.

You should also check out #keysAndValuesRemove:

Removing from a collection while iterating over it is dangerous.

> On 9 Apr 2018, at 10:00, Trussardi Dario Romano <[hidden email]> wrote:
>
> Ciao,
>
> in the Pharo 4.0 image i have a IdentityDictionary with ten items.
>
> Now when i reset the dictionary the system behaves as follows:
>
> Version A)  resetAllLocks
>
> locks keysAndValuesDo:[:k :v | locks removeKey: k ifAbsent: [self halt]]
>
>
> Not all items are removed fro the dictionary.
>
>
> Version B) resetAllLocks
>
> locks keys do:[:k | locks removeKey: k ifAbsent: [self halt]]
>
> All the items are always removed.
>
>
> Considerations about it?
>
> Thanks,
>
> Dario
>


Reply | Threaded
Open this post in threaded view
|

Re: IdentityDictionary reset

Stephane Ducasse-3
In reply to this post by dario trussardi
Hello

Can you let us know why you are using pharo 40? It is around at least
three years old and we made a lot of progress in the last 3 years.

Stef

On Mon, Apr 9, 2018 at 10:00 AM, Trussardi Dario Romano
<[hidden email]> wrote:

> Ciao,
>
>         in the Pharo 4.0 image i have a IdentityDictionary with ten items.
>
>         Now when i reset the dictionary the system behaves as follows:
>
>         Version A)  resetAllLocks
>
>                                 locks keysAndValuesDo:[:k :v | locks removeKey: k ifAbsent: [self halt]]
>
>
>                 Not all items are removed fro the dictionary.
>
>
>         Version B) resetAllLocks
>
>                                 locks keys do:[:k | locks removeKey: k ifAbsent: [self halt]]
>
>                 All the items are always removed.
>
>
>         Considerations about it?
>
>         Thanks,
>
>                 Dario
>