1 | /* |
---|
2 | * This file is part of the Cforall project |
---|
3 | * |
---|
4 | * $Id: Cost.h,v 1.2 2003/01/27 14:46:59 rcbilson Exp $ |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef RESOLVEXPR_COST_H |
---|
9 | #define RESOLVEXPR_COST_H |
---|
10 | |
---|
11 | #include <iostream> |
---|
12 | |
---|
13 | namespace ResolvExpr { |
---|
14 | |
---|
15 | class Cost |
---|
16 | { |
---|
17 | public: |
---|
18 | Cost(); |
---|
19 | Cost( int unsafe, int poly, int safe ); |
---|
20 | |
---|
21 | void incUnsafe( int inc = 1 ); |
---|
22 | void incPoly( int inc = 1 ); |
---|
23 | void incSafe( int inc = 1 ); |
---|
24 | |
---|
25 | Cost operator+( const Cost &other ) const; |
---|
26 | Cost operator-( const Cost &other ) const; |
---|
27 | Cost &operator+=( const Cost &other ); |
---|
28 | bool operator<( const Cost &other ) const; |
---|
29 | bool operator==( const Cost &other ) const; |
---|
30 | bool operator!=( const Cost &other ) const; |
---|
31 | friend std::ostream &operator<<( std::ostream &os, const Cost &cost ); |
---|
32 | |
---|
33 | static const Cost zero; |
---|
34 | static const Cost infinity; |
---|
35 | |
---|
36 | private: |
---|
37 | int compare( const Cost &other ) const; |
---|
38 | |
---|
39 | int unsafe; |
---|
40 | int poly; |
---|
41 | int safe; |
---|
42 | }; |
---|
43 | |
---|
44 | inline |
---|
45 | Cost::Cost() |
---|
46 | : unsafe( 0 ), poly( 0 ), safe( 0 ) |
---|
47 | { |
---|
48 | } |
---|
49 | |
---|
50 | inline |
---|
51 | Cost::Cost( int unsafe, int poly, int safe ) |
---|
52 | : unsafe( unsafe ), poly( poly ), safe( safe ) |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | inline void |
---|
57 | Cost::incUnsafe( int inc ) |
---|
58 | { |
---|
59 | unsafe += inc; |
---|
60 | } |
---|
61 | |
---|
62 | inline void |
---|
63 | Cost::incPoly( int inc ) |
---|
64 | { |
---|
65 | unsafe += inc; |
---|
66 | } |
---|
67 | |
---|
68 | inline void |
---|
69 | Cost::incSafe( int inc ) |
---|
70 | { |
---|
71 | unsafe += inc; |
---|
72 | } |
---|
73 | |
---|
74 | inline Cost |
---|
75 | Cost::operator+( const Cost &other ) const |
---|
76 | { |
---|
77 | return Cost( unsafe + other.unsafe, poly + other.poly, safe + other.safe ); |
---|
78 | } |
---|
79 | |
---|
80 | inline Cost |
---|
81 | Cost::operator-( const Cost &other ) const |
---|
82 | { |
---|
83 | return Cost( unsafe - other.unsafe, poly - other.poly, safe - other.safe ); |
---|
84 | } |
---|
85 | |
---|
86 | inline Cost & |
---|
87 | Cost::operator+=( const Cost &other ) |
---|
88 | { |
---|
89 | unsafe += other.unsafe; |
---|
90 | poly += other.poly; |
---|
91 | safe += other.safe; |
---|
92 | return *this; |
---|
93 | } |
---|
94 | |
---|
95 | inline bool |
---|
96 | Cost::operator<( const Cost &other ) const |
---|
97 | { |
---|
98 | if( *this == infinity ) return false; |
---|
99 | if( other == infinity ) return true; |
---|
100 | if( unsafe > other.unsafe ) { |
---|
101 | return false; |
---|
102 | } else if( unsafe < other.unsafe ) { |
---|
103 | return true; |
---|
104 | } else if( poly > other.poly ) { |
---|
105 | return false; |
---|
106 | } else if( poly < other.poly ) { |
---|
107 | return true; |
---|
108 | } else if( safe > other.safe ) { |
---|
109 | return false; |
---|
110 | } else if( safe < other.safe ) { |
---|
111 | return true; |
---|
112 | } else { |
---|
113 | return false; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | inline bool |
---|
118 | Cost::operator==( const Cost &other ) const |
---|
119 | { |
---|
120 | return unsafe == other.unsafe |
---|
121 | && poly == other.poly |
---|
122 | && safe == other.safe; |
---|
123 | } |
---|
124 | |
---|
125 | inline bool |
---|
126 | Cost::operator!=( const Cost &other ) const |
---|
127 | { |
---|
128 | return !( *this == other ); |
---|
129 | } |
---|
130 | |
---|
131 | inline std::ostream & |
---|
132 | operator<<( std::ostream &os, const Cost &cost ) |
---|
133 | { |
---|
134 | os << "( " << cost.unsafe << ", " << cost.poly << ", " << cost.safe << " )"; |
---|
135 | return os; |
---|
136 | } |
---|
137 | |
---|
138 | } // namespace ResolvExpr |
---|
139 | |
---|
140 | #endif /* #ifndef RESOLVEXPR_COST_H */ |
---|