1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  *  Main authors:
4  *     Christian Schulte <schulte@gecode.org>
5  *     Matthias Balzer <matthias.balzer@itwm.fraunhofer.de>
6  *
7  *  Copyright:
8  *     Christian Schulte, 2005
9  *     Fraunhofer ITWM, 2017
10  *
11  *  This file is part of Gecode, the generic constraint
12  *  development environment:
13  *     http://www.gecode.org
14  *
15  *  Permission is hereby granted, free of charge, to any person obtaining
16  *  a copy of this software and associated documentation files (the
17  *  "Software"), to deal in the Software without restriction, including
18  *  without limitation the rights to use, copy, modify, merge, publish,
19  *  distribute, sublicense, and/or sell copies of the Software, and to
20  *  permit persons to whom the Software is furnished to do so, subject to
21  *  the following conditions:
22  *
23  *  The above copyright notice and this permission notice shall be
24  *  included in all copies or substantial portions of the Software.
25  *
26  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34  */
35 
36 #include <gecode/minimodel.hh>
37 
38 #ifdef GECODE_HAS_SET_VARS
39 
40 namespace Gecode {
41 
42   /*
43    * Operators
44    *
45    */
46   SetRel
operator ==(const SetExpr & e0,const SetExpr & e1)47   operator ==(const SetExpr& e0, const SetExpr& e1) {
48     return SetRel(e0, SRT_EQ, e1);
49   }
50   SetRel
operator !=(const SetExpr & e0,const SetExpr & e1)51   operator !=(const SetExpr& e0, const SetExpr& e1) {
52     return SetRel(e0, SRT_NQ, e1);
53   }
54   SetCmpRel
operator <=(const SetExpr & e0,const SetExpr & e1)55   operator <=(const SetExpr& e0, const SetExpr& e1) {
56     return SetCmpRel(e0, SRT_SUB, e1);
57   }
58   BoolExpr
operator <=(const SetCmpRel & r,const SetExpr & l)59   operator <=(const SetCmpRel& r, const SetExpr& l) {
60     return BoolExpr(r) && BoolExpr(r.r <= l);
61   }
62   SetCmpRel
operator >=(const SetExpr & e0,const SetExpr & e1)63   operator >=(const SetExpr& e0, const SetExpr& e1) {
64     return SetCmpRel(e0, SRT_SUP, e1);
65   }
66   BoolExpr
operator >=(const SetCmpRel & r,const SetExpr & l)67   operator >=(const SetCmpRel& r, const SetExpr& l) {
68     return BoolExpr(r) && BoolExpr(r.r >= l);
69   }
70   SetRel
operator ||(const SetExpr & e0,const SetExpr & e1)71   operator ||(const SetExpr& e0, const SetExpr& e1) {
72     return SetRel(e0, SRT_DISJ, e1);
73   }
74 
75   namespace {
76 
77     /// Boolean expression for IRT relations with SetExpr
78     class SetIRTRel : public BoolExpr::Misc {
79       /// The set expression
80       SetExpr _s;
81       /// The integer expression
82       LinIntExpr _x;
83       /// The integer relation type
84       IntRelType _irt;
85     public:
86       /// Constructor
87       SetIRTRel(const SetExpr&, IntRelType, const LinIntExpr&);
88       /// Constrain \a b to be equivalent to the expression (negated if \a neg)
89       virtual void post(Home, BoolVar b, bool neg,
90                         const IntPropLevels&) override;
91     };
92 
SetIRTRel(const SetExpr & s,IntRelType irt,const LinIntExpr & x)93     SetIRTRel::SetIRTRel(const SetExpr& s, IntRelType irt, const LinIntExpr& x)
94       : _s(s), _x(x), _irt(irt) {}
95 
96     void
post(Home home,BoolVar b,bool neg,const IntPropLevels & ipls)97     SetIRTRel::post(Home home, BoolVar b, bool neg,
98                     const IntPropLevels& ipls) {
99       if (b.zero()) {
100         rel(home, _s.post(home), neg ? _irt : Gecode::neg(_irt),
101             _x.post(home, ipls));
102       } else if (b.one()) {
103         rel(home, _s.post(home), neg ? Gecode::neg(_irt) : _irt,
104             _x.post(home, ipls));
105       } else {
106         rel(home, _s.post(home), neg ? Gecode::neg(_irt) : _irt,
107             _x.post(home, ipls), b);
108       }
109     }
110   }
111 
112 
113   /*
114    * IRT relations with SetExpr
115    *
116    */
117   BoolExpr
operator ==(const SetExpr & x,const LinIntExpr & y)118   operator ==(const SetExpr& x, const LinIntExpr& y) {
119     return BoolExpr(new SetIRTRel(x, IRT_EQ, y));
120   }
121   BoolExpr
operator ==(const LinIntExpr & x,const SetExpr & y)122   operator ==(const LinIntExpr& x, const SetExpr& y) {
123     return operator ==(y, x);
124   }
125 
126   BoolExpr
operator !=(const SetExpr & x,const LinIntExpr & y)127   operator !=(const SetExpr& x, const LinIntExpr& y) {
128     return BoolExpr(new SetIRTRel(x, IRT_NQ, y));
129   }
130 
131   BoolExpr
operator !=(const LinIntExpr & x,const SetExpr & y)132   operator !=(const LinIntExpr& x, const SetExpr& y) {
133     return operator !=(y, x);
134   }
135 
136   BoolExpr
operator <=(const SetExpr & x,const LinIntExpr & y)137   operator <=(const SetExpr& x, const LinIntExpr& y) {
138     return BoolExpr(new SetIRTRel(x, IRT_LQ, y));
139   }
140 
141   BoolExpr
operator <=(const LinIntExpr & x,const SetExpr & y)142   operator <=(const LinIntExpr& x, const SetExpr& y) {
143     return operator >=(y, x);
144   }
145 
146   BoolExpr
operator <(const SetExpr & x,const LinIntExpr & y)147   operator <(const SetExpr& x, const LinIntExpr& y) {
148     return BoolExpr(new SetIRTRel(x, IRT_LE, y));
149   }
150 
151   BoolExpr
operator <(const LinIntExpr & x,const SetExpr & y)152   operator <(const LinIntExpr& x, const SetExpr& y) {
153     return operator >(y, x);
154   }
155 
156   BoolExpr
operator >=(const SetExpr & x,const LinIntExpr & y)157   operator >=(const SetExpr& x, const LinIntExpr& y) {
158     return BoolExpr(new SetIRTRel(x, IRT_GQ, y));
159   }
160 
161   BoolExpr
operator >=(const LinIntExpr & x,const SetExpr & y)162   operator >=(const LinIntExpr& x, const SetExpr& y) {
163     return operator <=(y, x);
164   }
165 
166   BoolExpr
operator >(const SetExpr & x,const LinIntExpr & y)167   operator >(const SetExpr& x, const LinIntExpr& y) {
168     return BoolExpr(new SetIRTRel(x, IRT_GR, y));
169   }
170 
171   BoolExpr
operator >(const LinIntExpr & x,const SetExpr & y)172   operator >(const LinIntExpr& x, const SetExpr& y) {
173     return operator <(y, x);
174   }
175 
176 }
177 
178 #endif
179 
180 // STATISTICS: minimodel-any
181