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