Generated on Sat Feb 7 2015 02:01:29 for Gecode by doxygen 1.8.9.1
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  * Last modified:
13  * $Date: 2010-05-15 16:19:43 +0200 (Sat, 15 May 2010) $ by $Author: schulte $
14  * $Revision: 10954 $
15  *
16  * This file is part of Gecode, the generic constraint
17  * development environment:
18  * http://www.gecode.org
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  *
39  */
40 
41 #include <algorithm>
42 
43 namespace Gecode {
44 
45  template<class A>
46  inline
47  Slice<A>::Slice(const Matrix<A>& a, int fc, int tc, int fr, int tr)
48  : _r(0), _fc(fc), _tc(tc), _fr(fr), _tr(tr) {
49  if (tc > a.width() || tr > a.height())
50  throw MiniModel::ArgumentOutOfRange("Slice::Slice");
51  if (fc >= tc || fr >= tr) {
52  _fc=0; _tc=0; _fr=0; _tr=0;
53  return;
54  }
55 
56  _r = ArgsType((tc-fc)*(tr-fr));
57 
58  int i = 0;
59  for (int h = fr; h < tr; h++)
60  for (int w = fc; w < tc; w++)
61  _r[i++] = a(w, h);
62  }
63 
64  template<class A>
65  Slice<A>&
67  for (int i = 0; i < _r.size()/2; i++)
68  std::swap(_r[i], _r[_r.size()-i-1]);
69  return *this;
70  }
71 
72  template<class A>
75  return _r;
76  }
77  template<class A>
80  return Matrix<ArgsType>(_r, _tc-_fc, _tr-_fr);
81  }
82  template<class A>
84  Slice<A>::operator const typename Slice<A>::ArgsType(void) const {
85  return _r;
86  }
87  template<class A>
89  Slice<A>::operator const Matrix<typename Slice<A>::ArgsType>(void) const {
90  return Matrix<ArgsType>(_r, _tc-_fc, _tr-_fr);
91  }
92 
93  template<class A>
94  typename Slice<A>::ArgsType
95  operator+(const Slice<A>& x, const Slice<A>& y) {
96  typename Slice<A>::ArgsType xx = x;
97  typename Slice<A>::ArgsType yy = y;
98  return xx+yy;
99  }
100 
101  template<class A>
102  typename Slice<A>::ArgsType
103  operator+(const Slice<A>& x, const typename ArrayTraits<A>::ArgsType& y) {
104  typename Slice<A>::ArgsType xx = x;
105  return xx+y;
106  }
107 
108  template<class A>
109  typename Slice<A>::ArgsType
110  operator+(const typename ArrayTraits<A>::ArgsType& x, const Slice<A>& y) {
111  typename Slice<A>::ArgsType yy = y;
112  return x+yy;
113  }
114 
115  template<class A>
116  typename Slice<A>::ArgsType
117  operator+(const Slice<A>& x, const typename ArrayTraits<A>::ValueType& y) {
118  typename Slice<A>::ArgsType xx = x;
119  return xx+y;
120  }
121 
122  template<class A>
123  typename Slice<A>::ArgsType
124  operator+(const typename ArrayTraits<A>::ValueType& x, const Slice<A>& y) {
125  typename Slice<A>::ArgsType yy = y;
126  return x+yy;
127  }
128 
129  template<class A>
131  Matrix<A>::Matrix(A a, int w, int h)
132  : _a(a), _w(w), _h(h) {
133  if ((_w * _h) != _a.size())
134  throw MiniModel::ArgumentSizeMismatch("Matrix::Matrix(A, w, h)");
135  }
136 
137  template<class A>
140  : _a(a), _w(n), _h(n) {
141  if (n*n != _a.size())
142  throw MiniModel::ArgumentSizeMismatch("Matrix::Matrix(A, n)");
143  }
144 
145  template<class A>
146  forceinline int
147  Matrix<A>::width(void) const { return _w; }
148  template<class A>
149  forceinline int
150  Matrix<A>::height(void) const { return _h; }
151  template<class A>
152  inline typename Matrix<A>::ArgsType const
153  Matrix<A>::get_array(void) const {
154  return ArgsType(_a);
155  }
156 
157  template<class A>
160  if ((c >= _w) || (r >= _h))
161  throw MiniModel::ArgumentOutOfRange("Matrix::operator ()");
162  return _a[r*_w + c];
163  }
164 
165  template<class A>
166  forceinline const typename Matrix<A>::ValueType&
167  Matrix<A>::operator ()(int c, int r) const {
168  if ((c >= _w) || (r >= _h))
169  throw MiniModel::ArgumentOutOfRange("Matrix::operator ()");
170  return _a[r*_w + c];
171  }
172 
173  template<class A>
175  Matrix<A>::slice(int fc, int tc, int fr, int tr) const {
176  return Slice<A>(*this, fc, tc, fr, tr);
177  }
178 
179  template<class A>
181  Matrix<A>::row(int r) const {
182  return slice(0, width(), r, r+1);
183  }
184 
185  template<class A>
187  Matrix<A>::col(int c) const {
188  return slice(c, c+1, 0, height());
189  }
190 
191  template<class Char, class Traits, class A>
192  std::basic_ostream<Char,Traits>&
193  operator <<(std::basic_ostream<Char,Traits>& os, const Matrix<A>& m) {
194  std::basic_ostringstream<Char,Traits> s;
195  s.copyfmt(os); s.width(0);
196  for (int i=0; i<m.height(); i++) {
197  for (int j=0; j<m.width(); j++) {
198  s << m(j,i) << "\t";
199  }
200  s << std::endl;
201  }
202  return os << s.str();
203  }
204 
205  template<class Char, class Traits, class A>
206  std::basic_ostream<Char,Traits>&
207  operator <<(std::basic_ostream<Char,Traits>& os, const Slice<A>& s) {
208  return os << static_cast<typename Slice<A>::ArgsType>(s);
209  }
210 
211  forceinline void
212  element(Home home, const Matrix<IntArgs>& m, IntVar x, IntVar y,
213  IntVar z, IntConLevel icl) {
214  element(home, m.get_array(), x, m.width(), y, m.height(), z, icl);
215  }
216  forceinline void
217  element(Home home, const Matrix<IntArgs>& m, IntVar x, IntVar y,
218  BoolVar z, IntConLevel icl) {
219  element(home, m.get_array(), x, m.width(), y, m.height(), z, icl);
220  }
221  forceinline void
223  IntVar z, IntConLevel icl) {
224  element(home, m.get_array(), x, m.width(), y, m.height(), z, icl);
225  }
226  forceinline void
228  BoolVar z, IntConLevel icl) {
229  element(home, m.get_array(), x, m.width(), y, m.height(), z, icl);
230  }
231 
232 #ifdef GECODE_HAS_SET_VARS
233  forceinline void
235  SetVar z) {
236  element(home, m.get_array(), x, m.width(), y, m.height(), z);
237  }
238  forceinline void
240  SetVar z) {
241  element(home, m.get_array(), x, m.width(), y, m.height(), z);
242  }
243 #endif
244 
245 }
246 
247 // STATISTICS: minimodel-any
IntConLevel
Consistency levels for integer propagators.
Definition: int.hh:937
Exception: Argument out of range
Definition: exception.hpp:63
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
int height(void) const
Return the height of the matrix.
Definition: matrix.hpp:150
ArrayTraits< A >::ValueType ValueType
The type of elements of this array.
Definition: minimodel.hh:2001
int width(void) const
Return the width of the matrix.
Definition: matrix.hpp:147
Gecode::FloatVal c(-8, 8)
Single _a(2, 3)
A slice of a matrix.
Definition: minimodel.hh:1934
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Slice & reverse(void)
Reverses the contents of the slice, and returns a reference to it.
Definition: matrix.hpp:66
Matrix(A a, int w, int h)
Basic constructor.
Definition: matrix.hpp:131
FloatVal operator+(const FloatVal &x)
Definition: val.hpp:168
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
Slice< A > row(int r) const
Access row r.
Definition: matrix.hpp:181
ArrayTraits< A >::ArgsType ArgsType
The type of the Args-array type for ValueType values.
Definition: minimodel.hh:2003
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntConLevel)
Post domain consistent propagator for .
Definition: element.cpp:43
Boolean integer variables.
Definition: int.hh:491
ArgsType const get_array(void) const
Return an Args-array of the contents of the matrix.
Definition: matrix.hpp:153
ArrayTraits< A >::ArgsType ArgsType
The type of the Args-array type for ValueType values.
Definition: minimodel.hh:1937
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Set variables
Definition: set.hh:129
Integer variables.
Definition: int.hh:350
#define forceinline
Definition: config.hpp:132
ValueType & operator()(int c, int r)
Access element (c, r) of the matrix.
Definition: matrix.hpp:159
Matrix-interface for arrays.
Definition: minimodel.hh:1924
Traits of arrays in Gecode.
Definition: array.hpp:68
Gecode toplevel namespace
Slice(const Matrix< A > &a, int fc, int tc, int fr, int tr)
Construct slice.
Definition: matrix.hpp:47
Slice< A > col(int c) const
Access column c.
Definition: matrix.hpp:187
Exception: Sizes of arguments does not match
Definition: exception.hpp:56
Slice< A > slice(int fc, int tc, int fr, int tr) const
Access slice of the matrix.
Definition: matrix.hpp:175
Home class for posting propagators
Definition: core.hpp:717
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.