[a32b204] | 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 | // |
---|
[b46e3bd] | 7 | // Cost.h -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 09:39:50 2015 |
---|
[cdcddfe1] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Thu Feb 7 20:54:29 2019 |
---|
| 13 | // Update Count : 8 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
| 18 | #include <iostream> |
---|
| 19 | |
---|
| 20 | namespace ResolvExpr { |
---|
[a32b204] | 21 | class Cost { |
---|
[89be1c68] | 22 | private: |
---|
[cdcddfe1] | 23 | Cost( int unsafeCost, int polyCost, int safeCost, int signCost, |
---|
| 24 | int varCost, int specCost, int referenceCost ); |
---|
[89be1c68] | 25 | public: |
---|
| 26 | Cost & incUnsafe( int inc = 1 ); |
---|
| 27 | Cost & incPoly( int inc = 1 ); |
---|
[d97c3a4] | 28 | Cost & incSafe( int inc = 1 ); |
---|
[cdcddfe1] | 29 | Cost & incSign( int inc = 1 ); |
---|
[1dd1bd2] | 30 | Cost & incVar( int inc = 1 ); |
---|
| 31 | Cost & decSpec( int inc = 1 ); |
---|
[7ebaa56] | 32 | Cost & incReference( int inc = 1 ); |
---|
[b46e3bd] | 33 | |
---|
[ddf8a29] | 34 | int get_unsafeCost() const { return unsafeCost; } |
---|
| 35 | int get_polyCost() const { return polyCost; } |
---|
[d97c3a4] | 36 | int get_safeCost() const { return safeCost; } |
---|
[cdcddfe1] | 37 | int get_signCost() const { return signCost; } |
---|
[1dd1bd2] | 38 | int get_varCost() const { return varCost; } |
---|
| 39 | int get_specCost() const { return specCost; } |
---|
[ddf8a29] | 40 | int get_referenceCost() const { return referenceCost; } |
---|
| 41 | |
---|
[a32b204] | 42 | Cost operator+( const Cost &other ) const; |
---|
| 43 | Cost &operator+=( const Cost &other ); |
---|
| 44 | bool operator<( const Cost &other ) const; |
---|
| 45 | bool operator==( const Cost &other ) const; |
---|
| 46 | bool operator!=( const Cost &other ) const; |
---|
| 47 | friend std::ostream &operator<<( std::ostream &os, const Cost &cost ); |
---|
[6d6e829] | 48 | // returns negative for *this < other, 0 for *this == other, positive for *this > other |
---|
| 49 | int compare( const Cost &other ) const; |
---|
[b46e3bd] | 50 | |
---|
[a32b204] | 51 | static const Cost zero; |
---|
| 52 | static const Cost infinity; |
---|
[7ebaa56] | 53 | |
---|
[b46e3bd] | 54 | static const Cost unsafe; |
---|
| 55 | static const Cost poly; |
---|
[d97c3a4] | 56 | static const Cost safe; |
---|
[cdcddfe1] | 57 | static const Cost sign; |
---|
[1dd1bd2] | 58 | static const Cost var; |
---|
| 59 | static const Cost spec; |
---|
[7ebaa56] | 60 | static const Cost reference; |
---|
[a32b204] | 61 | |
---|
[6d6e829] | 62 | private: |
---|
[1dd1bd2] | 63 | int unsafeCost; ///< Unsafe (narrowing) conversions |
---|
| 64 | int polyCost; ///< Count of parameters and return values bound to some poly type |
---|
[d97c3a4] | 65 | int safeCost; ///< Safe (widening) conversions |
---|
[cdcddfe1] | 66 | int signCost; ///< Count of safe sign conversions |
---|
[1dd1bd2] | 67 | int varCost; ///< Count of polymorphic type variables |
---|
| 68 | int specCost; ///< Polymorphic type specializations (type assertions), negative cost |
---|
| 69 | int referenceCost; ///< reference conversions |
---|
[a32b204] | 70 | }; |
---|
| 71 | |
---|
[cdcddfe1] | 72 | inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int signCost, |
---|
| 73 | int varCost, int specCost, int referenceCost ) |
---|
| 74 | : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), signCost( signCost ), |
---|
| 75 | varCost( varCost ), specCost( specCost ), referenceCost( referenceCost ) {} |
---|
[a32b204] | 76 | |
---|
[89be1c68] | 77 | inline Cost & Cost::incUnsafe( int inc ) { |
---|
[17f22e78] | 78 | if ( *this == infinity ) return *this; |
---|
[b46e3bd] | 79 | unsafeCost += inc; |
---|
[89be1c68] | 80 | return *this; |
---|
[a32b204] | 81 | } |
---|
| 82 | |
---|
[89be1c68] | 83 | inline Cost & Cost::incPoly( int inc ) { |
---|
[17f22e78] | 84 | if ( *this == infinity ) return *this; |
---|
[b46e3bd] | 85 | polyCost += inc; |
---|
[89be1c68] | 86 | return *this; |
---|
[a32b204] | 87 | } |
---|
| 88 | |
---|
[d97c3a4] | 89 | inline Cost & Cost::incSafe( int inc ) { |
---|
[1dd1bd2] | 90 | if ( *this == infinity ) return *this; |
---|
[d97c3a4] | 91 | safeCost += inc; |
---|
[1dd1bd2] | 92 | return *this; |
---|
| 93 | } |
---|
| 94 | |
---|
[cdcddfe1] | 95 | inline Cost & Cost::incSign( int inc ) { |
---|
| 96 | if ( *this == infinity ) return *this; |
---|
| 97 | signCost += inc; |
---|
| 98 | return *this; |
---|
| 99 | } |
---|
| 100 | |
---|
[d97c3a4] | 101 | inline Cost & Cost::incVar( int inc ) { |
---|
[1dd1bd2] | 102 | if ( *this == infinity ) return *this; |
---|
[d97c3a4] | 103 | varCost += inc; |
---|
[1dd1bd2] | 104 | return *this; |
---|
| 105 | } |
---|
| 106 | |
---|
[d97c3a4] | 107 | inline Cost& Cost::decSpec( int dec ) { |
---|
[17f22e78] | 108 | if ( *this == infinity ) return *this; |
---|
[d97c3a4] | 109 | specCost -= dec; |
---|
[89be1c68] | 110 | return *this; |
---|
[a32b204] | 111 | } |
---|
| 112 | |
---|
[7ebaa56] | 113 | inline Cost & Cost::incReference( int inc ) { |
---|
[17f22e78] | 114 | if ( *this == infinity ) return *this; |
---|
[7ebaa56] | 115 | referenceCost += inc; |
---|
| 116 | return *this; |
---|
| 117 | } |
---|
| 118 | |
---|
[a32b204] | 119 | inline Cost Cost::operator+( const Cost &other ) const { |
---|
[17f22e78] | 120 | if ( *this == infinity || other == infinity ) return infinity; |
---|
[cdcddfe1] | 121 | return Cost{ |
---|
| 122 | unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost, |
---|
| 123 | signCost + other.signCost, varCost + other.varCost, specCost + other.specCost, |
---|
[d97c3a4] | 124 | referenceCost + other.referenceCost }; |
---|
[a32b204] | 125 | } |
---|
| 126 | |
---|
| 127 | inline Cost &Cost::operator+=( const Cost &other ) { |
---|
[17f22e78] | 128 | if ( *this == infinity ) return *this; |
---|
| 129 | if ( other == infinity ) { |
---|
| 130 | *this = infinity; |
---|
| 131 | return *this; |
---|
| 132 | } |
---|
[b46e3bd] | 133 | unsafeCost += other.unsafeCost; |
---|
| 134 | polyCost += other.polyCost; |
---|
[d97c3a4] | 135 | safeCost += other.safeCost; |
---|
[cdcddfe1] | 136 | signCost += other.signCost; |
---|
[1dd1bd2] | 137 | varCost += other.varCost; |
---|
| 138 | specCost += other.specCost; |
---|
[7ebaa56] | 139 | referenceCost += other.referenceCost; |
---|
[a32b204] | 140 | return *this; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | inline bool Cost::operator<( const Cost &other ) const { |
---|
[b46e3bd] | 144 | if ( *this == infinity ) return false; |
---|
| 145 | if ( other == infinity ) return true; |
---|
| 146 | |
---|
| 147 | if ( unsafeCost > other.unsafeCost ) { |
---|
[a32b204] | 148 | return false; |
---|
[b46e3bd] | 149 | } else if ( unsafeCost < other.unsafeCost ) { |
---|
[a32b204] | 150 | return true; |
---|
[b46e3bd] | 151 | } else if ( polyCost > other.polyCost ) { |
---|
[a32b204] | 152 | return false; |
---|
[b46e3bd] | 153 | } else if ( polyCost < other.polyCost ) { |
---|
[a32b204] | 154 | return true; |
---|
[d97c3a4] | 155 | } else if ( safeCost > other.safeCost ) { |
---|
| 156 | return false; |
---|
| 157 | } else if ( safeCost < other.safeCost ) { |
---|
| 158 | return true; |
---|
[cdcddfe1] | 159 | } else if ( signCost > other.signCost ) { |
---|
| 160 | return false; |
---|
| 161 | } else if ( signCost < other.signCost ) { |
---|
| 162 | return true; |
---|
[1dd1bd2] | 163 | } else if ( varCost > other.varCost ) { |
---|
| 164 | return false; |
---|
| 165 | } else if ( varCost < other.varCost ) { |
---|
| 166 | return true; |
---|
| 167 | } else if ( specCost > other.specCost ) { |
---|
| 168 | return false; |
---|
| 169 | } else if ( specCost > other.specCost ) { |
---|
| 170 | return true; |
---|
[7ebaa56] | 171 | } else if ( referenceCost > other.referenceCost ) { |
---|
| 172 | return false; |
---|
| 173 | } else if ( referenceCost < other.referenceCost ) { |
---|
| 174 | return true; |
---|
[b46e3bd] | 175 | } else { |
---|
[a32b204] | 176 | return false; |
---|
[b46e3bd] | 177 | } // if |
---|
[d9a0e76] | 178 | } |
---|
| 179 | |
---|
[6d6e829] | 180 | inline int Cost::compare( const Cost &other ) const { |
---|
| 181 | if ( *this == infinity ) return +1; |
---|
| 182 | if ( other == infinity ) return -1; |
---|
| 183 | |
---|
| 184 | int c = unsafeCost - other.unsafeCost; if ( c ) return c; |
---|
| 185 | c = polyCost - other.polyCost; if ( c ) return c; |
---|
[d97c3a4] | 186 | c = safeCost - other.safeCost; if ( c ) return c; |
---|
[cdcddfe1] | 187 | c = signCost - other.signCost; if ( c ) return c; |
---|
[6d6e829] | 188 | c = varCost - other.varCost; if ( c ) return c; |
---|
| 189 | c = specCost - other.specCost; if ( c ) return c; |
---|
| 190 | return referenceCost - other.referenceCost; |
---|
| 191 | } |
---|
| 192 | |
---|
[a32b204] | 193 | inline bool Cost::operator==( const Cost &other ) const { |
---|
[b46e3bd] | 194 | return unsafeCost == other.unsafeCost |
---|
| 195 | && polyCost == other.polyCost |
---|
[d97c3a4] | 196 | && safeCost == other.safeCost |
---|
[cdcddfe1] | 197 | && signCost == other.signCost |
---|
[1dd1bd2] | 198 | && varCost == other.varCost |
---|
| 199 | && specCost == other.specCost |
---|
[7ebaa56] | 200 | && referenceCost == other.referenceCost; |
---|
[a32b204] | 201 | } |
---|
[d9a0e76] | 202 | |
---|
[a32b204] | 203 | inline bool Cost::operator!=( const Cost &other ) const { |
---|
| 204 | return !( *this == other ); |
---|
| 205 | } |
---|
[d9a0e76] | 206 | |
---|
[a32b204] | 207 | inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) { |
---|
[cdcddfe1] | 208 | return os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", " |
---|
| 209 | << cost.safeCost << ", " << cost.signCost << ", " |
---|
| 210 | << cost.varCost << ", " << cost.specCost << ", " |
---|
[d97c3a4] | 211 | << cost.referenceCost << " )"; |
---|
[a32b204] | 212 | } |
---|
[51b7345] | 213 | } // namespace ResolvExpr |
---|
| 214 | |
---|
[a32b204] | 215 | // Local Variables: // |
---|
| 216 | // tab-width: 4 // |
---|
| 217 | // mode: c++ // |
---|
| 218 | // compile-command: "make install" // |
---|
| 219 | // End: // |
---|