view-values.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, 2008
*
* 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 { namespace Branch {
class GECODE_VTABLE_EXPORT PosValuesChoice : public PosChoice {
private:
class PosMin {
public:
unsigned int pos;
int min;
};
unsigned int n;
PosMin* pm;
public:
GECODE_INT_EXPORT
PosValuesChoice(const Brancher& b, const Pos& p, IntView x);
GECODE_INT_EXPORT
PosValuesChoice(const Brancher& b, unsigned int alt, Pos p, Archive& e);
int val(unsigned int a) const;
GECODE_INT_EXPORT
virtual ~PosValuesChoice(void);
GECODE_INT_EXPORT
virtual void archive(Archive& e) const;
};
forceinline int
PosValuesChoice::val(unsigned int a) const {
PosMin* l = &pm[0];
PosMin* r = &pm[n-1];
while (true) {
PosMin* m = l + (r-l)/2;
if (a < m->pos) {
r=m-1;
} else if (a >= (m+1)->pos) {
l=m+1;
} else {
return m->min + static_cast<int>(a - m->pos);
}
}
GECODE_NEVER;
return 0;
}
template<int n, bool min, class Filter, class Print>
forceinline
ViewValuesBrancher<n,min,Filter,Print>::
ViewValuesBrancher(Home home, ViewArray<IntView>& x,
ViewSel<IntView>* vs[n],
IntBranchFilter bf,
IntVarValPrint vvp)
: ViewBrancher<IntView,Filter,n>(home,x,vs,bf), p(vvp) {
if (p.notice())
home.notice(*this,AP_DISPOSE,true);
}
template<int n, bool min, class Filter, class Print>
forceinline void
ViewValuesBrancher<n,min,Filter,Print>::post(Home home,
ViewArray<IntView>& x,
ViewSel<IntView>* vs[n],
IntBranchFilter bf,
IntVarValPrint vvp) {
(void) new (home) ViewValuesBrancher<n,min,Filter,Print>(home,x,vs,bf,vvp);
}
template<int n, bool min, class Filter, class Print>
forceinline
ViewValuesBrancher<n,min,Filter,Print>::
ViewValuesBrancher(Space& home, ViewValuesBrancher& b)
: ViewBrancher<IntView,Filter,n>(home,b), p(b.p) {}
template<int n, bool min, class Filter, class Print>
Actor*
ViewValuesBrancher<n,min,Filter,Print>::copy(Space& home) {
return new (home) ViewValuesBrancher<n,min,Filter,Print>
(home,*this);
}
template<int n, bool min, class Filter, class Print>
const Choice*
ViewValuesBrancher<n,min,Filter,Print>::choice(Space& home) {
Pos p = ViewBrancher<IntView,Filter,n>::pos(home);
return new PosValuesChoice(*this,p,
ViewBrancher<IntView,Filter,n>::view(p));
}
template<int n, bool min, class Filter, class Print>
const Choice*
ViewValuesBrancher<n,min,Filter,Print>::choice
(const Space& home, Archive& e) {
(void) home;
int p;
unsigned int a;
e >> p >> a;
return new PosValuesChoice(*this,a,p,e);
}
template<int n, bool min, class Filter, class Print>
ExecStatus
ViewValuesBrancher<n,min,Filter,Print>::commit(Space& home, const Choice& c,
unsigned int a) {
const PosValuesChoice& pvc
= static_cast<const PosValuesChoice&>(c);
IntView x(ViewBrancher<IntView,Filter,n>::view(pvc.pos()));
unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
return me_failed(x.eq(home,pvc.val(b))) ? ES_FAILED : ES_OK;
}
template<int n, bool min, class Filter, class Print>
NGL*
ViewValuesBrancher<n,min,Filter,Print>::ngl(Space& home, const Choice& c,
unsigned int a) const {
const PosValuesChoice& pvc
= static_cast<const PosValuesChoice&>(c);
IntView x(ViewBrancher<IntView,Filter,n>::view(pvc.pos()));
unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
return new (home) EqNGL<IntView>(home,x,pvc.val(b));
}
template<int n, bool min, class Filter, class Print>
void
ViewValuesBrancher<n,min,Filter,Print>::print(const Space& home,
const Choice& c,
unsigned int a,
std::ostream& o) const {
const PosValuesChoice& pvc
= static_cast<const PosValuesChoice&>(c);
IntView x(ViewBrancher<IntView,Filter,n>::view(pvc.pos()));
unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
int nn = pvc.val(b);
if (p)
p(home,*this,a,x,pvc.pos().pos,nn,o);
else
o << "var[" << pvc.pos().pos << "] = " << nn;
}
template<int n, bool min, class Filter, class Print>
forceinline size_t
ViewValuesBrancher<n,min,Filter,Print>::dispose(Space& home) {
if (p.notice())
home.ignore(*this,AP_DISPOSE,true);
(void) ViewBrancher<IntView,Filter,n>::dispose(home);
return sizeof(ViewValuesBrancher<n,min,Filter,Print>);
}
template<int n, bool min>
forceinline void
postviewvaluesbrancher(Home home, ViewArray<IntView>& x,
ViewSel<IntView>* vs[n],
IntBranchFilter bf,
IntVarValPrint vvp) {
if (bf) {
if (vvp) {
ViewValuesBrancher<n,min,BrancherFilter<IntView>,
BrancherPrint<IntView,int> >
::post(home,x,vs,bf,vvp);
} else {
ViewValuesBrancher<n,min,BrancherFilter<IntView>,
BrancherNoPrint<IntView,int> >
::post(home,x,vs,bf,vvp);
}
} else {
if (vvp) {
ViewValuesBrancher<n,min,BrancherNoFilter<IntView>,
BrancherPrint<IntView,int> >
::post(home,x,vs,bf,vvp);
} else {
ViewValuesBrancher<n,min,BrancherNoFilter<IntView>,
BrancherNoPrint<IntView,int> >
::post(home,x,vs,bf,vvp);
}
}
}
}}}
// STATISTICS: int-branch
Gecode::Actor
Base-class for both propagators and branchers.
Definition core.hpp:628
Gecode::Archive
Archive representation
Definition archive.hpp:42
Gecode::BrancherNoPrint
Class without print function.
Definition print.hpp:73
Gecode::BrancherPrint
Class storing a print function.
Definition print.hpp:47
Gecode::Brancher
Base-class for branchers.
Definition core.hpp:1442
Gecode::Brancher::Space
friend class Space
Definition core.hpp:1444
Gecode::Brancher::Choice
friend class Choice
Definition core.hpp:1445
Gecode::Choice
Choice for performing commit
Definition core.hpp:1412
Gecode::Choice::alternatives
unsigned int alternatives(void) const
Return number of alternatives.
Definition core.hpp:3770
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::Home::notice
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition core.hpp:3219
Gecode::Int::Branch::EqNGL
No-good literal for equality.
Definition branch.hh:411
Gecode::Int::Branch::PosValuesChoice
Choice storing position and values for integer views
Definition view-values.hpp:37
Gecode::Int::Branch::PosValuesChoice::val
int val(unsigned int a) const
Return value to branch with for alternative a.
Definition view-values.hpp:69
Gecode::Int::Branch::PosValuesChoice::PosValuesChoice
PosValuesChoice(const Brancher &b, const Pos &p, IntView x)
Initialize choice for brancher b, position p, and view x.
Definition view-values.cpp:38
Gecode::Int::Branch::PosValuesChoice::archive
virtual void archive(Archive &e) const
Archive into e.
Definition view-values.cpp:69
Gecode::Int::Branch::ViewValuesBrancher
Brancher by view and values selection
Definition branch.hh:626
Gecode::Int::Branch::ViewValuesBrancher::print
virtual void print(const Space &home, const Choice &c, unsigned int a, std::ostream &o) const
Print branch for choice c and alternative a.
Definition view-values.hpp:165
Gecode::Int::Branch::ViewValuesBrancher::p
Print p
Print function.
Definition branch.hh:631
Gecode::Int::Branch::ViewValuesBrancher::ViewValuesBrancher
ViewValuesBrancher(Space &home, ViewValuesBrancher &b)
Constructor for cloning b.
Definition view-values.hpp:112
Gecode::Int::Branch::ViewValuesBrancher::copy
virtual Actor * copy(Space &home)
Perform cloning.
Definition view-values.hpp:117
Gecode::Int::Branch::ViewValuesBrancher::post
static void post(Home home, ViewArray< IntView > &x, ViewSel< IntView > *vs[n], IntBranchFilter bf, IntVarValPrint vvp)
Post function for creation.
Definition view-values.hpp:101
Gecode::Int::Branch::ViewValuesBrancher::commit
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int a)
Perform commit for choice c and alternative a.
Definition view-values.hpp:143
Gecode::Int::Branch::ViewValuesBrancher::choice
virtual const Choice * choice(Space &home)
Return choice.
Definition view-values.hpp:124
Gecode::Int::Branch::ViewValuesBrancher::dispose
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition view-values.hpp:182
Gecode::Int::Branch::ViewValuesBrancher::ngl
virtual NGL * ngl(Space &home, const Choice &c, unsigned int a) const
Create no-good literal for choice c and alternative a.
Definition view-values.hpp:154
Gecode::Int::IntView
Integer view for integer variables.
Definition view.hpp:129
Gecode::NGL
No-good literal recorded during search.
Definition core.hpp:1340
Gecode::PosChoice::pos
const Pos & pos(void) const
Return position in array.
Definition view.hpp:126
Gecode::PosChoice::PosChoice
PosChoice(const PosChoice &c)
Initialize.
Gecode::Pos
Position information.
Definition view.hpp:44
Gecode::Pos::pos
const int pos
Position of view.
Definition view.hpp:47
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Gecode::ViewBrancher::pos
Pos pos(Space &home)
Return position information.
Definition view.hpp:173
Gecode::ViewBrancher< IntView, Filter, n >::x
ViewArray< IntView > x
Definition view.hpp:83
Gecode::ViewBrancher< IntView, Filter, n >::ViewBrancher
ViewBrancher(Space &home, ViewBrancher< IntView, Filter, n > &b)
Definition view.hpp:152
Gecode::ViewBrancher::view
View view(const Pos &p) const
Return view according to position information p.
Definition view.hpp:213
Gecode::ViewBrancher::dispose
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition view.hpp:219
Gecode::ViewBrancher< IntView, Filter, n >::vs
ViewSel< IntView > * vs[n]
Definition view.hpp:87
Gecode::ViewSel
Abstract class for view selection.
Definition view-sel.hpp:44
GECODE_INT_EXPORT
#define GECODE_INT_EXPORT
Definition int.hh:81
Gecode::Space::ignore
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4074
Gecode::me_failed
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition modevent.hpp:54
Gecode::AP_DISPOSE
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
Gecode::IntBranchFilter
std::function< bool(const Space &home, IntVar x, int i)> IntBranchFilter
Branch filter function type for integer variables.
Definition int.hh:4176
Gecode::Int::Branch
Integer branchers.
Gecode::Int::Branch::postviewvaluesbrancher
void postviewvaluesbrancher(Home home, ViewArray< IntView > &x, ViewSel< IntView > *vs[n], IntBranchFilter bf, IntVarValPrint vvp)
Post brancher for view and values.
Definition view-values.hpp:191
Gecode::Int
Finite domain integers.
Gecode
Gecode toplevel namespace
Gecode::min
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:67
Gecode::IntVarValPrint
std::function< void(const Space &home, const Brancher &b, unsigned int a, IntVar x, int i, const int &n, std::ostream &o)> IntVarValPrint
Function type for printing branching alternatives for integer variables.
Definition int.hh:4552
Gecode::post
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition filter.cpp:138
Gecode::ExecStatus
ExecStatus
Definition core.hpp:472
Gecode::ES_OK
@ ES_OK
Execution is okay.
Definition core.hpp:476
Gecode::ES_FAILED
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
forceinline
#define forceinline
Definition config.hpp:187
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56
GECODE_VTABLE_EXPORT
#define GECODE_VTABLE_EXPORT
Definition support.hh:72