| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo | 
|---|
| 3 | // | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
| 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| 6 | // | 
|---|
| 7 | // Cost.h -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Sun May 17 09:39:50 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Sat Jul 22 09:35:55 2017 | 
|---|
| 13 | // Update Count     : 5 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include <iostream> | 
|---|
| 19 |  | 
|---|
| 20 | namespace ResolvExpr { | 
|---|
| 21 | class Cost { | 
|---|
| 22 | private: | 
|---|
| 23 | Cost( int unsafeCost, int polyCost, int safeCost, int referenceCost ); | 
|---|
| 24 |  | 
|---|
| 25 | public: | 
|---|
| 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 |  | 
|---|
| 36 | Cost operator+( const Cost &other ) const; | 
|---|
| 37 | Cost operator-( const Cost &other ) const; | 
|---|
| 38 | Cost &operator+=( const Cost &other ); | 
|---|
| 39 | bool operator<( const Cost &other ) const; | 
|---|
| 40 | bool operator==( const Cost &other ) const; | 
|---|
| 41 | bool operator!=( const Cost &other ) const; | 
|---|
| 42 | friend std::ostream &operator<<( std::ostream &os, const Cost &cost ); | 
|---|
| 43 |  | 
|---|
| 44 | static const Cost zero; | 
|---|
| 45 | 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; | 
|---|
| 51 | private: | 
|---|
| 52 | int compare( const Cost &other ) const; | 
|---|
| 53 |  | 
|---|
| 54 | int unsafeCost; | 
|---|
| 55 | int polyCost; | 
|---|
| 56 | int safeCost; | 
|---|
| 57 | int referenceCost; | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int referenceCost ) : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), referenceCost( referenceCost ) {} | 
|---|
| 61 |  | 
|---|
| 62 | inline Cost & Cost::incUnsafe( int inc ) { | 
|---|
| 63 | if ( *this == infinity ) return *this; | 
|---|
| 64 | unsafeCost += inc; | 
|---|
| 65 | return *this; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | inline Cost & Cost::incPoly( int inc ) { | 
|---|
| 69 | if ( *this == infinity ) return *this; | 
|---|
| 70 | polyCost += inc; | 
|---|
| 71 | return *this; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 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; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | inline Cost Cost::operator+( const Cost &other ) const { | 
|---|
| 87 | if ( *this == infinity || other == infinity ) return infinity; | 
|---|
| 88 | return Cost( unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost, referenceCost + other.referenceCost ); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | inline Cost Cost::operator-( const Cost &other ) const { | 
|---|
| 92 | if ( *this == infinity || other == infinity ) return infinity; | 
|---|
| 93 | return Cost( unsafeCost - other.unsafeCost, polyCost - other.polyCost, safeCost - other.safeCost, referenceCost - other.referenceCost ); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | inline Cost &Cost::operator+=( const Cost &other ) { | 
|---|
| 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; | 
|---|
| 106 | return *this; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | inline bool Cost::operator<( const Cost &other ) const { | 
|---|
| 110 | if ( *this == infinity ) return false; | 
|---|
| 111 | if ( other == infinity ) return true; | 
|---|
| 112 |  | 
|---|
| 113 | if ( unsafeCost > other.unsafeCost ) { | 
|---|
| 114 | return false; | 
|---|
| 115 | } else if ( unsafeCost < other.unsafeCost ) { | 
|---|
| 116 | return true; | 
|---|
| 117 | } else if ( polyCost > other.polyCost ) { | 
|---|
| 118 | return false; | 
|---|
| 119 | } else if ( polyCost < other.polyCost ) { | 
|---|
| 120 | return true; | 
|---|
| 121 | } else if ( safeCost > other.safeCost ) { | 
|---|
| 122 | return false; | 
|---|
| 123 | } else if ( safeCost < other.safeCost ) { | 
|---|
| 124 | return true; | 
|---|
| 125 | } else if ( referenceCost > other.referenceCost ) { | 
|---|
| 126 | return false; | 
|---|
| 127 | } else if ( referenceCost < other.referenceCost ) { | 
|---|
| 128 | return true; | 
|---|
| 129 | } else { | 
|---|
| 130 | return false; | 
|---|
| 131 | } // if | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | inline bool Cost::operator==( const Cost &other ) const { | 
|---|
| 135 | return unsafeCost == other.unsafeCost | 
|---|
| 136 | && polyCost == other.polyCost | 
|---|
| 137 | && safeCost == other.safeCost | 
|---|
| 138 | && referenceCost == other.referenceCost; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | inline bool Cost::operator!=( const Cost &other ) const { | 
|---|
| 142 | return !( *this == other ); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) { | 
|---|
| 146 | os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", " << cost.safeCost << ", " << cost.referenceCost << " )"; | 
|---|
| 147 | return os; | 
|---|
| 148 | } | 
|---|
| 149 | } // namespace ResolvExpr | 
|---|
| 150 |  | 
|---|
| 151 | // Local Variables: // | 
|---|
| 152 | // tab-width: 4 // | 
|---|
| 153 | // mode: c++ // | 
|---|
| 154 | // compile-command: "make install" // | 
|---|
| 155 | // End: // | 
|---|