Generated on Sat Feb 7 2015 02:01:26 for Gecode by doxygen 1.8.9.1
int.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, 2002
8  *
9  * Last modified:
10  * $Date: 2013-05-14 01:34:01 +0200 (Tue, 14 May 2013) $ by $Author: tack $
11  * $Revision: 13635 $
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.hh>
39 
40 namespace Gecode { namespace Int {
41 
42  forceinline bool
43  IntVarImp::closer_min(int n) const {
44  unsigned int l = static_cast<unsigned int>(n - dom.min());
45  unsigned int r = static_cast<unsigned int>(dom.max() - n);
46  return l < r;
47  }
48 
49  int
50  IntVarImp::med(void) const {
51  // Computes the median
52  if (fst() == NULL)
53  return (dom.min()+dom.max())/2 - ((dom.min()+dom.max())%2 < 0 ? 1 : 0);
54  unsigned int i = size() / 2;
55  if (size() % 2 == 0)
56  i--;
57  const RangeList* p = NULL;
58  const RangeList* c = fst();
59  while (i >= c->width()) {
60  i -= c->width();
61  const RangeList* n=c->next(p); p=c; c=n;
62  }
63  return c->min() + static_cast<int>(i);
64  }
65 
66  bool
67  IntVarImp::in_full(int m) const {
68  if (closer_min(m)) {
69  const RangeList* p = NULL;
70  const RangeList* c = fst();
71  while (m > c->max()) {
72  const RangeList* n=c->next(p); p=c; c=n;
73  }
74  return (m >= c->min());
75  } else {
76  const RangeList* n = NULL;
77  const RangeList* c = lst();
78  while (m < c->min()) {
79  const RangeList* p=c->prev(n); n=c; c=p;
80  }
81  return (m <= c->max());
82  }
83  }
84 
85  /*
86  * "Standard" tell operations
87  *
88  */
89 
90  ModEvent
91  IntVarImp::lq_full(Space& home, int m) {
92  assert((m >= dom.min()) && (m <= dom.max()));
93  IntDelta d(m+1,dom.max());
95  if (range()) { // Is already range...
96  dom.max(m);
97  if (assigned()) me = ME_INT_VAL;
98  } else if (m < fst()->next(NULL)->min()) { // Becomes range...
99  dom.max(std::min(m,fst()->max()));
100  fst()->dispose(home,NULL,lst());
101  fst(NULL); holes = 0;
102  if (assigned()) me = ME_INT_VAL;
103  } else { // Stays non-range...
104  RangeList* n = NULL;
105  RangeList* c = lst();
106  unsigned int h = 0;
107  while (m < c->min()) {
108  RangeList* p = c->prev(n); c->fix(n);
109  h += (c->min() - p->max() - 1);
110  n=c; c=p;
111  }
112  holes -= h;
113  int max_c = std::min(m,c->max());
114  dom.max(max_c); c->max(max_c);
115  if (c != lst()) {
116  n->dispose(home,lst());
117  c->next(n,NULL); lst(c);
118  }
119  }
120  return notify(home,me,d);
121  }
122 
123  ModEvent
124  IntVarImp::gq_full(Space& home, int m) {
125  assert((m >= dom.min()) && (m <= dom.max()));
126  IntDelta d(dom.min(),m-1);
127  ModEvent me = ME_INT_BND;
128  if (range()) { // Is already range...
129  dom.min(m);
130  if (assigned()) me = ME_INT_VAL;
131  } else if (m > lst()->prev(NULL)->max()) { // Becomes range...
132  dom.min(std::max(m,lst()->min()));
133  fst()->dispose(home,NULL,lst());
134  fst(NULL); holes = 0;
135  if (assigned()) me = ME_INT_VAL;
136  } else { // Stays non-range...
137  RangeList* p = NULL;
138  RangeList* c = fst();
139  unsigned int h = 0;
140  while (m > c->max()) {
141  RangeList* n = c->next(p); c->fix(n);
142  h += (n->min() - c->max() - 1);
143  p=c; c=n;
144  }
145  holes -= h;
146  int min_c = std::max(m,c->min());
147  dom.min(min_c); c->min(min_c);
148  if (c != fst()) {
149  fst()->dispose(home,p);
150  c->prev(p,NULL); fst(c);
151  }
152  }
153  return notify(home,me,d);
154  }
155 
156  ModEvent
157  IntVarImp::eq_full(Space& home, int m) {
158  dom.min(m); dom.max(m);
159  if (!range()) {
160  bool failed = false;
161  RangeList* p = NULL;
162  RangeList* c = fst();
163  while (m > c->max()) {
164  RangeList* n=c->next(p); c->fix(n); p=c; c=n;
165  }
166  if (m < c->min())
167  failed = true;
168  while (c != NULL) {
169  RangeList* n=c->next(p); c->fix(n); p=c; c=n;
170  }
171  assert(p == lst());
172  fst()->dispose(home,p);
173  fst(NULL); holes = 0;
174  if (failed)
175  return ME_INT_FAILED;
176  }
177  IntDelta d;
178  return notify(home,ME_INT_VAL,d);
179  }
180 
181  ModEvent
182  IntVarImp::nq_full(Space& home, int m) {
183  assert(!((m < dom.min()) || (m > dom.max())));
184  ModEvent me = ME_INT_DOM;
185  if (range()) {
186  if ((m == dom.min()) && (m == dom.max()))
187  return ME_INT_FAILED;
188  if (m == dom.min()) {
189  dom.min(m+1);
190  me = assigned() ? ME_INT_VAL : ME_INT_BND;
191  } else if (m == dom.max()) {
192  dom.max(m-1);
193  me = assigned() ? ME_INT_VAL : ME_INT_BND;
194  } else {
195  RangeList* f = new (home) RangeList(dom.min(),m-1);
196  RangeList* l = new (home) RangeList(m+1,dom.max());
197  f->prevnext(NULL,l);
198  l->prevnext(f,NULL);
199  fst(f); lst(l); holes = 1;
200  }
201  } else if (m < fst()->next(NULL)->min()) { // Concerns the first range...
202  int f_max = fst()->max();
203  if (m > f_max)
204  return ME_INT_NONE;
205  int f_min = dom.min();
206  if ((m == f_min) && (m == f_max)) {
207  RangeList* f_next = fst()->next(NULL);
208  dom.min(f_next->min());
209  if (f_next == lst()) { // Turns into range
210  // Works as at the ends there are only NULL pointers
211  fst()->dispose(home,f_next);
212  fst(NULL); holes = 0;
213  me = assigned() ? ME_INT_VAL : ME_INT_BND;
214  } else { // Remains non-range
215  f_next->prev(fst(),NULL);
216  fst()->dispose(home); fst(f_next);
217  holes -= dom.min() - f_min - 1;
218  me = ME_INT_BND;
219  }
220  } else if (m == f_min) {
221  dom.min(m+1); fst()->min(m+1);
222  me = ME_INT_BND;
223  } else if (m == f_max) {
224  fst()->max(m-1); holes += 1;
225  } else {
226  // Create new hole
227  RangeList* f = new (home) RangeList(f_min,m-1);
228  f->prevnext(NULL,fst());
229  fst()->min(m+1); fst()->prev(NULL,f);
230  fst(f); holes += 1;
231  }
232  } else if (m > lst()->prev(NULL)->max()) { // Concerns the last range...
233  int l_min = lst()->min();
234  if (m < l_min)
235  return ME_INT_NONE;
236  int l_max = dom.max();
237  if ((m == l_min) && (m == l_max)) {
238  RangeList* l_prev = lst()->prev(NULL);
239  dom.max(l_prev->max());
240  if (l_prev == fst()) {
241  // Turns into range
242  l_prev->dispose(home,lst());
243  fst(NULL); holes = 0;
244  me = assigned() ? ME_INT_VAL : ME_INT_BND;
245  } else { // Remains non-range
246  l_prev->next(lst(),NULL);
247  lst()->dispose(home); lst(l_prev);
248  holes -= l_max - dom.max() - 1;
249  me = ME_INT_BND;
250  }
251  } else if (m == l_max) {
252  dom.max(m-1); lst()->max(m-1);
253  me = ME_INT_BND;
254  } else if (m == l_min) {
255  lst()->min(m+1); holes += 1;
256  } else { // Create new hole
257  RangeList* l = new (home) RangeList(m+1,l_max);
258  l->prevnext(lst(),NULL);
259  lst()->max(m-1); lst()->next(NULL,l);
260  lst(l); holes += 1;
261  }
262  } else { // Concerns element in the middle of the list of ranges
263  RangeList* p;
264  RangeList* c;
265  RangeList* n;
266  if (closer_min(m)) {
267  assert(m > fst()->max());
268  p = NULL;
269  c = fst();
270  do {
271  n=c->next(p); p=c; c=n;
272  } while (m > c->max());
273  if (m < c->min())
274  return ME_INT_NONE;
275  n=c->next(p);
276  } else {
277  assert(m < lst()->min());
278  n = NULL;
279  c = lst();
280  do {
281  p=c->prev(n); n=c; c=p;
282  } while (m < c->min());
283  if (m > c->max())
284  return ME_INT_NONE;
285  p=c->prev(n);
286  }
287  assert((fst() != c) && (lst() != c));
288  assert((m >= c->min()) && (m <= c->max()));
289  holes += 1;
290  int c_min = c->min();
291  int c_max = c->max();
292  if ((c_min == m) && (c_max == m)) {
293  c->dispose(home);
294  p->next(c,n); n->prev(c,p);
295  } else if (c_min == m) {
296  c->min(m+1);
297  } else {
298  c->max(m-1);
299  if (c_max != m) {
300  RangeList* l = new (home) RangeList(m+1,c_max);
301  l->prevnext(c,n);
302  c->next(n,l);
303  n->prev(c,l);
304  }
305  }
306  }
307  IntDelta d(m,m);
308  return notify(home,me,d);
309  }
310 
311 
312 
313  /*
314  * Copying variables
315  *
316  */
317 
319  IntVarImp::IntVarImp(Space& home, bool share, IntVarImp& x)
320  : IntVarImpBase(home,share,x), dom(x.dom.min(),x.dom.max()) {
321  holes = x.holes;
322  if (holes) {
323  int m = 1;
324  // Compute length
325  {
326  RangeList* s_p = x.fst();
327  RangeList* s_c = s_p->next(NULL);
328  do {
329  m++;
330  RangeList* s_n = s_c->next(s_p); s_p=s_c; s_c=s_n;
331  } while (s_c != NULL);
332  }
333  RangeList* d_c = home.alloc<RangeList>(m);
334  fst(d_c); lst(d_c+m-1);
335  d_c->min(x.fst()->min());
336  d_c->max(x.fst()->max());
337  d_c->prevnext(NULL,NULL);
338  RangeList* s_p = x.fst();
339  RangeList* s_c = s_p->next(NULL);
340  do {
341  RangeList* d_n = d_c + 1;
342  d_c->next(NULL,d_n);
343  d_n->prevnext(d_c,NULL);
344  d_n->min(s_c->min()); d_n->max(s_c->max());
345  d_c = d_n;
346  RangeList* s_n=s_c->next(s_p); s_p=s_c; s_c=s_n;
347  } while (s_c != NULL);
348  d_c->next(NULL,NULL);
349  } else {
350  fst(NULL);
351  }
352  }
353 
354  IntVarImp*
355  IntVarImp::perform_copy(Space& home, bool share) {
356  return new (home) IntVarImp(home,share,*this);
357  }
358 
359 }}
360 
361 // STATISTICS: int-var
unsigned int size(void) const
Return size (cardinality) of domain.
Definition: int.hpp:257
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
void dispose(Space &home, RangeList *p, RangeList *l)
Free memory for all elements between this and l (inclusive)
Definition: int.hpp:138
int min(void) const
Return minimum.
Definition: int.hpp:102
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition: dom.cpp:44
int ModEvent
Type for modification events.
Definition: core.hpp:146
int min(void) const
Return minimum.
Definition: range-list.hpp:142
Base-class for Int-variable implementations.
Definition: var-imp.hpp:47
Computation spaces.
Definition: core.hpp:1362
RangeList * lst(void) const
Return last element of rangelist.
Definition: int.hpp:177
int max(void) const
Return maximum.
Definition: range-list.hpp:146
Gecode::IntSet d(v, 7)
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition: core.hpp:2397
const Gecode::ModEvent ME_INT_FAILED
Domain operation has resulted in failure.
Definition: var-type.hpp:52
Gecode::FloatVal c(-8, 8)
void max(Home home, SetVar s, IntVar x, Reify r)
Post reified propagator for b iff x is the maximal element of s.
Definition: int.cpp:160
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
bool assigned(void) const
Test whether variable is assigned.
Definition: int.hpp:246
static ModEvent me(const ModEventDelta &med)
Project modification event for this variable type from med.
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
bool range(void) const
Test whether domain is a range.
Definition: int.hpp:242
const Gecode::ModEvent ME_INT_VAL
Domain operation has resulted in a value (assigned variable)
Definition: var-type.hpp:56
int max(void) const
Return maximum.
Definition: int.hpp:106
RangeList * next(void) const
Return next element.
Definition: range-list.hpp:124
const Gecode::ModEvent ME_INT_BND
Domain operation has changed the minimum or maximum of the domain.
Definition: var-type.hpp:65
Gecode::ModEvent notify(Gecode::Space &home, Gecode::ModEvent me, Gecode::Delta &d)
Notify that variable implementation has been modified with modification event me and delta informatio...
Definition: var-imp.hpp:208
RangeList * prev(const RangeList *n) const
Return previous element (from next n)
Definition: int.hpp:70
unsigned int holes
Size of holes in the domain.
Definition: var-imp.hpp:202
void min(Home home, SetVar s, IntVar x, Reify r)
Post reified propagator for b iff x is the minimal element of s.
Definition: int.cpp:131
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
Integer variable implementation.
Definition: var-imp.hpp:91
RangeList dom
Domain information.
Definition: var-imp.hpp:190
IntVarImp(Space &home, bool share, IntVarImp &x)
Constructor for cloning x.
Definition: int.cpp:319
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Lists of ranges (intervals)
Definition: var-imp.hpp:104
VarImp * next(void) const
Return next copied variable.
int max(void) const
Return maximum of domain.
Definition: int.hpp:232
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition: var-type.hpp:72
unsigned int width(void) const
Return width (distance between maximum and minimum)
Definition: int.hpp:110
#define forceinline
Definition: config.hpp:132
void prevnext(RangeList *p, RangeList *n)
Set previous element to p and next element to n.
Definition: int.hpp:74
RangeList * next(const RangeList *p) const
Return next element (from previous p)
Definition: int.hpp:66
int med(void) const
Return median of domain (greatest element not greater than the median)
Definition: int.cpp:50
Lists of ranges (intervals)
Definition: range-list.hpp:53
Gecode toplevel namespace
int min(void) const
Return minimum of domain.
Definition: int.hpp:228
const Gecode::ModEvent ME_INT_NONE
Domain operation has not changed domain.
Definition: var-type.hpp:54
RangeList * fst(void) const
Return first element of rangelist.
Definition: int.hpp:167