Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
take_as_init.hh
1 // Copyright (C) 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_ACCU_IMAGE_TAKE_AS_INIT_HH
27 # define MLN_ACCU_IMAGE_TAKE_AS_INIT_HH
28 
32 
33 # include <mln/core/concept/accumulator.hh>
34 # include <mln/core/concept/image.hh>
35 # include <mln/border/resize_equal.hh>
36 
37 
38 namespace mln
39 {
40 
41  namespace accu
42  {
43 
44  namespace image
45  {
46 
47  template <typename I>
48  void
49  take_as_init(Image<I>& input, const mln_deduce(I, value, argument)& v);
50 
51  template <typename I, typename J>
52  void
53  take_as_init(Image<I>& input, const Image<J>& values);
54 
55 
56 
57 # ifndef MLN_INCLUDE_ONLY
58 
59  // Tests.
60 
61  namespace internal
62  {
63 
64  template <typename I, typename J>
65  inline
66  void
67  take_as_init_tests(Image<I>& input_, const Image<J>& values_)
68  {
69  I& input = exact(input_);
70  const J& values = exact(values_);
71 
72  mln_precondition(input.is_valid());
73  mln_precondition(values.is_valid());
74 
75  mln_precondition(values.domain() <= input.domain());
76 
77  (void) input;
78  (void) values;
79  }
80 
81  } // end of namespace mln::accu::image::internal
82 
83 
84  namespace impl
85  {
86 
87  // Generic versions.
88 
89  namespace generic
90  {
91 
92  template <typename I>
93  void
94  take_as_init(Image<I>& input_, const mln_deduce(I, value, argument)& v)
95  {
96  trace::entering("accu::impl::image::generic::take_as_init");
97 
98  mlc_is_a(mln_value(I), Accumulator)::check();
99 
100  I& input = exact(input_);
101  mln_precondition(input.is_valid());
102 
103  mln_piter(I) p(input.domain());
104  for_all(p)
105  input(p).take_as_init(v);
106 
107  trace::exiting("accu::impl::image::generic::take_as_init");
108  }
109 
110  template <typename I, typename J>
111  void
112  take_as_init(Image<I>& input_, const Image<J>& values_)
113  {
114  trace::entering("accu::impl::image::generic::take_as_init");
115 
116  typedef mln_value(I) A;
117  mlc_is_a(A, Accumulator)::check();
118  mlc_converts_to(mln_value(J), mln_argument(A))::check();
119 
120  I& input = exact(input_);
121  const J& values = exact(values_);
122 
123  internal::take_as_init_tests(input, values);
124 
125  mln_piter(J) p(values.domain());
126  for_all(p)
127  input(p).take_as_init(values(p));
128 
129  trace::exiting("accu::impl::image::generic::take_as_init");
130  }
131 
132  } // end of namespace mln::accu::image::impl::generic
133 
134 
135  // Fastest versions.
136 
137  template <typename I>
138  void
139  take_as_init_fastest(Image<I>& input_, const mln_deduce(I, value, argument)& v)
140  {
141  trace::entering("accu::impl::image::take_as_init_fastest");
142 
143  mlc_is_a(mln_value(I), Accumulator)::check();
144 
145  I& input = exact(input_);
146  mln_precondition(input.is_valid());
147 
148  mln_pixter(I) px(input);
149  for_all(px)
150  px.val().take_as_init(v);
151 
152  trace::exiting("accu::impl::image::take_as_init_fastest");
153  }
154 
155  template <typename I, typename J>
156  void
157  take_as_init_fastest(Image<I>& input_, const Image<J>& values_)
158  {
159  trace::entering("accu::impl::image::take_as_init_fastest");
160 
161  typedef mln_value(I) A;
162  mlc_is_a(A, Accumulator)::check();
163  mlc_converts_to(mln_value(J), mln_argument(A))::check();
164 
165  I& input = exact(input_);
166  const J& values = exact(values_);
167 
168  internal::take_as_init_tests(input, values);
169  // Extra test:
170  mln_precondition(values.domain() == input.domain());
171 
172  border::resize_equal(input, values);
173 
174  mln_pixter(I) p_in(input);
175  mln_pixter(const J) p_v(values);
176  for_all_2(p_in, p_v)
177  p_in.val().take_as_init(p_v.val());
178 
179  trace::exiting("accu::impl::image::take_as_init_fastest");
180  }
181 
182  } // end of namespace mln::accu::image::impl
183 
184 
185 
186  // Dispatch.
187 
188  namespace internal
189  {
190 
191  // With a single value.
192 
193  template <typename I, typename V>
194  inline
195  void
196  take_as_init_dispatch(trait::image::speed::any,
197  Image<I>& input, const V& v)
198  {
199  impl::generic::take_as_init(input, v);
200  }
201 
202  template <typename I, typename V>
203  inline
204  void
205  take_as_init_dispatch(trait::image::speed::fastest,
206  Image<I>& input, const V& v)
207  {
208  impl::take_as_init_fastest(input, v);
209  }
210 
211  template <typename I, typename V>
212  inline
213  void
214  take_as_init_dispatch(Image<I>& input, const V& v)
215  {
216  take_as_init_dispatch(mln_trait_image_speed(I)(),
217  input, v);
218  }
219 
220  // With an image of values.
221 
222  template <typename I, typename J>
223  inline
224  void
225  take_as_init_dispatch(trait::image::speed::any,
226  trait::image::speed::any,
227  Image<I>& input, const Image<J>& values)
228  {
229  impl::generic::take_as_init(input, values);
230  }
231 
232  template <typename I, typename J>
233  inline
234  void
235  take_as_init_dispatch(trait::image::speed::fastest,
236  trait::image::speed::fastest,
237  Image<I>& input, const Image<J>& values)
238  {
239  if (exact(values).domain() == exact(input).domain())
240  impl::take_as_init_fastest(input, values);
241  else
242  impl::generic::take_as_init(input, values);
243  }
244 
245  template <typename I, typename J>
246  inline
247  void
248  take_as_init_dispatch(Image<I>& input,
249  const Image<J>& values)
250  {
251  take_as_init_dispatch(mln_trait_image_speed(I)(),
252  mln_trait_image_speed(J)(),
253  input, values);
254  }
255 
256  } // end of namespace mln::accu::image::internal
257 
258 
259  // Facades.
260 
261  template <typename I>
262  inline
263  void
264  take_as_init(Image<I>& input, const mln_deduce(I, value, argument)& v)
265  {
266  trace::entering("accu::image::take_as_init");
267 
268  mlc_is_a(mln_value(I), Accumulator)::check();
269 
270  mln_precondition(exact(input).is_valid());
271  internal::take_as_init_dispatch(input, v);
272 
273  trace::exiting("accu::image::take_as_init");
274  }
275 
276  template <typename I, typename J>
277  inline
278  void
279  take_as_init(Image<I>& input, const Image<J>& values)
280  {
281  trace::entering("accu::image::take_as_init");
282 
283  typedef mln_value(I) A;
284  mlc_is_a(A, Accumulator)::check();
285  mlc_converts_to(mln_value(J), mln_argument(A))::check();
286 
287  internal::take_as_init_tests(input, values);
288  internal::take_as_init_dispatch(input, values);
289 
290  trace::exiting("accu::image::take_as_init");
291  }
292 
293 # endif // ! MLN_INCLUDE_ONLY
294 
295  } // end of namespace mln::accu::image
296 
297  } // end of namespace mln::accu
298 
299 } // end of namespace mln
300 
301 
302 #endif // ! MLN_ACCU_IMAGE_TAKE_AS_INIT_HH