Generated on Fri Jan 28 2022 04:43:06 for Gecode by doxygen 1.8.13
matrix.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Mikael Lagerkvist <lagerkvist@gecode.org>
5  *
6  * Copyright:
7  * Mikael Lagerkvist, 2005
8  *
9  * Bugfixes provided by:
10  * Olof Sivertsson <olof@olofsivertsson.com>
11  *
12  * This file is part of Gecode, the generic constraint
13  * development environment:
14  * http://www.gecode.org
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining
17  * a copy of this software and associated documentation files (the
18  * "Software"), to deal in the Software without restriction, including
19  * without limitation the rights to use, copy, modify, merge, publish,
20  * distribute, sublicense, and/or sell copies of the Software, and to
21  * permit persons to whom the Software is furnished to do so, subject to
22  * the following conditions:
23  *
24  * The above copyright notice and this permission notice shall be
25  * included in all copies or substantial portions of the Software.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34  *
35  */
36 
37 #include <algorithm>
38 
39 namespace Gecode {
40 
41  template<class A>
42  inline
43  Slice<A>::Slice(const Matrix<A>& a, int fc, int tc, int fr, int tr)
44  : _r(0), _fc(fc), _tc(tc), _fr(fr), _tr(tr) {
45  if (tc > a.width() || tr > a.height())
46  throw MiniModel::ArgumentOutOfRange("Slice::Slice");
47  if (fc >= tc || fr >= tr) {
48  _fc=0; _tc=0; _fr=0; _tr=0;
49  return;
50  }
51 
52  _r = ArgsType((tc-fc)*(tr-fr));
53 
54  int i = 0;
55  for (int h = fr; h < tr; h++)
56  for (int w = fc; w < tc; w++)
57  _r[i++] = a(w, h);
58  }
59 
60  template<class A>
61  Slice<A>&
63  for (int i = 0; i < _r.size()/2; i++)
64  std::swap(_r[i], _r[_r.size()-i-1]);
65  return *this;
66  }
67 
68  template<class A>
69  inline
71  return _r;
72  }
73  template<class A>
74  inline
76  return Matrix<ArgsType>(_r, _tc-_fc, _tr-_fr);
77  }
78  template<class A>
79  inline
80  Slice<A>::operator const typename Slice<A>::ArgsType(void) const {
81  return _r;
82  }
83  template<class A>
84  inline
86  return Matrix<ArgsType>(_r, _tc-_fc, _tr-_fr);
87  }
88 
89  template<class A>
90  typename Slice<A>::ArgsType
91  operator+(const Slice<A>& x, const Slice<A>& y) {
92  typename Slice<A>::ArgsType xx = x;
93  typename Slice<A>::ArgsType yy = y;
94  return xx+yy;
95  }
96 
97  template<class A>
98  typename Slice<A>::ArgsType
99  operator+(const Slice<A>& x, const typename ArrayTraits<A>::ArgsType& y) {
100  typename Slice<A>::ArgsType xx = x;
101  return xx+y;
102  }
103 
104  template<class A>
105  typename Slice<A>::ArgsType
106  operator+(const typename ArrayTraits<A>::ArgsType& x, const Slice<A>& y) {
107  typename Slice<A>::ArgsType yy = y;
108  return x+yy;
109  }
110 
111  template<class A>
112  typename Slice<A>::ArgsType
113  operator+(const Slice<A>& x, const typename ArrayTraits<A>::ValueType& y) {
114  typename Slice<A>::ArgsType xx = x;
115  return xx+y;
116  }
117 
118  template<class A>
119  typename Slice<A>::ArgsType
120  operator+(const typename ArrayTraits<A>::ValueType& x, const Slice<A>& y) {
121  typename Slice<A>::ArgsType yy = y;
122  return x+yy;
123  }
124 
125  template<class A>
127  Matrix<A>::Matrix(A a, int w, int h)
128  : _a(a), _w(w), _h(h) {
129  if ((_w * _h) != _a.size())
130  throw MiniModel::ArgumentSizeMismatch("Matrix::Matrix(A, w, h)");
131  }
132 
133  template<class A>
136  : _a(a), _w(n), _h(n) {
137  if (n*n != _a.size())
138  throw MiniModel::ArgumentSizeMismatch("Matrix::Matrix(A, n)");
139  }
140 
141  template<class A>
142  forceinline int
143  Matrix<A>::width(void) const { return _w; }
144  template<class A>
145  forceinline int
146  Matrix<A>::height(void) const { return _h; }
147  template<class A>
148  inline typename Matrix<A>::ArgsType const
149  Matrix<A>::get_array(void) const {
150  return ArgsType(_a);
151  }
152 
153  template<class A>
156  if ((c >= _w) || (r >= _h))
157  throw MiniModel::ArgumentOutOfRange("Matrix::operator ()");
158  return _a[r*_w + c];
159  }
160 
161  template<class A>
162  forceinline const typename Matrix<A>::ValueType&
163  Matrix<A>::operator ()(int c, int r) const {
164  if ((c >= _w) || (r >= _h))
165  throw MiniModel::ArgumentOutOfRange("Matrix::operator ()");
166  return _a[r*_w + c];
167  }
168 
169  template<class A>
170  inline Slice<A>
171  Matrix<A>::slice(int fc, int tc, int fr, int tr) const {
172  return Slice<A>(*this, fc, tc, fr, tr);
173  }
174 
175  template<class A>
176  inline Slice<A>
177  Matrix<A>::row(int r) const {
178  return slice(0, width(), r, r+1);
179  }
180 
181  template<class A>
182  inline Slice<A>
183  Matrix<A>::col(int c) const {
184  return slice(c, c+1, 0, height());
185  }
186 
187  template<class Char, class Traits, class A>
188  std::basic_ostream<Char,Traits>&
189  operator <<(std::basic_ostream<Char,Traits>& os, const Matrix<A>& m) {
190  std::basic_ostringstream<Char,Traits> s;
191  s.copyfmt(os); s.width(0);
192  for (int i=0; i<m.height(); i++) {
193  for (int j=0; j<m.width(); j++) {
194  s << m(j,i) << "\t";
195  }
196  s << std::endl;
197  }
198  return os << s.str();
199  }
200 
201  template<class Char, class Traits, class A>
202  std::basic_ostream<Char,Traits>&
203  operator <<(std::basic_ostream<Char,Traits>& os, const Slice<A>& s) {
204  return os << static_cast<typename Slice<A>::ArgsType>(s);
205  }
206 
207  forceinline void
209  IntVar z, IntPropLevel ipl) {
210  element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl);
211  }
212  forceinline void
214  BoolVar z, IntPropLevel ipl) {
215  element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl);
216  }
217  forceinline void
219  IntVar z, IntPropLevel ipl) {
220  element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl);
221  }
222  forceinline void
224  BoolVar z, IntPropLevel ipl) {
225  element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl);
226  }
227 
228 #ifdef GECODE_HAS_SET_VARS
229  forceinline void
231  SetVar z) {
232  element(home, m.get_array(), x, m.width(), y, m.height(), z);
233  }
234  forceinline void
236  SetVar z) {
237  element(home, m.get_array(), x, m.width(), y, m.height(), z);
238  }
239 #endif
240 
241 }
242 
243 // STATISTICS: minimodel-any
Slice< A > col(int c) const
Access column c.
Definition: matrix.hpp:183
Exception: Argument out of range
Definition: exception.hpp:59
void element(Home home, const Matrix< IntArgs > &m, IntVar x, IntVar y, IntVar z, IntPropLevel ipl=IPL_DEF)
Element constraint for matrix.
Definition: matrix.hpp:208
int height(void) const
Return the height of the matrix.
Definition: matrix.hpp:146
Slice< A > slice(int fc, int tc, int fr, int tr) const
Access slice of the matrix.
Definition: matrix.hpp:171
#define forceinline
Definition: config.hpp:185
ArrayTraits< A >::ValueType ValueType
The type of elements of this array.
Definition: minimodel.hh:2125
Gecode::FloatVal c(-8, 8)
Single _a(2, 3)
A slice of a matrix.
Definition: minimodel.hh:2058
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Slice & reverse(void)
Reverses the contents of the slice, and returns a reference to it.
Definition: matrix.hpp:62
Matrix(A a, int w, int h)
Basic constructor.
Definition: matrix.hpp:127
Gecode::IntArgs i({1, 2, 3, 4})
FloatVal operator+(const FloatVal &x)
Definition: val.hpp:164
ArrayTraits< A >::ArgsType ArgsType
The type of the Args-array type for ValueType values.
Definition: minimodel.hh:2127
Post propagator for SetVar SetOpType SetVar SetRelType SetVar z
Definition: set.hh:767
int width(void) const
Return the width of the matrix.
Definition: matrix.hpp:143
Boolean integer variables.
Definition: int.hh:512
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:767
ArrayTraits< A >::ArgsType ArgsType
The type of the Args-array type for ValueType values.
Definition: minimodel.hh:2061
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:974
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
Set variables
Definition: set.hh:127
Slice< A > row(int r) const
Access row r.
Definition: matrix.hpp:177
Integer variables.
Definition: int.hh:371
ValueType & operator()(int c, int r)
Access element (c, r) of the matrix.
Definition: matrix.hpp:155
Post propagator for SetVar x
Definition: set.hh:767
Matrix-interface for arrays.
Definition: minimodel.hh:2048
Traits of arrays in Gecode.
Definition: array.hpp:76
Gecode toplevel namespace
Slice(const Matrix< A > &a, int fc, int tc, int fr, int tr)
Construct slice.
Definition: matrix.hpp:43
ArgsType const get_array(void) const
Return an Args-array of the contents of the matrix.
Definition: matrix.hpp:149
Exception: Sizes of arguments does not match
Definition: exception.hpp:52
Home class for posting propagators
Definition: core.hpp:853
struct Gecode::@593::NNF::@62::@64 a
For atomic nodes.
IntRelType swap(IntRelType irt)
Return swapped relation type of irt.
Definition: irt.hpp:37