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 TaskBranchValCommit Generic value commit for brancher based on view and value selection
38 *
39 * \ingroup TaskBranchViewVal
40 */
41 //@{
42 /// Base class for value commit
43 template<class View_, class Val_>
44 class ValCommit {
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 ValCommit(Space& home, const ValBranch<Var>& vb);
55 /// Constructor for cloning
56 ValCommit(Space& home, ValCommit<View,Val>& vs);
57 /// Whether dispose must always be called (that is, notice is needed)
58 bool notice(void) const;
59 /// Delete value commit
60 void dispose(Space& home);
61 };
62
63 /// Class for user-defined value commit
64 template<class View>
65 class ValCommitFunction : public
66 ValCommit<View,
67 typename BranchTraits<typename View::VarType>::ValType> {
68 typedef typename ValCommit<View,
69 typename BranchTraits<typename View::VarType>
70 ::ValType>::Val Val;
71 public:
72 /// The corresponding variable type
73 typedef typename View::VarType Var;
74 /// The corresponding commit function
75 typedef typename BranchTraits<Var>::Commit CommitFunction;
76 protected:
77 /// The user-defined commit function
78 SharedData<CommitFunction> c;
79 public:
80 /// Constructor for initialization
81 ValCommitFunction(Space& home, const ValBranch<Var>& vb);
82 /// Constructor for cloning during copying
83 ValCommitFunction(Space& home, ValCommitFunction& vc);
84 /// Perform user-defined commit
85 ModEvent commit(Space& home, unsigned int a, View x, int i, Val n);
86 /// Create no-good literal for alternative \a a
87 NGL* ngl(Space& home, unsigned int a, View x, Val n) const;
88 /// Print on \a o the alternative \a with view \a x at position \a i and value \a n
89 void print(const Space& home, unsigned int a, View x, int i,
90 const Val& n, std::ostream& o) const;
91 /// Whether dispose must always be called (that is, notice is needed)
92 bool notice(void) const;
93 /// Delete value commit
94 void dispose(Space& home);
95 };
96 //@}
97
98 // Baseclass for value commit
99 template<class View, class Val>
100 forceinline
101 ValCommit<View,Val>::ValCommit(Space&, const ValBranch<Var>&) {}
102 template<class View, class Val>
103 forceinline
104 ValCommit<View,Val>::ValCommit(Space&, ValCommit<View,Val>&) {}
105 template<class View, class Val>
106 forceinline bool
107 ValCommit<View,Val>::notice(void) const {
108 return false;
109 }
110 template<class View, class Val>
111 forceinline void
112 ValCommit<View,Val>::dispose(Space&) {}
113
114
115 // User-defined value selection
116 template<class View>
117 forceinline
118 ValCommitFunction<View>::ValCommitFunction(Space& home,
119 const ValBranch<Var>& vb)
120 : ValCommit<View,Val>(home,vb), c(vb.commit()) {
121 if (!c())
122 throw InvalidFunction("ValCommitFunction::ValCommitFunction");
123 }
124 template<class View>
125 forceinline
126 ValCommitFunction<View>::ValCommitFunction(Space& home,
127 ValCommitFunction<View>& vc)
128 : ValCommit<View,Val>(home,vc), c(vc.c) {
129 }
130 template<class View>
131 forceinline ModEvent
132 ValCommitFunction<View>::commit(Space& home, unsigned int a, View x, int i,
133 Val n) {
134 typename View::VarType y(x.varimp());
135 GECODE_VALID_FUNCTION(c());
136 c()(home,a,y,i,n);
137 return home.failed() ? ES_FAILED : ES_OK;
138 }
139 template<class View>
140 forceinline NGL*
141 ValCommitFunction<View>::ngl(Space&, unsigned int, View, Val) const {
142 return nullptr;
143 }
144 template<class View>
145 forceinline void
146 ValCommitFunction<View>::print(const Space&, unsigned int,
147 View, int i, const Val&,
148 std::ostream& o) const {
149 o << "var[" << i << "] is user-defined.";
150 }
151 template<class View>
152 forceinline bool
153 ValCommitFunction<View>::notice(void) const {
154 return true;
155 }
156 template<class View>
157 forceinline void
158 ValCommitFunction<View>::dispose(Space&) {
159 c.~SharedData<CommitFunction>();
160 }
161
162}
163
164// STATISTICS: kernel-branch