/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Matthias Balzer * * 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 #include #include #include /// reproduce C++-14 std::index_sequence namespace { namespace cxx14 { namespace detail { template struct sequence {}; template struct make_sequence : make_sequence {}; template struct make_sequence<0, I...> { using type = sequence; }; } template using index_sequence = detail::sequence; template using index_sequence_for = typename detail::make_sequence::type; }} namespace Gecode { namespace { /// Buffer for domain constraint arguments template class DomArgs { public: /// Constructor DomArgs(Args...); protected: /// Post reified domain constraint using the stored arguments template void apply(Home, BoolVar, const IntPropLevels&, cxx14::index_sequence); private: /// Storage for the arguments std::tuple _args; }; /// Buffer for domain constraint arguments - partial specialization for IntVar template class DomArgs { public: /// Constructor DomArgs(IntVar, Args...); protected: /// Post reified domain constraint using stored arguments template void apply(Home, BoolVar, const IntPropLevels&, cxx14::index_sequence); private: /// Storage for the arguments std::tuple _args; }; /* * Operations for argument buffer * */ template DomArgs::DomArgs(Args... args) : _args(std::forward(args)...) {} template template void DomArgs::apply(Home home, BoolVar b, const IntPropLevels&, cxx14::index_sequence) { dom(home, std::get(_args)..., b); } template DomArgs::DomArgs(IntVar x, Args... args) : _args (x, std::forward(args)...) {} template template void DomArgs::apply(Home home, BoolVar b, const IntPropLevels&, cxx14::index_sequence) { dom(home, std::get(_args)..., b); } /// Domain expression template class DomExpr : public DomArgs, public BoolExpr::Misc { public: using DomArgs::DomArgs; /// Constrain \a b to be equivalent to the expression (negated if \a neg) virtual void post(Home, BoolVar b, bool neg, const IntPropLevels&) override; /// Destructor virtual ~DomExpr() = default; }; template void DomExpr::post(Home home, BoolVar b, bool neg, const IntPropLevels& ipls) { DomArgs::apply(home, neg ? (!b).expr (home, ipls) : b, ipls, cxx14::index_sequence_for{}); } } /* * Domain constraints * */ BoolExpr dom(const IntVar& x, int n) { return BoolExpr(new DomExpr(x, n)); } BoolExpr dom(const IntVar& x, int l, int u) { return BoolExpr(new DomExpr(x, l, u)); } BoolExpr dom(const IntVar& x, const IntSet& s) { return BoolExpr(new DomExpr(x, s)); } #ifdef GECODE_HAS_SET_VARS BoolExpr dom(const SetVar& x, SetRelType rt, int i) { return BoolExpr(new DomExpr(x, rt, i)); } BoolExpr dom(const SetVar& x, SetRelType rt, int i, int j) { return BoolExpr(new DomExpr(x, rt, i, j)); } BoolExpr dom(const SetVar& x, SetRelType rt, const IntSet& s) { return BoolExpr(new DomExpr(x, rt, s)); } #endif #ifdef GECODE_HAS_FLOAT_VARS BoolExpr dom(const FloatVar& x, const FloatVal& n) { return BoolExpr(new DomExpr(x, n)); } BoolExpr dom(const FloatVar& x, FloatNum l, FloatNum u) { return BoolExpr(new DomExpr(x, l, u)); } #endif } // STATISTICS: minimodel-any