June 13, 2017
On 6/11/17 11:24 AM, Honey wrote:
> On Friday, 9 June 2017 at 17:50:28 UTC, Honey wrote:
>> Looking at the implementation of Tuple.opCmp, I'm not sure I'd bet on
>> existence of opCmp for fundamental types:
>>
>>         int opCmp(R)(R rhs)
>>         if (areCompatibleTuples!(typeof(this), R, "<"))
>>         {
>>             foreach (i, Unused; Types)
>>             {
>>                 if (field[i] != rhs.field[i])
>>                 {
>>                     return field[i] < rhs.field[i] ? -1 : 1;
>>                 }
>>             }
>>             return 0;
>>         }
>
> It turned out that there is a standard three way comparison function for
> ranges and strings [1].

Yes, I saw that when I was looking (you can see from my reply that you quoted below).

> Doesn't it make sense to introduce another overload of cmp similar to
> Steve's doCmp [2] right at that spot?

Yes I think it makes sense to have such a comparison function for non-ranges. Yes it probably belongs there, as there are other functions (e.g. swap) that are not specific to ranges in std.algorithm. It should probably not be called cmp, as it will be a default comparison (with the default ordering), although if we did something like:

int cmp(alias pred = "a < b", T)(T t1, T t2) if(!isInputRange!T)
{
   static if(pred == "a < b") { /* do default optimized way */ }
   else { /* more generic mechanism using pred */ }
}

it might be nice. Need to think about the API ramifications.

> This would simplify the implementation of opCmp for aggregates and can
> also lead to a moderate performance benefit [3]. (Note that [3] does not
> provide an additional overload of cmp but uses a less elegant approach
> with the same performance characteristics.)

I agree. It's a thing also that can be optimized in unintuitive ways. I think Andrei has a nice way to do opCmp for integers that's a simple subtraction and negation or something like that.

-Steve
June 13, 2017
On Tue, Jun 13, 2017 at 10:51:40AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: [...]
> I think Andrei has a nice way to do opCmp for integers that's a simple subtraction and negation or something like that.
[...]

In theory, cmp(int x, int y) can be implemented simply as (x - y).
However, this fails when integer overflow occurs.  Does Andrei have a
nice way of doing this that isn't vulnerable to integer overflow?


T

-- 
You are only young once, but you can stay immature indefinitely. -- azephrahel
June 13, 2017
On Tuesday, 13 June 2017 at 14:51:40 UTC, Steven Schveighoffer wrote:
> Yes, I saw that when I was looking (you can see from my reply that you quoted below).

Yes, I had missed that point.


> Yes I think it makes sense to have such a comparison function for non-ranges. Yes it probably belongs there, as there are other functions (e.g. swap) that are not specific to ranges in std.algorithm. It should probably not be called cmp, as it will be a default comparison (with the default ordering), although if we did something like:
>
> int cmp(alias pred = "a < b", T)(T t1, T t2) if(!isInputRange!T)
> {
>    static if(pred == "a < b") { /* do default optimized way */ }
>    else { /* more generic mechanism using pred */ }
> }
>
> it might be nice. Need to think about the API ramifications.

I retracted my earlier proposal after I had realized my confusion. I had thought that cmp would implement three way range comparison based on three way element comparison. Then I realized that it is based on "a < b" or alike. The latter is certainly useful but I am afraid that this approach does not always lead to optimal performance.

I gathered a few ideas about the subject. I have to sit down and write it down.


> I agree. It's a thing also that can be optimized in unintuitive ways. I think Andrei has a nice way to do opCmp for integers that's a simple subtraction and negation or something like that.

I observed that compiler optimizers are pretty smart about comparison of individual integers. I guess that we do not need to be clever, here.
June 13, 2017
On Tuesday, 13 June 2017 at 16:49:14 UTC, H. S. Teoh wrote:
> On Tue, Jun 13, 2017 at 10:51:40AM -0400, Steven Schveighoffer via Digitalmars-d-learn wrote: [...]
>> I think Andrei has a nice way to do opCmp for integers that's a simple subtraction and negation or something like that.
> [...]
>
> In theory, cmp(int x, int y) can be implemented simply as (x - y).
> However, this fails when integer overflow occurs.  Does Andrei have a
> nice way of doing this that isn't vulnerable to integer overflow?
>

return (x > y) - (x < y);

According to this stackoverflow question it looks like a good candidate
https://stackoverflow.com/questions/10996418/efficient-integer-compare-function



1 2
Next ›   Last »