source: src/ResolvExpr/Cost.h@ fbecee5

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since fbecee5 was 6d6e829, checked in by Aaron Moss <a3moss@…>, 7 years ago

First compiling draft of deferred assertions (build failure)

  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[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
[1dd1bd2]11// Last Modified By : Aaron B. Moss
[6d6e829]12// Last Modified On : Fri Oct 05 14:32:00 2018
13// Update Count : 7
[a32b204]14//
15
[6b0b624]16#pragma once
[51b73452]17
18#include <iostream>
19
20namespace ResolvExpr {
[a32b204]21 class Cost {
[89be1c68]22 private:
[1dd1bd2]23 Cost( int unsafeCost, int polyCost, int varCost, int specCost, int safeCost,
24 int referenceCost );
[b46e3bd]25
[89be1c68]26 public:
27 Cost & incUnsafe( int inc = 1 );
28 Cost & incPoly( int inc = 1 );
[1dd1bd2]29 Cost & incVar( int inc = 1 );
30 Cost & decSpec( int inc = 1 );
[89be1c68]31 Cost & incSafe( int inc = 1 );
[7ebaa56]32 Cost & incReference( int inc = 1 );
[b46e3bd]33
[ddf8a29]34 int get_unsafeCost() const { return unsafeCost; }
35 int get_polyCost() const { return polyCost; }
[1dd1bd2]36 int get_varCost() const { return varCost; }
37 int get_specCost() const { return specCost; }
[ddf8a29]38 int get_safeCost() const { return safeCost; }
39 int get_referenceCost() const { return referenceCost; }
40
[a32b204]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 );
[6d6e829]48 // returns negative for *this < other, 0 for *this == other, positive for *this > other
49 int compare( const Cost &other ) const;
[b46e3bd]50
[a32b204]51 static const Cost zero;
52 static const Cost infinity;
[7ebaa56]53
[b46e3bd]54 static const Cost unsafe;
55 static const Cost poly;
[1dd1bd2]56 static const Cost var;
57 static const Cost spec;
[b46e3bd]58 static const Cost safe;
[7ebaa56]59 static const Cost reference;
[a32b204]60
[6d6e829]61 private:
[1dd1bd2]62 int unsafeCost; ///< Unsafe (narrowing) conversions
63 int polyCost; ///< Count of parameters and return values bound to some poly type
64 int varCost; ///< Count of polymorphic type variables
65 int specCost; ///< Polymorphic type specializations (type assertions), negative cost
66 int safeCost; ///< Safe (widening) conversions
67 int referenceCost; ///< reference conversions
[a32b204]68 };
69
[1dd1bd2]70 inline Cost::Cost( int unsafeCost, int polyCost, int varCost, int specCost, int safeCost,
71 int referenceCost )
72 : unsafeCost( unsafeCost ), polyCost( polyCost ), varCost( varCost ), specCost( specCost ),
73 safeCost( safeCost ), referenceCost( referenceCost ) {}
[a32b204]74
[89be1c68]75 inline Cost & Cost::incUnsafe( int inc ) {
[17f22e78]76 if ( *this == infinity ) return *this;
[b46e3bd]77 unsafeCost += inc;
[89be1c68]78 return *this;
[a32b204]79 }
80
[89be1c68]81 inline Cost & Cost::incPoly( int inc ) {
[17f22e78]82 if ( *this == infinity ) return *this;
[b46e3bd]83 polyCost += inc;
[89be1c68]84 return *this;
[a32b204]85 }
86
[1dd1bd2]87 inline Cost & Cost::incVar( int inc ) {
88 if ( *this == infinity ) return *this;
89 varCost += inc;
90 return *this;
91 }
92
93 inline Cost& Cost::decSpec( int dec ) {
94 if ( *this == infinity ) return *this;
95 specCost -= dec;
96 return *this;
97 }
98
[89be1c68]99 inline Cost & Cost::incSafe( int inc ) {
[17f22e78]100 if ( *this == infinity ) return *this;
[b46e3bd]101 safeCost += inc;
[89be1c68]102 return *this;
[a32b204]103 }
104
[7ebaa56]105 inline Cost & Cost::incReference( int inc ) {
[17f22e78]106 if ( *this == infinity ) return *this;
[7ebaa56]107 referenceCost += inc;
108 return *this;
109 }
110
[a32b204]111 inline Cost Cost::operator+( const Cost &other ) const {
[17f22e78]112 if ( *this == infinity || other == infinity ) return infinity;
[1dd1bd2]113 return Cost{
114 unsafeCost + other.unsafeCost, polyCost + other.polyCost,
115 varCost + other.varCost, specCost + other.specCost,
116 safeCost + other.safeCost, referenceCost + other.referenceCost };
[a32b204]117 }
118
119 inline Cost Cost::operator-( const Cost &other ) const {
[17f22e78]120 if ( *this == infinity || other == infinity ) return infinity;
[1dd1bd2]121 return Cost{
122 unsafeCost - other.unsafeCost, polyCost - other.polyCost,
123 varCost - other.varCost, specCost - other.specCost,
124 safeCost - other.safeCost, referenceCost - other.referenceCost };
[a32b204]125 }
126
127 inline Cost &Cost::operator+=( const Cost &other ) {
[17f22e78]128 if ( *this == infinity ) return *this;
129 if ( other == infinity ) {
130 *this = infinity;
131 return *this;
132 }
[b46e3bd]133 unsafeCost += other.unsafeCost;
134 polyCost += other.polyCost;
[1dd1bd2]135 varCost += other.varCost;
136 specCost += other.specCost;
[b46e3bd]137 safeCost += other.safeCost;
[7ebaa56]138 referenceCost += other.referenceCost;
[a32b204]139 return *this;
140 }
141
142 inline bool Cost::operator<( const Cost &other ) const {
[b46e3bd]143 if ( *this == infinity ) return false;
144 if ( other == infinity ) return true;
145
146 if ( unsafeCost > other.unsafeCost ) {
[a32b204]147 return false;
[b46e3bd]148 } else if ( unsafeCost < other.unsafeCost ) {
[a32b204]149 return true;
[b46e3bd]150 } else if ( polyCost > other.polyCost ) {
[a32b204]151 return false;
[b46e3bd]152 } else if ( polyCost < other.polyCost ) {
[a32b204]153 return true;
[1dd1bd2]154 } else if ( varCost > other.varCost ) {
155 return false;
156 } else if ( varCost < other.varCost ) {
157 return true;
158 } else if ( specCost > other.specCost ) {
159 return false;
160 } else if ( specCost > other.specCost ) {
161 return true;
[b46e3bd]162 } else if ( safeCost > other.safeCost ) {
[a32b204]163 return false;
[b46e3bd]164 } else if ( safeCost < other.safeCost ) {
[a32b204]165 return true;
[7ebaa56]166 } else if ( referenceCost > other.referenceCost ) {
167 return false;
168 } else if ( referenceCost < other.referenceCost ) {
169 return true;
[b46e3bd]170 } else {
[a32b204]171 return false;
[b46e3bd]172 } // if
[d9a0e76]173 }
174
[6d6e829]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 = varCost - other.varCost; if ( c ) return c;
182 c = specCost - other.specCost; if ( c ) return c;
183 c = safeCost - other.safeCost; if ( c ) return c;
184 return referenceCost - other.referenceCost;
185 }
186
[a32b204]187 inline bool Cost::operator==( const Cost &other ) const {
[b46e3bd]188 return unsafeCost == other.unsafeCost
189 && polyCost == other.polyCost
[1dd1bd2]190 && varCost == other.varCost
191 && specCost == other.specCost
[7ebaa56]192 && safeCost == other.safeCost
193 && referenceCost == other.referenceCost;
[a32b204]194 }
[d9a0e76]195
[a32b204]196 inline bool Cost::operator!=( const Cost &other ) const {
197 return !( *this == other );
198 }
[d9a0e76]199
[a32b204]200 inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) {
[1dd1bd2]201 return os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", "
202 << cost.varCost << ", " << cost.specCost << ", "
203 << cost.safeCost << ", " << cost.referenceCost << " )";
[a32b204]204 }
[51b73452]205} // namespace ResolvExpr
206
[a32b204]207// Local Variables: //
208// tab-width: 4 //
209// mode: c++ //
210// compile-command: "make install" //
211// End: //
Note: See TracBrowser for help on using the repository browser.