main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Oct 22 2013 00:49:06 for Gecode by
doxygen
1.8.4
gecode
kernel
brancher-view-val.hpp
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, 2012
8
*
9
* Last modified:
10
* $Date: 2013-07-08 17:25:13 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11
* $Revision: 13825 $
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
namespace
Gecode {
39
48
template
<
class
Val>
50
class
GECODE_VTABLE_EXPORT
PosValChoice
:
public
PosChoice
{
51
private
:
53
const
Val _val;
54
public
:
56
PosValChoice
(
const
Brancher
&
b
,
unsigned
int
a
,
const
Pos
&
p
,
const
Val&
n
);
58
const
Val& val(
void
)
const
;
60
virtual
size_t
size
(
void
)
const
;
62
virtual
void
archive(
Archive
& e)
const
;
63
};
64
65
67
template
<
class
View,
class
Val, PropCond pc>
68
class
ViewValNGL
:
public
NGL
{
69
protected
:
71
View
x
;
73
Val
n
;
74
public
:
76
ViewValNGL
(
Space
& home, View
x
, Val
n
);
78
ViewValNGL
(
Space
& home,
bool
share,
ViewValNGL
& ngl);
80
virtual
void
subscribe
(
Space
& home,
Propagator
&
p
);
82
virtual
void
cancel
(
Space
& home,
Propagator
&
p
);
84
virtual
size_t
dispose
(
Space
& home);
85
};
86
94
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
95
class
ViewValBrancher
:
public
ViewBrancher
<View,n> {
96
typedef
typename
ViewBrancher<View,n>::BranchFilter
BranchFilter;
97
protected
:
98
using
ViewBrancher<View,n>::vs
;
99
using
ViewBrancher<View,n>::x
;
101
ValSelCommitBase<View,Val>
*
vsc
;
103
typedef
void (*
VarValPrint
)(
const
Space
& home,
const
BrancherHandle
&
bh
,
104
unsigned
int
b
,
105
typename
View::VarType
x
,
int
i
,
106
const
Val& m,
107
std::ostream& o);
109
VarValPrint
vvp
;
111
ViewValBrancher
(
Space
& home,
bool
share,
ViewValBrancher
&
b
);
113
ViewValBrancher
(
Home
home,
114
ViewArray<View>
&
x
,
115
ViewSel<View>
*
vs
[
n
],
116
ValSelCommitBase<View,Val>
*
vsc
,
117
BranchFilter
bf
,
118
VarValPrint
vvp
);
119
public
:
121
virtual
const
Choice
*
choice
(
Space
& home);
123
virtual
const
Choice
*
choice
(
const
Space
& home,
Archive
& e);
125
virtual
ExecStatus
commit
(
Space
& home,
const
Choice
&
c
,
unsigned
int
b
);
127
virtual
NGL
*
ngl
(
Space
& home,
const
Choice
&
c
,
unsigned
int
b
)
const
;
135
virtual
void
print
(
const
Space
& home,
const
Choice
&
c
,
unsigned
int
b
,
136
std::ostream& o)
const
;
138
virtual
Actor
*
copy
(
Space
& home,
bool
share);
140
virtual
size_t
dispose
(
Space
& home);
142
static
BrancherHandle
post
(
Home
home,
143
ViewArray<View>
&
x
,
144
ViewSel<View>
*
vs
[
n
],
145
ValSelCommitBase<View,Val>
*
vsc
,
146
BranchFilter
bf
,
147
VarValPrint
vvp
);
148
};
150
151
/*
152
* %Choice with position and value
153
*
154
*/
155
template
<
class
Val>
156
forceinline
157
PosValChoice<Val>::PosValChoice
(
const
Brancher
&
b
,
unsigned
int
a
,
158
const
Pos
&
p
,
const
Val&
n
)
159
:
PosChoice
(b,a,p), _val(n) {}
160
161
template
<
class
Val>
162
forceinline
const
Val&
163
PosValChoice<Val>::val
(
void
)
const
{
164
return
_val;
165
}
166
167
template
<
class
Val>
168
forceinline
size_t
169
PosValChoice<Val>::size
(
void
)
const
{
170
return
sizeof
(
PosValChoice<Val>
);
171
}
172
173
template
<
class
Val>
174
forceinline
void
175
PosValChoice<Val>::archive
(
Archive
& e)
const
{
176
PosChoice::archive
(e);
177
e << _val;
178
}
179
180
181
/*
182
* View-value no-good literal
183
*
184
*/
185
template
<
class
View,
class
Val, PropCond pc>
186
forceinline
187
ViewValNGL<View,Val,pc>::ViewValNGL
(
Space
& home, View x0, Val n0)
188
:
NGL
(home),
x
(x0),
n
(n0) {}
189
190
template
<
class
View,
class
Val, PropCond pc>
191
forceinline
192
ViewValNGL<View,Val,pc>::ViewValNGL
(
Space
& home,
bool
share,
ViewValNGL
& ngl)
193
:
NGL
(home,share,ngl),
n
(ngl.
n
) {
194
x
.update(home,share,ngl.
x
);
195
}
196
197
template
<
class
View,
class
Val, PropCond pc>
198
void
199
ViewValNGL<View,Val,pc>::subscribe
(
Space
& home,
Propagator
&
p
) {
200
x
.subscribe(home,p,pc);
201
}
202
203
template
<
class
View,
class
Val, PropCond pc>
204
void
205
ViewValNGL<View,Val,pc>::cancel
(
Space
& home,
Propagator
&
p
) {
206
x
.cancel(home,p,pc);
207
}
208
209
template
<
class
View,
class
Val, PropCond pc>
210
size_t
211
ViewValNGL<View,Val,pc>::dispose
(
Space
& home) {
212
(void)
NGL::dispose
(home);
213
return
sizeof
(*this);
214
}
215
216
217
218
/*
219
* Generic brancher based on variable/value selection
220
*
221
*/
222
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
223
forceinline
224
ViewValBrancher<View,n,Val,a>::
225
ViewValBrancher
(
Home
home,
226
ViewArray<View>
&
x
,
227
ViewSel<View>
* vs[
n
],
228
ValSelCommitBase<View,Val>
* vsc0,
229
BranchFilter bf,
230
VarValPrint vvp0)
231
:
ViewBrancher
<View,n>(home,x,vs,bf), vsc(vsc0), vvp(vvp0) {
232
if
(
vsc
->
notice
())
233
home.
notice
(*
this
,
AP_DISPOSE
,
true
);
234
}
235
236
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
237
forceinline
BrancherHandle
238
ViewValBrancher<View,n,Val,a>::
239
post
(
Home
home,
ViewArray<View>
&
x
,
240
ViewSel<View>
* vs[
n
],
ValSelCommitBase<View,Val>
* vsc,
241
BranchFilter bf,
242
VarValPrint vvp) {
243
return
*
new
(home)
ViewValBrancher<View,n,Val,a>
(home,x,vs,vsc,bf,vvp);
244
}
245
246
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
247
forceinline
248
ViewValBrancher<View,n,Val,a>::
249
ViewValBrancher
(
Space
& home,
bool
shared
,
ViewValBrancher<View,n,Val,a>
&
b
)
250
:
ViewBrancher
<View,
n
>(home,shared,b),
251
vsc(b.vsc->copy(home,shared)), vvp(b.vvp) {}
252
253
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
254
Actor
*
255
ViewValBrancher<View,n,Val,a>::copy
(
Space
& home,
bool
shared
) {
256
return
new
(home)
ViewValBrancher<View,n,Val,a>
(home,shared,*
this
);
257
}
258
259
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
260
const
Choice
*
261
ViewValBrancher<View,n,Val,a>::choice
(
Space
& home) {
262
Pos
p
=
ViewBrancher<View,n>::pos
(home);
263
View
v
=
ViewBrancher<View,n>::view
(p);
264
return
new
PosValChoice<Val>
(*
this
,
a
,
p
,vsc->val(home,v,p.
pos
));
265
}
266
267
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
268
const
Choice
*
269
ViewValBrancher<View,n,Val,a>::choice
(
const
Space
& home,
Archive
& e) {
270
(void) home;
271
int
p
; e >>
p
;
272
Val
v
; e >>
v
;
273
return
new
PosValChoice<Val>
(*
this
,
a
,
p
,
v
);
274
}
275
276
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
277
ExecStatus
278
ViewValBrancher<View,n,Val,a>
279
::commit
(
Space
& home,
const
Choice
&
c
,
unsigned
int
b
) {
280
const
PosValChoice<Val>
& pvc
281
=
static_cast<
const
PosValChoice<Val>
&
>
(
c
);
282
return
me_failed
(vsc->commit(home,b,
283
ViewBrancher<View,n>::view
(pvc.
pos
()),
284
pvc.
pos
().
pos
,
285
pvc.
val
()))
286
?
ES_FAILED
:
ES_OK
;
287
}
288
289
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
290
NGL
*
291
ViewValBrancher<View,n,Val,a>
292
::ngl
(
Space
& home,
const
Choice
&
c
,
unsigned
int
b
)
const
{
293
const
PosValChoice<Val>
& pvc
294
=
static_cast<
const
PosValChoice<Val>
&
>
(
c
);
295
return
vsc->ngl(home,b,
296
ViewBrancher<View,n>::view
(pvc.
pos
()),pvc.
val
());
297
}
298
299
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
300
void
301
ViewValBrancher<View,n,Val,a>
302
::print
(
const
Space
& home,
const
Choice
&
c
,
unsigned
int
b
,
303
std::ostream& o)
const
{
304
const
PosValChoice<Val>
& pvc
305
=
static_cast<
const
PosValChoice<Val>
&
>
(
c
);
306
View xi =
ViewBrancher<View,n>::view
(pvc.
pos
());
307
typename
View::VarType y(
ViewBrancher<View,n>::view
(pvc.
pos
()).varimp());
308
if
(vvp != NULL)
309
vvp(home,*
this
,b,y,pvc.
pos
().
pos
,pvc.
val
(),o);
310
else
311
vsc->print(home,b,xi,pvc.
pos
().
pos
,pvc.
val
(),o);
312
}
313
314
template
<
class
View,
int
n,
class
Val,
unsigned
int
a>
315
forceinline
size_t
316
ViewValBrancher<View,n,Val,a>::dispose
(
Space
& home) {
317
if
(vsc->notice())
318
home.
ignore
(*
this
,
AP_DISPOSE
,
true
);
319
vsc->dispose(home);
320
(void)
ViewBrancher<View,n>::dispose
(home);
321
return
sizeof
(
ViewValBrancher<View,n,Val,a>
);
322
}
323
324
}
325
326
// STATISTICS: kernel-branch