| [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
 | 
|---|
| [6b0b624] | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
 | 12 | // Last Modified On : Sat Jul 22 09:35:55 2017
 | 
|---|
 | 13 | // Update Count     : 5
 | 
|---|
| [a32b204] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [6b0b624] | 16 | #pragma once
 | 
|---|
| [51b73452] | 17 | 
 | 
|---|
 | 18 | #include <iostream>
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | namespace ResolvExpr {
 | 
|---|
| [a32b204] | 21 |         class Cost {
 | 
|---|
| [89be1c68] | 22 |           private:
 | 
|---|
| [7ebaa56] | 23 |                 Cost( int unsafeCost, int polyCost, int safeCost, int referenceCost );
 | 
|---|
| [b46e3bd] | 24 | 
 | 
|---|
| [89be1c68] | 25 |           public:
 | 
|---|
 | 26 |                 Cost & incUnsafe( int inc = 1 );
 | 
|---|
 | 27 |                 Cost & incPoly( int inc = 1 );
 | 
|---|
 | 28 |                 Cost & incSafe( int inc = 1 );
 | 
|---|
| [7ebaa56] | 29 |                 Cost & incReference( int inc = 1 );
 | 
|---|
| [b46e3bd] | 30 | 
 | 
|---|
| [ddf8a29] | 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 | 
 | 
|---|
| [a32b204] | 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 );
 | 
|---|
| [b46e3bd] | 43 | 
 | 
|---|
| [a32b204] | 44 |                 static const Cost zero;
 | 
|---|
 | 45 |                 static const Cost infinity;
 | 
|---|
| [7ebaa56] | 46 | 
 | 
|---|
| [b46e3bd] | 47 |                 static const Cost unsafe;
 | 
|---|
 | 48 |                 static const Cost poly;
 | 
|---|
 | 49 |                 static const Cost safe;
 | 
|---|
| [7ebaa56] | 50 |                 static const Cost reference;
 | 
|---|
| [a32b204] | 51 |           private:
 | 
|---|
 | 52 |                 int compare( const Cost &other ) const;
 | 
|---|
 | 53 | 
 | 
|---|
| [b46e3bd] | 54 |                 int unsafeCost;
 | 
|---|
 | 55 |                 int polyCost;
 | 
|---|
 | 56 |                 int safeCost;
 | 
|---|
| [7ebaa56] | 57 |                 int referenceCost;
 | 
|---|
| [a32b204] | 58 |         };
 | 
|---|
 | 59 | 
 | 
|---|
| [7ebaa56] | 60 |         inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int referenceCost ) : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), referenceCost( referenceCost ) {}
 | 
|---|
| [a32b204] | 61 | 
 | 
|---|
| [89be1c68] | 62 |         inline Cost & Cost::incUnsafe( int inc ) {
 | 
|---|
| [17f22e78] | 63 |                 if ( *this == infinity ) return *this;
 | 
|---|
| [b46e3bd] | 64 |                 unsafeCost += inc;
 | 
|---|
| [89be1c68] | 65 |                 return *this;
 | 
|---|
| [a32b204] | 66 |         }
 | 
|---|
 | 67 | 
 | 
|---|
| [89be1c68] | 68 |         inline Cost & Cost::incPoly( int inc ) {
 | 
|---|
| [17f22e78] | 69 |                 if ( *this == infinity ) return *this;
 | 
|---|
| [b46e3bd] | 70 |                 polyCost += inc;
 | 
|---|
| [89be1c68] | 71 |                 return *this;
 | 
|---|
| [a32b204] | 72 |         }
 | 
|---|
 | 73 | 
 | 
|---|
| [89be1c68] | 74 |         inline Cost & Cost::incSafe( int inc ) {
 | 
|---|
| [17f22e78] | 75 |                 if ( *this == infinity ) return *this;
 | 
|---|
| [b46e3bd] | 76 |                 safeCost += inc;
 | 
|---|
| [89be1c68] | 77 |                 return *this;
 | 
|---|
| [a32b204] | 78 |         }
 | 
|---|
 | 79 | 
 | 
|---|
| [7ebaa56] | 80 |         inline Cost & Cost::incReference( int inc ) {
 | 
|---|
| [17f22e78] | 81 |                 if ( *this == infinity ) return *this;
 | 
|---|
| [7ebaa56] | 82 |                 referenceCost += inc;
 | 
|---|
 | 83 |                 return *this;
 | 
|---|
 | 84 |         }
 | 
|---|
 | 85 | 
 | 
