Generated on Sat May 25 2013 18:00:36 for Gecode by doxygen 1.8.3.1
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-02-25 21:43:24 +0100 (Mon, 25 Feb 2013) $ by $Author: schulte $
11  * $Revision: 13406 $
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 
41  class PosValuesChoice : public PosChoice {
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], BranchFilter bf)
99  : ViewBrancher<IntView,n>(home,x,vs,bf) {}
100 
101  template<int n, bool min>
104  ViewSel<IntView>* vs[n],
105  BranchFilter bf) {
106  return *new (home) ViewValuesBrancher<n,min>(home,x,vs,bf);
107  }
108 
109  template<int n, bool min>
113  : ViewBrancher<IntView,n>(home,shared,b) {}
114 
115  template<int n, bool min>
116  Actor*
118  return new (home) ViewValuesBrancher<n,min>(home,shared,*this);
119  }
120 
121  template<int n, bool min>
122  const Choice*
125  return new PosValuesChoice(*this,p,
127  }
128 
129  template<int n, bool min>
130  const Choice*
132  (void) home;
133  int p;
134  unsigned int a;
135  e >> p >> a;
136  return new PosValuesChoice(*this,a,p,e);
137  }
138 
139  template<int n, bool min>
140  ExecStatus
142  unsigned int a) {
143  const PosValuesChoice& pvc
144  = static_cast<const PosValuesChoice&>(c);
146  unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
147  return me_failed(x.eq(home,pvc.val(b))) ? ES_FAILED : ES_OK;
148  }
149 
150 }}}
151 
152 // STATISTICS: int-branch