How to implement opCmp?
    Honey via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Tue Jun 13 11:17:02 PDT 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.
    
    
More information about the Digitalmars-d-learn
mailing list