Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include <gecode/int/rel.hh>
35#include <gecode/int/bool.hh>
36
41
42namespace Gecode { namespace Int { namespace Unshare {
43
45 template<class Var>
46 class VarPtrLess {
47 public:
48 forceinline bool
49 operator ()(const Var* a, const Var* b) {
50 return a->varimp() < b->varimp();
51 }
52 };
53
54
56 forceinline void
57 link(Home home, IntVar** x, int n, IntPropLevel ipl) {
58 if (home.failed()) {
59 for (int i=1; i<n; i++)
60 *x[i]=IntVar(home,x[0]->min(),x[0]->min());
61 } else if (n > 2) {
62 ViewArray<IntView> y(home,n);
63 y[0]=*x[0];
64 for (int i=1; i<n; i++)
65 y[i]=*x[i]=IntVar(home,x[0]->min(),x[0]->max());
66 if ((ipl == IPL_DOM) || (ipl == IPL_DEF)) {
68 (void) es; assert(es == ES_OK);
69 } else {
71 (void) es; assert(es == ES_OK);
72 }
73 } else if (n == 2) {
74 *x[1]=IntVar(home,x[0]->min(),x[0]->max());
75 if ((ipl == IPL_DOM) || (ipl == IPL_DEF)) {
77 (void) es; assert(es == ES_OK);
78 } else {
80 (void) es; assert(es == ES_OK);
81 }
82 }
83 }
84
86 forceinline void
87 link(Home home, BoolVar** x, int n, IntPropLevel) {
88 if (home.failed()) {
89 for (int i=1; i<n; i++)
90 *x[i]=BoolVar(home,0,0);
91 } else if (n > 2) {
92 ViewArray<BoolView> y(home,n);
93 y[0]=*x[0];
94 for (int i=1; i<n; i++)
95 y[i]=*x[i]=BoolVar(home,0,1);
97 (void) es; assert(es == ES_OK);
98 } else if (n == 2) {
99 *x[1] = BoolVar(home,0,1);
101 (void) es; assert(es == ES_OK);
102 }
103 }
104
106 template<class Var>
107 forceinline void
109 int n=x.size();
110 if (n < 2)
111 return;
112
113 Region r;
114 Var** y = r.alloc<Var*>(n);
115 for (int i=0; i<n; i++)
116 y[i]=&x[i];
117
118 VarPtrLess<Var> vpl;
120
121 // Replace all shared variables with new and equal variables
122 for (int i=0; i<n;) {
123 int j=i++;
124 while ((i<n) && (y[j]->varimp() == y[i]->varimp()))
125 i++;
126 if (!y[j]->assigned())
127 link(home,&y[j],i-j,ipl);
128 }
129 }
130
131}}}
132
133namespace Gecode {
134
135 void
139
140 void
144
145}
146
147// STATISTICS: int-post
Passing Boolean variables.
Definition int.hh:721
Boolean integer variables.
Definition int.hh:515
Home class for posting propagators
Definition core.hpp:856
bool failed(void) const
Check whether corresponding space is failed.
Definition core.hpp:4055
Passing integer variables.
Definition int.hh:662
Integer variables.
Definition int.hh:371
static ExecStatus post(Home home, BVA x0, BVB x1)
Post propagator .
Definition eq.hpp:60
static ExecStatus post(Home home, ViewArray< BV > &x)
Post propagator .
Definition eq.hpp:131
static ExecStatus post(Home home, View0 x0, View1 x1)
Post bounds consistent propagator .
Definition eq.hpp:108
static ExecStatus post(Home home, View0 x0, View1 x1)
Post domain consistent propagator .
Definition eq.hpp:176
static ExecStatus post(Home home, ViewArray< View > &x)
Post bounds consistent propagator .
Definition eq.hpp:402
static ExecStatus post(Home home, ViewArray< View > &x)
Post domain consistent propagator .
Definition eq.hpp:270
Sort order for variables.
Definition unshare.cpp:46
bool operator()(const Var *a, const Var *b)
Definition unshare.cpp:49
Handle to region.
Definition region.hpp:55
Argument array for variables.
Definition array.hpp:777
Base class for variables.
Definition var.hpp:40
View arrays.
Definition array.hpp:253
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:989
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:994
@ IPL_DEF
Simple propagation levels.
Definition int.hh:991
Unsharing shared variables.
void unshare(Home home, VarArgArray< Var > &x, IntPropLevel ipl)
Replace unassigned shared variables by fresh, yet equal variables.
Definition unshare.cpp:108
void link(Home home, IntVar **x, int n, IntPropLevel ipl)
Replace by fresh yet equal integer variables.
Definition unshare.cpp:57
Finite domain integers.
void quicksort(Type *l, Type *r, Less &less)
Standard quick sort.
Definition sort.hpp:130
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:773
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
IntPropLevel vbd(IntPropLevel ipl)
Extract value, bounds, or domain propagation from propagation level.
Definition ipl.hpp:37
Post propagator for SetVar SetOpType SetVar y
Definition set.hh:773
ExecStatus
Definition core.hpp:472
@ ES_OK
Execution is okay.
Definition core.hpp:476
void unshare(Home home, IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Replace multiple variable occurences in x by fresh variables.
Definition unshare.cpp:136
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Post propagator for SetVar x
Definition set.hh:773
#define forceinline
Definition config.hpp:194