main page
modules
namespaces
classes
files
Gecode home
Generated on Sat Aug 25 2012 03:32:44 for Gecode by
doxygen
1.8.1.2
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: 2010-06-04 04:34:20 +1000 (Fri, 04 Jun 2010) $ by $Author: schulte $
11
* $Revision: 11017 $
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
using namespace
Int;
43
44
void
45
abs
(
Home
home,
IntVar
x0,
IntVar
x1,
IntConLevel
icl) {
46
if
(home.
failed
())
return
;
47
if
(icl ==
ICL_DOM
) {
48
GECODE_ES_FAIL
(
Arithmetic::AbsDom<IntView>::post
(home,x0,x1));
49
}
else
{
50
GECODE_ES_FAIL
(
Arithmetic::AbsBnd<IntView>::post
(home,x0,x1));
51
}
52
}
53
54
55
void
56
max
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
57
IntConLevel
icl) {
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
if
(x.
size
() == 0)
70
throw
TooFewArguments
(
"Int::max"
);
71
if
(home.
failed
())
return
;
72
ViewArray<IntView>
xv(home,x);
73
if
(icl ==
ICL_DOM
) {
74
GECODE_ES_FAIL
(
Arithmetic::NaryMaxDom<IntView>::post
(home,xv,y));
75
}
else
{
76
GECODE_ES_FAIL
(
Arithmetic::NaryMaxBnd<IntView>::post
(home,xv,y));
77
}
78
}
79
80
81
void
82
min
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
83
IntConLevel
icl) {
84
if
(home.
failed
())
return
;
85
MinusView
m0(x0);
MinusView
m1(x1);
MinusView
m2(x2);
86
if
(icl ==
ICL_DOM
) {
87
GECODE_ES_FAIL
(
Arithmetic::MaxDom<MinusView>::post
(home,m0,m1,m2));
88
}
else
{
89
GECODE_ES_FAIL
(
Arithmetic::MaxBnd<MinusView>::post
(home,m0,m1,m2));
90
}
91
}
92
93
void
94
min
(
Home
home,
const
IntVarArgs
& x,
IntVar
y,
95
IntConLevel
icl) {
96
if
(x.
size
() == 0)
97
throw
TooFewArguments
(
"Int::min"
);
98
if
(home.
failed
())
return
;
99
ViewArray<MinusView>
m
(home,x.
size
());
100
for
(
int
i
=x.
size
();
i
--; )
101
m
[
i
] =
MinusView
(x[
i
]);
102
MinusView
my(y);
103
if
(icl ==
ICL_DOM
) {
104
GECODE_ES_FAIL
(
Arithmetic::NaryMaxDom<MinusView>::post
(home,
m
,my));
105
}
else
{
106
GECODE_ES_FAIL
(
Arithmetic::NaryMaxBnd<MinusView>::post
(home,
m
,my));
107
}
108
}
109
110
111
void
112
mult
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
113
IntConLevel
icl) {
114
if
(home.
failed
())
return
;
115
if
(icl ==
ICL_DOM
) {
116
GECODE_ES_FAIL
(
Arithmetic::MultDom<IntView>::post
(home,x0,x1,x2));
117
}
else
{
118
GECODE_ES_FAIL
(
Arithmetic::MultBnd<IntView>::post
(home,x0,x1,x2));
119
}
120
}
121
122
123
void
124
sqr
(
Home
home,
IntVar
x0,
IntVar
x1,
IntConLevel
icl) {
125
if
(home.
failed
())
return
;
126
if
(icl ==
ICL_DOM
) {
127
GECODE_ES_FAIL
(
Arithmetic::SqrDom<IntView>::post
(home,x0,x1));
128
}
else
{
129
GECODE_ES_FAIL
(
Arithmetic::SqrBnd<IntView>::post
(home,x0,x1));
130
}
131
}
132
133
void
134
sqrt
(
Home
home,
IntVar
x0,
IntVar
x1,
IntConLevel
icl) {
135
if
(home.
failed
())
return
;
136
if
(icl ==
ICL_DOM
) {
137
GECODE_ES_FAIL
(
Arithmetic::SqrtDom<IntView>::post
(home,x0,x1));
138
}
else
{
139
GECODE_ES_FAIL
(
Arithmetic::SqrtBnd<IntView>::post
(home,x0,x1));
140
}
141
}
142
143
void
144
divmod
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
IntVar
x3,
145
IntConLevel
) {
146
if
(home.
failed
())
return
;
147
148
IntVar
prod(home,
Int::Limits::min
,
Int::Limits::max
);
149
GECODE_ES_FAIL
(
150
Arithmetic::MultBnd<IntView>::post
(home,x1,x2,prod));
151
Linear::Term<IntView>
t[3];
152
t[0].
a
= 1; t[0].
x
= prod;
153
t[1].
a
= 1; t[1].
x
= x3;
154
int
min
,
max
;
155
Linear::estimate
(t,2,0,min,max);
156
IntView
x0v(x0);
157
GECODE_ME_FAIL
( x0v.
gq
(home,min));
158
GECODE_ME_FAIL
( x0v.
lq
(home,max));
159
t[2].
a
=-1; t[2].
x
=x0;
160
Linear::post
(home,t,3,
IRT_EQ
,0);
161
if
(home.
failed
())
return
;
162
IntView
x1v(x1);
163
GECODE_ES_FAIL
(
164
Arithmetic::DivMod<IntView>::post
(home,x0,x1,x3));
165
}
166
167
void
168
div
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
169
IntConLevel
) {
170
if
(home.
failed
())
return
;
171
GECODE_ES_FAIL
(
172
(
Arithmetic::DivBnd<IntView>::post
(home,x0,x1,x2)));
173
}
174
175
void
176
mod
(
Home
home,
IntVar
x0,
IntVar
x1,
IntVar
x2,
177
IntConLevel
icl) {
178
if
(home.
failed
())
return
;
179
IntVar
_div(home,
Int::Limits::min
,
Int::Limits::max
);
180
divmod
(home, x0, x1, _div, x2, icl);
181
}
182
183
}
184
185
// STATISTICS: int-post