Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Cost.h

    r6b0b624 rddf8a29  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // Cost.h -- 
     7// Cost.h --
    88//
    99// Author           : Richard C. Bilson
     
    2020namespace ResolvExpr {
    2121        class Cost {
     22          private:
     23                Cost( int unsafeCost, int polyCost, int safeCost, int referenceCost );
     24
    2225          public:
    23                 Cost();
    24                 Cost( int unsafe, int poly, int safe );
    25  
    26                 void incUnsafe( int inc = 1 );
    27                 void incPoly( int inc = 1 );
    28                 void incSafe( int inc = 1 );
    29  
     26                Cost & incUnsafe( int inc = 1 );
     27                Cost & incPoly( int inc = 1 );
     28                Cost & incSafe( int inc = 1 );
     29                Cost & incReference( int inc = 1 );
     30
     31                int get_unsafeCost() const { return unsafeCost; }
     32                int get_polyCost() const { return polyCost; }
     33                int get_safeCost() const { return safeCost; }
     34                int get_referenceCost() const { return referenceCost; }
     35
    3036                Cost operator+( const Cost &other ) const;
    3137                Cost operator-( const Cost &other ) const;
     
    3541                bool operator!=( const Cost &other ) const;
    3642                friend std::ostream &operator<<( std::ostream &os, const Cost &cost );
    37  
     43
    3844                static const Cost zero;
    3945                static const Cost infinity;
     46
     47                static const Cost unsafe;
     48                static const Cost poly;
     49                static const Cost safe;
     50                static const Cost reference;
    4051          private:
    4152                int compare( const Cost &other ) const;
    4253
    43                 int unsafe;
    44                 int poly;
    45                 int safe;
     54                int unsafeCost;
     55                int polyCost;
     56                int safeCost;
     57                int referenceCost;
    4658        };
    4759
    48         inline Cost::Cost() : unsafe( 0 ), poly( 0 ), safe( 0 ) {}
     60        inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int referenceCost ) : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), referenceCost( referenceCost ) {}
    4961
    50         inline Cost::Cost( int unsafe, int poly, int safe ) : unsafe( unsafe ), poly( poly ), safe( safe ) {}
    51 
    52         inline void Cost::incUnsafe( int inc ) {
    53                 unsafe += inc;
     62        inline Cost & Cost::incUnsafe( int inc ) {
     63                if ( *this == infinity ) return *this;
     64                unsafeCost += inc;
     65                return *this;
    5466        }
    5567
    56         inline void Cost::incPoly( int inc ) {
    57                 poly += inc;
     68        inline Cost & Cost::incPoly( int inc ) {
     69                if ( *this == infinity ) return *this;
     70                polyCost += inc;
     71                return *this;
    5872        }
    5973
    60         inline void Cost::incSafe( int inc ) {
    61                 safe += inc;
     74        inline Cost & Cost::incSafe( int inc ) {
     75                if ( *this == infinity ) return *this;
     76                safeCost += inc;
     77                return *this;
     78        }
     79
     80        inline Cost & Cost::incReference( int inc ) {
     81                if ( *this == infinity ) return *this;
     82                referenceCost += inc;
     83                return *this;
    6284        }
    6385
    6486        inline Cost Cost::operator+( const Cost &other ) const {
    65                 return Cost( unsafe + other.unsafe, poly + other.poly, safe + other.safe );
     87                if ( *this == infinity || other == infinity ) return infinity;
     88                return Cost( unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost, referenceCost + other.referenceCost );
    6689        }
    6790
    6891        inline Cost Cost::operator-( const Cost &other ) const {
    69                 return Cost( unsafe - other.unsafe, poly - other.poly, safe - other.safe );
     92                if ( *this == infinity || other == infinity ) return infinity;
     93                return Cost( unsafeCost - other.unsafeCost, polyCost - other.polyCost, safeCost - other.safeCost, referenceCost - other.referenceCost );
    7094        }
    7195
    7296        inline Cost &Cost::operator+=( const Cost &other ) {
    73                 unsafe += other.unsafe;
    74                 poly += other.poly;
    75                 safe += other.safe;
     97                if ( *this == infinity ) return *this;
     98                if ( other == infinity ) {
     99                        *this = infinity;
     100                        return *this;
     101                }
     102                unsafeCost += other.unsafeCost;
     103                polyCost += other.polyCost;
     104                safeCost += other.safeCost;
     105                referenceCost += other.referenceCost;
    76106                return *this;
    77107        }
    78108
    79109        inline bool Cost::operator<( const Cost &other ) const {
    80             if ( *this == infinity ) return false;
    81             if ( other == infinity ) return true;
    82             if ( unsafe > other.unsafe ) {
     110                if ( *this == infinity ) return false;
     111                if ( other == infinity ) return true;
     112
     113                if ( unsafeCost > other.unsafeCost ) {
    83114                        return false;
    84             } else if ( unsafe < other.unsafe ) {
     115                } else if ( unsafeCost < other.unsafeCost ) {
    85116                        return true;
    86             } else if ( poly > other.poly ) {
     117                } else if ( polyCost > other.polyCost ) {
    87118                        return false;
    88             } else if ( poly < other.poly ) {
     119                } else if ( polyCost < other.polyCost ) {
    89120                        return true;
    90             } else if ( safe > other.safe ) {
     121                } else if ( safeCost > other.safeCost ) {
    91122                        return false;
    92             } else if ( safe < other.safe ) {
     123                } else if ( safeCost < other.safeCost ) {
    93124                        return true;
    94             } else {
     125                } else if ( referenceCost > other.referenceCost ) {
    95126                        return false;
    96             } // if
     127                } else if ( referenceCost < other.referenceCost ) {
     128                        return true;
     129                } else {
     130                        return false;
     131                } // if
    97132        }
    98133
    99134        inline bool Cost::operator==( const Cost &other ) const {
    100                 return unsafe == other.unsafe
    101                         && poly == other.poly
    102                         && safe == other.safe;
     135                return unsafeCost == other.unsafeCost
     136                        && polyCost == other.polyCost
     137                        && safeCost == other.safeCost
     138                        && referenceCost == other.referenceCost;
    103139        }
    104140
     
    108144
    109145        inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) {
    110                 os << "( " << cost.unsafe << ", " << cost.poly << ", " << cost.safe << " )";
     146                os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", " << cost.safeCost << ", " << cost.referenceCost << " )";
    111147                return os;
    112148        }
Note: See TracChangeset for help on using the changeset viewer.