Bug in teh program
Bob Arning
arning at charm.net
Sun Dec 1 18:04:47 UTC 2002
Jaril,
Your problem is a basic one: "Do not alter collections while iterating over them". The messages, such as #do:, that iterate over collections are unaware that that the collection has changed, so they keep chugging from 1 to n. As a result, they can skip valid elements, process the same one twice and/or try to access beyond the end of the collection.
Your solution is a good one. If the situation were more complicated, then replacing
helpSet do: [:vector |
with
helpSet copy do: [:vector |
might be useful.
Cheers,
Bob
On Sun, 1 Dec 2002 18:16:27 +0100 "Jaril" <diris at volja.net> wrote:
>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']
> ].
> ].
>
>
>
>
>But once I applied the following code on the same data:
>
> helpSet := helpSet reject: [:vector | pointsOnPath includes: (vector
>endPoint )].
>
>the result was correct.
>
>
>I am new to ST and can't find my mistake. Any help is appreciated.
More information about the Squeak-dev
mailing list
|