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 TaskBranchValSel Generic value selection for brancher based on view and value selection
38 *
39 * \ingroup TaskBranchViewVal
40 */
41 //@{
42 /// Base class for value selection
43 template<class View_, class Val_>
44 class ValSel {
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 ValSel(Space& home, const ValBranch<Var>& vb);
55 /// Constructor for cloning
56 ValSel(Space& home, ValSel<View,Val>& vs);
57 /// Whether dispose must always be called (that is, notice is needed)
58 bool notice(void) const;
59 /// Delete value selection
60 void dispose(Space& home);
61 };
62
63 /// User-defined value selection
64 template<class View>
65 class ValSelFunction :
66 public ValSel<View,
67 typename BranchTraits<typename View::VarType>::ValType> {
68 public:
69 /// The corresponding variable type
70 using typename ValSel<View,
71 typename BranchTraits<typename View::VarType>::ValType>::Var;
72 /// The corresponding value type
73 typedef typename ValSel<View,
74 typename BranchTraits<typename View::VarType>
75 ::ValType>::Val Val;
76 /// The corresponding value function
77 typedef typename BranchTraits<Var>::Val ValFunction;
78 protected:
79 /// The user-defined value function
80 SharedData<ValFunction> v;
81 public:
82 /// Constructor for initialization
83 ValSelFunction(Space& home, const ValBranch<Var>& vb);
84 /// Constructor for cloning
85 ValSelFunction(Space& home, ValSelFunction<View>& vs);
86 /// Return user-defined value of view \a x at position \a i
87 Val val(const Space& home, View x, int i);
88 /// Whether dispose must always be called (that is, notice is needed)
89 bool notice(void) const;
90 /// Delete value selection
91 void dispose(Space& home);
92 };
93 //@}
94
95
96 // Baseclass value selection
97 template<class View, class Val>
98 forceinline
99 ValSel<View,Val>::ValSel(Space&, const ValBranch<Var>&) {}
100 template<class View, class Val>
101 forceinline
102 ValSel<View,Val>::ValSel(Space&, ValSel<View,Val>&) {}
103 template<class View, class Val>
104 forceinline bool
105 ValSel<View,Val>::notice(void) const {
106 return false;
107 }
108 template<class View, class Val>
109 forceinline void
110 ValSel<View,Val>::dispose(Space&) {}
111
112
113 // User-defined value selection
114 template<class View>
115 forceinline
116 ValSelFunction<View>::ValSelFunction
117 (Space& home, const ValBranch<Var>& vb)
118 : ValSel<View,Val>(home,vb), v(vb.val()) {
119 if (!v())
120 throw InvalidFunction("ValSelFunction::ValSelFunction");
121 }
122 template<class View>
123 forceinline
124 ValSelFunction<View>::ValSelFunction(Space& home, ValSelFunction<View>& vs)
125 : ValSel<View,Val>(home,vs), v(vs.v) {
126 }
127 template<class View>
128 forceinline typename ValSelFunction<View>::Val
129 ValSelFunction<View>::val(const Space& home, View x, int i) {
130 typename View::VarType y(x.varimp());
131 GECODE_VALID_FUNCTION(v());
132 return v()(home,y,i);
133 }
134 template<class View>
135 forceinline bool
136 ValSelFunction<View>::notice(void) const {
137 return true;
138 }
139 template<class View>
140 forceinline void
141 ValSelFunction<View>::dispose(Space&) {
142 v.~SharedData<ValFunction>();
143 }
144
145}
146
147// STATISTICS: kernel-branch