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