| 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 : Aaron B. Moss
|
|---|
| 12 | // Last Modified On : Tue Oct 02 14:40:00 2018
|
|---|
| 13 | // Update Count : 6
|
|---|
| 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 varCost, int specCost, int safeCost,
|
|---|
| 24 | int referenceCost );
|
|---|
| 25 |
|
|---|
| 26 | public:
|
|---|
| 27 | Cost & incUnsafe( int inc = 1 );
|
|---|
| 28 | Cost & incPoly( int inc = 1 );
|
|---|
| 29 | Cost & incVar( int inc = 1 );
|
|---|
| 30 | Cost & decSpec( int inc = 1 );
|
|---|
| 31 | Cost & incSafe( int inc = 1 );
|
|---|
| 32 | Cost & incReference( int inc = 1 );
|
|---|
| 33 |
|
|---|
| 34 | int get_unsafeCost() const { return unsafeCost; }
|
|---|
| 35 | int get_polyCost() const { return polyCost; }
|
|---|
| 36 | int get_varCost() const { return varCost; }
|
|---|
| 37 | int get_specCost() const { return specCost; }
|
|---|
| 38 | int get_safeCost() const { return safeCost; }
|
|---|
| 39 | int get_referenceCost() const { return referenceCost; }
|
|---|
| 40 |
|
|---|
| 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 );
|
|---|
| 48 |
|
|---|
| 49 | static const Cost zero;
|
|---|
| 50 | static const Cost infinity;
|
|---|
| 51 |
|
|---|
| 52 | static const Cost unsafe;
|
|---|
| 53 | static const Cost poly;
|
|---|
| 54 | static const Cost var;
|
|---|
| 55 | static const Cost spec;
|
|---|
| 56 | static const Cost safe;
|
|---|
| 57 | static const Cost reference;
|
|---|
| 58 | private:
|
|---|
| 59 | int compare( const Cost &other ) const;
|
|---|
| 60 |
|
|---|
| 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
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 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 ) {}
|
|---|
| 73 |
|
|---|
| 74 | inline Cost & Cost::incUnsafe( int inc ) {
|
|---|
| 75 | if ( *this == infinity ) return *this;
|
|---|
| 76 | unsafeCost += inc;
|
|---|
| 77 | return *this;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | inline Cost & Cost::incPoly( int inc ) {
|
|---|
| 81 | if ( *this == infinity ) return *this;
|
|---|
| 82 | polyCost += inc;
|
|---|
| 83 | return *this;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 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 |
|
|---|
| 98 | inline Cost & Cost::incSafe( int inc ) {
|
|---|
| 99 | if ( *this == infinity ) return *this;
|
|---|
| 100 | safeCost += inc;
|
|---|
| 101 | return *this;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | inline Cost & Cost::incReference( int inc ) {
|
|---|
| 105 | if ( *this == infinity ) return *this;
|
|---|
| 106 | referenceCost += inc;
|
|---|
| 107 | return *this;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | inline Cost Cost::operator+( const Cost &other ) const {
|
|---|
| 111 | if ( *this == infinity || other == infinity ) return infinity;
|
|---|
| 112 | return Cost{
|
|---|
| 113 | unsafeCost + other.unsafeCost, polyCost + other.polyCost,
|
|---|
| 114 | varCost + other.varCost, specCost + other.specCost,
|
|---|
| 115 | safeCost + other.safeCost, referenceCost + other.referenceCost };
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | inline Cost Cost::operator-( const Cost &other ) const {
|
|---|
| 119 | if ( *this == infinity || other == infinity ) return infinity;
|
|---|
| 120 | return Cost{
|
|---|
| 121 | unsafeCost - other.unsafeCost, polyCost - other.polyCost,
|
|---|
| 122 | varCost - other.varCost, specCost - other.specCost,
|
|---|
| 123 | safeCost - other.safeCost, referenceCost - other.referenceCost };
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | inline Cost &Cost::operator+=( const Cost &other ) {
|
|---|
| 127 | if ( *this == infinity ) return *this;
|
|---|
| 128 | if ( other == infinity ) {
|
|---|
| 129 | *this = infinity;
|
|---|
| 130 | return *this;
|
|---|
| 131 | }
|
|---|
| 132 | unsafeCost += other.unsafeCost;
|
|---|
| 133 | polyCost += other.polyCost;
|
|---|
| 134 | varCost += other.varCost;
|
|---|
| 135 | specCost += other.specCost;
|
|---|
| 136 | safeCost += other.safeCost;
|
|---|
| 137 | referenceCost += other.referenceCost;
|
|---|
| 138 | return *this;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | inline bool Cost::operator<( const Cost &other ) const {
|
|---|
| 142 | if ( *this == infinity ) return false;
|
|---|
| 143 | if ( other == infinity ) return true;
|
|---|
| 144 |
|
|---|
| 145 | if ( unsafeCost > other.unsafeCost ) {
|
|---|
| 146 | return false;
|
|---|
| 147 | } else if ( unsafeCost < other.unsafeCost ) {
|
|---|
| 148 | return true;
|
|---|
| 149 | } else if ( polyCost > other.polyCost ) {
|
|---|
| 150 | return false;
|
|---|
| 151 | } else if ( polyCost < other.polyCost ) {
|
|---|
| 152 | return true;
|
|---|
| 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;
|
|---|
| 161 | } else if ( safeCost > other.safeCost ) {
|
|---|
| 162 | return false;
|
|---|
| 163 | } else if ( safeCost < other.safeCost ) {
|
|---|
| 164 | return true;
|
|---|
| 165 | } else if ( referenceCost > other.referenceCost ) {
|
|---|
| 166 | return false;
|
|---|
| 167 | } else if ( referenceCost < other.referenceCost ) {
|
|---|
| 168 | return true;
|
|---|
| 169 | } else {
|
|---|
| 170 | return false;
|
|---|
| 171 | } // if
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | inline bool Cost::operator==( const Cost &other ) const {
|
|---|
| 175 | return unsafeCost == other.unsafeCost
|
|---|
| 176 | && polyCost == other.polyCost
|
|---|
| 177 | && varCost == other.varCost
|
|---|
| 178 | && specCost == other.specCost
|
|---|
| 179 | && safeCost == other.safeCost
|
|---|
| 180 | && referenceCost == other.referenceCost;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | inline bool Cost::operator!=( const Cost &other ) const {
|
|---|
| 184 | return !( *this == other );
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) {
|
|---|
| 188 | return os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", "
|
|---|
| 189 | << cost.varCost << ", " << cost.specCost << ", "
|
|---|
| 190 | << cost.safeCost << ", " << cost.referenceCost << " )";
|
|---|
| 191 | }
|
|---|
| 192 | } // namespace ResolvExpr
|
|---|
| 193 |
|
|---|
| 194 | // Local Variables: //
|
|---|
| 195 | // tab-width: 4 //
|
|---|
| 196 | // mode: c++ //
|
|---|
| 197 | // compile-command: "make install" //
|
|---|
| 198 | // End: //
|
|---|