main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Oct 22 2013 00:48:59 for Gecode by
doxygen
1.8.4
gecode
int
arithmetic.cpp
Go to the documentation of this file.
1
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
/*
3
* Main authors:
4
* Christian Schulte <schulte@gecode.org>
5
*
6
* Copyright:
7
* Christian Schulte, 2002
8
*
9
* Last modified:
10
* $Date: 2012-09-03 17:08:20 +0200 (Mon, 03 Sep 2012) $ by $Author: schulte $
11
* $Revision: 13038 $
12
*
13
* This file is part of Gecode, the generic constraint
14
* development environment:
15
* http://www.gecode.org
16
*
17
* Permission is hereby granted, free of charge, to any person obtaining
18
* a copy of this software and associated documentation files (the
19
* "Software"), to deal in the Software without restriction, including
20
* without limitation the rights to use, copy, modify, merge, publish,
21
* distribute, sublicense, and/or sell copies of the Software, and to
22
* permit persons to whom the Software is furnished to do so, subject to
23
* the following conditions:
24
*
25
* The above copyright notice and this permission notice shall be
26
* included in all copies or substantial portions of the Software.
27
*
28
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
*
36
*/
37
38
#include <
gecode/int/arithmetic.hh
>
39
40
namespace
Gecode {
41
42
void
43
abs
(
Home
home,
IntVar
x0,
IntVar
x1,
IntConLevel
icl) {
44
using namespace
Int;
45
if
(home.
failed
())
return
;
46
if
(icl ==
ICL_DOM
) {
47
GECODE_ES_FAIL
(
Arithmetic::AbsDom<IntView>::post
(home,x0,x1));
48
}
else
{
49
GECODE_ES_FAIL
(
Arithmetic::AbsBnd<IntView>::post
(home,x0,x1));
50
}
51
}
52
53
54
void
55
max
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
56
IntConLevel
icl) {
57
using namespace
Int;
58
if
(home.
failed
())
return
;
59
if
(icl ==
ICL_DOM
) {
60
GECODE_ES_FAIL
(
Arithmetic::MaxDom<IntView>::post
(home,x0,x1,x2));
61
}
else
{
62
GECODE_ES_FAIL
(
Arithmetic::MaxBnd<IntView>::post
(home,x0,x1,x2));
63
}
64
}
65
66
void
67
max
(
Home
home,
const
IntVarArgs
&
x
,
IntVar
y,
68
IntConLevel
icl) {
69
using namespace
Int;
70
if
(x.
size
() == 0)
71
throw
TooFewArguments
(
"Int::max"
);
72
if
(home.
failed
())
return
;
73
ViewArray<IntView>
xv(home,x);
74
if
(icl ==
ICL_DOM
) {
75
GECODE_ES_FAIL
(
Arithmetic::NaryMaxDom<IntView>::post
(home,xv,y));
76
}
else
{
77
GECODE_ES_FAIL
(
Arithmetic::NaryMaxBnd<IntView>::post
(home,xv,y));
78
}
79
}
80
81
void
82
min
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
83
IntConLevel
icl) {
84
using namespace
Int;
85
if
(home.
failed
())
return
;
86
MinusView
m0(x0);
MinusView
m1(x1);
MinusView
m2(x2);
87
if
(icl ==
ICL_DOM
) {
88
GECODE_ES_FAIL
(
Arithmetic::MaxDom<MinusView>::post
(home,m0,m1,m2));
89
}
else
{
90
GECODE_ES_FAIL
(
Arithmetic::MaxBnd<MinusView>::post
(home,m0,m1,m2));
91
}
92
}
93
94
void
95
min
(
Home
home,
const
IntVarArgs
&
x
,
IntVar
y,
96
IntConLevel
icl) {
97
using namespace
Int;
98
if
(x.
size
() == 0)
99
throw
TooFewArguments
(
"Int::min"
);
100
if
(home.
failed
())
return
;
101
ViewArray<MinusView>
m(home,x.
size
());
102
for
(
int
i
=x.
size
();
i
--; )
103
m[
i
] =
MinusView
(x[
i
]);
104
MinusView
my(y);
105
if
(icl ==
ICL_DOM
) {
106
GECODE_ES_FAIL
(
Arithmetic::NaryMaxDom<MinusView>::post
(home,m,my));
107
}
else
{
108
GECODE_ES_FAIL
(
Arithmetic::NaryMaxBnd<MinusView>::post
(home,m,my));
109
}
110
}
111
112
113
void
114
mult
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
115
IntConLevel
icl) {
116
using namespace
Int;
117
if
(home.
failed
())
return
;
118
if
(icl ==
ICL_DOM
) {
119
GECODE_ES_FAIL
(
Arithmetic::MultDom::post
(home,x0,x1,x2));
120
}
else
{
121
GECODE_ES_FAIL
(
Arithmetic::MultBnd::post
(home,x0,x1,x2));
122
}
123
}
124
125
126
void
127
divmod
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
IntVar
x3,
128
IntConLevel
) {
129
using namespace
Int;
130
if
(home.
failed
())
return
;
131
132
IntVar
prod(home,
Int::Limits::min
,
Int::Limits::max
);
133
GECODE_ES_FAIL
(
Arithmetic::MultBnd::post
(home,x1,x2,prod));
134
Linear::Term<IntView>
t
[3];
135
t
[0].a = 1;
t
[0].x = prod;
136
t
[1].a = 1;
t
[1].x = x3;
137
int
min
,
max
;
138
Linear::estimate
(
t
,2,0,
min
,
max
);
139
IntView
x0v(x0);
140
GECODE_ME_FAIL
(x0v.
gq
(home,
min
));
141
GECODE_ME_FAIL
(x0v.
lq
(home,
max
));
142
t
[2].a=-1;
t
[2].x=x0;
143
Linear::post
(home,
t
,3,
IRT_EQ
,0);
144
if
(home.
failed
())
return
;
145
IntView
x1v(x1);
146
GECODE_ES_FAIL
(
147
Arithmetic::DivMod<IntView>::post
(home,x0,x1,x3));
148
}
149
150
void
151
div
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
152
IntConLevel
) {
153
using namespace
Int;
154
if
(home.
failed
())
return
;
155
GECODE_ES_FAIL
(
156
(
Arithmetic::DivBnd<IntView>::post
(home,x0,x1,x2)));
157
}
158
159
void
160
mod
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
161
IntConLevel
icl) {
162
using namespace
Int;
163
if
(home.
failed
())
return
;
164
IntVar
_div(home,
Int::Limits::min
,
Int::Limits::max
);
165
divmod
(home, x0, x1, _div, x2, icl);
166
}
167
168
void
169
sqr
(
Home
home,
IntVar
x0,
IntVar
x1,
IntConLevel
icl) {
170
using namespace
Int;
171
if
(home.
failed
())
return
;
172
Arithmetic::SqrOps
ops;
173
if
(icl ==
ICL_DOM
) {
174
GECODE_ES_FAIL
(
Arithmetic::PowDom<Arithmetic::SqrOps>
175
::
post
(home,x0,x1,ops));
176
}
else
{
177
GECODE_ES_FAIL
(
Arithmetic::PowBnd<Arithmetic::SqrOps>
178
::
post
(home,x0,x1,ops));
179
}
180
}
181
182
void
183
sqrt
(
Home
home,
IntVar
x0,
IntVar
x1,
IntConLevel
icl) {
184
using namespace
Int;
185
if
(home.
failed
())
return
;
186
Arithmetic::SqrOps
ops;
187
if
(icl ==
ICL_DOM
) {
188
GECODE_ES_FAIL
(
Arithmetic::NrootDom<Arithmetic::SqrOps>
189
::
post
(home,x0,x1,ops));
190
}
else
{
191
GECODE_ES_FAIL
(
Arithmetic::NrootBnd<Arithmetic::SqrOps>
192
::
post
(home,x0,x1,ops));
193
}
194
}
195
196
void
197
pow
(
Home
home,
IntVar
x0,
int
n
,
IntVar
x1,
IntConLevel
icl) {
198
using namespace
Int;
199
Limits::nonnegative
(n,
"Int::pow"
);
200
if
(home.
failed
())
return
;
201
if
(n == 2) {
202
sqr
(home, x0, x1, icl);
203
return
;
204
}
205
Arithmetic::PowOps
ops(n);
206
if
(icl ==
ICL_DOM
) {
207
GECODE_ES_FAIL
(
Arithmetic::PowDom<Arithmetic::PowOps>
208
::
post
(home,x0,x1,ops));
209
}
else
{
210
GECODE_ES_FAIL
(
Arithmetic::PowBnd<Arithmetic::PowOps>
211
::
post
(home,x0,x1,ops));
212
}
213
}
214
215
void
216
nroot
(
Home
home,
IntVar
x0,
int
n
,
IntVar
x1,
IntConLevel
icl) {
217
using namespace
Int;
218
Limits::positive
(n,
"Int::nroot"
);
219
if
(home.
failed
())
return
;
220
if
(n == 2) {
221
sqrt
(home, x0, x1, icl);
222
return
;
223
}
224
Arithmetic::PowOps
ops(n);
225
if
(icl ==
ICL_DOM
) {
226
GECODE_ES_FAIL
(
Arithmetic::NrootDom<Arithmetic::PowOps>
227
::
post
(home,x0,x1,ops));
228
}
else
{
229
GECODE_ES_FAIL
(
Arithmetic::NrootBnd<Arithmetic::PowOps>
230
::
post
(home,x0,x1,ops));
231
}
232
}
233
234
}
235
236
// STATISTICS: int-post