26 #ifndef MLN_METAL_ARRAY2D_HH
27 # define MLN_METAL_ARRAY2D_HH
29 # include <mln/core/concept/object.hh>
31 # include <mln/trait/all.hh>
32 # include <mln/trait/value_.hh>
34 # include <mln/value/ops.hh>
41 template <
typename T,
unsigned r,
unsigned c>
struct array2d;
47 template <
typename T,
unsigned r,
unsigned c>
48 struct value_< mln::metal::array2d<T, r, c> >
50 typedef trait::value::nature::vectorial
nature;
51 typedef trait::value::kind::data
kind;
54 nbits = r * c * mln_nbits(
T),
55 card = r * c * mln_card(
T)
57 typedef mln_value_quant_from_(card) quant;
59 typedef metal::array2d<mln_sum(
T),r, c>
sum;
68 template <
typename T,
unsigned r,
unsigned c>
69 struct array2d :
public Object< array2d<T, r, c> >
81 array2d(
const array2d<T, r, c>&
rhs);
83 array2d<T, r, c>& operator=(
const array2d<T, r, c>&
rhs);
91 array2d<mln_trait_op_div(T,U), r, c>
95 array2d<mln_trait_op_plus(T,U), r, c>
100 array2d<mln_trait_op_minus(T,U), r, c>
107 T operator()(
unsigned row,
unsigned col)
const {
108 mln_precondition(row < r * c);
109 return buffer_[col * r + row];
111 T& operator()(
unsigned row,
unsigned col) {
112 mln_precondition(row < r * c);
113 return buffer_[col * r + row];
118 template<
unsigned row,
unsigned col>
120 return buffer_[col * r + row];
122 template<
unsigned row,
unsigned col>
124 return buffer_[col * r + row];
127 template<
unsigned row,
unsigned col>
129 mln_precondition(col * r + row < r *c);
130 return buffer_[col * r + row];
132 template<
unsigned row,
unsigned col>
134 mln_precondition(col * r + row < r *c);
135 return buffer_[col * r + row];
138 enum { length = r * c };
151 template <
template <
class>
class Name,
152 unsigned r,
unsigned c,
typename T >
153 struct set_precise_unary_< Name, metal::array2d<T, r, c> >
155 typedef mln_trait_unary(Name,
T) V;
156 typedef metal::array2d<V, r, c>
ret;
161 template < template <class, class> class Name,
162 unsigned r,
unsigned c, typename
T,
164 struct set_precise_binary_< Name,
165 metal::array2d<T, r, c>, metal::array2d<U, r, c> >
167 typedef mln_trait_binary(Name, T, U) V;
168 typedef metal::array2d<V, r, c> ret;
171 template <
unsigned r,
unsigned c, typename T,
173 struct set_precise_binary_< op::times,
174 metal::array2d<T, r, c>, metal::array2d<U, r, c> >
176 typedef mln_sum_product(T,U) ret;
179 template < template <class, class> class Name,
180 unsigned r,
unsigned c, typename T,
182 struct set_precise_binary_< Name,
183 metal::array2d<T, r, c>, mln::
value::scalar_<S> >
185 typedef mln_trait_binary(Name, T, S) V;
186 typedef metal::array2d<V, r, c> ret;
189 template < template<class, class> class Name,
190 unsigned r,
unsigned c, typename T,
192 struct set_binary_< Name,
193 mln::Object, metal::array2d<T, r, c>,
194 mln::
value::Scalar, S >
196 typedef mln_trait_binary(Name, T, S) V;
197 typedef metal::array2d<T, r, c> ret;
210 template <
typename T,
unsigned r,
unsigned c>
211 array2d<T, r, c>::array2d()
215 template <
typename T,
unsigned r,
unsigned c>
216 array2d<T, r, c>::array2d(T* ptr)
218 for (
unsigned i = 0; i < r * c; ++i)
224 template <
typename T,
unsigned r,
unsigned c>
225 array2d<T, r, c>::array2d(
const array2d<T, r, c>&
rhs)
227 for (
unsigned i = 0; i < r * c; ++i)
230 template <
typename T,
unsigned r,
unsigned c>
232 array2d<T, r, c>::operator=(
const array2d<T, r, c>& rhs)
234 for (
unsigned i = 0; i < r * c; ++i)
241 template <
typename T,
unsigned r,
unsigned c>
247 array2d<T, r, c> tmp;
248 for (
unsigned i = 0; i < r * c; ++i)
249 tmp[i] = this->buffer_[i] * w;
253 template <
typename T,
unsigned r,
unsigned c>
255 array2d<mln_trait_op_div(T,U), r, c>
258 array2d<T, r, c> tmp;
259 for (
unsigned i = 0; i < r * c; ++i)
260 tmp[i] = this->buffer_[i] / w;
264 template <
typename T,
unsigned r,
unsigned c>
265 template <
typename U>
266 array2d<mln_trait_op_plus(T,U), r, c>
269 array2d<T, r, c> tmp;
270 for (
unsigned i = 0; i < r * c; ++i)
271 tmp[i] = this->buffer_[i] + rhs.buffer_[i];
274 template <
typename T,
unsigned r,
unsigned c>
278 for (
unsigned i = 0; i < r * c; ++i)
279 this->buffer_[i] += rhs.buffer_[i];
283 template <
typename T,
unsigned r,
unsigned c>
284 template <
typename U>
285 array2d<mln_trait_op_minus(T,U), r, c>
288 array2d<T, r, c> tmp;
289 for (
unsigned i = 0; i < r * c; ++i)
290 tmp[i] = this->buffer_[i] - rhs.buffer_[i];
293 template <
typename T,
unsigned r,
unsigned c>
297 for (
unsigned i = 0; i < r * c; ++i)
298 this->buffer_[i] -= rhs.buffer_[i];
306 #endif // ! MLN_METAL_ARRAY2D_HH