filter.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2017
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <functional>
namespace Gecode {
template<class Var>
using BranchFilter = std::function<bool(const Space& home,
Var x, int i)>;
template<class View>
class BrancherFilter {
public:
typedef typename View::VarType Var;
protected:
SharedData<BranchFilter<Var>> f;
public:
BrancherFilter(BranchFilter<Var> bf);
BrancherFilter(BrancherFilter& bf);
operator bool(void) const;
bool operator ()(const Space& home, View x, int i) const;
bool notice(void) const;
void dispose(Space& home);
};
template<class View>
class BrancherNoFilter {
public:
typedef typename View::VarType Var;
public:
BrancherNoFilter(BranchFilter<Var> bf);
BrancherNoFilter(BrancherNoFilter& bf);
operator bool(void) const;
bool operator ()(const Space& home, View x, int i) const;
bool notice(void) const;
void dispose(Space& home);
};
template<class View>
forceinline
BrancherFilter<View>::BrancherFilter(BranchFilter<Var> bf) : f(bf) {
if (!bf)
throw Gecode::InvalidFunction("BrancherFilter::BrancherFilter");
}
template<class View>
forceinline
BrancherFilter<View>::BrancherFilter(BrancherFilter<View>& bf)
: f(bf.f) {
}
template<class View>
forceinline
BrancherFilter<View>::operator bool(void) const {
return true;
}
template<class View>
forceinline bool
BrancherFilter<View>::operator ()(const Space& home, View x, int i) const {
GECODE_VALID_FUNCTION(f());
Var xv(x.varimp());
return f()(home,xv,i);
}
template<class View>
forceinline bool
BrancherFilter<View>::notice(void) const {
return true;
}
template<class View>
forceinline void
BrancherFilter<View>::dispose(Space&) {
f.~SharedData<BranchFilter<Var>>();
}
template<class View>
forceinline
BrancherNoFilter<View>::BrancherNoFilter(BranchFilter<Var> bf) {
assert(!bf);
(void) bf;
}
template<class View>
forceinline
BrancherNoFilter<View>::BrancherNoFilter(BrancherNoFilter<View>&) {}
template<class View>
forceinline
BrancherNoFilter<View>::operator bool(void) const {
return false;
}
template<class View>
forceinline bool
BrancherNoFilter<View>::operator ()(const Space&, View, int) const {
return true;
}
template<class View>
forceinline bool
BrancherNoFilter<View>::notice(void) const {
return false;
}
template<class View>
forceinline void
BrancherNoFilter<View>::dispose(Space&) {
}
}
// STATISTICS: kernel-branch
Gecode::BrancherFilter
Class storing a branch filter function.
Definition filter.hpp:45
Gecode::BrancherFilter::BrancherFilter
BrancherFilter(BrancherFilter &bf)
Initialize during cloning.
Gecode::BrancherFilter::f
SharedData< BranchFilter< Var > > f
Definition filter.hpp:50
Gecode::BrancherFilter::operator()
bool operator()(const Space &home, View x, int i) const
Invoke filter function.
Definition filter.hpp:109
Gecode::BrancherFilter::BrancherFilter
BrancherFilter(BranchFilter< Var > bf)
Initialize.
Definition filter.hpp:90
Gecode::BrancherFilter::notice
bool notice(void) const
Whether dispose must always be called (that is, notice is needed).
Definition filter.hpp:117
Gecode::BrancherFilter::Var
View::VarType Var
The corresponding variable type.
Definition filter.hpp:48
Gecode::BrancherFilter::dispose
void dispose(Space &home)
Delete object.
Definition filter.hpp:123
Gecode::BrancherNoFilter
Class withot a branch filter function.
Definition filter.hpp:68
Gecode::BrancherNoFilter::dispose
void dispose(Space &home)
Delete object.
Definition filter.hpp:158
Gecode::BrancherNoFilter::BrancherNoFilter
BrancherNoFilter(BrancherNoFilter &bf)
Initialize during cloning.
Gecode::BrancherNoFilter::operator()
bool operator()(const Space &home, View x, int i) const
Invoke filter function.
Definition filter.hpp:147
Gecode::BrancherNoFilter::notice
bool notice(void) const
Whether dispose must always be called (that is, notice is needed).
Definition filter.hpp:152
Gecode::BrancherNoFilter::Var
View::VarType Var
The corresponding variable type.
Definition filter.hpp:71
Gecode::BrancherNoFilter::BrancherNoFilter
BrancherNoFilter(BranchFilter< Var > bf)
Initialize.
Definition filter.hpp:130
Gecode::InvalidFunction
Exception: invalid function
Definition exception.hpp:114
Gecode::SharedData
Class for sharing data between spaces.
Definition shared-data.hpp:38
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::View
Gecode::Var
Base class for variables.
Definition var.hpp:40
Gecode
Gecode toplevel namespace
Gecode::BranchFilter
std::function< bool(const Space &home, Var x, int i)> BranchFilter
Function type for branch filter functions.
Definition filter.hpp:40
forceinline
#define forceinline
Definition config.hpp:187
GECODE_VALID_FUNCTION
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition macros.hpp:94