Generated on Thu Feb 21 2013 23:11:46 for Gecode by doxygen 1.8.3.1
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: 2010-06-05 00:13:00 +1000 (Sat, 05 Jun 2010) $ by $Author: schulte $
11  * $Revision: 11028 $
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 {
47 
48  namespace Int { namespace Unshare {
49 
51  template<class Var>
52  class VarPtrLess {
53  public:
54  forceinline bool
55  operator ()(const Var* a, const Var* b) {
56  return a->before(*b);
57  }
58  };
59 
60 
63  link(Home home, IntVar** x, int n, IntConLevel icl) {
64  if (n > 2) {
65  ViewArray<IntView> y(home,n);
66  y[0]=*x[0];
67  for (int i=1; i<n; i++)
68  y[i]=*x[i]=IntVar(home,x[0]->min(),x[0]->max());
69  if ((icl == ICL_DOM) || (icl == ICL_DEF)) {
71  } else {
73  }
74  } else if (n == 2) {
75  *x[1]=IntVar(home,x[0]->min(),x[0]->max());
76  if ((icl == ICL_DOM) || (icl == ICL_DEF)) {
78  (home,*x[0],*x[1])));
79  } else {
81  (home,*x[0],*x[1])));
82  }
83  }
84  return ES_OK;
85  }
86 
89  link(Home home, BoolVar** x, int n, IntConLevel) {
90  if (n > 2) {
91  ViewArray<BoolView> y(home,n);
92  y[0]=*x[0];
93  for (int i=1; i<n; i++)
94  y[i]=*x[i]=BoolVar(home,0,1);
96  } else if (n == 2) {
97  *x[1] = BoolVar(home,0,1);
99  }
100  return ES_OK;
101  }
102 
104  template<class Var>
107  int n=x.size();
108  if (n < 2)
109  return ES_OK;
110 
111  Region r(home);
112  Var** y = r.alloc<Var*>(n);
113  for (int i=n; i--; )
114  y[i]=&x[i];
115 
116  VarPtrLess<Var> vpl;
117  Support::quicksort<Var*,VarPtrLess<Var> >(y,n,vpl);
118 
119  // Replace all shared variables with new and equal variables
120  for (int i=0; i<n;) {
121  int j=i++;
122  while ((i<n) && y[j]->same(*y[i]))
123  i++;
124  if (!y[j]->assigned())
125  link(home,&y[j],i-j,icl);
126  }
127  return ES_OK;
128  }
129 
130  }}
131 
132  void
134  if (home.failed()) return;
135  GECODE_ES_FAIL(Int::Unshare::unshare<IntVar>(home,x,icl));
136  }
137 
138  void
140  if (home.failed()) return;
141  GECODE_ES_FAIL(Int::Unshare::unshare<BoolVar>(home,x,ICL_DEF));
142  }
143 
144 }
145 
146 // STATISTICS: int-post