$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fun/i2v/array.hh
1 // Copyright (C) 2008, 2009, 2012 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_FUN_I2V_ARRAY_HH
28 # define MLN_FUN_I2V_ARRAY_HH
29 
33 
34 # include <vector>
35 # include <algorithm>
36 # include <mln/core/concept/function.hh>
37 # include <mln/util/array.hh>
38 # include <mln/metal/equal.hh>
39 # include <mln/tag/init.hh>
40 
41 # include <mln/fun/internal/selector.hh>
42 
43 namespace mln
44 {
45 
47  namespace fun {
48  namespace i2v {
49  template <typename T> class array;
50  } // end of namespace mln::fun::i2v
51  } // end of namespace mln::fun
52 
53  namespace util {
54  template <typename T> class array;
55  }
56 
57 
58  namespace fun
59  {
60 
61  namespace i2v
62  {
63 
64  template <typename T>
65  class array
66  : public fun::internal::selector_from_result_<T, array<T> >::ret
67  {
68  public:
69 
72 
73  typedef T result;
74  typedef typename std::vector<T>::reference mutable_result;
75 
77 
80 
82  array();
84  array(unsigned n);
86  array(unsigned n, const T& val);
87 
90  array(const util::array<T>& from);
93  array(const std::vector<T>& from);
94 
96 
97 
99  void reserve(unsigned n);
100 
102  void resize(unsigned n);
105  void resize(unsigned n, const T& val);
106 
108  void append(const T& val);
109 
111  unsigned size() const;
112 
114  result operator()(unsigned i) const;
116  mutable_result operator()(unsigned i);
117 
119  void init_(unsigned n);
120 
122  const std::vector<T>& std_vector() const;
123 
124  protected:
125  std::vector<T> v_;
126 
127  };
128 
129 
131  template <typename T>
132  std::ostream& operator<<(std::ostream& ostr,
133  const array<T>& a);
134 
135 
137  template <typename T1, typename T2>
138  void
139  from_to_(const fun::i2v::array<T1>& from, util::array<T2>& to);
140 
141  } // end of namespace mln::fun::i2v
142 
143  } // end of namespace mln::fun
144 
145 } // end of namespace mln
146 
147 
148 namespace std
149 {
150 
152  template <typename T>
153  inline
154  void
155  from_to_(const vector<T>& from, mln::fun::i2v::array<T>& to);
156 
158  template <typename T, typename U>
159  inline
160  void
161  from_to_(const vector<T>& from, mln::fun::i2v::array<U>& to);
162 
163 } // end of namespace std
164 
165 
166 # ifndef MLN_INCLUDE_ONLY
167 
168 namespace mln
169 {
170 
171  // Init.
172 
173  template <typename T1, typename T2>
174  void init_(tag::function_t,
175  fun::i2v::array<T1>& f,
176  const fun::i2v::array<T2>& model)
177  {
178  f.init_(model.size());
179  }
180 
181 
183 
184  namespace fun
185  {
186 
187  namespace i2v
188  {
189 
190  template <typename T>
191  inline
193  {
194  }
195 
196  template <typename T>
197  inline
198  array<T>::array(unsigned n)
199  : v_(n)
200  {
201  }
202 
203  template <typename T>
204  inline
205  array<T>::array(unsigned n, const T& val)
206  : v_(n, val)
207  {
208  }
209 
210  template <typename T>
211  inline
212  array<T>::array(const util::array<T>& from)
213  : v_(from.std_vector())
214  {
215 
216  }
217 
218  template <typename T>
219  inline
220  array<T>::array(const std::vector<T>& from)
221  : v_(from)
222  {
223 
224  }
225 
226  template <typename T>
227  inline
228  void
229  array<T>::reserve(unsigned n)
230  {
231  v_.reserve(n);
232  }
233 
234  template <typename T>
235  inline
236  void
237  array<T>::resize(unsigned n)
238  {
239  v_.resize(n);
240  }
241 
242  template <typename T>
243  inline
244  void
245  array<T>::append(const T& val)
246  {
247  v_.push_back(val);
248  }
249 
250  template <typename T>
251  inline
252  void
253  array<T>::resize(unsigned n, const T& val)
254  {
255  v_.resize(n, val);
256  }
257 
258  template <typename T>
259  inline
260  unsigned
261  array<T>::size() const
262  {
263  return v_.size();
264  }
265 
266  template <typename T>
267  inline
268  typename array<T>::result
269  array<T>::operator()(unsigned i) const
270  {
271  mln_precondition(i < v_.size());
272  return v_[i];
273  }
274 
275  template <typename T>
276  inline
277  typename array<T>::mutable_result
278  array<T>::operator()(unsigned i)
279  {
280  mln_precondition(i < v_.size());
281  return v_[i];
282  }
283 
284  template <typename T>
285  inline
286  void
287  array<T>::init_(unsigned n)
288  {
289  v_.resize(n);
290  }
291 
292  template <typename T>
293  inline
294  const std::vector<T>&
295  array<T>::std_vector() const
296  {
297  return v_;
298  }
299 
300 
301  // Operator <<.
302 
303  template <typename T>
304  std::ostream& operator<<(std::ostream& ostr,
305  const array<T>& a)
306  {
307  ostr << '(';
308  const unsigned n = a.size();
309  for (unsigned i = 0; i < n; ++i)
310  {
311  ostr << a(i);
312  if (i != n - 1)
313  ostr << ", ";
314  }
315  ostr << ')';
316  return ostr;
317  }
318 
319 
320  // Conversion
321 
322  template <typename T1, typename T2>
323  void
324  from_to_(const array<T1>& from, util::array<T2>& to)
325  {
326  to.resize(from.size());
327 
328  for (unsigned i = 0; i < from.size(); ++i)
329  to[i] = convert::to<T2>(from(i));
330  }
331 
332  } // end of namespace mln::fun::i2v
333 
334  } // end of namespace mln::fun
335 
336 
337  template <typename T>
338  inline
339  fun::i2v::array<T> array(unsigned n, const T& t)
340  {
341  fun::i2v::array<T> tmp(n, t);
342  return tmp;
343  }
344 
345 } // end of namespace mln
346 
347 namespace std
348 {
349 
350  template <typename T>
351  inline
352  void
353  from_to_(const vector<T>& from, mln::fun::i2v::array<T>& to)
354  {
355  to = mln::fun::i2v::array<T>(from);
356  }
357 
358  template <typename T, typename U>
359  inline
360  void
361  from_to_(const vector<T>& from, mln::fun::i2v::array<U>& to)
362  {
363  to.resize(from.nelements());
364  for (unsigned i = 0; i < from.size(); ++i)
365  to(i) = mln::convert::to<U>(from[i]);
366  }
367 
368 } // end of namespace std
369 
370 # endif // ! MLN_INCLUDE_ONLY
371 
372 #endif // ! MLN_FUN_I2V_ARRAY_HH