count.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>
*
* Copyright:
* Christian Schulte, 2002
*
* 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/int/count.hh>
#include <gecode/int/rel.hh>
namespace Gecode {
void
count(Home home, const IntVarArgs& x, int n,
IntRelType irt, int m, IntPropLevel) {
using namespace Int;
Limits::check(n,"Int::count");
Limits::check(m,"Int::count");
GECODE_POST;
ViewArray<IntView> xv(home,x);
ConstIntView y(n);
switch (irt) {
case IRT_EQ:
GECODE_ES_FAIL((Count::EqInt<IntView,ConstIntView>
::post(home,xv,y,m)));
break;
case IRT_NQ:
{
IntVar z(home,0,x.size());
GECODE_ME_FAIL(IntView(z).nq(home,m));
GECODE_ES_FAIL((Count::EqView<IntView,ConstIntView,IntView,true,false>
::post(home,xv,y,z,0)));
}
break;
case IRT_LE:
m--; // FALL THROUGH
case IRT_LQ:
GECODE_ES_FAIL((Count::LqInt<IntView,ConstIntView>
::post(home,xv,y,m)));
break;
case IRT_GR:
m++; // FALL THROUGH
case IRT_GQ:
GECODE_ES_FAIL((Count::GqInt<IntView,ConstIntView>
::post(home,xv,y,m)));
break;
default:
throw UnknownRelation("Int::count");
}
}
void
count(Home home, const IntVarArgs& x, IntVar y,
IntRelType irt, int m, IntPropLevel ipl) {
using namespace Int;
Limits::check(m,"Int::count");
GECODE_POST;
ViewArray<IntView> xv(home,x);
switch (irt) {
case IRT_EQ:
{
ConstIntView z(m);
if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
GECODE_ES_FAIL((Count::EqView<IntView,IntView,ConstIntView,true,true>
::post(home,xv,y,z,0)));
else
GECODE_ES_FAIL((Count::EqView<IntView,IntView,ConstIntView,true,false>
::post(home,xv,y,z,0)));
}
break;
case IRT_NQ:
{
IntVar z(home,0,x.size());
GECODE_ME_FAIL(IntView(z).nq(home,m));
GECODE_ES_FAIL((Count::EqView<IntView,IntView,IntView,true,false>
::post(home,xv,y,z,0)));
}
break;
case IRT_LE:
m--; // FALL THROUGH
case IRT_LQ:
GECODE_ES_FAIL((Count::LqInt<IntView,IntView>
::post(home,xv,y,m)));
break;
case IRT_GR:
m++; // FALL THROUGH
case IRT_GQ:
{
ConstIntView z(m);
if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
GECODE_ES_FAIL((Count::GqView<IntView,IntView,ConstIntView,true,true>
::post(home,xv,y,z,0)));
else
GECODE_ES_FAIL((Count::GqView<IntView,IntView,ConstIntView,true,false>
::post(home,xv,y,z,0)));
}
break;
default:
throw UnknownRelation("Int::count");
}
}
void
count(Home home, const IntVarArgs& x, const IntSet& y,
IntRelType irt, int m, IntPropLevel) {
using namespace Int;
if (y.size() == 1) {
count(home,x,y.min(),irt,m);
return;
}
Limits::check(y.min(),"Int::count");
Limits::check(y.max(),"Int::count");
Limits::check(m,"Int::count");
GECODE_POST;
ViewArray<IntView> xv(home,x);
switch (irt) {
case IRT_EQ:
GECODE_ES_FAIL((Count::EqInt<IntView,IntSet>::post(home,xv,y,m)));
break;
case IRT_NQ:
{
IntVar z(home,0,x.size());
GECODE_ME_FAIL(IntView(z).nq(home,m));
GECODE_ES_FAIL((Count::EqView<IntView,IntSet,IntView,true,false>
::post(home,xv,y,z,0)));
}
break;
case IRT_LE:
m--; // FALL THROUGH
case IRT_LQ:
GECODE_ES_FAIL((Count::LqInt<IntView,IntSet>::post(home,xv,y,m)));
break;
case IRT_GR:
m++; // FALL THROUGH
case IRT_GQ:
GECODE_ES_FAIL((Count::GqInt<IntView,IntSet>::post(home,xv,y,m)));
break;
default:
throw UnknownRelation("Int::count");
}
}
void
count(Home home, const IntVarArgs& x, const IntArgs& y,
IntRelType irt, int m, IntPropLevel) {
using namespace Int;
if (x.size() != y.size())
throw ArgumentSizeMismatch("Int::count");
Limits::check(m,"Int::count");
GECODE_POST;
ViewArray<OffsetView> xy(home,x.size());
for (int i=0; i<x.size(); i++)
xy[i] = OffsetView(x[i],-y[i]);
ZeroIntView zero;
switch (irt) {
case IRT_EQ:
GECODE_ES_FAIL((Count::EqInt<OffsetView,ZeroIntView>
::post(home,xy,zero,m)));
break;
case IRT_NQ:
{
IntVar z(home,0,x.size());
GECODE_ME_FAIL(IntView(z).nq(home,m));
GECODE_ES_FAIL((Count::EqView<OffsetView,ZeroIntView,IntView,true,false>
::post(home,xy,zero,z,0)));
}
break;
case IRT_LE:
m--; // FALL THROUGH
case IRT_LQ:
GECODE_ES_FAIL((Count::LqInt<OffsetView,ZeroIntView>
::post(home,xy,zero,m)));
break;
case IRT_GR:
m++; // FALL THROUGH
case IRT_GQ:
GECODE_ES_FAIL((Count::GqInt<OffsetView,ZeroIntView>
::post(home,xy,zero,m)));
break;
default:
throw UnknownRelation("Int::count");
}
}
void
count(Home home, const IntVarArgs& x, int n,
IntRelType irt, IntVar z, IntPropLevel) {
using namespace Int;
Limits::check(n,"Int::count");
GECODE_POST;
ViewArray<IntView> xv(home,x);
ConstIntView yv(n);
switch (irt) {
case IRT_EQ:
GECODE_ES_FAIL((Count::EqView<IntView,ConstIntView,IntView,true,false>
::post(home,xv,yv,z,0)));
break;
case IRT_NQ:
{
IntVar nz(home,0,x.size());
GECODE_ES_FAIL((Rel::Nq<IntView,IntView>::post(home,z,nz)));
GECODE_ES_FAIL((Count::EqView<IntView,ConstIntView,IntView,true,false>
::post(home,xv,yv,nz,0)));
}
break;
case IRT_LE:
GECODE_ES_FAIL((Count::LqView<IntView,ConstIntView,IntView,true>
::post(home,xv,yv,z,-1)));
break;
case IRT_LQ:
GECODE_ES_FAIL((Count::LqView<IntView,ConstIntView,IntView,true>
::post(home,xv,yv,z,0)));
break;
case IRT_GR:
GECODE_ES_FAIL((Count::GqView<IntView,ConstIntView,IntView,true,false>
::post(home,xv,yv,z,1)));
break;
case IRT_GQ:
GECODE_ES_FAIL((Count::GqView<IntView,ConstIntView,IntView,true,false>
::post(home,xv,yv,z,0)));
break;
default:
throw UnknownRelation("Int::count");
}
}
void
count(Home home, const IntVarArgs& x, IntVar y,
IntRelType irt, IntVar z, IntPropLevel ipl) {
using namespace Int;
GECODE_POST;
ViewArray<IntView> xv(home,x);
switch (irt) {
case IRT_EQ:
if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
GECODE_ES_FAIL((Count::EqView<IntView,IntView,IntView,true,true>
::post(home,xv,y,z,0)));
else
GECODE_ES_FAIL((Count::EqView<IntView,IntView,IntView,true,false>
::post(home,xv,y,z,0)));
break;
case IRT_NQ:
{
IntVar nz(home,0,x.size());
GECODE_ES_FAIL((Rel::Nq<IntView,IntView>::post(home,z,nz)));
GECODE_ES_FAIL((Count::EqView<IntView,IntView,IntView,true,false>
::post(home,xv,y,nz,0)));
}
break;
case IRT_LE:
GECODE_ES_FAIL((Count::LqView<IntView,IntView,IntView,true>
::post(home,xv,y,z,-1)));
break;
case IRT_LQ:
GECODE_ES_FAIL((Count::LqView<IntView,IntView,IntView,true>
::post(home,xv,y,z,0)));
break;
case IRT_GR:
if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
GECODE_ES_FAIL((Count::GqView<IntView,IntView,IntView,true,true>
::post(home,xv,y,z,1)));
else
GECODE_ES_FAIL((Count::GqView<IntView,IntView,IntView,true,false>
::post(home,xv,y,z,1)));
break;
case IRT_GQ:
if ((vbd(ipl) == IPL_DOM) || (vbd(ipl) == IPL_DEF))
GECODE_ES_FAIL((Count::GqView<IntView,IntView,IntView,true,true>
::post(home,xv,y,z,0)));
else
GECODE_ES_FAIL((Count::GqView<IntView,IntView,IntView,true,false>
::post(home,xv,y,z,0)));
break;
default:
throw UnknownRelation("Int::count");
}
}
void
count(Home home, const IntVarArgs& x, const IntSet& y,
IntRelType irt, IntVar z, IntPropLevel) {
using namespace Int;
if (y.size() == 1) {
count(home,x,y.min(),irt,z);
return;
}
Limits::check(y.min(),"Int::count");
Limits::check(y.max(),"Int::count");
GECODE_POST;
ViewArray<IntView> xv(home,x);
switch (irt) {
case IRT_EQ:
GECODE_ES_FAIL((Count::EqView<IntView,IntSet,IntView,true,false>
::post(home,xv,y,z,0)));
break;
case IRT_NQ:
{
IntVar nz(home,0,x.size());
GECODE_ES_FAIL((Rel::Nq<IntView,IntView>::post(home,z,nz)));
GECODE_ES_FAIL((Count::EqView<IntView,IntSet,IntView,true,false>
::post(home,xv,y,nz,0)));
}
break;
case IRT_LE:
GECODE_ES_FAIL((Count::LqView<IntView,IntSet,IntView,true>
::post(home,xv,y,z,-1)));
break;
case IRT_LQ:
GECODE_ES_FAIL((Count::LqView<IntView,IntSet,IntView,true>
::post(home,xv,y,z,0)));
break;
case IRT_GR:
GECODE_ES_FAIL((Count::GqView<IntView,IntSet,IntView,true,false>
::post(home,xv,y,z,1)));
break;
case IRT_GQ:
GECODE_ES_FAIL((Count::GqView<IntView,IntSet,IntView,true,false>
::post(home,xv,y,z,0)));
break;
default:
throw UnknownRelation("Int::count");
}
}
void
count(Home home, const IntVarArgs& x, const IntArgs& y,
IntRelType irt, IntVar z, IntPropLevel) {
using namespace Int;
if (x.size() != y.size())
throw ArgumentSizeMismatch("Int::count");
GECODE_POST;
ViewArray<OffsetView> xy(home,x.size());
for (int i=0; i<x.size(); i++)
xy[i] = OffsetView(x[i],-y[i]);
ZeroIntView u;
switch (irt) {
case IRT_EQ:
GECODE_ES_FAIL((Count::EqView<OffsetView,ZeroIntView,IntView,true,false>
::post(home,xy,u,z,0)));
break;
case IRT_NQ:
{
IntVar nz(home,0,x.size());
GECODE_ES_FAIL((Rel::Nq<IntView,IntView>::post(home,z,nz)));
GECODE_ES_FAIL((Count::EqView<OffsetView,ZeroIntView,IntView,true,false>
::post(home,xy,u,nz,0)));
}
break;
case IRT_LE:
GECODE_ES_FAIL((Count::LqView<OffsetView,ZeroIntView,IntView,true>
::post(home,xy,u,z,-1)));
break;
case IRT_LQ:
GECODE_ES_FAIL((Count::LqView<OffsetView,ZeroIntView,IntView,true>
::post(home,xy,u,z,0)));
break;
case IRT_GR:
GECODE_ES_FAIL((Count::GqView<OffsetView,ZeroIntView,IntView,true,false>
::post(home,xy,u,z,1)));
break;
case IRT_GQ:
GECODE_ES_FAIL((Count::GqView<OffsetView,ZeroIntView,IntView,true,false>
::post(home,xy,u,z,0)));
break;
default:
throw UnknownRelation("Int::count");
}
}
}
// STATISTICS: int-post
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::IntArgs
Passing integer arguments.
Definition int.hh:628
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntSet::min
int min(int i) const
Return minimum of range at position i.
Definition int-set-1.hpp:152
Gecode::IntSet::max
int max(int i) const
Return maximum of range at position i.
Definition int-set-1.hpp:158
Gecode::IntSet::size
unsigned int size(void) const
Return size (cardinality) of set.
Definition int-set-1.hpp:198
Gecode::IntVarArgs
Passing integer variables.
Definition int.hh:656
Gecode::IntVar
Integer variables.
Definition int.hh:371
Gecode::Int::ArgumentSizeMismatch
Exception: Arguments are of different size
Definition exception.hpp:73
Gecode::Int::ConstIntView
Constant integer view.
Definition view.hpp:851
Gecode::Int::Count::EqInt
Propagator for counting views (equal integer to number of equal views)
Definition count.hh:172
Gecode::Int::Count::EqInt::post
static ExecStatus post(Home home, ViewArray< VX > &x, VY y, int c)
Post propagator for .
Definition int-eq.hpp:43
Gecode::Int::Count::EqView
Propagator for counting views (equal to number of equal views)
Definition count.hh:309
Gecode::Int::Count::GqInt
Propagator for counting views (greater or equal integer to number of equal views)
Definition count.hh:202
Gecode::Int::Count::GqInt::post
static ExecStatus post(Home home, ViewArray< VX > &x, VY y, int c)
Post propagator for .
Definition int-gq.hpp:43
Gecode::Int::Count::GqView
Propagator for counting views (greater or equal to number of equal views)
Definition count.hh:379
Gecode::Int::Count::LqInt
Propagator for counting views (less or equal integer to number of equal views)
Definition count.hh:232
Gecode::Int::Count::LqInt::post
static ExecStatus post(Home home, ViewArray< VX > &x, VY y, int c)
Post propagator for .
Definition int-lq.hpp:43
Gecode::Int::Count::LqView
Propagator for counting views (less or equal to number of equal views)
Definition count.hh:344
Gecode::Int::IntView
Integer view for integer variables.
Definition view.hpp:129
Gecode::Int::OffsetView
Offset integer view.
Definition view.hpp:443
Gecode::Int::Rel::Nq::post
static ExecStatus post(Home home, V0 x0, V1 x1)
Post propagator .
Definition nq.hpp:49
Gecode::Int::UnknownRelation
Exception: Unknown relation passed as argument
Definition exception.hpp:87
Gecode::Int::ZeroIntView
Zero integer view.
Definition view.hpp:1014
Gecode::ViewArray
View arrays.
Definition array.hpp:253
count.hh
GECODE_POST
#define GECODE_POST
Check for failure in a constraint post function.
Definition macros.hpp:40
GECODE_ES_FAIL
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition macros.hpp:103
GECODE_ME_FAIL
#define GECODE_ME_FAIL(me)
Check whether modification event me is failed, and fail space home.
Definition macros.hpp:77
Gecode::IntRelType
IntRelType
Relation types for integers.
Definition int.hh:925
Gecode::IntPropLevel
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:974
Gecode::IRT_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
Gecode::IRT_NQ
@ IRT_NQ
Disequality ( ).
Definition int.hh:927
Gecode::IRT_GQ
@ IRT_GQ
Greater or equal ( ).
Definition int.hh:930
Gecode::IRT_LE
@ IRT_LE
Less ( ).
Definition int.hh:929
Gecode::IRT_GR
@ IRT_GR
Greater ( ).
Definition int.hh:931
Gecode::IRT_LQ
@ IRT_LQ
Less or equal ( ).
Definition int.hh:928
Gecode::IPL_DOM
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
Gecode::IPL_DEF
@ IPL_DEF
Simple propagation levels.
Definition int.hh:976
rel.hh
Gecode::Int::Limits::check
void check(int n, const char *l)
Check whether n is in range, otherwise throw out of limits with information l.
Definition limits.hpp:46
Gecode::Int
Finite domain integers.
Gecode
Gecode toplevel namespace
Gecode::count
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition count.cpp:40
Gecode::vbd
IntPropLevel vbd(IntPropLevel ipl)
Extract value, bounds, or domain propagation from propagation level.
Definition ipl.hpp:37
Gecode::post
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition filter.cpp:138