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