this repo has no description
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 39namespace 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>& 62 Slice<A>::reverse(void) { 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 70 Slice<A>::operator ArgsType(void) { 71 return _r; 72 } 73 template<class A> 74 inline 75 Slice<A>::operator Matrix<typename Slice<A>::ArgsType>(void) { 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 85 Slice<A>::operator const Matrix<typename Slice<A>::ArgsType>(void) const { 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> 126 forceinline 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> 134 forceinline 135 Matrix<A>::Matrix(A a, int n) 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> 154 forceinline typename Matrix<A>::ValueType& 155 Matrix<A>::operator ()(int c, int r) { 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 208 element(Home home, const Matrix<IntArgs>& m, IntVar x, IntVar y, 209 IntVar z, IntPropLevel ipl) { 210 element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl); 211 } 212 forceinline void 213 element(Home home, const Matrix<IntArgs>& m, IntVar x, IntVar y, 214 BoolVar z, IntPropLevel ipl) { 215 element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl); 216 } 217 forceinline void 218 element(Home home, const Matrix<IntVarArgs>& m, IntVar x, IntVar y, 219 IntVar z, IntPropLevel ipl) { 220 element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl); 221 } 222 forceinline void 223 element(Home home, const Matrix<BoolVarArgs>& m, IntVar x, IntVar y, 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 230 element(Home home, const Matrix<IntSetArgs>& m, IntVar x, IntVar y, 231 SetVar z) { 232 element(home, m.get_array(), x, m.width(), y, m.height(), z); 233 } 234 forceinline void 235 element(Home home, const Matrix<SetVarArgs>& m, IntVar x, IntVar y, 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