|---|
| [a32b204] | 86 |         inline Cost Cost::operator+( const Cost &other ) const {
 | 
|---|
| [17f22e78] | 87 |                 if ( *this == infinity || other == infinity ) return infinity;
 | 
|---|
| [7ebaa56] | 88 |                 return Cost( unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost, referenceCost + other.referenceCost );
 | 
|---|
| [a32b204] | 89 |         }
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |         inline Cost Cost::operator-( const Cost &other ) const {
 | 
|---|
| [17f22e78] | 92 |                 if ( *this == infinity || other == infinity ) return infinity;
 | 
|---|
| [7ebaa56] | 93 |                 return Cost( unsafeCost - other.unsafeCost, polyCost - other.polyCost, safeCost - other.safeCost, referenceCost - other.referenceCost );
 | 
|---|
| [a32b204] | 94 |         }
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 |         inline Cost &Cost::operator+=( const Cost &other ) {
 | 
|---|
| [17f22e78] | 97 |                 if ( *this == infinity ) return *this;
 | 
|---|
 | 98 |                 if ( other == infinity ) {
 | 
|---|
 | 99 |                         *this = infinity;
 | 
|---|
 | 100 |                         return *this;
 | 
|---|
 | 101 |                 }
 | 
|---|
| [b46e3bd] | 102 |                 unsafeCost += other.unsafeCost;
 | 
|---|
 | 103 |                 polyCost += other.polyCost;
 | 
|---|
 | 104 |                 safeCost += other.safeCost;
 | 
|---|
| [7ebaa56] | 105 |                 referenceCost += other.referenceCost;
 | 
|---|
| [a32b204] | 106 |                 return *this;
 | 
|---|
 | 107 |         }
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |         inline bool Cost::operator<( const Cost &other ) const {
 | 
|---|
| [b46e3bd] | 110 |                 if ( *this == infinity ) return false;
 | 
|---|
 | 111 |                 if ( other == infinity ) return true;
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |                 if ( unsafeCost > other.unsafeCost ) {
 | 
|---|
| [a32b204] | 114 |                         return false;
 | 
|---|
| [b46e3bd] | 115 |                 } else if ( unsafeCost < other.unsafeCost ) {
 | 
|---|
| [a32b204] | 116 |                         return true;
 | 
|---|
| [b46e3bd] | 117 |                 } else if ( polyCost > other.polyCost ) {
 | 
|---|
| [a32b204] | 118 |                         return false;
 | 
|---|
| [b46e3bd] | 119 |                 } else if ( polyCost < other.polyCost ) {
 | 
|---|
| [a32b204] | 120 |                         return true;
 | 
|---|
| [b46e3bd] | 121 |                 } else if ( safeCost > other.safeCost ) {
 | 
|---|
| [a32b204] | 122 |                         return false;
 | 
|---|
| [b46e3bd] | 123 |                 } else if ( safeCost < other.safeCost ) {
 | 
|---|
| [a32b204] | 124 |                         return true;
 | 
|---|
| [7ebaa56] | 125 |                 } else if ( referenceCost > other.referenceCost ) {
 | 
|---|
 | 126 |                         return false;
 | 
|---|
 | 127 |                 } else if ( referenceCost < other.referenceCost ) {
 | 
|---|
 | 128 |                         return true;
 | 
|---|
| [b46e3bd] | 129 |                 } else {
 | 
|---|
| [a32b204] | 130 |                         return false;
 | 
|---|
| [b46e3bd] | 131 |                 } // if
 | 
|---|
| [d9a0e76] | 132 |         }
 | 
|---|
 | 133 | 
 | 
|---|
| [a32b204] | 134 |         inline bool Cost::operator==( const Cost &other ) const {
 | 
|---|
| [b46e3bd] | 135 |                 return unsafeCost == other.unsafeCost
 | 
|---|
 | 136 |                         && polyCost == other.polyCost
 | 
|---|
| [7ebaa56] | 137 |                         && safeCost == other.safeCost
 | 
|---|
 | 138 |                         && referenceCost == other.referenceCost;
 | 
|---|
| [a32b204] | 139 |         }
 | 
|---|
| [d9a0e76] | 140 | 
 | 
|---|
| [a32b204] | 141 |         inline bool Cost::operator!=( const Cost &other ) const {
 | 
|---|
 | 142 |                 return !( *this == other );
 | 
|---|
 | 143 |         }
 | 
|---|
| [d9a0e76] | 144 | 
 | 
|---|
| [a32b204] | 145 |         inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) {
 | 
|---|
| [7ebaa56] | 146 |                 os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", " << cost.safeCost << ", " << cost.referenceCost << " )";
 | 
|---|
| [a32b204] | 147 |                 return os;
 | 
|---|
 | 148 |         }
 | 
|---|
| [51b73452] | 149 | } // namespace ResolvExpr
 | 
|---|
 | 150 | 
 | 
|---|
| [a32b204] | 151 | // Local Variables: //
 | 
|---|
 | 152 | // tab-width: 4 //
 | 
|---|
 | 153 | // mode: c++ //
 | 
|---|
 | 154 | // compile-command: "make install" //
 | 
|---|
 | 155 | // End: //
 | 
|---|