function.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
* Mikael Lagerkvist <lagerkvist@gecode.org>
*
* Copyright:
* Christian Schulte, 2008
* Mikael Lagerkvist, 2008
*
* 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 <gecode/kernel.hh>
namespace Gecode { namespace Kernel {
class FunctionBranch : public Brancher {
protected:
class Description : public Choice {
public:
Description(const Brancher& b, unsigned int a);
virtual void archive(Archive& e) const;
};
SharedData<std::function<void(Space& home)>> f;
bool done;
FunctionBranch(Home home, std::function<void(Space& home)> f0);
FunctionBranch(Space& home, FunctionBranch& b);
public:
virtual bool status(const Space& home) const;
virtual const Choice* choice(Space& home);
virtual const Choice* choice(const Space& home, Archive& a);
virtual ExecStatus commit(Space& home, const Choice& ch, unsigned int a);
virtual void print(const Space&, const Choice&, unsigned int,
std::ostream& o) const;
virtual Actor* copy(Space& home);
static void post(Home home, std::function<void(Space& home)> f);
virtual size_t dispose(Space& home);
};
forceinline
FunctionBranch::Description::Description(const Brancher& b, unsigned int a)
: Choice(b,a) {}
void
FunctionBranch::Description::archive(Archive& e) const {
Choice::archive(e);
}
forceinline
FunctionBranch::FunctionBranch(Home home,
std::function<void(Space& home)> f0)
: Brancher(home), f(f0), done(false) {
if (!f())
throw InvalidFunction("FunctionBranch::FunctionBranch");
home.notice(*this,AP_DISPOSE);
}
forceinline
FunctionBranch::FunctionBranch(Space& home, FunctionBranch& b)
: Brancher(home,b), f(b.f), done(b.done) {
}
bool
FunctionBranch::status(const Space&) const {
return !done;
}
const Choice*
FunctionBranch::choice(Space&) {
assert(!done);
return new Description(*this,1);
}
const Choice*
FunctionBranch::choice(const Space&, Archive&) {
return new Description(*this,1);
}
ExecStatus
FunctionBranch::commit(Space& home, const Choice&, unsigned int) {
done = true;
GECODE_VALID_FUNCTION(f());
f()(home);
return home.failed() ? ES_FAILED : ES_OK;
}
void
FunctionBranch::print(const Space&, const Choice&, unsigned int,
std::ostream& o) const {
o << "FunctionBranch()";
}
Actor*
FunctionBranch::copy(Space& home) {
return new (home) FunctionBranch(home,*this);
}
forceinline void
FunctionBranch::post(Home home, std::function<void(Space& home)> f) {
if (!f)
throw InvalidFunction("FunctionBranch::post");
(void) new (home) FunctionBranch(home,f);
}
size_t
FunctionBranch::dispose(Space& home) {
home.ignore(*this,AP_DISPOSE);
f.~SharedData<std::function<void(Space& home)>>();
(void) Brancher::dispose(home);
return sizeof(*this);
}
}}
namespace Gecode {
void
branch(Home home, std::function<void(Space& home)> f) {
Kernel::FunctionBranch::post(home,f);
}
}
// STATISTICS: kernel-branch
Gecode::ActorLink::Actor
friend class Actor
Definition core.hpp:590
Gecode::Actor
Base-class for both propagators and branchers.
Definition core.hpp:628
Gecode::Actor::dispose
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition core.hpp:3252
Gecode::Archive
Archive representation
Definition archive.hpp:42
Gecode::Brancher::Space
friend class Space
Definition core.hpp:1444
Gecode::Brancher::Brancher
Brancher(Home home)
Constructor for creation.
Definition core.hpp:3605
Gecode::Brancher::Choice
friend class Choice
Definition core.hpp:1445
Gecode::Choice
Choice for performing commit
Definition core.hpp:1412
Gecode::Choice::Choice
Choice(const Brancher &b, const unsigned int a)
Initialize for particular brancher b and alternatives a.
Definition core.hpp:3766
Gecode::Choice::archive
virtual void archive(Archive &e) const
Archive into e.
Definition core.cpp:891
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::InvalidFunction
Exception: invalid function
Definition exception.hpp:114
Gecode::Kernel::FunctionBranch::Description
Minimal brancher description storing no information.
Definition function.cpp:44
Gecode::Kernel::FunctionBranch::Description::archive
virtual void archive(Archive &e) const
Archive into e.
Definition function.cpp:83
Gecode::Kernel::FunctionBranch::Description::Description
Description(const Brancher &b, unsigned int a)
Initialize description for brancher b, number of alternatives a.
Definition function.cpp:80
Gecode::Kernel::FunctionBranch::f
SharedData< std::function< void(Space &home)> > f
Function to call.
Definition function.cpp:52
Gecode::Kernel::FunctionBranch::commit
virtual ExecStatus commit(Space &home, const Choice &ch, unsigned int a)
Perform commit.
Definition function.cpp:113
Gecode::Kernel::FunctionBranch::choice
virtual const Choice * choice(Space &home)
Return choice.
Definition function.cpp:104
Gecode::Kernel::FunctionBranch::status
virtual bool status(const Space &home) const
Check status of brancher, return true if alternatives left.
Definition function.cpp:100
Gecode::Kernel::FunctionBranch::done
bool done
Call function just once.
Definition function.cpp:54
Gecode::Kernel::FunctionBranch::copy
virtual Actor * copy(Space &home)
Copy brancher.
Definition function.cpp:125
Gecode::Kernel::FunctionBranch::dispose
virtual size_t dispose(Space &home)
Dispose brancher.
Definition function.cpp:135
Gecode::Kernel::FunctionBranch::print
virtual void print(const Space &, const Choice &, unsigned int, std::ostream &o) const
Print explanation.
Definition function.cpp:120
Gecode::Kernel::FunctionBranch::post
static void post(Home home, std::function< void(Space &home)> f)
Post brancher.
Definition function.cpp:129
Gecode::Kernel::FunctionBranch::FunctionBranch
FunctionBranch(Home home, std::function< void(Space &home)> f0)
Construct brancher.
Definition function.cpp:88
Gecode::SharedData
Class for sharing data between spaces.
Definition shared-data.hpp:38
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::Space::ignore
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4074
Gecode::Space::failed
bool failed(void) const
Check whether space is failed.
Definition core.hpp:4044
Gecode::Space::notice
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition core.hpp:4059
Gecode::AP_DISPOSE
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
Gecode::branch
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
kernel.hh
Gecode::Kernel
Kernel functionality
Gecode
Gecode toplevel namespace
Gecode::ExecStatus
ExecStatus
Definition core.hpp:472
Gecode::ES_OK
@ ES_OK
Execution is okay.
Definition core.hpp:476
Gecode::ES_FAILED
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
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