Generated on Tue Apr 18 10:22:03 2017 for Gecode by doxygen 1.6.3

brancher-filter.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2017
00008  *
00009  *  Last modified:
00010  *     $Date: 2017-04-01 20:27:10 +0200 (Sat, 01 Apr 2017) $ by $Author: schulte $
00011  *     $Revision: 15623 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include <functional>
00039 
00040 namespace Gecode {
00041 
00043   template<class Var>
00044   using BranchFilter = std::function<bool(const Space& home, 
00045                                           Var x, int i)>;
00046  
00048   template<class View>
00049   class BrancherFilter {
00050   public:
00052     typedef typename View::VarType Var;
00053   protected:
00054     SharedData<BranchFilter<Var>> f;
00055   public:
00057     BrancherFilter(BranchFilter<Var> bf);
00059     BrancherFilter(Space& home, bool shared, BrancherFilter& bf);
00061     operator bool(void) const;
00063     bool operator ()(const Space& home, View x, int i) const;
00065     bool notice(void) const;
00067     void dispose(Space& home);
00068   };
00069 
00071   template<class View>
00072   class BrancherNoFilter {
00073   public:
00075     typedef typename View::VarType Var;
00076   public:
00078     BrancherNoFilter(BranchFilter<Var> bf);
00080     BrancherNoFilter(Space& home, bool shared, BrancherNoFilter& bf);
00082     operator bool(void) const;
00084     bool operator ()(const Space& home, View x, int i) const;
00086     bool notice(void) const;
00088     void dispose(Space& home);
00089   };
00090 
00091 
00092   template<class View>
00093   forceinline
00094   BrancherFilter<View>::BrancherFilter(BranchFilter<Var> bf) : f(bf) {
00095     if (!bf)
00096       throw Gecode::InvalidFunction("BrancherFilter::BrancherFilter");
00097   }
00098   
00099   template<class View>
00100   forceinline
00101   BrancherFilter<View>::BrancherFilter(Space& home, bool shared,
00102                                        BrancherFilter<View>& bf) {
00103     f.update(home,shared,bf.f);
00104   }
00105 
00106   template<class View>
00107   forceinline
00108   BrancherFilter<View>::operator bool(void) const {
00109     return true;
00110   }
00111 
00112   template<class View>
00113   forceinline bool
00114   BrancherFilter<View>::operator ()(const Space& home, View x, int i) const {
00115     GECODE_VALID_FUNCTION(f());
00116     Var xv(x.varimp());
00117     return f()(home,xv,i);
00118   }
00119 
00120   template<class View>
00121   forceinline bool
00122   BrancherFilter<View>::notice(void) const {
00123     return true;
00124   }
00125 
00126   template<class View>
00127   forceinline void
00128   BrancherFilter<View>::dispose(Space&) {
00129     f.~SharedData<BranchFilter<Var>>();
00130   }
00131 
00132 
00133   template<class View>
00134   forceinline
00135   BrancherNoFilter<View>::BrancherNoFilter(BranchFilter<Var> bf) {
00136     assert(!bf);
00137   }
00138   
00139   template<class View>
00140   forceinline
00141   BrancherNoFilter<View>::BrancherNoFilter(Space&, bool,
00142                                            BrancherNoFilter<View>&) {}
00143 
00144   template<class View>
00145   forceinline
00146   BrancherNoFilter<View>::operator bool(void) const {
00147     return false;
00148   }
00149 
00150   template<class View>
00151   forceinline bool
00152   BrancherNoFilter<View>::operator ()(const Space&, View, int) const {
00153     return true;
00154   }
00155   template<class View>
00156   forceinline bool
00157   BrancherNoFilter<View>::notice(void) const {
00158     return false;
00159   }
00160 
00161   template<class View>
00162   forceinline void
00163   BrancherNoFilter<View>::dispose(Space&) {
00164   }
00165 
00166 }
00167 
00168 // STATISTICS: kernel-branch