source: src/ResolvExpr/Cost.h@ 6d267ca

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 6d267ca was 17f22e78, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Update Cost operations to perform infinity checks internally

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