~ubuntu-branches/ubuntu/natty/gecode/natty

« back to all changes in this revision

Viewing changes to minimodel/matrix.icc

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2005-12-24 07:51:25 UTC
  • Revision ID: james.westby@ubuntu.com-20051224075125-klkiqofvbfvusfvt
Tags: upstream-1.0.0.dfsg.1
ImportĀ upstreamĀ versionĀ 1.0.0.dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Main authors:
 
3
 *     Mikael Lagerkvist <lagerkvist@gecode.org>
 
4
 *
 
5
 *  Copyright:
 
6
 *     Mikael Lagerkvist, 2005
 
7
 *
 
8
 *  Last modified:
 
9
 *     $Date: 2005-07-28 22:52:19 +0200 (Thu, 28 Jul 2005) $ by $Author: schulte $
 
10
 *     $Revision: 2072 $
 
11
 *
 
12
 *  This file is part of Gecode, the generic constraint
 
13
 *  development environment:
 
14
 *     http://www.gecode.org
 
15
 *
 
16
 *  See the file "LICENSE" for information on usage and
 
17
 *  redistribution of this file, and for a
 
18
 *     DISCLAIMER OF ALL WARRANTIES.
 
19
 *
 
20
 */
 
21
 
 
22
namespace Gecode { namespace MiniModel {
 
23
 
 
24
  template <class A>
 
25
  inline 
 
26
  Matrix<A>::Slice::Slice(Matrix<A>& a, 
 
27
                          unsigned int fc, unsigned int tc, 
 
28
                          unsigned int fr, unsigned int tr)
 
29
    : _r((tc-fc)*(tr-fr)), _fc(fc), _tc(tc), _fr(fr), _tr(tr) {
 
30
    if (tc > a.width() || tr > a.height())
 
31
      throw ArgumentOutOfRange("Arguments out of range");
 
32
    if (fc >= tc || fr >= tr)
 
33
      return;
 
34
 
 
35
    int i = 0;
 
36
    for (unsigned int w = fc; w < tc; ++w) {
 
37
      for (unsigned int h = fr; h < tr; ++h) {
 
38
        _r[i++] = a(w, h);
 
39
      }
 
40
    }
 
41
  }
 
42
  
 
43
  template <class A>
 
44
  forceinline 
 
45
  Matrix<A>::Slice::operator typename Matrix<A>::args_type(void) { 
 
46
    return _r; 
 
47
  }
 
48
  template <class A>
 
49
  forceinline 
 
50
  Matrix<A>::Slice::operator Matrix<typename Matrix<A>::args_type>(void) { 
 
51
    return Matrix<args_type>(_r, _tc-_fc, _tr-_fr);
 
52
  }
 
53
 
 
54
 
 
55
  template <class A>
 
56
  forceinline 
 
57
  Matrix<A>::Matrix(A a, unsigned int w, unsigned int h) 
 
58
    : _a(a), _w(w), _h(h) {
 
59
    if(_w * _h != static_cast<unsigned int>(_a.size()))
 
60
      throw ArgumentSizeMismatch("Size w*h is not compatible with"
 
61
                                 " array length.");
 
62
  }
 
63
  
 
64
  template <class A>
 
65
  forceinline 
 
66
  Matrix<A>::Matrix(A a, unsigned int n) 
 
67
    : _a(a), _w(n), _h(n) {
 
68
    if(n*n != static_cast<unsigned int>(_a.size()))
 
69
      throw ArgumentSizeMismatch("Size n*n are not compatible with"
 
70
                                 " array length.");
 
71
  }
 
72
  
 
73
  template <class A>
 
74
  forceinline unsigned int const
 
75
  Matrix<A>::width(void)  { return _w; }
 
76
  template <class A>
 
77
  forceinline unsigned int const
 
78
  Matrix<A>::height(void) { return _h; }
 
79
  template <class A>
 
80
  forceinline typename Matrix<A>::args_type const
 
81
  Matrix<A>::get_array(void) {
 
82
    return args_type(_a);
 
83
  }
 
84
  
 
85
  template <class A>
 
86
  forceinline typename Matrix<A>::value_type& 
 
87
  Matrix<A>::operator()(unsigned int c, unsigned int r) {
 
88
    if(c > _w || r > _h)
 
89
      throw ArgumentOutOfRange("Arguments out of range");
 
90
    
 
91
    return _a[r*_w + c]; 
 
92
  }
 
93
  
 
94
  template <class A>
 
95
  forceinline typename Matrix<A>::Slice 
 
96
  Matrix<A>::slice(unsigned int fc, unsigned int tc, 
 
97
              unsigned int fr, unsigned int tr) {
 
98
    return Slice(*this, fc, tc, fr, tr);
 
99
  }
 
100
  
 
101
  template <class A>
 
102
  forceinline typename Matrix<A>::args_type 
 
103
  Matrix<A>::row(int r) {
 
104
    return slice(0, width(), r, r+1);
 
105
  }
 
106
  
 
107
  template <class A>
 
108
  forceinline typename Matrix<A>::args_type 
 
109
  Matrix<A>::col(int c) {
 
110
    return slice(c, c+1, 0, height());
 
111
  }
 
112
 
 
113
}}
 
114
 
 
115
// STATISTICS: minimodel-any