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, 2017 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 34 #include <functional> 35 36 namespace Gecode { 37 38 /// Function type for branch filter functions 39 template<class Var> 40 using BranchFilter = std::function<bool(const Space& home, 41 Var x, int i)>; 42 43 /// Class storing a branch filter function 44 template<class View> 45 class BrancherFilter { 46 public: 47 /// The corresponding variable type 48 typedef typename View::VarType Var; 49 protected: 50 SharedData<BranchFilter<Var>> f; 51 public: 52 /// Initialize 53 BrancherFilter(BranchFilter<Var> bf); 54 /// Initialize during cloning 55 BrancherFilter(BrancherFilter& bf); 56 /// Whether filtering is enabled 57 operator bool(void) const; 58 /// Invoke filter function 59 bool operator ()(const Space& home, View x, int i) const; 60 /// Whether dispose must always be called (that is, notice is needed) 61 bool notice(void) const; 62 /// Delete object 63 void dispose(Space& home); 64 }; 65 66 /// Class withot a branch filter function 67 template<class View> 68 class BrancherNoFilter { 69 public: 70 /// The corresponding variable type 71 typedef typename View::VarType Var; 72 public: 73 /// Initialize 74 BrancherNoFilter(BranchFilter<Var> bf); 75 /// Initialize during cloning 76 BrancherNoFilter(BrancherNoFilter& bf); 77 /// Whether filtering is enabled 78 operator bool(void) const; 79 /// Invoke filter function 80 bool operator ()(const Space& home, View x, int i) const; 81 /// Whether dispose must always be called (that is, notice is needed) 82 bool notice(void) const; 83 /// Delete object 84 void dispose(Space& home); 85 }; 86 87 88 template<class View> 89 forceinline BrancherFilter(BranchFilter<Var> bf)90 BrancherFilter<View>::BrancherFilter(BranchFilter<Var> bf) : f(bf) { 91 if (!bf) 92 throw Gecode::InvalidFunction("BrancherFilter::BrancherFilter"); 93 } 94 95 template<class View> 96 forceinline BrancherFilter(BrancherFilter<View> & bf)97 BrancherFilter<View>::BrancherFilter(BrancherFilter<View>& bf) 98 : f(bf.f) { 99 } 100 101 template<class View> 102 forceinline operator bool(void) const103 BrancherFilter<View>::operator bool(void) const { 104 return true; 105 } 106 107 template<class View> 108 forceinline bool operator ()(const Space & home,View x,int i) const109 BrancherFilter<View>::operator ()(const Space& home, View x, int i) const { 110 GECODE_VALID_FUNCTION(f()); 111 Var xv(x.varimp()); 112 return f()(home,xv,i); 113 } 114 115 template<class View> 116 forceinline bool notice(void) const117 BrancherFilter<View>::notice(void) const { 118 return true; 119 } 120 121 template<class View> 122 forceinline void dispose(Space &)123 BrancherFilter<View>::dispose(Space&) { 124 f.~SharedData<BranchFilter<Var>>(); 125 } 126 127 128 template<class View> 129 forceinline BrancherNoFilter(BranchFilter<Var> bf)130 BrancherNoFilter<View>::BrancherNoFilter(BranchFilter<Var> bf) { 131 assert(!bf); 132 (void) bf; 133 } 134 135 template<class View> 136 forceinline BrancherNoFilter(BrancherNoFilter<View> &)137 BrancherNoFilter<View>::BrancherNoFilter(BrancherNoFilter<View>&) {} 138 139 template<class View> 140 forceinline operator bool(void) const141 BrancherNoFilter<View>::operator bool(void) const { 142 return false; 143 } 144 145 template<class View> 146 forceinline bool operator ()(const Space &,View,int) const147 BrancherNoFilter<View>::operator ()(const Space&, View, int) const { 148 return true; 149 } 150 template<class View> 151 forceinline bool notice(void) const152 BrancherNoFilter<View>::notice(void) const { 153 return false; 154 } 155 156 template<class View> 157 forceinline void dispose(Space &)158 BrancherNoFilter<View>::dispose(Space&) { 159 } 160 161 } 162 163 // STATISTICS: kernel-branch 164