$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
array1d.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_ARRAY1D_HH
27 # define MLN_METAL_ARRAY1D_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 Size> struct array1d;
42  }
43 
44  namespace trait
45  {
46 
47  template <typename T, unsigned Size>
48  struct value_< mln::metal::array1d<T,Size> >
49  {
50  typedef trait::value::nature::vectorial nature;
51  typedef trait::value::kind::data kind;
52 
53  enum {
54  nbits = Size * mln_nbits(T),
55  card = Size * mln_card(T)
56  };
57  typedef mln_value_quant_from_(card) quant;
58 
59  typedef metal::array1d<mln_sum(T),Size> sum;
60  };
61 
62  } // end of namespace mln::trait
63 
64 
65  namespace metal
66  {
67 
68  template <typename T, unsigned Size>
69  struct array1d : public Object< array1d<T,Size> >
70  {
71 
72  //
73  // Constructors
74  //
75 
76  array1d();
77  array1d(T* ptr);
78 
79  // Copy
80 
81  array1d(const array1d<T, Size>& rhs);
82  array1d<T, Size>& operator=(const array1d<T, Size>& rhs);
83 
84  // Operators
85 
86  template <class U>
87  array1d<T, Size> operator*(U w);
88 
89  template <class U>
90  array1d<mln_trait_op_div(T,U), Size>
91  operator/(U w);
92 
93  template <typename U>
94  array1d<mln_trait_op_plus(T,U), Size>
95  operator+(const array1d<U, Size>& rhs) const;
96  array1d<T, Size>& operator+=(const array1d<T, Size>& rhs);
97 
98  template <typename U>
99  array1d<mln_trait_op_minus(T,U), Size>
100  operator-(const array1d<U, Size>& rhs) const;
101  array1d<T, Size>&
102  operator-=(const array1d<T, Size>& rhs);
103 
104 
105  // dynamic accessors:
106 
107  T operator[](unsigned i) const {
108  mln_precondition(i < Size);
109  return buffer_[i];
110  }
111  T& operator[](unsigned i) {
112  mln_precondition(i < Size);
113  return buffer_[i];
114  }
115 
116  // static accessor
117 
118  template<unsigned i>
119  T get() const {
120  return buffer_[i];
121  }
122  template<unsigned i>
123  T& get() {
124  return buffer_[i];
125  }
126 
127  enum { length = Size };
128  protected:
129 
130  T buffer_[Size];
131  };
132 
133  }
134 
135  namespace trait
136  {
137 
138  // For unary traits.
139 
140  template < template <class> class Name,
141  unsigned n, typename T >
142  struct set_precise_unary_< Name, metal::array1d<T, n> >
143  {
144  typedef mln_trait_unary(Name, T) V;
145  typedef metal::array1d<V, n> ret;
146  };
147 
148  // For binary traits.
149 
150  template < template <class, class> class Name,
151  unsigned n, typename T,
152  typename U >
153  struct set_precise_binary_< Name,
154  metal::array1d<T, n>, metal::array1d<U, n> >
155  {
156  typedef mln_trait_binary(Name, T, U) V;
157  typedef metal::array1d<V, n> ret;
158  };
159 
160  template < unsigned n, typename T,
161  typename U >
162  struct set_precise_binary_< op::times,
163  metal::array1d<T, n>, metal::array1d<U, n> >
164  {
165  typedef mln_sum_product(T,U) ret;
166  };
167 
168  template < template <class, class> class Name,
169  unsigned n, typename T,
170  typename S >
171  struct set_precise_binary_< Name,
172  metal::array1d<T, n>, mln::value::scalar_<S> >
173  {
174  typedef mln_trait_binary(Name, T, S) V;
175  typedef metal::array1d<V, n> ret;
176  };
177 
178  template < template<class, class> class Name,
179  unsigned n, typename T,
180  typename S >
181  struct set_binary_< Name,
182  mln::Object, metal::array1d<T, n>,
183  mln::value::Scalar, S >
184  {
185  typedef mln_trait_binary(Name, T, S) V;
186  typedef metal::array1d<T, n> ret;
187  };
188 
189  } // end of namespace mln::trait
190 
191 
192  namespace metal
193  {
194 
195  //
196  // Constructors
197  //
198 
199  template <typename T, unsigned Size>
200  array1d<T,Size>::array1d()
201  {
202  }
203 
204  template <typename T, unsigned Size>
205  array1d<T,Size>::array1d(T* ptr)
206  {
207  for (unsigned i = 0; i < Size; ++i)
208  buffer_[i] = *ptr++;
209  }
210 
211  // Copy
212 
213  template <typename T, unsigned Size>
214  array1d<T,Size>::array1d(const array1d<T, Size>& rhs)
215  {
216  for (unsigned i = 0; i < Size; ++i)
217  buffer_[i] = rhs[i];
218  }
219  template <typename T, unsigned Size>
220  array1d<T, Size>&
221  array1d<T,Size>::operator=(const array1d<T, Size>& rhs)
222  {
223  for (unsigned i = 0; i < Size; ++i)
224  buffer_[i] = rhs[i];
225  return *this;
226  }
227 
228  // Operators
229 
230  template <typename T, unsigned Size>
231  template <class U>
232  array1d<T, Size>
234  {
235  //fixme mln_trait_op_mult<int,U>
236  array1d<T, Size> tmp;
237  for (unsigned i = 0; i < Size; ++i)
238  tmp[i] = this->buffer_[i] * w;
239  return tmp;
240  }
241 
242  template <typename T, unsigned Size>
243  template <class U>
244  array1d<mln_trait_op_div(T,U), Size>
246  {
247  array1d<T, Size> tmp;
248  for (unsigned i = 0; i < Size; ++i)
249  tmp[i] = this->buffer_[i] / w;
250  return tmp;
251  }
252 
253  template <typename T, unsigned Size>
254  template <typename U>
255  array1d<mln_trait_op_plus(T,U), Size>
256  array1d<T,Size>::operator+(const array1d<U, Size>& rhs) const
257  {
258  array1d<T, Size> tmp;
259  for (unsigned i = 0; i < Size; ++i)
260  tmp[i] = this->buffer_[i] + rhs.buffer_[i];
261  return tmp;
262  }
263  template <typename T, unsigned Size>
264  array1d<T, Size>&
265  array1d<T,Size>::operator+=(const array1d<T, Size>& rhs)
266  {
267  for (unsigned i = 0; i < Size; ++i)
268  this->buffer_[i] += rhs.buffer_[i];
269  return *this;
270  }
271 
272  template <typename T, unsigned Size>
273  template <typename U>
274  array1d<mln_trait_op_minus(T,U), Size>
275  array1d<T,Size>::operator-(const array1d<U, Size>& rhs) const
276  {
277  array1d<T, Size> tmp;
278  for (unsigned i = 0; i < Size; ++i)
279  tmp[i] = this->buffer_[i] - rhs.buffer_[i];
280  return tmp;
281  }
282  template <typename T, unsigned Size>
283  array1d<T, Size>&
284  array1d<T,Size>::operator-=(const array1d<T, Size>& rhs)
285  {
286  for (unsigned i = 0; i < Size; ++i)
287  this->buffer_[i] -= rhs.buffer_[i];
288  return *this;
289  }
290 
291  } // end of namespace metal
292 
293 } // end of namespace mln
294 
295 #endif // ! MLN_METAL_ARRAY1D_HH