Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
unshare.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2007
8  *
9  * Last modified:
10  * $Date$ by $Author$
11  * $Revision$
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/int/rel.hh>
39 #include <gecode/int/bool.hh>
40 
46 namespace Gecode { namespace Int { namespace Unshare {
47 
49  template<class Var>
50  class VarPtrLess {
51  public:
52  forceinline bool
53  operator ()(const Var* a, const Var* b) {
54  return a->before(*b);
55  }
56  };
57 
58 
60  forceinline void
61  link(Home home, IntVar** x, int n, IntPropLevel ipl) {
62  if (home.failed()) {
63  for (int i=1; i<n; i++)
64  *x[i]=IntVar(home,x[0]->min(),x[0]->min());
65  } else if (n > 2) {
66  ViewArray<IntView> y(home,n);
67  y[0]=*x[0];
68  for (int i=1; i<n; i++)
69  y[i]=*x[i]=IntVar(home,x[0]->min(),x[0]->max());
70  if ((ipl == IPL_DOM) || (ipl == IPL_DEF)) {
72  (void) es; assert(es == ES_OK);
73  } else {
75  (void) es; assert(es == ES_OK);
76  }
77  } else if (n == 2) {
78  *x[1]=IntVar(home,x[0]->min(),x[0]->max());
79  if ((ipl == IPL_DOM) || (ipl == IPL_DEF)) {
80  ExecStatus es = Rel::EqDom<IntView,IntView>::post(home,*x[0],*x[1]);
81  (void) es; assert(es == ES_OK);
82  } else {
83  ExecStatus es = Rel::EqBnd<IntView,IntView>::post(home,*x[0],*x[1]);
84  (void) es; assert(es == ES_OK);
85  }
86  }
87  }
88 
90  forceinline void
91  link(Home home, BoolVar** x, int n, IntPropLevel) {
92  if (home.failed()) {
93  for (int i=1; i<n; i++)
94  *x[i]=BoolVar(home,0,0);
95  } else if (n > 2) {
96  ViewArray<BoolView> y(home,n);
97  y[0]=*x[0];
98  for (int i=1; i<n; i++)
99  y[i]=*x[i]=BoolVar(home,0,1);
101  (void) es; assert(es == ES_OK);
102  } else if (n == 2) {
103  *x[1] = BoolVar(home,0,1);
104  ExecStatus es = Bool::Eq<BoolView,BoolView>::post(home,*x[0],*x[1]);
105  (void) es; assert(es == ES_OK);
106  }
107  }
108 
110  template<class Var>
111  forceinline void
113  int n=x.size();
114  if (n < 2)
115  return;
116 
117  Region r;
118  Var** y = r.alloc<Var*>(n);
119  for (int i=n; i--; )
120  y[i]=&x[i];
121 
122  VarPtrLess<Var> vpl;
123  Support::quicksort<Var*,VarPtrLess<Var> >(y,n,vpl);
124 
125  // Replace all shared variables with new and equal variables
126  for (int i=0; i<n;) {
127  int j=i++;
128  while ((i<n) && y[j]->same(*y[i]))
129  i++;
130  if (!y[j]->assigned())
131  link(home,&y[j],i-j,ipl);
132  }
133  }
134 
135 }}}
136 
137 namespace Gecode {
138 
139  void
141  Int::Unshare::unshare<IntVar>(home,x,vbd(ipl));
142  }
143 
144  void
146  Int::Unshare::unshare<BoolVar>(home,x,IPL_DEF);
147  }
148 
149 }
150 
151 // STATISTICS: int-post
IntPropLevel vbd(IntPropLevel ipl)
Extract value, bounds, or domain propagation from propagation level.
Definition: ipl.hpp:41
static ExecStatus post(Home home, ViewArray< BV > &x)
Post propagator .
Definition: eq.hpp:135
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1657
static ExecStatus post(Home home, ViewArray< View > &x)
Post bounds consistent propagator .
Definition: eq.hpp:406
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition: region.hpp:384
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:53
static ExecStatus post(Home home, View0 x0, View1 x1)
Post domain consistent propagator .
Definition: eq.hpp:180
void unshare(Home home, VarArgArray< Var > &x, IntPropLevel ipl)
Replace unassigned shared variables by fresh, yet equal variables.
Definition: unshare.cpp:112
Handle to region.
Definition: region.hpp:57
#define forceinline
Definition: config.hpp:182
static ExecStatus post(Home home, View0 x0, View1 x1)
Post bounds consistent propagator .
Definition: eq.hpp:112
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
static ExecStatus post(Home home, BVA x0, BVB x1)
Post propagator .
Definition: eq.hpp:64
Simple propagation levels.
Definition: int.hh:955
bool failed(void) const
Check whether corresponding space is failed.
Definition: core.hpp:3918
void link(Home home, IntVar **x, int n, IntPropLevel ipl)
Replace by fresh yet equal integer variables.
Definition: unshare.cpp:61
Passing integer variables.
Definition: int.hh:637
Passing Boolean variables.
Definition: int.hh:691
Boolean integer variables.
Definition: int.hh:492
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:769
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:71
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:953
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:769
Base class for variables.
Definition: var.hpp:44
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
int min(void) const
Return minimum of domain.
Definition: int.hpp:66
Sort order for variables.
Definition: unshare.cpp:50
ExecStatus
Definition: core.hpp:464
Integer variables.
Definition: int.hh:351
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:958
Post propagator for SetVar x
Definition: set.hh:769
Execution is okay.
Definition: core.hpp:468
Gecode toplevel namespace
Argument array for variables.
Definition: array.hpp:53
static ExecStatus post(Home home, ViewArray< View > &x)
Post domain consistent propagator .
Definition: eq.hpp:274
Home class for posting propagators
Definition: core.hpp:846
bool operator()(const Var *a, const Var *b)
Definition: unshare.cpp:53