Generated on Fri Aug 24 2012 04:52:08 for Gecode by doxygen 1.8.1.2
select-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: 2011-05-11 20:44:17 +1000 (Wed, 11 May 2011) $ by $Author: tack $
11  * $Revision: 12001 $
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 
45  template<class ViewSel, class View>
46  class PosValuesChoice : public PosChoice<ViewSel> {
47  private:
49  class PosMin {
50  public:
52  unsigned int pos;
54  int min;
55  };
57  unsigned int n;
59  PosMin* pm;
60  public:
62  PosValuesChoice(const Brancher& b, const Pos& p,
63  const typename ViewSel::Choice& viewc, View x);
65  PosValuesChoice(const Brancher& b, unsigned int alt, Pos p,
66  const typename ViewSel::Choice& viewc,
67  Archive& e);
69  int val(unsigned int a) const;
71  virtual size_t size(void) const;
73  virtual ~PosValuesChoice(void);
75  virtual void archive(Archive& e) const;
76  };
77 
78 
79  template<class ViewSel, class View>
82  PosValuesChoice(const Brancher& b, const Pos& p,
83  const typename ViewSel::Choice& viewc, View x)
84  : PosChoice<ViewSel>(b,x.size(),p,viewc), n(0) {
85  for (ViewRanges<View> r(x); r(); ++r)
86  n++;
87  pm = heap.alloc<PosMin>(n+1);
88  unsigned int w=0;
89  int i=0;
90  for (ViewRanges<View> r(x); r(); ++r) {
91  pm[i].min = r.min();
92  pm[i].pos = w;
93  w += r.width(); i++;
94  }
95  pm[i].pos = w;
96  }
97 
98  template<class ViewSel, class View>
101  PosValuesChoice(const Brancher& b, unsigned int alt, Pos p,
102  const typename ViewSel::Choice& viewc, Archive& e)
103  : PosChoice<ViewSel>(b,alt,p,viewc) {
104  e >> n;
105  pm = heap.alloc<PosMin>(n+1);
106  for (unsigned int i=0; i<n+1; i++) {
107  e >> pm[i].pos;
108  e >> pm[i].min;
109  }
110  }
111 
112  template<class ViewSel, class View>
113  forceinline int
114  PosValuesChoice<ViewSel,View>::val(unsigned int a) const {
115  PosMin* l = &pm[0];
116  PosMin* r = &pm[n-1];
117  while (true) {
118  PosMin* m = l + (r-l)/2;
119  if (a < m->pos) {
120  r=m-1;
121  } else if (a >= (m+1)->pos) {
122  l=m+1;
123  } else {
124  return m->min + static_cast<int>(a - m->pos);
125  }
126  }
127  GECODE_NEVER;
128  return 0;
129  }
130 
131  template<class ViewSel, class View>
132  size_t
134  return sizeof(PosValuesChoice<ViewSel,View>)+(n+1)*sizeof(PosMin);
135  }
136 
137  template<class ViewSel, class View>
139  heap.free<PosMin>(pm,n+1);
140  }
141 
142  template<class ViewSel, class View>
143  forceinline void
146  e << this->alternatives() << n;
147  for (unsigned int i=0; i<n+1; i++) {
148  e << pm[i].pos;
149  e << pm[i].min;
150  }
151  }
152 
153  template<class ViewSel, class View>
157  ViewSel& vi_s, BranchFilter bf)
158  : ViewBrancher<ViewSel>(home,x,vi_s,bf) {}
159 
160  template<class ViewSel, class View>
161  void
164  BranchFilter bf) {
165  (void) new (home) ViewValuesBrancher<ViewSel,View>(home,x,vi_s,bf);
166  }
167 
168  template<class ViewSel, class View>
172  : ViewBrancher<ViewSel>(home,share,b) {}
173 
174  template<class ViewSel, class View>
175  Actor*
177  return new (home)
178  ViewValuesBrancher<ViewSel,View>(home,share,*this);
179  }
180 
181  template<class ViewSel, class View>
182  const Choice*
185  View v(ViewBrancher<ViewSel>::view(p).varimp());
187  (*this,p,viewsel.choice(home),v);
188  }
189 
190  template<class ViewSel, class View>
191  const Choice*
193  int p;
194  unsigned int alt;
195  e >> p >> alt;
197  (*this,alt,p,viewsel.choice(home,e),e);
198  }
199 
200  template<class ViewSel, class View>
201  ExecStatus
203  ::commit(Space& home, const Choice& c, unsigned int a) {
205  = static_cast<const PosValuesChoice<ViewSel,View>&>(c);
206  View v(ViewBrancher<ViewSel>::view(pvc.pos()).varimp());
207  viewsel.commit(home, pvc.viewchoice(), a);
208  return me_failed(v.eq(home,pvc.val(a))) ? ES_FAILED : ES_OK;
209  }
210 
211 }}}
212 
213 // STATISTICS: int-branch