On opCmp

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 27 03:04:50 PST 2015


Is there a more compact way to describe the opCmp function in the 
following struct

struct Hit
{
     size_t count; // number of walkers that found this node
     NWeight rank; // rank (either minimum distance or maximum 
strength)

     auto opCmp(const Hit rhs) const
     {
         if      (this.count < rhs.count)
         {
             return -1;
         }
         else if (this.count > rhs.count)
         {
             return +1;
         }
         else
         {
             if      (this.rank < rhs.rank)
             {
                 return -1;
             }
             else if (this.rank > rhs.rank)
             {
                 return +1;
             }
             else
             {
                 return 0;
             }
         }
     }
}

by reusing something like

     auto opCmp(const Hit rhs) const
     {
         if      (this.count < rhs.count)
         {
             return -1;
         }
         else if (this.count > rhs.count)
         {
             return +1;
         }
         else
         {
             return this.rank.standardOpCmp(rhs.rank)
         }
     }


More information about the Digitalmars-d-learn mailing list