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 | public: |
---|
23 | Cost(); |
---|
24 | Cost( int unsafe, int poly, int safe ); |
---|
25 | |
---|
26 | void incUnsafe( int inc = 1 ); |
---|
27 | void incPoly( int inc = 1 ); |
---|
28 | void incSafe( int inc = 1 ); |
---|
29 | |
---|
30 | Cost operator+( const Cost &other ) const; |
---|
31 | Cost operator-( const Cost &other ) const; |
---|
32 | Cost &operator+=( const Cost &other ); |
---|
33 | bool operator<( const Cost &other ) const; |
---|
34 | bool operator==( const Cost &other ) const; |
---|
35 | bool operator!=( const Cost &other ) const; |
---|
36 | friend std::ostream &operator<<( std::ostream &os, const Cost &cost ); |
---|
37 | |
---|
38 | static const Cost zero; |
---|
39 | static const Cost infinity; |
---|
40 | private: |
---|
41 | int compare( const Cost &other ) const; |
---|
42 | |
---|
43 | int unsafe; |
---|
44 | int poly; |
---|
45 | int safe; |
---|
46 | }; |
---|
47 | |
---|
48 | inline Cost::Cost() : unsafe( 0 ), poly( 0 ), safe( 0 ) {} |
---|
49 | |
---|
50 | inline Cost::Cost( int unsafe, int poly, int safe ) : unsafe( unsafe ), poly( poly ), safe( safe ) {} |
---|
51 | |
---|
52 | inline void Cost::incUnsafe( int inc ) { |
---|
53 | unsafe += inc; |
---|
54 | } |
---|
55 | |
---|
56 | inline void Cost::incPoly( int inc ) { |
---|
57 | poly += inc; |
---|
58 | } |
---|
59 | |
---|
60 | inline void Cost::incSafe( int inc ) { |
---|
61 | safe += inc; |
---|
62 | } |
---|
63 | |
---|
64 | inline Cost Cost::operator+( const Cost &other ) const { |
---|
65 | return Cost( unsafe + other.unsafe, poly + other.poly, safe + other.safe ); |
---|
66 | } |
---|
67 | |
---|
68 | inline Cost Cost::operator-( const Cost &other ) const { |
---|
69 | return Cost( unsafe - other.unsafe, poly - other.poly, safe - other.safe ); |
---|
70 | } |
---|
71 | |
---|
72 | inline Cost &Cost::operator+=( const Cost &other ) { |
---|
73 | unsafe += other.unsafe; |
---|
74 | poly += other.poly; |
---|
75 | safe += other.safe; |
---|
76 | return *this; |
---|
77 | } |
---|
78 | |
---|
79 | inline bool Cost::operator<( const Cost &other ) const { |
---|
80 | if ( *this == infinity ) return false; |
---|
81 | if ( other == infinity ) return true; |
---|
82 | if ( unsafe > other.unsafe ) { |
---|
83 | return false; |
---|
84 | } else if ( unsafe < other.unsafe ) { |
---|
85 | return true; |
---|
86 | } else if ( poly > other.poly ) { |
---|
87 | return false; |
---|
88 | } else if ( poly < other.poly ) { |
---|
89 | return true; |
---|
90 | } else if ( safe > other.safe ) { |
---|
91 | return false; |
---|
92 | } else if ( safe < other.safe ) { |
---|
93 | return true; |
---|
94 | } else { |
---|
95 | return false; |
---|
96 | } // if |
---|
97 | } |
---|
98 | |
---|
99 | inline bool Cost::operator==( const Cost &other ) const { |
---|
100 | return unsafe == other.unsafe |
---|
101 | && poly == other.poly |
---|
102 | && safe == other.safe; |
---|
103 | } |
---|
104 | |
---|
105 | inline bool Cost::operator!=( const Cost &other ) const { |
---|
106 | return !( *this == other ); |
---|
107 | } |
---|
108 | |
---|
109 | inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) { |
---|
110 | os << "( " << cost.unsafe << ", " << cost.poly << ", " << cost.safe << " )"; |
---|
111 | return os; |
---|
112 | } |
---|
113 | } // namespace ResolvExpr |
---|
114 | |
---|
115 | // Local Variables: // |
---|
116 | // tab-width: 4 // |
---|
117 | // mode: c++ // |
---|
118 | // compile-command: "make install" // |
---|
119 | // End: // |
---|