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