/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2002 * * Last modified: * $Date: 2011-09-20 21:58:39 +1000 (Tue, 20 Sep 2011) $ by $Author: schulte $ * $Revision: 12404 $ * * 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 namespace Gecode { void count(Home home, const IntVarArgs& x, int n, IntRelType r, int m, IntConLevel) { using namespace Int; Limits::check(n,"Int::count"); Limits::check(m,"Int::count"); if (home.failed()) return; ViewArray xv(home,x); ConstIntView y(n); switch (r) { case IRT_EQ: GECODE_ES_FAIL((Count::EqInt ::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 ::post(home,xv,y,z,0))); } break; case IRT_LE: m--; // FALL THROUGH case IRT_LQ: GECODE_ES_FAIL((Count::LqInt ::post(home,xv,y,m))); break; case IRT_GR: m++; // FALL THROUGH case IRT_GQ: GECODE_ES_FAIL((Count::GqInt ::post(home,xv,y,m))); break; default: throw UnknownRelation("Int::count"); } } void count(Home home, const IntVarArgs& x, IntVar y, IntRelType r, int m, IntConLevel icl) { using namespace Int; Limits::check(m,"Int::count"); if (home.failed()) return; ViewArray xv(home,x); switch (r) { case IRT_EQ: { ConstIntView z(m); if ((icl == ICL_DOM) || (icl == ICL_DEF)) GECODE_ES_FAIL((Count::EqView ::post(home,xv,y,z,0))); else GECODE_ES_FAIL((Count::EqView ::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 ::post(home,xv,y,z,0))); } break; case IRT_LE: m--; // FALL THROUGH case IRT_LQ: GECODE_ES_FAIL((Count::LqInt ::post(home,xv,y,m))); break; case IRT_GR: m++; // FALL THROUGH case IRT_GQ: { ConstIntView z(m); if ((icl == ICL_DOM) || (icl == ICL_DEF)) GECODE_ES_FAIL((Count::GqView ::post(home,xv,y,z,0))); else GECODE_ES_FAIL((Count::GqView ::post(home,xv,y,z,0))); } break; default: throw UnknownRelation("Int::count"); } } void count(Home home, const IntVarArgs& x, const IntSet& y, IntRelType r, int m, IntConLevel) { using namespace Int; if (y.size() == 1) { count(home,x,y.min(),r,m); return; } Limits::check(y.min(),"Int::count"); Limits::check(y.max(),"Int::count"); Limits::check(m,"Int::count"); if (home.failed()) return; ViewArray xv(home,x); switch (r) { case IRT_EQ: GECODE_ES_FAIL((Count::EqInt::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 ::post(home,xv,y,z,0))); } break; case IRT_LE: m--; // FALL THROUGH case IRT_LQ: GECODE_ES_FAIL((Count::LqInt::post(home,xv,y,m))); break; case IRT_GR: m++; // FALL THROUGH case IRT_GQ: GECODE_ES_FAIL((Count::GqInt::post(home,xv,y,m))); break; default: throw UnknownRelation("Int::count"); } } void count(Home home, const IntVarArgs& x, const IntArgs& y, IntRelType r, int m, IntConLevel) { using namespace Int; if (x.size() != y.size()) throw ArgumentSizeMismatch("Int::count"); Limits::check(m,"Int::count"); if (home.failed()) return; ViewArray xy(home,x.size()); for (int i=x.size(); i--; ) xy[i] = OffsetView(x[i],-y[i]); ZeroIntView zero; switch (r) { case IRT_EQ: GECODE_ES_FAIL((Count::EqInt ::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 ::post(home,xy,zero,z,0))); } break; case IRT_LE: m--; // FALL THROUGH case IRT_LQ: GECODE_ES_FAIL((Count::LqInt ::post(home,xy,zero,m))); break; case IRT_GR: m++; // FALL THROUGH case IRT_GQ: GECODE_ES_FAIL((Count::GqInt ::post(home,xy,zero,m))); break; default: throw UnknownRelation("Int::count"); } } void count(Home home, const IntVarArgs& x, int n, IntRelType r, IntVar z, IntConLevel) { using namespace Int; Limits::check(n,"Int::count"); if (home.failed()) return; ViewArray xv(home,x); ConstIntView yv(n); switch (r) { case IRT_EQ: GECODE_ES_FAIL((Count::EqView ::post(home,xv,yv,z,0))); break; case IRT_NQ: { IntVar nz(home,0,x.size()); GECODE_ES_FAIL(Rel::Nq::post(home,z,nz)); GECODE_ES_FAIL((Count::EqView ::post(home,xv,yv,nz,0))); } break; case IRT_LE: GECODE_ES_FAIL((Count::LqView ::post(home,xv,yv,z,-1))); break; case IRT_LQ: GECODE_ES_FAIL((Count::LqView ::post(home,xv,yv,z,0))); break; case IRT_GR: GECODE_ES_FAIL((Count::GqView ::post(home,xv,yv,z,1))); break; case IRT_GQ: GECODE_ES_FAIL((Count::GqView ::post(home,xv,yv,z,0))); break; default: throw UnknownRelation("Int::count"); } } void count(Home home, const IntVarArgs& x, IntVar y, IntRelType r, IntVar z, IntConLevel icl) { using namespace Int; if (home.failed()) return; ViewArray xv(home,x); switch (r) { case IRT_EQ: if ((icl == ICL_DOM) || (icl == ICL_DEF)) GECODE_ES_FAIL((Count::EqView ::post(home,xv,y,z,0))); else GECODE_ES_FAIL((Count::EqView ::post(home,xv,y,z,0))); break; case IRT_NQ: { IntVar nz(home,0,x.size()); GECODE_ES_FAIL(Rel::Nq::post(home,z,nz)); GECODE_ES_FAIL((Count::EqView ::post(home,xv,y,nz,0))); } break; case IRT_LE: GECODE_ES_FAIL((Count::LqView ::post(home,xv,y,z,-1))); break; case IRT_LQ: GECODE_ES_FAIL((Count::LqView ::post(home,xv,y,z,0))); break; case IRT_GR: if ((icl == ICL_DOM) || (icl == ICL_DEF)) GECODE_ES_FAIL((Count::GqView ::post(home,xv,y,z,1))); else GECODE_ES_FAIL((Count::GqView ::post(home,xv,y,z,0))); break; case IRT_GQ: if ((icl == ICL_DOM) || (icl == ICL_DEF)) GECODE_ES_FAIL((Count::GqView ::post(home,xv,y,z,0))); else GECODE_ES_FAIL((Count::GqView ::post(home,xv,y,z,0))); break; default: throw UnknownRelation("Int::count"); } } void count(Home home, const IntVarArgs& x, const IntSet& y, IntRelType r, IntVar z, IntConLevel) { using namespace Int; if (y.size() == 1) { count(home,x,y.min(),r,z); return; } Limits::check(y.min(),"Int::count"); Limits::check(y.max(),"Int::count"); if (home.failed()) return; ViewArray xv(home,x); switch (r) { case IRT_EQ: GECODE_ES_FAIL((Count::EqView ::post(home,xv,y,z,0))); break; case IRT_NQ: { IntVar nz(home,0,x.size()); GECODE_ES_FAIL(Rel::Nq::post(home,z,nz)); GECODE_ES_FAIL((Count::EqView ::post(home,xv,y,nz,0))); } break; case IRT_LE: GECODE_ES_FAIL((Count::LqView ::post(home,xv,y,z,-1))); break; case IRT_LQ: GECODE_ES_FAIL((Count::LqView ::post(home,xv,y,z,0))); break; case IRT_GR: GECODE_ES_FAIL((Count::GqView ::post(home,xv,y,z,1))); break; case IRT_GQ: GECODE_ES_FAIL((Count::GqView ::post(home,xv,y,z,0))); break; default: throw UnknownRelation("Int::count"); } } void count(Home home, const IntVarArgs& x, const IntArgs& y, IntRelType r, IntVar z, IntConLevel) { using namespace Int; if (x.size() != y.size()) throw ArgumentSizeMismatch("Int::count"); if (home.failed()) return; ViewArray xy(home,x.size()); for (int i=x.size(); i--; ) xy[i] = OffsetView(x[i],-y[i]); ZeroIntView u; switch (r) { case IRT_EQ: GECODE_ES_FAIL((Count::EqView ::post(home,xy,u,z,0))); break; case IRT_NQ: { IntVar nz(home,0,x.size()); GECODE_ES_FAIL(Rel::Nq::post(home,z,nz)); GECODE_ES_FAIL((Count::EqView ::post(home,xy,u,nz,0))); } break; case IRT_LE: GECODE_ES_FAIL((Count::LqView ::post(home,xy,u,z,-1))); break; case IRT_LQ: GECODE_ES_FAIL((Count::LqView ::post(home,xy,u,z,0))); break; case IRT_GR: GECODE_ES_FAIL((Count::GqView ::post(home,xy,u,z,1))); break; case IRT_GQ: GECODE_ES_FAIL((Count::GqView ::post(home,xy,u,z,0))); break; default: throw UnknownRelation("Int::count"); } } } // STATISTICS: int-post