Generated on Tue Oct 22 2013 00:49:03 for Gecode by doxygen 1.8.4
view-values.hpp
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, 2008
8  *
9  * Last modified:
10  * $Date: 2013-07-04 17:03:13 +0200 (Thu, 04 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13801 $
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 namespace Gecode { namespace Int { namespace Branch {
39 
42  private:
44  class PosMin {
45  public:
47  unsigned int pos;
49  int min;
50  };
52  unsigned int n;
54  PosMin* pm;
55  public:
58  PosValuesChoice(const Brancher& b, const Pos& p, IntView x);
61  PosValuesChoice(const Brancher& b, unsigned int alt, Pos p, Archive& e);
63  int val(unsigned int a) const;
66  virtual size_t size(void) const;
69  virtual ~PosValuesChoice(void);
72  virtual void archive(Archive& e) const;
73  };
74 
75  forceinline int
76  PosValuesChoice::val(unsigned int a) const {
77  PosMin* l = &pm[0];
78  PosMin* r = &pm[n-1];
79  while (true) {
80  PosMin* m = l + (r-l)/2;
81  if (a < m->pos) {
82  r=m-1;
83  } else if (a >= (m+1)->pos) {
84  l=m+1;
85  } else {
86  return m->min + static_cast<int>(a - m->pos);
87  }
88  }
90  return 0;
91  }
92 
93 
94  template<int n, bool min>
98  ViewSel<IntView>* vs[n],
99  BranchFilter bf, IntVarValPrint vvp0)
100  : ViewBrancher<IntView,n>(home,x,vs,bf), vvp(vvp0) {}
101 
102  template<int n, bool min>
105  ViewSel<IntView>* vs[n],
106  BranchFilter bf, IntVarValPrint vvp) {
107  return *new (home) ViewValuesBrancher<n,min>(home,x,vs,bf,vvp);
108  }
109 
110  template<int n, bool min>
114  : ViewBrancher<IntView,n>(home,shared,b), vvp(b.vvp) {}
115 
116  template<int n, bool min>
117  Actor*
119  return new (home) ViewValuesBrancher<n,min>(home,shared,*this);
120  }
121 
122  template<int n, bool min>
123  const Choice*
126  return new PosValuesChoice(*this,p,
128  }
129 
130  template<int n, bool min>
131  const Choice*
133  (void) home;
134  int p;
135  unsigned int a;
136  e >> p >> a;
137  return new PosValuesChoice(*this,a,p,e);
138  }
139 
140  template<int n, bool min>
141  ExecStatus
143  unsigned int a) {
144  const PosValuesChoice& pvc
145  = static_cast<const PosValuesChoice&>(c);
147  unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
148  return me_failed(x.eq(home,pvc.val(b))) ? ES_FAILED : ES_OK;
149  }
150 
151  template<int n, bool min>
152  NGL*
154  unsigned int a) const {
155  const PosValuesChoice& pvc
156  = static_cast<const PosValuesChoice&>(c);
158  unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
159  return new (home) EqNGL<IntView>(home,x,pvc.val(b));
160  }
161 
162  template<int n, bool min>
163  void
165  unsigned int a, std::ostream& o) const {
166  const PosValuesChoice& pvc
167  = static_cast<const PosValuesChoice&>(c);
168  IntVar x(ViewBrancher<IntView,n>::view(pvc.pos()).varimp());
169  unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
170  int nn = pvc.val(b);
171  if (vvp != NULL)
172  vvp(home,*this,a,x,pvc.pos().pos,nn,o);
173  else
174  o << "var[" << pvc.pos().pos << "] = " << nn;
175  }
176 
177 }}}
178 
179 // STATISTICS: int-branch