Otawa  0.10
expr.h
Go to the documentation of this file.
1 /*
2  * ilp::Expr and ilp::Cons classes interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2015, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef OTAWA_ILP_EXPR_H_
22 #define OTAWA_ILP_EXPR_H_
23 
24 #include <otawa/ilp/System.h>
25 
26 namespace otawa { namespace ilp {
27 
28 // var class
29 class var {
30 public:
31  inline explicit var(System& sys, Var::type_t t = Var::INT, string name = ""): _v(sys.newVar(name)) { }
32  inline explicit var(System *sys, Var::type_t t = Var::INT, string name = ""): _v(sys->newVar(name)) { }
33  inline var(Var *v): _v(v) { }
34  inline operator Var *(void) const { return _v; }
35  inline operator Term(void) const { return Term(1, _v); }
36 private:
37  Var *_v;
38 };
39 
40 inline var x(Var *v) { return var(v); }
41 inline Term operator*(coef_t c, var v) { return Term(v, c); }
42 inline Term operator*(var v, coef_t c) { return Term(v, c); }
43 
44 
45 // cons class
46 class cons {
47 public:
48  inline cons(void): l(true), c(0) { }
49  inline cons(Constraint *cc): l(cc->comparator() == Constraint::UNDEF), c(cc) { }
50 
51  inline cons operator+(const Term& t) { if(l) c->add(t); else c->sub(t); return *this; }
52  inline cons operator-(const Term& t) { if(l) c->sub(t); else c->add(t); return *this; }
53  inline cons operator+(const Expression& e) { if(l) c->add(e); else c->sub(e); return *this; }
54  inline cons operator-(const Expression& e) { if(l) c->sub(e); else c->add(e);; return *this; }
55 
56  inline cons operator+=(const Term& t) { if(l) c->add(t); else c->sub(t); return *this; }
57  inline cons operator-=(const Term& t) { if(l) c->sub(t); else c->add(t); return *this; }
58  inline cons operator+=(const Expression& e) { if(l) c->add(e); else c->sub(e); return *this; }
59  inline cons operator-=(const Expression& e) { if(l) c->sub(e); else c->add(e);; return *this; }
60 
61  inline cons operator==(const Term& t) { set(Constraint::EQ); return *this + t; }
62  inline cons operator==(const Expression& f) { set(Constraint::EQ); return *this + f; }
63  inline cons operator<=(const Term& t) { set(Constraint::LE); return *this + t; }
64  inline cons operator<=(const Expression& f) { set(Constraint::LE); return *this + f; }
65  inline cons operator<(const Term& t) { set(Constraint::LT); return *this + t; }
66  inline cons operator<(const Expression& f) { set(Constraint::LT); return *this + f; }
67  inline cons operator>=(const Term& t) { set(Constraint::GE); return *this + t; }
68  inline cons operator>=(const Expression& f) { set(Constraint::GE); return *this + f; }
69  inline cons operator>(const Term& t) { set(Constraint::GT); return *this + t; }
70  inline cons operator>(const Expression& f) { set(Constraint::GT); return *this + f; }
71 
72  inline Constraint *operator&(void) const { return c; }
73  inline Constraint *operator->(void) const { return c; }
74  inline void free(void) { delete c; c = 0; }
75 private:
76  void set(Constraint::comparator_t comp) { l = false; c->setComparator(comp); }
77  bool l;
79 };
80 
81 class model {
82 public:
83  inline model(System *s): _s(s) { }
84  inline cons operator()(void) const { return cons(_s->newConstraint(Constraint::UNDEF)); }
85  inline cons operator()(const string& label) const { cons c(_s->newConstraint(Constraint::UNDEF)); c->setLabel(label); return c; }
86  inline System *operator&(void) const { return _s; }
87  inline System *operator->(void) const { return _s; }
88  inline operator System *(void) const { return _s; }
89 private:
91 };
92 
93 
94 // Expression generation
95 inline Expression operator+(const Term& t1, const Term& t2) { Expression e; e.add(t1); e.add(t2); return e; }
96 inline Expression operator-(const Term& t1, const Term& t2) { Expression e; e.add(t1); e.sub(t2); return e; }
97 inline Expression operator+(const Expression& e, const Term& t) { Expression r(e); r.add(t); return r; }
98 inline Expression operator-(const Expression& e, const Term& t) { Expression r(e); r.sub(t); return r; }
99 
100 } } // otawa::ilp
101 
102 #endif /* OTAWA_ILP_EXPR_H_ */
An expression allows to represent a sum of terms and may be used to represent the value of an aliased...
Definition: Expression.h:43
cons(Constraint *cc)
Definition: expr.h:49
cons operator+=(const Term &t)
Definition: expr.h:56
cons operator==(const Term &t)
Definition: expr.h:61
cons operator+=(const Expression &e)
Definition: expr.h:58
comparator_t
Definition: Constraint.h:36
System * operator->(void) const
Definition: expr.h:87
double coef_t
Definition: Expression.h:31
var(System *sys, Var::type_t t=Var::INT, string name="")
Definition: expr.h:32
Expression operator-(const Term &t1, const Term &t2)
Definition: expr.h:96
var x(Var *v)
Convert an ilp::Var * to a var.
Definition: expr.h:40
Definition: Constraint.h:38
void set(Constraint::comparator_t comp)
Definition: expr.h:76
void sub(coef_t coef, ilp::Var *var=0)
Subtract a term to the expression.
Definition: Expression.h:49
cons operator<=(const Term &t)
Definition: expr.h:63
virtual void setComparator(comparator_t comp)=0
Definition: Constraint.h:42
cons operator==(const Expression &f)
Definition: expr.h:62
Definition: features.h:42
virtual void add(double coef, Var *var=0)=0
Add a term to the constraint to the left part of constraint.
Term operator*(coef_t c, var v)
Definition: expr.h:41
Definition: Constraint.h:37
System * _s
Definition: expr.h:90
Definition: Constraint.h:40
bool l
Definition: expr.h:77
cons operator+(const Term &t)
Definition: expr.h:51
cons operator>=(const Expression &f)
Definition: expr.h:68
cons operator()(const string &label) const
Definition: expr.h:85
System * operator&(void) const
Definition: expr.h:86
cons operator>=(const Term &t)
Definition: expr.h:67
var(Var *v)
Definition: expr.h:33
cons(void)
Definition: expr.h:48
virtual Constraint * newConstraint(Constraint::comparator_t comp, double constant=0)=0
Build a new constraint that may be initialized by the user.
Constraint * c
Definition: expr.h:78
Constraint * operator->(void) const
Definition: expr.h:73
virtual void setLabel(const string &label)=0
cons operator-(const Expression &e)
Definition: expr.h:54
cons operator-(const Term &t)
Definition: expr.h:52
cons operator<(const Term &t)
Definition: expr.h:65
Var * _v
Definition: expr.h:37
This class is used to represent constraints in an ILP system with the following form: ...
Definition: Constraint.h:33
Definition: Expression.h:35
var(System &sys, Var::type_t t=Var::INT, string name="")
Definition: expr.h:31
model(System *s)
Definition: expr.h:83
cons operator+(const Expression &e)
Definition: expr.h:53
cons operator>(const Term &t)
Definition: expr.h:69
virtual void sub(double coef, Var *var=0)=0
Substact a factor from the constraint.
cons operator-=(const Expression &e)
Definition: expr.h:59
cstring name
Definition: odisasm.cpp:107
Constraint * operator&(void) const
Definition: expr.h:72
void free(void)
Definition: expr.h:74
Definition: Constraint.h:41
cons operator<=(const Expression &f)
Definition: expr.h:64
Encapsulation for ilp::Constraint pointers for {ilp} expr user-fiendly interface. ...
Definition: expr.h:46
cons operator-=(const Term &t)
Definition: expr.h:57
void add(coef_t coef, ilp::Var *var=0)
Add a term to the expression.
Definition: ilp_Expression.cpp:55
Integer (default) type for ILP variable.
Definition: Var.h:40
A variable is an identifier used for performing ILP computation.
Definition: Var.h:36
Encapsulation for ilp::System pointers for {ilp} expr user-fiendly interface.
Definition: expr.h:81
cons operator<(const Expression &f)
Definition: expr.h:66
An ILP system is a colletion of ILP constraint that may maximize or minimize some object function...
Definition: System.h:42
Encapsulation for ilp::Var pointers for {ilp} expr user-fiendly interface.
Definition: expr.h:29
Expression operator+(const Term &t1, const Term &t2)
Definition: expr.h:95
cons operator>(const Expression &f)
Definition: expr.h:70
cons operator()(void) const
Definition: expr.h:84
type_t
Type of an ILP variable.
Definition: Var.h:38
Definition: Constraint.h:39