Bug in teh program

Boris Gaertner Boris.Gaertner at gmx.net
Sun Dec 1 17:51:00 UTC 2002


Jaril <diris at volja.net> asked about a strange effect:
> As a trial project I worked on the Path through Net problem, where I use a
> Point and Vector model. At some point I encountered a strange situation:
the
> following code did not work correctly for one of vectors in the helpSet,
> though I did test the situation thoroughly and endPoint is postively in
> pointsOnPath :
>
>
>
>
>     helpSet do: [:vector |
>            ( pointsOnPath includes: (vector endPoint ))
>                 ifTrue: [
>                     helpSet remove: vector ifAbsent: [^'Error 2, class
> NetClass']
>                          ].
>                ].
>
Your mistake is that you remove an element from a set
while you enumerate its elements with  #do: That is not
allowed, or better, it creates erroneous effects.
(Removal of elements rearranges the internal structure
of a collection. The #do:-method uses a counter that
is simply incremented. When the internal structure is
reearranged you may occasionally skip an element.)

The solution with reject is save, because reject adds the
"survivers" (the elements that do not satisfy the
rejection condition) into a new set. The set that you scan with
#do: is left unchanged.

Hope that helps,
Boris
>






More information about the Squeak-dev mailing list