| 1 | #ifndef RESOLVEXPR_COST_H | 
|---|
| 2 | #define RESOLVEXPR_COST_H | 
|---|
| 3 |  | 
|---|
| 4 | #include <iostream> | 
|---|
| 5 |  | 
|---|
| 6 | namespace ResolvExpr { | 
|---|
| 7 | class Cost { | 
|---|
| 8 | public: | 
|---|
| 9 | Cost(); | 
|---|
| 10 | Cost( int unsafe, int poly, int safe ); | 
|---|
| 11 |  | 
|---|
| 12 | void incUnsafe( int inc = 1 ); | 
|---|
| 13 | void incPoly( int inc = 1 ); | 
|---|
| 14 | void incSafe( int inc = 1 ); | 
|---|
| 15 |  | 
|---|
| 16 | Cost operator+( const Cost &other ) const; | 
|---|
| 17 | Cost operator-( const Cost &other ) const; | 
|---|
| 18 | Cost &operator+=( const Cost &other ); | 
|---|
| 19 | bool operator<( const Cost &other ) const; | 
|---|
| 20 | bool operator==( const Cost &other ) const; | 
|---|
| 21 | bool operator!=( const Cost &other ) const; | 
|---|
| 22 | friend std::ostream &operator<<( std::ostream &os, const Cost &cost ); | 
|---|
| 23 |  | 
|---|
| 24 | static const Cost zero; | 
|---|
| 25 | static const Cost infinity; | 
|---|
| 26 | private: | 
|---|
| 27 | int compare( const Cost &other ) const; | 
|---|
| 28 |  | 
|---|
| 29 | int unsafe; | 
|---|
| 30 | int poly; | 
|---|
| 31 | int safe; | 
|---|
| 32 | }; | 
|---|
| 33 |  | 
|---|
| 34 | inline Cost::Cost() : unsafe( 0 ), poly( 0 ), safe( 0 ) {} | 
|---|
| 35 |  | 
|---|
| 36 | inline Cost::Cost( int unsafe, int poly, int safe ) : unsafe( unsafe ), poly( poly ), safe( safe ) {} | 
|---|
| 37 |  | 
|---|
| 38 | inline void | 
|---|
| 39 | Cost::incUnsafe( int inc ) { | 
|---|
| 40 | unsafe += inc; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | inline void | 
|---|
| 44 | Cost::incPoly( int inc ) { | 
|---|
| 45 | unsafe += inc; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | inline void | 
|---|
| 49 | Cost::incSafe( int inc ) { | 
|---|
| 50 | unsafe += inc; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | inline Cost Cost::operator+( const Cost &other ) const { | 
|---|
| 54 | return Cost( unsafe + other.unsafe, poly + other.poly, safe + other.safe ); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | inline Cost Cost::operator-( const Cost &other ) const { | 
|---|
| 58 | return Cost( unsafe - other.unsafe, poly - other.poly, safe - other.safe ); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | inline Cost &Cost::operator+=( const Cost &other ) { | 
|---|
| 62 | unsafe += other.unsafe; | 
|---|
| 63 | poly += other.poly; | 
|---|
| 64 | safe += other.safe; | 
|---|
| 65 | return *this; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | inline bool Cost::operator<( const Cost &other ) const { | 
|---|
| 69 | if ( *this == infinity ) return false; | 
|---|
| 70 | if ( other == infinity ) return true; | 
|---|
| 71 | if ( unsafe > other.unsafe ) { | 
|---|
| 72 | return false; | 
|---|
| 73 | } else if ( unsafe < other.unsafe ) { | 
|---|
| 74 | return true; | 
|---|
| 75 | } else if ( poly > other.poly ) { | 
|---|
| 76 | return false; | 
|---|
| 77 | } else if ( poly < other.poly ) { | 
|---|
| 78 | return true; | 
|---|
| 79 | } else if ( safe > other.safe ) { | 
|---|
| 80 | return false; | 
|---|
| 81 | } else if ( safe < other.safe ) { | 
|---|
| 82 | return true; | 
|---|
| 83 | } else { | 
|---|
| 84 | return false; | 
|---|
| 85 | } | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | inline bool Cost::operator==( const Cost &other ) const { | 
|---|
| 89 | return unsafe == other.unsafe | 
|---|
| 90 | && poly == other.poly | 
|---|
| 91 | && safe == other.safe; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | inline bool Cost::operator!=( const Cost &other ) const { | 
|---|
| 95 | return !( *this == other ); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) { | 
|---|
| 99 | os << "( " << cost.unsafe << ", " << cost.poly << ", " << cost.safe << " )"; | 
|---|
| 100 | return os; | 
|---|
| 101 | } | 
|---|
| 102 | } // namespace ResolvExpr | 
|---|
| 103 |  | 
|---|
| 104 | #endif // RESOLVEXPR_COST_H | 
|---|