SharedQueue bug in #nextOrNilSuchThat

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

SharedQueue bug in #nextOrNilSuchThat

Jaromir Matas
Test for SharedQueue >> nextOrNilSuchThat fails due to incorrect
implementation of #nextOrNilSuchThat - I at least assume the semantics
should be the same as for SharedQueue2 and satisfy the test (i.e. pick one
item, leave the rest be).

I propose this correction to #nextOrNilSuchThat (the original one skipped
AND DISCARDED all items before the item found):

nextOrNilSuchThat: aBlock
        "Answer the next object that satisfies aBlock, skipping any intermediate
objects.
        If no object has been sent, answer <nil> and leave me intact.
        NOTA BENE:  aBlock MUST NOT contain a non-local return (^)."

        ^accessProtect critical: [
                | value readPos |
                value := nil.
                readPos := readPosition.
                [ readPos < writePosition and: [ value isNil ] ] whileTrue: [
                        value := contentsArray at: readPos.
                        readPos := readPos + 1.
                        (aBlock value: value)
                                ifFalse: [ value := nil ]
                                ifTrue: [
                                        readSynch waitIfLocked: [ ^nil ]. "We found the value, but someone else
booked it."
                                        *readPos-1 to: readPosition+1 by: -1 do: [ :j | contentsArray at: j
put: (contentsArray at: j-1) ].
                                        contentsArray at: readPosition put: nil.
                                        readPosition := readPosition+1 *] ].
                value ].


Is this the right way to propose changes and fixes? Thanks
Cheers,
Jaromir

Tested at Squeak6.0alpha-20182-64bit.image



--
Sent from: http://forum.world.st/Squeak-Dev-f45488.html

^[^ Jaromir
Reply | Threaded
Open this post in threaded view
|

Re: SharedQueue bug in #nextOrNilSuchThat

David T. Lewis
On Sat, Feb 06, 2021 at 11:17:50AM -0600, Jaromir wrote:

> Test for SharedQueue >> nextOrNilSuchThat fails due to incorrect
> implementation of #nextOrNilSuchThat - I at least assume the semantics
> should be the same as for SharedQueue2 and satisfy the test (i.e. pick one
> item, leave the rest be).
>
> I propose this correction to #nextOrNilSuchThat (the original one skipped
> AND DISCARDED all items before the item found):
>
> nextOrNilSuchThat: aBlock
> "Answer the next object that satisfies aBlock, skipping any intermediate
> objects.
> If no object has been sent, answer <nil> and leave me intact.
> NOTA BENE:  aBlock MUST NOT contain a non-local return (^)."
>
> ^accessProtect critical: [
> | value readPos |
> value := nil.
> readPos := readPosition.
> [ readPos < writePosition and: [ value isNil ] ] whileTrue: [
> value := contentsArray at: readPos.
> readPos := readPos + 1.
> (aBlock value: value)
> ifFalse: [ value := nil ]
> ifTrue: [
> readSynch waitIfLocked: [ ^nil ]. "We found the value, but someone else
> booked it."
> *readPos-1 to: readPosition+1 by: -1 do: [ :j | contentsArray at: j
> put: (contentsArray at: j-1) ].
> contentsArray at: readPosition put: nil.
> readPosition := readPosition+1 *] ].
> value ].
>
>
> Is this the right way to propose changes and fixes? Thanks

Hi Jaromir,

Yes it is perfectly fine to post suggestions to the list like this,
especially if you are asking for discussion of ideas like this.

Please also read about the contribution process here:

  https://squeak.org/development_process/

If you have a specific change or improvement to contribute, it is good
to publish your change to the inbox, and then discuss it here on the
list also.

Dave


Reply | Threaded
Open this post in threaded view
|

Re: SharedQueue bug in #nextOrNilSuchThat

Jaromir Matas
Perfect, thanks, Dave. I'll try the Inbox then to see how it works.
Jaromir



--
Sent from: http://forum.world.st/Squeak-Dev-f45488.html

^[^ Jaromir