/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Mikael Lagerkvist * * Copyright: * Mikael Lagerkvist, 2005 * * Bugfixes provided by: * Olof Sivertsson * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ #include namespace Gecode { template inline Slice::Slice(const Matrix& a, int fc, int tc, int fr, int tr) : _r(0), _fc(fc), _tc(tc), _fr(fr), _tr(tr) { if (tc > a.width() || tr > a.height()) throw MiniModel::ArgumentOutOfRange("Slice::Slice"); if (fc >= tc || fr >= tr) { _fc=0; _tc=0; _fr=0; _tr=0; return; } _r = ArgsType((tc-fc)*(tr-fr)); int i = 0; for (int h = fr; h < tr; h++) for (int w = fc; w < tc; w++) _r[i++] = a(w, h); } template Slice& Slice::reverse(void) { for (int i = 0; i < _r.size()/2; i++) std::swap(_r[i], _r[_r.size()-i-1]); return *this; } template inline Slice::operator ArgsType(void) { return _r; } template inline Slice::operator Matrix::ArgsType>(void) { return Matrix(_r, _tc-_fc, _tr-_fr); } template inline Slice::operator const typename Slice::ArgsType(void) const { return _r; } template inline Slice::operator const Matrix::ArgsType>(void) const { return Matrix(_r, _tc-_fc, _tr-_fr); } template typename Slice::ArgsType operator+(const Slice& x, const Slice& y) { typename Slice::ArgsType xx = x; typename Slice::ArgsType yy = y; return xx+yy; } template typename Slice::ArgsType operator+(const Slice& x, const typename ArrayTraits::ArgsType& y) { typename Slice::ArgsType xx = x; return xx+y; } template typename Slice::ArgsType operator+(const typename ArrayTraits::ArgsType& x, const Slice& y) { typename Slice::ArgsType yy = y; return x+yy; } template typename Slice::ArgsType operator+(const Slice& x, const typename ArrayTraits::ValueType& y) { typename Slice::ArgsType xx = x; return xx+y; } template typename Slice::ArgsType operator+(const typename ArrayTraits::ValueType& x, const Slice& y) { typename Slice::ArgsType yy = y; return x+yy; } template forceinline Matrix::Matrix(A a, int w, int h) : _a(a), _w(w), _h(h) { if ((_w * _h) != _a.size()) throw MiniModel::ArgumentSizeMismatch("Matrix::Matrix(A, w, h)"); } template forceinline Matrix::Matrix(A a, int n) : _a(a), _w(n), _h(n) { if (n*n != _a.size()) throw MiniModel::ArgumentSizeMismatch("Matrix::Matrix(A, n)"); } template forceinline int Matrix::width(void) const { return _w; } template forceinline int Matrix::height(void) const { return _h; } template inline typename Matrix::ArgsType const Matrix::get_array(void) const { return ArgsType(_a); } template forceinline typename Matrix::ValueType& Matrix::operator ()(int c, int r) { if ((c >= _w) || (r >= _h)) throw MiniModel::ArgumentOutOfRange("Matrix::operator ()"); return _a[r*_w + c]; } template forceinline const typename Matrix::ValueType& Matrix::operator ()(int c, int r) const { if ((c >= _w) || (r >= _h)) throw MiniModel::ArgumentOutOfRange("Matrix::operator ()"); return _a[r*_w + c]; } template inline Slice Matrix::slice(int fc, int tc, int fr, int tr) const { return Slice(*this, fc, tc, fr, tr); } template inline Slice Matrix::row(int r) const { return slice(0, width(), r, r+1); } template inline Slice Matrix::col(int c) const { return slice(c, c+1, 0, height()); } template std::basic_ostream& operator <<(std::basic_ostream& os, const Matrix& m) { std::basic_ostringstream s; s.copyfmt(os); s.width(0); for (int i=0; i std::basic_ostream& operator <<(std::basic_ostream& os, const Slice& s) { return os << static_cast::ArgsType>(s); } forceinline void element(Home home, const Matrix& m, IntVar x, IntVar y, IntVar z, IntPropLevel ipl) { element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl); } forceinline void element(Home home, const Matrix& m, IntVar x, IntVar y, BoolVar z, IntPropLevel ipl) { element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl); } forceinline void element(Home home, const Matrix& m, IntVar x, IntVar y, IntVar z, IntPropLevel ipl) { element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl); } forceinline void element(Home home, const Matrix& m, IntVar x, IntVar y, BoolVar z, IntPropLevel ipl) { element(home, m.get_array(), x, m.width(), y, m.height(), z, ipl); } #ifdef GECODE_HAS_SET_VARS forceinline void element(Home home, const Matrix& m, IntVar x, IntVar y, SetVar z) { element(home, m.get_array(), x, m.width(), y, m.height(), z); } forceinline void element(Home home, const Matrix& m, IntVar x, IntVar y, SetVar z) { element(home, m.get_array(), x, m.width(), y, m.height(), z); } #endif } // STATISTICS: minimodel-any