about threeWayCompareTo:

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

about threeWayCompareTo:

ducasse
I see

threeWayCompareTo: aString
        "Do a three-way comparison between the receiver and anotherObject, returning
        -1 if self < anotherObject
        0 if self = anotherObject
        1 if self > anotherObject
        This assumes a total order in accordance with the mathematical law of trichotomy.
        See also:  http://en.wikipedia.org/wiki/Three-way_comparison"

         ^ (self compare: self with: aString collated: AsciiOrder) - 2

And I thought that we got a new primitive returning -101 directly
Is it not the case?

Stef


String>>compare: string1 with: string2 collated: order
       
        "'abc' = 'abc' asWideString >>> true"
        "'abc' asWideString = 'abc' >>> true"
        "(ByteArray with: 97 with: 0 with: 0 with: 0) asString ~= 'a000' asWideString >>> true"
        "('abc' sameAs: 'aBc' asWideString) >>> true"
        "('aBc' asWideString sameAs: 'abc') >>> true"
        "('a000' asWideString ~= (ByteArray with: 97 with: 0 with: 0 with: 0) asString) >>> true"
        "((ByteArray with: 97 with: 0 with: 0 with: 0) asString sameAs: 'Abcd' asWideString) >>> false"
        "('a000' asWideString sameAs: (ByteArray with: 97 with: 0 with: 0 with: 0) asString) >>> false"
       
        (string1 isByteString and: [string2 isByteString]) ifTrue: [
                ^ ByteString compare: string1 with: string2 collated: order].
     "Primitive does not fail properly right now"
        ^ String compare: string1 with: string2 collated: order


ByteString>>compare: string1 with: string2 collated: order
        "Return 1, 2 or 3, if string1 is <, =, or > string2, with the collating order of characters given by the order array."

        | len1 len2 c1 c2 |
        <primitive: 'primitiveCompareString' module: 'MiscPrimitivePlugin'>
       
        <var: #string1 declareC: 'unsigned char *string1'>
        <var: #string2 declareC: 'unsigned char *string2'>
        <var: #order declareC: 'unsigned char *order'>

        len1 := string1 size.
        len2 := string2 size.
        1 to: (len1 min: len2) do:
                [:i |
                c1 := order at: (string1 basicAt: i) + 1.
                c2 := order at: (string2 basicAt: i) + 1.
                c1 = c2 ifFalse:
                        [c1 < c2 ifTrue: [^ 1] ifFalse: [^ 3]]].
        len1 = len2 ifTrue: [^ 2].
        len1 < len2 ifTrue: [^ 1] ifFalse: [^ 3].




Reply | Threaded
Open this post in threaded view
|

Re: about threeWayCompareTo:

Nicolas Cellier
This was WIP, don't know if it stalled...

Le lun. 16 sept. 2019 à 08:52, ducasse <[hidden email]> a écrit :
I see

threeWayCompareTo: aString
        "Do a three-way comparison between the receiver and anotherObject, returning
        -1 if self < anotherObject
        0 if self = anotherObject
        1 if self > anotherObject
        This assumes a total order in accordance with the mathematical law of trichotomy.
        See also:  http://en.wikipedia.org/wiki/Three-way_comparison"

         ^ (self compare: self with: aString collated: AsciiOrder) - 2

And I thought that we got a new primitive returning -101 directly
Is it not the case?

Stef


String>>compare: string1 with: string2 collated: order

        "'abc' = 'abc' asWideString >>> true"
        "'abc' asWideString = 'abc' >>> true"
        "(ByteArray with: 97 with: 0 with: 0 with: 0) asString ~= 'a000' asWideString >>> true"
        "('abc' sameAs: 'aBc' asWideString) >>> true"
        "('aBc' asWideString sameAs: 'abc') >>> true"
        "('a000' asWideString ~= (ByteArray with: 97 with: 0 with: 0 with: 0) asString) >>> true"
        "((ByteArray with: 97 with: 0 with: 0 with: 0) asString sameAs: 'Abcd' asWideString) >>> false"
        "('a000' asWideString sameAs: (ByteArray with: 97 with: 0 with: 0 with: 0) asString) >>> false"

        (string1 isByteString and: [string2 isByteString]) ifTrue: [
                ^ ByteString compare: string1 with: string2 collated: order].
     "Primitive does not fail properly right now"
        ^ String compare: string1 with: string2 collated: order


