Generated on Thu Apr 11 13:59:14 2019 for Gecode by doxygen 1.6.3

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