dom.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Matthias Balzer <matthias.balzer@itwm.fraunhofer.de>
*
* Copyright:
* Fraunhofer ITWM, 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 <gecode/minimodel.hh>
#include <cstddef>
#include <tuple>
#include <utility>
namespace { namespace cxx14 {
namespace detail {
template<std::size_t...>
struct sequence {};
template<std::size_t N, std::size_t... I>
struct make_sequence : make_sequence<N - 1, N - 1, I...> {};
template<std::size_t... I>
struct make_sequence<0, I...> {
using type = sequence<I...>;
};
}
template<std::size_t... I>
using index_sequence = detail::sequence<I...>;
template<typename... Ts>
using index_sequence_for = typename detail::make_sequence<sizeof...(Ts)>::type;
}}
namespace Gecode {
namespace {
template<typename... Args>
class DomArgs {
public:
DomArgs(Args...);
protected:
template<std::size_t... I>
void apply(Home, BoolVar, const IntPropLevels&,
cxx14::index_sequence<I...>);
private:
std::tuple<Args...> _args;
};
template<typename... Args>
class DomArgs<IntVar, Args...> {
public:
DomArgs(IntVar, Args...);
protected:
template<std::size_t... I>
void apply(Home, BoolVar, const IntPropLevels&,
cxx14::index_sequence<I...>);
private:
std::tuple<IntVar, Args...> _args;
};
/*
* Operations for argument buffer
*
*/
template<typename... Args>
DomArgs<Args...>::DomArgs(Args... args)
: _args(std::forward<Args>(args)...) {}
template<typename... Args>
template<std::size_t... I>
void
DomArgs<Args...>::apply(Home home, BoolVar b, const IntPropLevels&,
cxx14::index_sequence<I...>) {
dom(home, std::get<I>(_args)..., b);
}
template<typename... Args>
DomArgs<IntVar, Args...>::DomArgs(IntVar x, Args... args)
: _args (x, std::forward<Args>(args)...) {}
template<typename... Args>
template<std::size_t... I>
void
DomArgs<IntVar, Args...>::apply(Home home, BoolVar b,
const IntPropLevels&,
cxx14::index_sequence<I...>) {
dom(home, std::get<I>(_args)..., b);
}
template<typename... Args>
class DomExpr : public DomArgs<Args...>, public BoolExpr::Misc
{
public:
using DomArgs<Args...>::DomArgs;
virtual void post(Home, BoolVar b, bool neg,
const IntPropLevels&) override;
virtual ~DomExpr() = default;
};
template<typename... Args>
void
DomExpr<Args...>::post(Home home, BoolVar b, bool neg,
const IntPropLevels& ipls)
{
DomArgs<Args...>::apply(home, neg ? (!b).expr (home, ipls) : b, ipls,
cxx14::index_sequence_for<Args...>{});
}
}
/*
* Domain constraints
*
*/
BoolExpr
dom(const IntVar& x, int n) {
return BoolExpr(new DomExpr<IntVar, int>(x, n));
}
BoolExpr
dom(const IntVar& x, int l, int u) {
return BoolExpr(new DomExpr<IntVar, int, int>(x, l, u));
}
BoolExpr
dom(const IntVar& x, const IntSet& s) {
return BoolExpr(new DomExpr<IntVar, IntSet>(x, s));
}
#ifdef GECODE_HAS_SET_VARS
BoolExpr
dom(const SetVar& x, SetRelType rt, int i) {
return BoolExpr(new DomExpr<SetVar, SetRelType, int>(x, rt, i));
}
BoolExpr
dom(const SetVar& x, SetRelType rt, int i, int j) {
return BoolExpr(new DomExpr<SetVar, SetRelType, int, int>(x, rt, i, j));
}
BoolExpr
dom(const SetVar& x, SetRelType rt, const IntSet& s) {
return BoolExpr(new DomExpr<SetVar, SetRelType, IntSet>(x, rt, s));
}
#endif
#ifdef GECODE_HAS_FLOAT_VARS
BoolExpr
dom(const FloatVar& x, const FloatVal& n) {
return BoolExpr(new DomExpr<FloatVar, FloatVal>(x, n));
}
BoolExpr
dom(const FloatVar& x, FloatNum l, FloatNum u) {
return BoolExpr(new DomExpr<FloatVar, FloatNum, FloatNum>(x, l, u));
}
#endif
}
// STATISTICS: minimodel-any
Gecode::BoolExpr
Boolean expressions.
Definition minimodel.hh:1329
Gecode::FloatVal
Float value type.
Definition float.hh:334
Gecode::FloatVar
Float variables.
Definition float.hh:870
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntVar
Integer variables.
Definition int.hh:371
Gecode::SetVar
Set variables
Definition set.hh:127
Gecode::Float::Linear::post
void post(Home home, Term *t, int n, FloatRelType frt, FloatVal c)
Post propagator for linear constraint over floats.
Definition post.cpp:238
Gecode::FloatNum
double FloatNum
Floating point number base type.
Definition float.hh:106
Gecode::SetRelType
SetRelType
Common relation types for sets.
Definition set.hh:643
minimodel.hh
Gecode
Gecode toplevel namespace
Gecode::dom
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition dom.cpp:40
cxx14::detail
Definition dom.cpp:43
cxx14
Definition dom.cpp:41