main page
modules
namespaces
classes
files
Gecode home
Generated on Sat May 25 2013 18:00:36 for Gecode by
doxygen
1.8.3.1
gecode
int
branch
view-values.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, 2008
8
*
9
* Last modified:
10
* $Date: 2013-02-25 21:43:24 +0100 (Mon, 25 Feb 2013) $ by $Author: schulte $
11
* $Revision: 13406 $
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 {
namespace
Int {
namespace
Branch {
39
41
class
PosValuesChoice
:
public
PosChoice
{
42
private
:
44
class
PosMin {
45
public
:
47
unsigned
int
pos
;
49
int
min
;
50
};
52
unsigned
int
n;
54
PosMin* pm;
55
public
:
57
GECODE_INT_EXPORT
58
PosValuesChoice
(
const
Brancher
&
b
,
const
Pos
&
p
,
IntView
x
);
60
GECODE_INT_EXPORT
61
PosValuesChoice
(
const
Brancher
& b,
unsigned
int
alt,
Pos
p,
Archive
& e);
63
int
val
(
unsigned
int
a
)
const
;
65
GECODE_INT_EXPORT
66
virtual
size_t
size
(
void
)
const
;
68
GECODE_INT_EXPORT
69
virtual
~PosValuesChoice
(
void
);
71
GECODE_INT_EXPORT
72
virtual
void
archive
(
Archive
& e)
const
;
73
};
74
75
forceinline
int
76
PosValuesChoice::val
(
unsigned
int
a
)
const
{
77
PosMin*
l
= &pm[0];
78
PosMin*
r
= &pm[n-1];
79
while
(
true
) {
80
PosMin* m = l + (r-
l
)/2;
81
if
(a < m->
pos
) {
82
r=m-1;
83
}
else
if
(a >= (m+1)->pos) {
84
l=m+1;
85
}
else
{
86
return
m->min +
static_cast<
int
>
(a - m->pos);
87
}
88
}
89
GECODE_NEVER
;
90
return
0;
91
}
92
93
94
template
<
int
n,
bool
min>
95
forceinline
96
ViewValuesBrancher<n,min>::
97
ViewValuesBrancher
(
Home
home,
ViewArray<IntView>
&
x
,
98
ViewSel<IntView>
* vs[
n
], BranchFilter bf)
99
:
ViewBrancher
<
IntView
,n>(home,x,vs,bf) {}
100
101
template
<
int
n,
bool
min>
102
BrancherHandle
103
ViewValuesBrancher<n,min>::post
(
Home
home,
ViewArray<IntView>
&
x
,
104
ViewSel<IntView>
* vs[
n
],
105
BranchFilter bf) {
106
return
*
new
(home)
ViewValuesBrancher<n,min>
(home,x,vs,bf);
107
}
108
109
template
<
int
n,
bool
min>
110
forceinline
111
ViewValuesBrancher<n,min>::
112
ViewValuesBrancher
(
Space
& home,
bool
shared
,
ViewValuesBrancher
&
b
)
113
:
ViewBrancher
<
IntView
,
n
>(home,shared,b) {}
114
115
template
<
int
n,
bool
min>
116
Actor
*
117
ViewValuesBrancher<n,min>::copy
(
Space
& home,
bool
shared
) {
118
return
new
(home)
ViewValuesBrancher<n,min>
(home,shared,*
this
);
119
}
120
121
template
<
int
n,
bool
min>
122
const
Choice
*
123
ViewValuesBrancher<n,min>::choice
(
Space
& home) {
124
Pos
p
=
ViewBrancher<IntView,n>::pos
(home);
125
return
new
PosValuesChoice
(*
this
,p,
126
ViewBrancher<IntView,n>::view
(p));
127
}
128
129
template
<
int
n,
bool
min>
130
const
Choice
*
131
ViewValuesBrancher<n,min>::choice
(
const
Space
& home,
Archive
& e) {
132
(void) home;
133
int
p
;
134
unsigned
int
a
;
135
e >> p >>
a
;
136
return
new
PosValuesChoice
(*
this
,a,p,e);
137
}
138
139
template
<
int
n,
bool
min>
140
ExecStatus
141
ViewValuesBrancher<n,min>::commit
(
Space
& home,
const
Choice
&
c
,
142
unsigned
int
a
) {
143
const
PosValuesChoice
& pvc
144
=
static_cast<
const
PosValuesChoice
&
>
(
c
);
145
IntView
x
(
ViewBrancher<IntView,n>::view
(pvc.
pos
()));
146
unsigned
int
b
=
min
? a : (pvc.
alternatives
() - 1 -
a
);
147
return
me_failed
(x.
eq
(home,pvc.
val
(b))) ?
ES_FAILED
:
ES_OK
;
148
}
149
150
}}}
151
152
// STATISTICS: int-branch