bool.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, 2006
*
* 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.
*
*/
namespace Gecode { namespace Int {
/*
* Creation of new variable implementations
*
*/
forceinline
BoolVarImp::BoolVarImp(int n) {
assert(bits() == 0);
bits() |= (n << 1) | n;
}
forceinline
BoolVarImp::BoolVarImp(Space& home, int min, int max)
: BoolVarImpBase(home) {
assert(bits() == 0);
bits() |= (max << 1) | min;
}
/*
* Operations on Boolean variable implementations
*
*/
forceinline BoolStatus
BoolVarImp::status(void) const {
return bits() & 3;
}
forceinline int
BoolVarImp::min(void) const {
return static_cast<int>(bits() & 1);
}
forceinline int
BoolVarImp::max(void) const {
return static_cast<int>((bits() & 2) >> 1);
}
forceinline int
BoolVarImp::med(void) const {
return min();
}
forceinline int
BoolVarImp::val(void) const {
assert(status() != NONE);
return min();
}
forceinline bool
BoolVarImp::range(void) const {
return true;
}
forceinline bool
BoolVarImp::assigned(void) const {
return status() != NONE;
}
forceinline unsigned int
BoolVarImp::width(void) const {
return assigned() ? 1U : 2U;
}
forceinline unsigned int
BoolVarImp::size(void) const {
return assigned() ? 1U : 2U;
}
forceinline unsigned int
BoolVarImp::regret_min(void) const {
return assigned() ? 1U : 0U;
}
forceinline unsigned int
BoolVarImp::regret_max(void) const {
return assigned() ? 1U : 0U;
}
/*
* Tests
*
*/
forceinline bool
BoolVarImp::in(int n) const {
return (n >= min()) && (n <= max());
}
forceinline bool
BoolVarImp::in(long long int n) const {
return (n >= min()) && (n <= max());
}
/*
* Boolean domain tests
*
*/
forceinline bool
BoolVarImp::zero(void) const {
return status() < NONE;
}
forceinline bool
BoolVarImp::one(void) const {
return status() > NONE;
}
forceinline bool
BoolVarImp::none(void) const {
return status() == NONE;
}
/*
* Support for delta information
*
*/
forceinline ModEvent
BoolVarImp::modevent(const Delta&) {
return ME_BOOL_VAL;
}
forceinline int
BoolVarImp::min(const Delta& d) {
return static_cast<const IntDelta&>(d).min();
}
forceinline int
BoolVarImp::max(const Delta& d) {
return static_cast<const IntDelta&>(d).min();
}
forceinline unsigned int
BoolVarImp::width(const Delta& d) {
return static_cast<const IntDelta&>(d).width();
}
forceinline bool
BoolVarImp::any(const Delta&) {
return false;
}
forceinline bool
BoolVarImp::zero(const Delta& d) {
return static_cast<const IntDelta&>(d).min() != 0;
}
forceinline bool
BoolVarImp::one(const Delta& d) {
return static_cast<const IntDelta&>(d).min() == 0;
}
/*
* Boolean tell operations
*
*/
forceinline ModEvent
BoolVarImp::zero(Space& home) {
if (one()) return ME_BOOL_FAILED;
if (zero()) return ME_BOOL_NONE;
return zero_none(home);
}
forceinline ModEvent
BoolVarImp::one(Space& home) {
if (one()) return ME_BOOL_NONE;
if (zero()) return ME_BOOL_FAILED;
return one_none(home);
}
/*
* Tell operations
*
*/
forceinline ModEvent
BoolVarImp::gq(Space& home, int n) {
if (n <= 0) return ME_INT_NONE;
if (n > 1) return fail(home);
return one(home);
}
forceinline ModEvent
BoolVarImp::gq(Space& home, long long int n) {
if (n <= 0) return ME_INT_NONE;
if (n > 1) return fail(home);
return one(home);
}
forceinline ModEvent
BoolVarImp::lq(Space& home, int n) {
if (n < 0) return fail(home);
if (n >= 1) return ME_INT_NONE;
return zero(home);
}
forceinline ModEvent
BoolVarImp::lq(Space& home, long long int n) {
if (n < 0) return fail(home);
if (n >= 1) return ME_INT_NONE;
return zero(home);
}
forceinline ModEvent
BoolVarImp::eq(Space& home, int n) {
if ((n < 0) || (n > 1)) return fail(home);
return (n == 0) ? zero(home): one(home);
}
forceinline ModEvent
BoolVarImp::eq(Space& home, long long int n) {
if ((n < 0) || (n > 1)) return fail(home);
return (n == 0) ? zero(home): one(home);
}
forceinline ModEvent
BoolVarImp::nq(Space& home, int n) {
if ((n < 0) || (n > 1)) return ME_INT_NONE;
return (n == 0) ? one(home): zero(home);
}
forceinline ModEvent
BoolVarImp::nq(Space& home, long long int n) {
if ((n < 0) || (n > 1)) return ME_INT_NONE;
return (n == 0) ? one(home): zero(home);
}
/*
* Copying a variable
*
*/
forceinline
BoolVarImp::BoolVarImp(Space& home, BoolVarImp& x)
: BoolVarImpBase(home,x) {}
forceinline BoolVarImp*
BoolVarImp::copy(Space& home) {
if (copied())
return static_cast<BoolVarImp*>(forward());
else if (zero())
return &s_zero;
else if (one())
return &s_one;
else
return new (home) BoolVarImp(home,*this);
}
/*
* Iterator-based domain operations
*
*/
template<class I>
forceinline ModEvent
BoolVarImp::narrow_r(Space& home, I& i, bool) {
// Is new domain empty?
if (!i())
return fail(home);
assert((i.min() == 0) || (i.min() == 1));
assert((i.max() == 0) || (i.max() == 1));
if (i.max() == 0) {
assert(!one());
// Assign domain to be zero (domain cannot be one)
return zero(home);
}
if (i.min() == 1) {
// Assign domain to be one (domain cannot be zero)
assert(!zero());
return one(home);
}
assert(none());
return ME_INT_NONE;
}
template<class I>
forceinline ModEvent
BoolVarImp::inter_r(Space& home, I& i, bool) {
// Skip all ranges that are too small
while (i() && (i.max() < 0))
++i;
// Is new domain empty?
if (!i() || (i.min() > 1))
return fail(home);
assert(i.min() <= 1);
if (i.min() == 1)
return one(home);
if (i.max() == 0)
return zero(home);
assert((i.min() <= 0) && (i.max() >= 1));
return ME_INT_NONE;
}
template<class I>
forceinline ModEvent
BoolVarImp::minus_r(Space& home, I& i, bool) {
// Skip all ranges that are too small
while (i() && (i.max() < 0))
++i;
// Is new domain empty?
if (!i() || (i.min() > 1))
return ME_INT_NONE;
assert(i.min() <= 1);
if (i.min() == 1)
return zero(home);
if (i.max() == 0)
return one(home);
assert((i.min() <= 0) && (i.max() >= 1));
return fail(home);
}
template<class I>
forceinline ModEvent
BoolVarImp::narrow_v(Space& home, I& i, bool) {
if (!i())
return fail(home);
if (!none())
return ME_INT_NONE;
if (i.val() == 0) {
do {
++i;
} while (i() && (i.val() == 0));
if (!i())
return zero_none(home);
return ME_INT_NONE;
} else {
assert(i.val() == 1);
return one_none(home);
}
}
template<class I>
forceinline ModEvent
BoolVarImp::inter_v(Space& home, I& i, bool) {
while (i() && (i.val() < 0))
++i;
if (!i() || (i.val() > 1))
return fail(home);
if (i.val() == 0) {
do {
++i;
} while (i() && (i.val() == 0));
if (!i() || (i.val() > 1))
return zero(home);
return ME_INT_NONE;
} else {
assert(i.val() == 1);
return one(home);
}
}
template<class I>
forceinline ModEvent
BoolVarImp::minus_v(Space& home, I& i, bool) {
while (i() && (i.val() < 0))
++i;
if (!i() || (i.val() > 1))
return ME_INT_NONE;
if (i.val() == 0) {
do {
++i;
} while (i() && (i.val() == 0));
if (!i() || (i.val() > 1))
return one(home);
return fail(home);
} else {
assert(i.val() == 1);
return zero(home);
}
}
/*
* Dependencies
*
*/
forceinline void
BoolVarImp::cancel(Space& home, Propagator& p, PropCond) {
BoolVarImpBase::cancel(home,p,PC_BOOL_VAL);
}
forceinline void
BoolVarImp::cancel(Space& home, Advisor& a, bool fail) {
BoolVarImpBase::cancel(home,a,fail);
}
forceinline void
BoolVarImp::schedule(Space& home, Propagator& p, ModEvent) {
BoolVarImpBase::schedule(home,p,ME_BOOL_VAL);
}
forceinline ModEventDelta
BoolVarImp::med(ModEvent me) {
return BoolVarImpBase::med(me);
}
}}
// STATISTICS: int-var
Gecode::Advisor
Base-class for advisors.
Definition core.hpp:1292
Gecode::Delta
Generic domain change information to be supplied to advisors.
Definition core.hpp:204
Gecode::Int::BoolVarImpBase
Base-class for Bool-variable implementations.
Definition var-imp.hpp:93
Gecode::Int::BoolVarImpBase::BoolVarImpBase
BoolVarImpBase(Gecode::Space &home, BoolVarImpBase &x)
Constructor for cloning x.
Definition var-imp.hpp:300
Gecode::Int::BoolVarImpBase::schedule
static void schedule(Gecode::Space &home, Gecode::Propagator &p, Gecode::ModEvent me)
Schedule propagator p.
Definition var-imp.hpp:313
Gecode::Int::BoolVarImp
Boolean variable implementation.
Definition var-imp.hpp:491
Gecode::Int::BoolVarImp::gq
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition bool.hpp:201
Gecode::Int::BoolVarImp::zero
bool zero(void) const
Test whether variable is assigned to zero.
Definition bool.hpp:131
Gecode::Int::BoolVarImp::nq
ModEvent nq(Space &home, int n)
Restrict domain values to be different from n.
Definition bool.hpp:238
Gecode::Int::BoolVarImp::size
unsigned int size(void) const
Return size (cardinality) of domain.
Definition bool.hpp:96
Gecode::Int::BoolVarImp::inter_v
ModEvent inter_v(Space &home, I &i, bool depends=true)
Intersect domain with values described by i.
Definition bool.hpp:351
Gecode::Int::BoolVarImp::none
bool none(void) const
Test whether variable is not yet assigned.
Definition bool.hpp:139
Gecode::Int::BoolVarImp::max
int max(void) const
Return maximum of domain.
Definition bool.hpp:66
Gecode::Int::BoolVarImp::eq
ModEvent eq(Space &home, int n)
Restrict domain values to be equal to n.
Definition bool.hpp:227
Gecode::Int::BoolVarImp::schedule
static void schedule(Space &home, Propagator &p, ModEvent me)
Schedule propagator p with modification event me.
Definition bool.hpp:405
Gecode::Int::BoolVarImp::narrow_v
ModEvent narrow_v(Space &home, I &i, bool depends=true)
Replace domain by values described by i.
Definition bool.hpp:332
Gecode::Int::BoolVarImp::width
unsigned int width(void) const
Return width of domain (distance between maximum and minimum).
Definition bool.hpp:91
Gecode::Int::BoolVarImp::one
bool one(void) const
Test whether variable is assigned to one.
Definition bool.hpp:135
Gecode::Int::BoolVarImp::narrow_r
ModEvent narrow_r(Space &home, I &i, bool depends=true)
Replace domain by ranges described by i.
Definition bool.hpp:276
Gecode::Int::BoolVarImp::min
int min(void) const
Return minimum of domain.
Definition bool.hpp:62
Gecode::Int::BoolVarImp::inter_r
ModEvent inter_r(Space &home, I &i, bool depends=true)
Intersect domain with ranges described by i.
Definition bool.hpp:297
Gecode::Int::BoolVarImp::one_none
ModEvent one_none(Space &home)
Assign unassigned variable to one.
Definition bool.cpp:42
Gecode::Int::BoolVarImp::any
static bool any(const Delta &d)
Test whether arbitrary values got pruned.
Definition bool.hpp:165
Gecode::Int::BoolVarImp::NONE
static const BoolStatus NONE
Status of domain not yet assigned.
Definition var-imp.hpp:525
Gecode::Int::BoolVarImp::med
int med(void) const
Return median of domain (greatest element not greater than the median).
Definition bool.hpp:70
Gecode::Int::BoolVarImp::copy
BoolVarImp * copy(Space &home)
Return copy of this variable.
Definition bool.hpp:258
Gecode::Int::BoolVarImp::regret_max
unsigned int regret_max(void) const
Return regret of domain maximum (distance to next smaller value).
Definition bool.hpp:105
Gecode::Int::BoolVarImp::minus_r
ModEvent minus_r(Space &home, I &i, bool depends=true)
Remove from domain the ranges described by i.
Definition bool.hpp:314
Gecode::Int::BoolVarImp::assigned
bool assigned(void) const
Test whether variable is assigned.
Definition bool.hpp:85
Gecode::Int::BoolVarImp::in
bool in(int n) const
Test whether n is contained in domain.
Definition bool.hpp:117
Gecode::Int::BoolVarImp::zero_none
ModEvent zero_none(Space &home)
Assign unassigned variable to zero.
Definition bool.cpp:51
Gecode::Int::BoolVarImp::lq
ModEvent lq(Space &home, int n)
Restrict domain values to be less or equal than n.
Definition bool.hpp:214
Gecode::Int::BoolVarImp::regret_min
unsigned int regret_min(void) const
Return regret of domain minimum (distance to next larger value).
Definition bool.hpp:101
Gecode::Int::BoolVarImp::range
bool range(void) const
Test whether domain is a range.
Definition bool.hpp:81
Gecode::Int::BoolVarImp::status
BoolStatus status(void) const
Return current domain status.
Definition bool.hpp:58
Gecode::Int::BoolVarImp::minus_v
ModEvent minus_v(Space &home, I &i, bool depends=true)
Remove from domain the values described by i.
Definition bool.hpp:370
Gecode::Int::BoolVarImp::val
int val(void) const
Return assigned value (only if assigned).
Definition bool.hpp:75
Gecode::Int::BoolVarImp::modevent
static ModEvent modevent(const Delta &d)
Return modification event.
Definition bool.hpp:149
Gecode::Int::BoolVarImp::cancel
void cancel(Space &home, Propagator &p, PropCond pc)
Cancel subscription of propagator p with propagation condition pc.
Definition bool.hpp:395
Gecode::Int::IntDelta
Integer delta information for advisors.
Definition var-imp.hpp:51
Gecode::Propagator
Base-class for propagators.
Definition core.hpp:1064
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::VarImp< Gecode::Int::BoolVarImpConf >::fail
ModEvent fail(Space &home)
Definition core.hpp:4570
Gecode::VarImp< Gecode::Int::BoolVarImpConf >::cancel
void cancel(Space &home)
Definition core.hpp:4487
Gecode::VarImp< Gecode::Int::BoolVarImpConf >::bits
unsigned int bits(void) const
Definition core.hpp:4196
Gecode::VarImp< Gecode::Int::BoolVarImpConf >::copied
bool copied(void) const
Definition core.hpp:4222
Gecode::VarImp< Gecode::Int::BoolVarImpConf >::forward
VarImp * forward(void) const
Definition core.hpp:4228
Gecode::VarImp< Gecode::Int::BoolVarImpConf >::me
static ModEvent me(const ModEventDelta &med)
Definition core.hpp:4270
Gecode::VarImp< Gecode::Int::BoolVarImpConf >::med
static ModEventDelta med(ModEvent me)
Definition core.hpp:4276
Gecode::ModEventDelta
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
Gecode::Int
Finite domain integers.
Gecode::Int::ME_BOOL_NONE
const Gecode::ModEvent ME_BOOL_NONE
Domain operation has not changed domain.
Definition var-type.hpp:114
Gecode::Int::ME_BOOL_FAILED
const Gecode::ModEvent ME_BOOL_FAILED
Domain operation has resulted in failure.
Definition var-type.hpp:112
Gecode::Int::ME_BOOL_VAL
const Gecode::ModEvent ME_BOOL_VAL
Domain operation has resulted in a value (assigned variable).
Definition var-type.hpp:116
Gecode::Int::PC_BOOL_VAL
const Gecode::PropCond PC_BOOL_VAL
Propagate when a view becomes assigned (single value).
Definition var-type.hpp:126
Gecode::Int::BoolStatus
unsigned int BoolStatus
Type for status of a Boolean variable.
Definition var-imp.hpp:484
Gecode::Int::ME_INT_NONE
const Gecode::ModEvent ME_INT_NONE
Domain operation has not changed domain.
Definition var-type.hpp:54
Gecode
Gecode toplevel namespace
Gecode::PropCond
int PropCond
Type for propagation conditions.
Definition core.hpp:72
Gecode::ModEvent
int ModEvent
Type for modification events.
Definition core.hpp:62
forceinline
#define forceinline
Definition config.hpp:187