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