main page
modules
namespaces
classes
files
Gecode home
Generated on Thu Feb 14 2013 20:59:33 for Gecode by
doxygen
1.8.3.1
gecode
int
gcc.cpp
Go to the documentation of this file.
1
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
/*
3
* Main authors:
4
* Patrick Pekczynski <pekczynski@ps.uni-sb.de>
5
* Guido Tack <tack@gecode.org>
6
*
7
* Contributing authors:
8
* Christian Schulte <schulte@gecode.org>
9
*
10
* Copyright:
11
* Patrick Pekczynski, 2004
12
* Christian Schulte, 2009
13
* Guido Tack, 2006
14
*
15
* Last modified:
16
* $Date: 2010-03-04 03:40:32 +1100 (Thu, 04 Mar 2010) $ by $Author: schulte $
17
* $Revision: 10365 $
18
*
19
* This file is part of Gecode, the generic constraint
20
* development environment:
21
* http://www.gecode.org
22
*
23
* Permission is hereby granted, free of charge, to any person obtaining
24
* a copy of this software and associated documentation files (the
25
* "Software"), to deal in the Software without restriction, including
26
* without limitation the rights to use, copy, modify, merge, publish,
27
* distribute, sublicense, and/or sell copies of the Software, and to
28
* permit persons to whom the Software is furnished to do so, subject to
29
* the following conditions:
30
*
31
* The above copyright notice and this permission notice shall be
32
* included in all copies or substantial portions of the Software.
33
*
34
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
37
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
38
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
39
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
40
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41
*
42
*/
43
44
#include <
gecode/int/gcc.hh
>
45
46
namespace
Gecode {
47
48
void
count
(
Home
home,
const
IntVarArgs
& x,
49
const
IntVarArgs
&
c
,
const
IntArgs
&
v
,
50
IntConLevel
icl) {
51
using namespace
Int;
52
if
(v.
size
() != c.
size
())
53
throw
ArgumentSizeMismatch
(
"Int::count"
);
54
if
(x.
same
(home))
55
throw
ArgumentSame
(
"Int::count"
);
56
if
(home.
failed
())
57
return
;
58
59
ViewArray<IntView>
xv(home, x);
60
ViewArray<GCC::CardView>
cv(home, c.
size
());
61
// set the cardinality
62
for
(
int
i
= v.
size
();
i
--; )
63
cv[
i
].init(c[
i
],v[i]);
64
switch
(icl) {
65
case
ICL_BND
:
66
GECODE_ES_FAIL
(
67
(
GCC::Bnd<GCC::CardView>::post
(home,xv,cv)));
68
break
;
69
case
ICL_DOM
:
70
GECODE_ES_FAIL
(
71
(
GCC::Dom<GCC::CardView>::post
(home,xv,cv)));
72
break
;
73
default
:
74
GECODE_ES_FAIL
(
75
(
GCC::Val<GCC::CardView>::post
(home,xv,cv)));
76
}
77
}
78
79
// domain is 0..|cards|- 1
80
void
count
(
Home
home,
const
IntVarArgs
& x,
const
IntVarArgs
&
c
,
81
IntConLevel
icl) {
82
IntArgs
values
(c.
size
());
83
for
(
int
i
= c.
size
();
i
--; )
84
values
[
i
] =
i
;
85
count
(home, x, c,
values
, icl);
86
}
87
88
// constant cards
89
void
count
(
Home
home,
const
IntVarArgs
& x,
90
const
IntSetArgs
&
c
,
const
IntArgs
&
v
,
91
IntConLevel
icl) {
92
using namespace
Int;
93
if
(v.
size
() != c.
size
())
94
throw
ArgumentSizeMismatch
(
"Int::count"
);
95
if
(x.
same
(home))
96
throw
ArgumentSame
(
"Int::count"
);
97
for
(
int
i
=c.
size
();
i
--; ) {
98
Limits::check
(v[
i
],
"Int::count"
);
99
Limits::check
(c[i].
min
(),
"Int::count"
);
100
Limits::check
(c[i].
max
(),
"Int::count"
);
101
}
102
103
if
(home.
failed
())
104
return
;
105
106
ViewArray<IntView>
xv(home, x);
107
108
for
(
int
i
= v.
size
();
i
--; ) {
109
if
(c[
i
].ranges() > 1) {
110
// Found hole, so create temporary variables
111
ViewArray<GCC::CardView>
cv(home, v.
size
());
112
for
(
int
j = v.
size
(); j--; )
113
cv[j].init(home,c[j],v[j]);
114
switch
(icl) {
115
case
ICL_BND
:
116
GECODE_ES_FAIL
(
117
(
GCC::Bnd<GCC::CardView>::post
(home, xv, cv)));
118
break
;
119
case
ICL_DOM
:
120
GECODE_ES_FAIL
(
121
(
GCC::Dom<GCC::CardView>::post
(home, xv, cv)));
122
break
;
123
default
:
124
GECODE_ES_FAIL
(
125
(
GCC::Val<GCC::CardView>::post
(home, xv, cv)));
126
}
127
return
;
128
}
129
}
130
131
// No holes: create CardConsts
132
ViewArray<GCC::CardConst>
cv(home, c.
size
());
133
134
for
(
int
i
= c.
size
();
i
--; )
135
cv[
i
].init(home,c[
i
].
min
(),c[
i
].max(),v[
i
]);
136
137
switch
(icl) {
138
case
ICL_BND
:
139
GECODE_ES_FAIL
(
140
(
GCC::Bnd<GCC::CardConst>::post
(home, xv, cv)));
141
break
;
142
case
ICL_DOM
:
143
GECODE_ES_FAIL
(
144
(
GCC::Dom<GCC::CardConst>::post
(home, xv, cv)));
145
break
;
146
default
:
147
GECODE_ES_FAIL
(
148
(
GCC::Val<GCC::CardConst>::post
(home, xv, cv)));
149
}
150
}
151
152
// domain is 0..|cards|- 1
153
void
count
(
Home
home,
const
IntVarArgs
& x,
const
IntSetArgs
&
c
,
154
IntConLevel
icl) {
155
IntArgs
values
(c.
size
());
156
for
(
int
i
= c.
size
();
i
--; )
157
values
[
i
] =
i
;
158
count
(home, x, c,
values
, icl);
159
}
160
161
void
count
(
Home
home,
const
IntVarArgs
& x,
162
const
IntSet
&
c
,
const
IntArgs
&
v
,
163
IntConLevel
icl) {
164
IntSetArgs
cards(v.
size
());
165
for
(
int
i
= v.
size
();
i
--; )
166
cards[
i
] = c;
167
count
(home, x, cards, v, icl);
168
}
169
170
}
171
172
// STATISTICS: int-post