this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main author:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2012
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode {
35
36 /**
37 * \defgroup TaskBranchValSelCommit Generic value selection and value commit for brancher based on view and value selection
38 *
39 * \ingroup TaskBranchViewVal
40 */
41 //@{
42 /// Base class for value selection and commit
43 template<class View_, class Val_>
44 class ValSelCommitBase {
45 public:
46 /// View type
47 typedef View_ View;
48 /// Corresponding variable type
49 typedef typename View::VarType Var;
50 /// Value type
51 typedef Val_ Val;
52 public:
53 /// Constructor for initialization
54 ValSelCommitBase(Space& home, const ValBranch<Var>& vb);
55 /// Constructor for cloning
56 ValSelCommitBase(Space& home, ValSelCommitBase<View,Val>& vsc);
57 /// Return value of view \a x at position \a i
58 virtual Val val(const Space& home, View x, int i) = 0;
59 /// Commit view \a x at position \a i to value \a n for alternative \a a
60 virtual ModEvent commit(Space& home, unsigned int a,
61 View x, int i, Val n) = 0;
62 /// Create no-good literal for choice \a c and alternative \a a
63 virtual NGL* ngl(Space& home, unsigned int a,
64 View x, Val n) const = 0;
65 /// Print on \a o branch for alternative \a a, view \a x at position \a i, and value \a n
66 virtual void print(const Space& home, unsigned int a,
67 View x, int i, const Val& n,
68 std::ostream& o) const = 0;
69 /// Perform cloning
70 virtual ValSelCommitBase<View,Val>* copy(Space& home) = 0;
71 /// Whether dispose must always be called (that is, notice is needed)
72 virtual bool notice(void) const = 0;
73 /// Delete value selection
74 virtual void dispose(Space& home) = 0;
75 /// Unused destructor
76 virtual ~ValSelCommitBase(void);
77 /// \name Memory management
78 //@{
79 /// Allocate memory from space
80 static void* operator new(size_t s, Space& home);
81 /// Return memory to space
82 static void operator delete(void* p, Space& home);
83 /// Needed for exceptions
84 static void operator delete(void* p);
85 //@}
86 };
87
88 /// Class for value selection and commit
89 template<class ValSel, class ValCommit>
90 class ValSelCommit
91 : public ValSelCommitBase<typename ValSel::View,typename ValSel::Val> {
92 protected:
93 typedef typename ValSelCommitBase<typename ValSel::View,
94 typename ValSel::Val>::Var Var;
95 typedef typename ValSelCommitBase<typename ValSel::View,
96 typename ValSel::Val>::Val Val;
97 typedef typename ValSelCommitBase<typename ValSel::View,
98 typename ValSel::Val>::View View;
99 /// The value selection object used
100 ValSel s;
101 /// The commit object used
102 ValCommit c;
103 public:
104 /// Constructor for initialization
105 ValSelCommit(Space& home, const ValBranch<Var>& vb);
106 /// Constructor for cloning
107 ValSelCommit(Space& home, ValSelCommit<ValSel,ValCommit>& vsc);
108 /// Return value of view \a x at position \a i
109 virtual Val val(const Space& home, View x, int i);
110 /// Commit view \a x at position \a i to value \a n for alternative \a a
111 virtual ModEvent commit(Space& home, unsigned int a, View x, int i, Val n);
112 /// Create no-good literal for choice \a c and alternative \a a
113 virtual NGL* ngl(Space& home, unsigned int a,
114 View x, Val n) const;
115 /// Print on \a o branch for alternative \a a, view \a x at position \a i, and value \a n
116 virtual void print(const Space& home, unsigned int a,
117 View x, int i, const Val& n,
118 std::ostream& o) const;
119 /// Perform cloning
120 virtual ValSelCommit<ValSel,ValCommit>* copy(Space& home);
121 /// Whether dispose must always be called (that is, notice is needed)
122 virtual bool notice(void) const;
123 /// Delete value selection
124 virtual void dispose(Space& home);
125 };
126 //@}
127
128
129 template<class View, class Val>
130 forceinline
131 ValSelCommitBase<View,Val>::ValSelCommitBase(Space&,
132 const ValBranch<Var>&) {}
133 template<class View, class Val>
134 forceinline
135 ValSelCommitBase<View,Val>::
136 ValSelCommitBase(Space&, ValSelCommitBase<View,Val>&) {}
137 template<class View, class Val>
138 ValSelCommitBase<View,Val>::~ValSelCommitBase(void) {}
139
140 template<class View, class Val>
141 forceinline void
142 ValSelCommitBase<View,Val>::operator delete(void*) {}
143 template<class View, class Val>
144 forceinline void
145 ValSelCommitBase<View,Val>::operator delete(void*, Space&) {}
146 template<class View, class Val>
147 forceinline void*
148 ValSelCommitBase<View,Val>::operator new(size_t s, Space& home) {
149 return home.ralloc(s);
150 }
151
152
153
154
155 template<class ValSel, class ValCommit>
156 forceinline
157 ValSelCommit<ValSel,ValCommit>::ValSelCommit(Space& home,
158 const ValBranch<Var>& vb)
159 : ValSelCommitBase<View,Val>(home,vb), s(home,vb), c(home,vb) {}
160
161 template<class ValSel, class ValCommit>
162 forceinline
163 ValSelCommit<ValSel,ValCommit>::ValSelCommit(Space& home,
164 ValSelCommit<ValSel,ValCommit>& vsc)
165 : ValSelCommitBase<View,Val>(home,vsc),
166 s(home,vsc.s), c(home,vsc.c) {}
167
168 template<class ValSel, class ValCommit>
169 typename ValSelCommit<ValSel,ValCommit>::Val
170 ValSelCommit<ValSel,ValCommit>::val(const Space& home, View x, int i) {
171 return s.val(home,x,i);
172 }
173
174 template<class ValSel, class ValCommit>
175 ModEvent
176 ValSelCommit<ValSel,ValCommit>::commit(Space& home, unsigned int a,
177 View x, int i, Val n) {
178 return c.commit(home,a,x,i,n);
179 }
180
181 template<class ValSel, class ValCommit>
182 NGL*
183 ValSelCommit<ValSel,ValCommit>::ngl(Space& home, unsigned int a,
184 View x, Val n) const {
185 return c.ngl(home, a, x, n);
186 }
187
188 template<class ValSel, class ValCommit>
189 void
190 ValSelCommit<ValSel,ValCommit>::print(const Space& home, unsigned int a,
191 View x, int i, const Val& n,
192 std::ostream& o) const {
193 c.print(home,a,x,i,n,o);
194 }
195
196 template<class ValSel, class ValCommit>
197 ValSelCommit<ValSel,ValCommit>*
198 ValSelCommit<ValSel,ValCommit>::copy(Space& home) {
199 return new (home) ValSelCommit<ValSel,ValCommit>(home,*this);
200 }
201
202 template<class ValSel, class ValCommit>
203 bool
204 ValSelCommit<ValSel,ValCommit>::notice(void) const {
205 return s.notice() || c.notice();
206 }
207
208 template<class ValSel, class ValCommit>
209 void
210 ValSelCommit<ValSel,ValCommit>::dispose(Space& home) {
211 s.dispose(home);
212 c.dispose(home);
213 }
214
215}
216
217// STATISTICS: kernel-branch