$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
array2d.hh
1 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_METAL_ARRAY2D_HH
27 # define MLN_METAL_ARRAY2D_HH
28 
29 # include <mln/core/concept/object.hh>
30 
31 # include <mln/trait/all.hh>
32 # include <mln/trait/value_.hh>
33 
34 # include <mln/value/ops.hh>
35 
36 namespace mln
37 {
38 
39  // Fwd decls.
40  namespace metal {
41  template <typename T, unsigned r, unsigned c> struct array2d;
42  }
43 
44  namespace trait
45  {
46 
47  template <typename T, unsigned r, unsigned c>
48  struct value_< mln::metal::array2d<T, r, c> >
49  {
50  typedef trait::value::nature::vectorial nature;
51  typedef trait::value::kind::data kind;
52 
53  enum {
54  nbits = r * c * mln_nbits(T),
55  card = r * c * mln_card(T)
56  };
57  typedef mln_value_quant_from_(card) quant;
58 
59  typedef metal::array2d<mln_sum(T),r, c> sum;
60  };
61 
62  } // end of namespace mln::trait
63 
64 
65  namespace metal
66  {
67 
68  template <typename T, unsigned r, unsigned c>
69  struct array2d : public Object< array2d<T, r, c> >
70  {
71 
72  //
73  // Constructors
74  //
75 
76  array2d();
77  array2d(T* ptr);
78 
79  // Copy
80 
81  array2d(const array2d<T, r, c>& rhs);
82 
83  array2d<T, r, c>& operator=(const array2d<T, r, c>& rhs);
84 
85  // Operators
86 
87  template <class U>
88  array2d<T, r, c> operator*(U w);
89 
90  template <class U>
91  array2d<mln_trait_op_div(T,U), r, c>
92  operator/(U w);
93 
94  template <typename U>
95  array2d<mln_trait_op_plus(T,U), r, c>
96  operator+(const array2d<U, r, c>& rhs) const;
97  array2d<T, r, c>& operator+=(const array2d<T, r, c>& rhs);
98 
99  template <typename U>
100  array2d<mln_trait_op_minus(T,U), r, c>
101  operator-(const array2d<U, r, c>& rhs) const;
102  array2d<T, r, c>&
103  operator-=(const array2d<T, r, c>& rhs);
104 
105  // dynamic accessors:
106 
107  T operator()(unsigned row, unsigned col) const {
108  mln_precondition(row < r * c);
109  return buffer_[col * r + row];
110  }
111  T& operator()(unsigned row, unsigned col) {
112  mln_precondition(row < r * c);
113  return buffer_[col * r + row];
114  }
115 
116  // static accessor
117 
118  template<unsigned row, unsigned col>
119  T get() const {
120  return buffer_[col * r + row];
121  }
122  template<unsigned row, unsigned col>
123  T& get() {
124  return buffer_[col * r + row];
125  }
126 
127  template<unsigned row, unsigned col>
128  T get_at() const {
129  mln_precondition(col * r + row < r *c);
130  return buffer_[col * r + row];
131  }
132  template<unsigned row, unsigned col>
133  T& get_at() {
134  mln_precondition(col * r + row < r *c);
135  return buffer_[col * r + row];
136  }
137 
138  enum { length = r * c };
139  protected:
140 
141  T buffer_[r * c];
142  };
143 
144  }
145 
146  namespace trait
147  {
148 
149  // For unary traits.
150 
151  template < template <class> class Name,
152  unsigned r, unsigned c, typename T >
153  struct set_precise_unary_< Name, metal::array2d<T, r, c> >
154  {
155  typedef mln_trait_unary(Name, T) V;
156  typedef metal::array2d<V, r, c> ret;
157  };
158 
159  // For binary traits.
160 
161  template < template <class, class> class Name,
162  unsigned r, unsigned c, typename T,
163  typename U >
164  struct set_precise_binary_< Name,
165  metal::array2d<T, r, c>, metal::array2d<U, r, c> >
166  {
167  typedef mln_trait_binary(Name, T, U) V;
168  typedef metal::array2d<V, r, c> ret;
169  };
170 
171  template < unsigned r, unsigned c, typename T,
172  typename U >
173  struct set_precise_binary_< op::times,
174  metal::array2d<T, r, c>, metal::array2d<U, r, c> >
175  {
176  typedef mln_sum_product(T,U) ret;
177  };
178 
179  template < template <class, class> class Name,
180  unsigned r, unsigned c, typename T,
181  typename S >
182  struct set_precise_binary_< Name,
183  metal::array2d<T, r, c>, mln::value::scalar_<S> >
184  {
185  typedef mln_trait_binary(Name, T, S) V;
186  typedef metal::array2d<V, r, c> ret;
187  };
188 
189  template < template<class, class> class Name,
190  unsigned r, unsigned c, typename T,
191  typename S >
192  struct set_binary_< Name,
193  mln::Object, metal::array2d<T, r, c>,
194  mln::value::Scalar, S >
195  {
196  typedef mln_trait_binary(Name, T, S) V;
197  typedef metal::array2d<T, r, c> ret;
198  };
199 
200  } // end of namespace mln::trait
201 
202 
203  namespace metal
204  {
205 
206  //
207  // Constructors
208  //
209 
210  template <typename T, unsigned r, unsigned c>
211  array2d<T, r, c>::array2d()
212  {
213  }
214 
215  template <typename T, unsigned r, unsigned c>
216  array2d<T, r, c>::array2d(T* ptr)
217  {
218  for (unsigned i = 0; i < r * c; ++i)
219  buffer_[i] = *ptr++;
220  }
221 
222  // Copy
223 
224  template <typename T, unsigned r, unsigned c>
225  array2d<T, r, c>::array2d(const array2d<T, r, c>& rhs)
226  {
227  for (unsigned i = 0; i < r * c; ++i)
228  buffer_[i] = rhs[i];
229  }
230  template <typename T, unsigned r, unsigned c>
231  array2d<T, r, c>&
232  array2d<T, r, c>::operator=(const array2d<T, r, c>& rhs)
233  {
234  for (unsigned i = 0; i < r * c; ++i)
235  buffer_[i] = rhs[i];
236  return *this;
237  }
238 
239  // Operators
240 
241  template <typename T, unsigned r, unsigned c>
242  template <class U>
243  array2d<T, r, c>
245  {
246  //fixme mln_trait_op_mult<int,U>
247  array2d<T, r, c> tmp;
248  for (unsigned i = 0; i < r * c; ++i)
249  tmp[i] = this->buffer_[i] * w;
250  return tmp;
251  }
252 
253  template <typename T, unsigned r, unsigned c>
254  template <class U>
255  array2d<mln_trait_op_div(T,U), r, c>
257  {
258  array2d<T, r, c> tmp;
259  for (unsigned i = 0; i < r * c; ++i)
260  tmp[i] = this->buffer_[i] / w;
261  return tmp;
262  }
263 
264  template <typename T, unsigned r, unsigned c>
265  template <typename U>
266  array2d<mln_trait_op_plus(T,U), r, c>
267  array2d<T,r, c>::operator+(const array2d<U, r, c>& rhs) const
268  {
269  array2d<T, r, c> tmp;
270  for (unsigned i = 0; i < r * c; ++i)
271  tmp[i] = this->buffer_[i] + rhs.buffer_[i];
272  return tmp;
273  }
274  template <typename T, unsigned r, unsigned c>
275  array2d<T, r, c>&
276  array2d<T, r, c>::operator+=(const array2d<T, r, c>& rhs)
277  {
278  for (unsigned i = 0; i < r * c; ++i)
279  this->buffer_[i] += rhs.buffer_[i];
280  return *this;
281  }
282 
283  template <typename T, unsigned r, unsigned c>
284  template <typename U>
285  array2d<mln_trait_op_minus(T,U), r, c>
286  array2d<T,r, c>::operator-(const array2d<U, r, c>& rhs) const
287  {
288  array2d<T, r, c> tmp;
289  for (unsigned i = 0; i < r * c; ++i)
290  tmp[i] = this->buffer_[i] - rhs.buffer_[i];
291  return tmp;
292  }
293  template <typename T, unsigned r, unsigned c>
294  array2d<T, r, c>&
295  array2d<T, r, c>::operator-=(const array2d<T, r, c>& rhs)
296  {
297  for (unsigned i = 0; i < r * c; ++i)
298  this->buffer_[i] -= rhs.buffer_[i];
299  return *this;
300  }
301 
302  } // end of namespace metal
303 
304 } // end of namespace mln
305 
306 #endif // ! MLN_METAL_ARRAY2D_HH