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