Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
fun/i2v/array.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_FUN_I2V_ARRAY_HH
27 # define MLN_FUN_I2V_ARRAY_HH
28 
32 
33 # include <vector>
34 # include <algorithm>
35 # include <mln/core/concept/function.hh>
36 # include <mln/util/array.hh>
37 # include <mln/metal/equal.hh>
38 # include <mln/tag/init.hh>
39 
40 # include <mln/fun/internal/selector.hh>
41 
42 namespace mln
43 {
44 
46  namespace fun {
47  namespace i2v {
48  template <typename T> class array;
49  } // end of namespace mln::fun::i2v
50  } // end of namespace mln::fun
51 
52 
53 
54  namespace convert
55  {
56 
57  namespace over_load
58  {
59 
60  template <typename T>
61  inline
62  void
63  from_to_(const util::array<T>& from, fun::i2v::array<T>& to);
64 
65  template <typename T, typename U>
66  inline
67  void
68  from_to_(const util::array<T>& from, fun::i2v::array<U>& to);
69 
70  template <typename T>
71  inline
72  void
73  from_to_(const std::vector<T>& from, fun::i2v::array<T>& to);
74 
75  template <typename T, typename U>
76  inline
77  void
78  from_to_(const std::vector<T>& from, fun::i2v::array<U>& to);
79 
80 
81  } // end of namespace mln::convert::over_load
82 
83  } // end of namespace mln::convert
84 
85 
86  namespace fun
87  {
88 
89  namespace i2v
90  {
91 
92  template <typename T>
93  class array
94  : public fun::internal::selector_from_result_<T, array<T> >::ret
95  {
96  public:
97 
100  typedef T result;
101  typedef typename std::vector<T>::reference mutable_result;
103 
106 
108  array();
110  array(unsigned n);
112  array(unsigned n, const T& val);
113 
116  array(const util::array<T>& from);
119  array(const std::vector<T>& from);
121 
122 
124  void reserve(unsigned n);
125 
127  void resize(unsigned n);
130  void resize(unsigned n, const T& val);
131 
133  void append(const T& val);
134 
136  unsigned size() const;
137 
139  result operator()(unsigned i) const;
141  mutable_result operator()(unsigned i);
142 
144  void init_(unsigned n);
145 
147  const std::vector<T>& std_vector() const;
148 
149  protected:
150  std::vector<T> v_;
151 
152  };
153 
154 
156  template <typename T>
157  std::ostream& operator<<(std::ostream& ostr,
158  const array<T>& a);
159 
160 
161  } // end of namespace mln::fun::i2v
162 
163  } // end of namespace mln::fun
164 
165 
166 
167 # ifndef MLN_INCLUDE_ONLY
168 
169  // Init.
170 
171  template <typename T1, typename T2>
172  void init_(tag::function_t,
173  fun::i2v::array<T1>& f,
174  const fun::i2v::array<T2>& model)
175  {
176  f.init_(model.size());
177  }
178 
179 
180  // convert::from_to
181 
182  namespace convert
183  {
184 
185  namespace over_load
186  {
187 
188  template <typename T>
189  inline
190  void
191  from_to_(const util::array<T>& from, fun::i2v::array<T>& to)
192  {
193  to = fun::i2v::array<T>(from);
194  }
195 
196  template <typename T, typename U>
197  inline
198  void
199  from_to_(const util::array<T>& from, fun::i2v::array<U>& to)
200  {
201  to.resize(from.nelements());
202  for (unsigned i = 0; i < from.nelements(); ++i)
203  to(i) = convert::to<U>(from[i]);
204  }
205 
206  template <typename T>
207  inline
208  void
209  from_to_(const std::vector<T>& from, fun::i2v::array<T>& to)
210  {
211  to = fun::i2v::array<T>(from);
212  }
213 
214  template <typename T, typename U>
215  inline
216  void
217  from_to_(const std::vector<T>& from, fun::i2v::array<U>& to)
218  {
219  to.resize(from.nelements());
220  for (unsigned i = 0; i < from.size(); ++i)
221  to(i) = convert::to<U>(from[i]);
222  }
223 
224 
225  } // end of namespace mln::convert::over_load
226 
227  } // end of namespace mln::convert
228 
229 
230 
232 
233  namespace fun
234  {
235 
236  namespace i2v
237  {
238 
239  template <typename T>
240  inline
241  array<T>::array()
242  {
243  }
244 
245  template <typename T>
246  inline
247  array<T>::array(unsigned n)
248  : v_(n)
249  {
250  }
251 
252  template <typename T>
253  inline
254  array<T>::array(unsigned n, const T& val)
255  : v_(n, val)
256  {
257  }
258 
259  template <typename T>
260  inline
261  array<T>::array(const util::array<T>& from)
262  : v_(from.std_vector())
263  {
264 
265  }
266 
267  template <typename T>
268  inline
269  array<T>::array(const std::vector<T>& from)
270  : v_(from)
271  {
272 
273  }
274 
275  template <typename T>
276  inline
277  void
278  array<T>::reserve(unsigned n)
279  {
280  v_.reserve(n);
281  }
282 
283  template <typename T>
284  inline
285  void
286  array<T>::resize(unsigned n)
287  {
288  v_.resize(n);
289  }
290 
291  template <typename T>
292  inline
293  void
294  array<T>::append(const T& val)
295  {
296  v_.push_back(val);
297  }
298 
299  template <typename T>
300  inline
301  void
302  array<T>::resize(unsigned n, const T& val)
303  {
304  v_.resize(n, val);
305  }
306 
307  template <typename T>
308  inline
309  unsigned
310  array<T>::size() const
311  {
312  return v_.size();
313  }
314 
315  template <typename T>
316  inline
317  typename array<T>::result
318  array<T>::operator()(unsigned i) const
319  {
320  mln_precondition(i < v_.size());
321  return v_[i];
322  }
323 
324  template <typename T>
325  inline
326  typename array<T>::mutable_result
327  array<T>::operator()(unsigned i)
328  {
329  mln_precondition(i < v_.size());
330  return v_[i];
331  }
332 
333  template <typename T>
334  inline
335  void
336  array<T>::init_(unsigned n)
337  {
338  v_.resize(n);
339  }
340 
341  template <typename T>
342  inline
343  const std::vector<T>&
344  array<T>::std_vector() const
345  {
346  return v_;
347  }
348 
349 
350  // Operator <<.
351 
352  template <typename T>
353  std::ostream& operator<<(std::ostream& ostr,
354  const array<T>& a)
355  {
356  ostr << '(';
357  const unsigned n = a.size();
358  for (unsigned i = 0; i < n; ++i)
359  {
360  ostr << a(i);
361  if (i != n - 1)
362  ostr << ", ";
363  }
364  ostr << ')';
365  return ostr;
366  }
367 
368 
369  } // end of namespace mln::fun::i2v
370 
371  } // end of namespace mln::fun
372 
373 
374  template <typename T>
375  inline
376  fun::i2v::array<T> array(unsigned n, const T& t)
377  {
378  fun::i2v::array<T> tmp(n, t);
379  return tmp;
380  }
381 
382 # endif // ! MLN_INCLUDE_ONLY
383 
384 } // end of namespace mln
385 
386 
387 #endif // ! MLN_FUN_I2V_ARRAY_HH