ByteString>>compare: string1 with: string2 collated: order
        "Return 1, 2 or 3, if string1 is <, =, or > string2, with the collating order of characters given by the order array."

        | len1 len2 c1 c2 |
        <primitive: 'primitiveCompareString' module: 'MiscPrimitivePlugin'>

        <var: #string1 declareC: 'unsigned char *string1'>
        <var: #string2 declareC: 'unsigned char *string2'>
        <var: #order declareC: 'unsigned char *order'>

        len1 := string1 size.
        len2 := string2 size.
        1 to: (len1 min: len2) do:
                [:i |
                c1 := order at: (string1 basicAt: i) + 1.
                c2 := order at: (string2 basicAt: i) + 1.
                c1 = c2 ifFalse:
                        [c1 < c2 ifTrue: [^ 1] ifFalse: [^ 3]]].
        len1 = len2 ifTrue: [^ 2].
        len1 < len2 ifTrue: [^ 1] ifFalse: [^ 3].




Reply | Threaded
Open this post in threaded view
|

Re: about threeWayCompareTo:

Esteban A. Maringolo
In reply to this post by ducasse
I haven't followed nor maintained the SortFunctions library, but it
relied heavily on the response of that method, by means of the
"spaceship operator" ( #<=>), I think that Sven later changed such
operator to "threeWayCompareTo:".

I don't know whether the primitive is available now, it wasn't back then.

Regards,

Esteban A. Maringolo

On Mon, Sep 16, 2019 at 3:52 AM ducasse <[hidden email]> wrote:

>
> I see
>
> threeWayCompareTo: aString
>         "Do a three-way comparison between the receiver and anotherObject, returning
>         -1 if self < anotherObject
>         0 if self = anotherObject
>         1 if self > anotherObject
>         This assumes a total order in accordance with the mathematical law of trichotomy.
>         See also:  http://en.wikipedia.org/wiki/Three-way_comparison"
>
>          ^ (self compare: self with: aString collated: AsciiOrder) - 2
>
> And I thought that we got a new primitive returning -101 directly
> Is it not the case?
>
> Stef
>
>
> String>>compare: string1 with: string2 collated: order
>
>         "'abc' = 'abc' asWideString >>> true"
>         "'abc' asWideString = 'abc' >>> true"
>         "(ByteArray with: 97 with: 0 with: 0 with: 0) asString ~= 'a000' asWideString >>> true"
>         "('abc' sameAs: 'aBc' asWideString) >>> true"
>         "('aBc' asWideString sameAs: 'abc') >>> true"
>         "('a000' asWideString ~= (ByteArray with: 97 with: 0 with: 0 with: 0) asString) >>> true"
>         "((ByteArray with: 97 with: 0 with: 0 with: 0) asString sameAs: 'Abcd' asWideString) >>> false"
>         "('a000' asWideString sameAs: (ByteArray with: 97 with: 0 with: 0 with: 0) asString) >>> false"
>
>         (string1 isByteString and: [string2 isByteString]) ifTrue: [
>                 ^ ByteString compare: string1 with: string2 collated: order].
>      "Primitive does not fail properly right now"
>         ^ String compare: string1 with: string2 collated: order
>
>
> ByteString>>compare: string1 with: string2 collated: order
>         "Return 1, 2 or 3, if string1 is <, =, or > string2, with the collating order of characters given by the order array."
>
>         | len1 len2 c1 c2 |
>         <primitive: 'primitiveCompareString' module: 'MiscPrimitivePlugin'>
>
>         <var: #string1 declareC: 'unsigned char *string1'>
>         <var: #string2 declareC: 'unsigned char *string2'>
>         <var: #order declareC: 'unsigned char *order'>
>
>         len1 := string1 size.
>         len2 := string2 size.
>         1 to: (len1 min: len2) do:
>                 [:i |
>                 c1 := order at: (string1 basicAt: i) + 1.
>                 c2 := order at: (string2 basicAt: i) + 1.
>                 c1 = c2 ifFalse:
>                         [c1 < c2 ifTrue: [^ 1] ifFalse: [^ 3]]].
>         len1 = len2 ifTrue: [^ 2].
>         len1 < len2 ifTrue: [^ 1] ifFalse: [^ 3].
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: about threeWayCompareTo:

Nicolas Cellier
This comparison was part of MiscPrimitivePlugin.
MiscPrimitivePlugin is a false good idea:
Smalltalk fallback code = VMMaker (slang) source

Unfortunately, this is an illusion because unlike the C source inclusions in ST/X we don't have dynamic slang compilation!
The consequence is that source code is not managed in VMMaker specific package, but in a dialect specific/version specific package!
Squeak/Pharo/Cuis/Newspeak may all have a different version of String at image side.
This is making VMMaker recipe for code generation more fragile than necessary (and not easily portable to Pharo/Cuis/whatever...).

It seems to me that Clement did start a rewrite of essential primitives into proper plugin...
The plan was then to retract support of MiscPrimitivePlugin after a grace period (after all, these are optional primitives).


Le lun. 16 sept. 2019 à 13:39, Esteban Maringolo <[hidden email]> a écrit :
I haven't followed nor maintained the SortFunctions library, but it
relied heavily on the response of that method, by means of the
"spaceship operator" ( #<=>), I think that Sven later changed such
operator to "threeWayCompareTo:".

I don't know whether the primitive is available now, it wasn't back then.

Regards,

Esteban A. Maringolo

On Mon, Sep 16, 2019 at 3:52 AM ducasse <[hidden email]> wrote:
>
> I see
>
> threeWayCompareTo: aString
>         "Do a three-way comparison between the receiver and anotherObject, returning
>         -1 if self < anotherObject
>         0 if self = anotherObject
>         1 if self > anotherObject
>         This assumes a total order in accordance with the mathematical law of trichotomy.
>         See also:  http://en.wikipedia.org/wiki/Three-way_comparison"
>
>          ^ (self compare: self with: aString collated: AsciiOrder) - 2
>
> And I thought that we got a new primitive returning -101 directly
> Is it not the case?
>
> Stef
>
>
> String>>compare: string1 with: string2 collated: order
>
>         "'abc' = 'abc' asWideString >>> true"
>         "'abc' asWideString = 'abc' >>> true"
>         "(ByteArray with: 97 with: 0 with: 0 with: 0) asString ~= 'a000' asWideString >>> true"
>         "('abc' sameAs: 'aBc' asWideString) >>> true"
>         "('aBc' asWideString sameAs: 'abc') >>> true"
>         "('a000' asWideString ~= (ByteArray with: 97 with: 0 with: 0 with: 0) asString) >>> true"
>         "((ByteArray with: 97 with: 0 with: 0 with: 0) asString sameAs: 'Abcd' asWideString) >>> false"
>         "('a000' asWideString sameAs: (ByteArray with: 97 with: 0 with: 0 with: 0) asString) >>> false"
>
>         (string1 isByteString and: [string2 isByteString]) ifTrue: [
>                 ^ ByteString compare: string1 with: string2 collated: order].
>      "Primitive does not fail properly right now"
>         ^ String compare: string1 with: string2 collated: order
>
>
> ByteString>>compare: string1 with: string2 collated: order
>         "Return 1, 2 or 3, if string1 is <, =, or > string2, with the collating order of characters given by the order array."
>
>         | len1 len2 c1 c2 |
>         <primitive: 'primitiveCompareString' module: 'MiscPrimitivePlugin'>
>
>         <var: #string1 declareC: 'unsigned char *string1'>
>         <var: #string2 declareC: 'unsigned char *string2'>
>         <var: #order declareC: 'unsigned char *order'>
>
>         len1 := string1 size.
>         len2 := string2 size.
>         1 to: (len1 min: len2) do:
>                 [:i |
>                 c1 := order at: (string1 basicAt: i) + 1.
>                 c2 := order at: (string2 basicAt: i) + 1.
>                 c1 = c2 ifFalse:
>                         [c1 < c2 ifTrue: [^ 1] ifFalse: [^ 3]]].
>         len1 = len2 ifTrue: [^ 2].
>         len1 < len2 ifTrue: [^ 1] ifFalse: [^ 3].
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: about threeWayCompareTo:

ducasse
Thanks Nicolas.
We should check because cyril told me that the primitive was implemented but not published in the primitive table.
We will have a look. 

S.

On 16 Sep 2019, at 19:42, Nicolas Cellier <[hidden email]> wrote:

This comparison was part of MiscPrimitivePlugin.
MiscPrimitivePlugin is a false good idea:
Smalltalk fallback code = VMMaker (slang) source

Unfortunately, this is an illusion because unlike the C source inclusions in ST/X we don't have dynamic slang compilation!
The consequence is that source code is not managed in VMMaker specific package, but in a dialect specific/version specific package!
Squeak/Pharo/Cuis/Newspeak may all have a different version of String at image side.
This is making VMMaker recipe for code generation more fragile than necessary (and not easily portable to Pharo/Cuis/whatever...).

It seems to me that Clement did start a rewrite of essential primitives into proper plugin...
The plan was then to retract support of MiscPrimitivePlugin after a grace period (after all, these are optional primitives).


Le lun. 16 sept. 2019 à 13:39, Esteban Maringolo <[hidden email]> a écrit :
I haven't followed nor maintained the SortFunctions library, but it
relied heavily on the response of that method, by means of the
"spaceship operator" ( #<=>), I think that Sven later changed such
operator to "threeWayCompareTo:".

I don't know whether the primitive is available now, it wasn't back then.

Regards,

Esteban A. Maringolo

On Mon, Sep 16, 2019 at 3:52 AM ducasse <[hidden email]> wrote:
>
> I see
>
> threeWayCompareTo: aString
>         "Do a three-way comparison between the receiver and anotherObject, returning
>         -1 if self < anotherObject
>         0 if self = anotherObject
>         1 if self > anotherObject
>         This assumes a total order in accordance with the mathematical law of trichotomy.
>         See also:  http://en.wikipedia.org/wiki/Three-way_comparison"
>
>          ^ (self compare: self with: aString collated: AsciiOrder) - 2
>
> And I thought that we got a new primitive returning -101 directly
> Is it not the case?
>
> Stef
>
>
> String>>compare: string1 with: string2 collated: order
>
>         "'abc' = 'abc' asWideString >>> true"
>         "'abc' asWideString = 'abc' >>> true"
>         "(ByteArray with: 97 with: 0 with: 0 with: 0) asString ~= 'a000' asWideString >>> true"
>         "('abc' sameAs: 'aBc' asWideString) >>> true"
>         "('aBc' asWideString sameAs: 'abc') >>> true"
>         "('a000' asWideString ~= (ByteArray with: 97 with: 0 with: 0 with: 0) asString) >>> true"
>         "((ByteArray with: 97 with: 0 with: 0 with: 0) asString sameAs: 'Abcd' asWideString) >>> false"
>         "('a000' asWideString sameAs: (ByteArray with: 97 with: 0 with: 0 with: 0) asString) >>> false"
>
>         (string1 isByteString and: [string2 isByteString]) ifTrue: [
>                 ^ ByteString compare: string1 with: string2 collated: order].
>      "Primitive does not fail properly right now"
>         ^ String compare: string1 with: string2 collated: order
>
>
> ByteString>>compare: string1 with: string2 collated: order
>         "Return 1, 2 or 3, if string1 is <, =, or > string2, with the collating order of characters given by the order array."
>
>         | len1 len2 c1 c2 |
>         <primitive: 'primitiveCompareString' module: 'MiscPrimitivePlugin'>
>
>         <var: #string1 declareC: 'unsigned char *string1'>
>         <var: #string2 declareC: 'unsigned char *string2'>
>         <var: #order declareC: 'unsigned char *order'>
>
>         len1 := string1 size.
>         len2 := string2 size.
>         1 to: (len1 min: len2) do:
>                 [:i |
>                 c1 := order at: (string1 basicAt: i) + 1.
>                 c2 := order at: (string2 basicAt: i) + 1.
>                 c1 = c2 ifFalse:
>                         [c1 < c2 ifTrue: [^ 1] ifFalse: [^ 3]]].
>         len1 = len2 ifTrue: [^ 2].
>         len1 < len2 ifTrue: [^ 1] ifFalse: [^ 3].
>
>
>
>