Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Cost.h

    rd97c3a4 r6d6e829  
    2121        class Cost {
    2222          private:
    23                 Cost( int unsafeCost, int polyCost, int safeCost, int varCost, int specCost,
    24                         int referenceCost );
     23                Cost( int unsafeCost, int polyCost, int varCost, int specCost, int safeCost,
     24                      int referenceCost );
    2525
    2626          public:
    2727                Cost & incUnsafe( int inc = 1 );
    2828                Cost & incPoly( int inc = 1 );
    29                 Cost & incSafe( int inc = 1 );
    3029                Cost & incVar( int inc = 1 );
    3130                Cost & decSpec( int inc = 1 );
     31                Cost & incSafe( int inc = 1 );
    3232                Cost & incReference( int inc = 1 );
    3333
    3434                int get_unsafeCost() const { return unsafeCost; }
    3535                int get_polyCost() const { return polyCost; }
    36                 int get_safeCost() const { return safeCost; }
    3736                int get_varCost() const { return varCost; }
    3837                int get_specCost() const { return specCost; }
     38                int get_safeCost() const { return safeCost; }
    3939                int get_referenceCost() const { return referenceCost; }
    4040
     
    5454                static const Cost unsafe;
    5555                static const Cost poly;
    56                 static const Cost safe;
    5756                static const Cost var;
    5857                static const Cost spec;
     58                static const Cost safe;
    5959                static const Cost reference;
    6060
     
    6262                int unsafeCost;     ///< Unsafe (narrowing) conversions
    6363                int polyCost;       ///< Count of parameters and return values bound to some poly type
    64                 int safeCost;       ///< Safe (widening) conversions
    6564                int varCost;        ///< Count of polymorphic type variables
    6665                int specCost;       ///< Polymorphic type specializations (type assertions), negative cost
     66                int safeCost;       ///< Safe (widening) conversions
    6767                int referenceCost;  ///< reference conversions
    6868        };
    6969
    70         inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int varCost, int specCost,
     70        inline Cost::Cost( int unsafeCost, int polyCost, int varCost, int specCost, int safeCost,
    7171                        int referenceCost )
    72                 : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), varCost( varCost ),
    73                   specCost( specCost ), referenceCost( referenceCost ) {}
     72                : unsafeCost( unsafeCost ), polyCost( polyCost ), varCost( varCost ), specCost( specCost ),
     73                  safeCost( safeCost ), referenceCost( referenceCost ) {}
    7474
    7575        inline Cost & Cost::incUnsafe( int inc ) {
     
    8585        }
    8686
     87        inline Cost & Cost::incVar( int inc ) {
     88                if ( *this == infinity ) return *this;
     89                varCost += inc;
     90                return *this;
     91        }
     92
     93        inline Cost& Cost::decSpec( int dec ) {
     94                if ( *this == infinity ) return *this;
     95                specCost -= dec;
     96                return *this;
     97        }
     98
    8799        inline Cost & Cost::incSafe( int inc ) {
    88100                if ( *this == infinity ) return *this;
    89101                safeCost += inc;
    90                 return *this;
    91         }
    92 
    93         inline Cost & Cost::incVar( int inc ) {
    94                 if ( *this == infinity ) return *this;
    95                 varCost += inc;
    96                 return *this;
    97         }
    98 
    99         inline Cost& Cost::decSpec( int dec ) {
    100                 if ( *this == infinity ) return *this;
    101                 specCost -= dec;
    102102                return *this;
    103103        }
     
    112112                if ( *this == infinity || other == infinity ) return infinity;
    113113                return Cost{
    114                         unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost,
     114                        unsafeCost + other.unsafeCost, polyCost + other.polyCost,
    115115                        varCost + other.varCost, specCost + other.specCost,
    116                         referenceCost + other.referenceCost };
     116                        safeCost + other.safeCost, referenceCost + other.referenceCost };
    117117        }
    118118
     
    120120                if ( *this == infinity || other == infinity ) return infinity;
    121121                return Cost{
    122                         unsafeCost - other.unsafeCost, polyCost - other.polyCost, safeCost - other.safeCost,
     122                        unsafeCost - other.unsafeCost, polyCost - other.polyCost,
    123123                        varCost - other.varCost, specCost - other.specCost,
    124                         referenceCost - other.referenceCost };
     124                        safeCost - other.safeCost, referenceCost - other.referenceCost };
    125125        }
    126126
     
    133133                unsafeCost += other.unsafeCost;
    134134                polyCost += other.polyCost;
    135                 safeCost += other.safeCost;
    136135                varCost += other.varCost;
    137136                specCost += other.specCost;
     137                safeCost += other.safeCost;
    138138                referenceCost += other.referenceCost;
    139139                return *this;
     
    152152                } else if ( polyCost < other.polyCost ) {
    153153                        return true;
     154                } else if ( varCost > other.varCost ) {
     155                        return false;
     156                } else if ( varCost < other.varCost ) {
     157                        return true;
     158                } else if ( specCost > other.specCost ) {
     159                        return false;
     160                } else if ( specCost > other.specCost ) {
     161                        return true;
    154162                } else if ( safeCost > other.safeCost ) {
    155163                        return false;
    156164                } else if ( safeCost < other.safeCost ) {
    157                         return true;
    158                 } else if ( varCost > other.varCost ) {
    159                         return false;
    160                 } else if ( varCost < other.varCost ) {
    161                         return true;
    162                 } else if ( specCost > other.specCost ) {
    163                         return false;
    164                 } else if ( specCost > other.specCost ) {
    165165                        return true;
    166166                } else if ( referenceCost > other.referenceCost ) {
     
    179179                int c = unsafeCost - other.unsafeCost; if ( c ) return c;
    180180                c = polyCost - other.polyCost; if ( c ) return c;
    181                 c = safeCost - other.safeCost; if ( c ) return c;
    182181                c = varCost - other.varCost; if ( c ) return c;
    183182                c = specCost - other.specCost; if ( c ) return c;
     183                c = safeCost - other.safeCost; if ( c ) return c;
    184184                return referenceCost - other.referenceCost;
    185185        }
     
    188188                return unsafeCost == other.unsafeCost
    189189                        && polyCost == other.polyCost
    190                         && safeCost == other.safeCost
    191190                        && varCost == other.varCost
    192191                        && specCost == other.specCost
     192                        && safeCost == other.safeCost
    193193                        && referenceCost == other.referenceCost;
    194194        }
     
    200200        inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) {
    201201                return os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", "
    202                           << cost.safeCost << ", " << cost.varCost << ", " << cost.specCost << ", "
    203                           << cost.referenceCost << " )";
     202                          << cost.varCost << ", " << cost.specCost << ", "
     203                          << cost.safeCost << ", " << cost.referenceCost << " )";
    204204        }
    205205} // namespace ResolvExpr
Note: See TracChangeset for help on using the changeset viewer.