Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
predicate.hh
1 // Copyright (C) 2007, 2008, 2009 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_TEST_PREDICATE_HH
28 # define MLN_TEST_PREDICATE_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/concept/function.hh>
36 # include <mln/core/concept/site_set.hh>
37 
38 
39 namespace mln
40 {
41 
42  namespace test
43  {
44 
50  //
51  template <typename I, typename F>
52  bool predicate(const Image<I>& ima, const Function_v2b<F>& f);
53 
54 
61  //
62  template <typename I, typename J, typename F>
63  bool predicate(const Image<I>& lhs, const Image<J>& rhs, const Function_vv2b<F>& f);
64 
65 
70  //
71  template <typename S, typename F>
72  bool predicate(const Site_Set<S>& pset, const Function_v2b<F>& f);
73 
74 
75 # ifndef MLN_INCLUDE_ONLY
76 
77 
78  // Tests.
79 
80  namespace internal
81  {
82 
83  template <typename I, typename F>
84  inline
85  void predicate_tests(const Image<I>& ima,
86  const Function_v2b<F>& f)
87  {
88  mln_precondition(exact(ima).is_valid());
89  (void) ima;
90  (void) f;
91  }
92 
93  template <typename I, typename J, typename F>
94  inline
95  void predicate_tests(const Image<I>& lhs_,
96  const Image<J>& rhs_,
97  const Function_vv2b<F>& f)
98  {
99  const I& lhs = exact(lhs_);
100  const J& rhs = exact(rhs_);
101 
102  mln_precondition(lhs.is_valid());
103  mln_precondition(rhs.is_valid());
104  mln_precondition(lhs.domain() == rhs.domain());
105  (void) lhs;
106  (void) rhs;
107  (void) f;
108  }
109 
110  template <typename S, typename F>
111  inline
112  void predicate_tests(const Site_Set<S>& pset,
113  const Function_v2b<F>& f)
114  {
115  mln_precondition(exact(pset).is_valid());
116  (void) pset;
117  (void) f;
118  }
119 
120  } // end of namespace mln::test::internal
121 
122 
123  // Implementations.
124 
125  namespace impl
126  {
127 
128  template <typename I, typename F>
129  inline
130  bool predicate_(trait::image::speed::any, const I& ima, const F& f)
131  {
132  internal::predicate_tests(ima, f);
133 
134  mln_piter(I) p(ima.domain());
135  for_all(p)
136  if (! f(ima(p)))
137  return false;
138  return true;
139  }
140 
141  template <typename I, typename F>
142  inline
143  bool predicate_(trait::image::speed::fastest, const I& ima, const F& f)
144  {
145  internal::predicate_tests(ima, f);
146 
147  mln_pixter(const I) pxl(ima);
148  for_all(pxl)
149  if (! f(pxl.val()))
150  return false;
151  return true;
152  }
153 
154  template <typename I, typename J, typename F>
155  inline
156  bool predicate_(trait::image::speed::any,
157  trait::image::speed::any,
158  const I& lhs, const J& rhs, const F& f)
159  {
160  internal::predicate_tests(lhs, rhs, f);
161 
162  mln_piter(I) p(lhs.domain());
163  for_all(p)
164  if (! f(lhs(p), rhs(p)))
165  return false;
166  return true;
167  }
168 
169  template <typename I, typename J, typename F>
170  inline
171  bool predicate_(trait::image::speed::fastest,
172  trait::image::speed::fastest,
173  const I& lhs, const J& rhs, const F& f)
174  {
175  internal::predicate_tests(lhs, rhs, f);
176 
177  mln_pixter(const I) pxl1(lhs);
178  mln_pixter(const J) pxl2(rhs);
179  for_all_2(pxl1, pxl2)
180  if (! f(pxl1.val(), pxl2.val()))
181  return false;
182  return true;
183  }
184 
185  template <typename S, typename F>
186  inline
187  bool predicate_(const Site_Set<S>& pset, const F& f)
188  {
189  internal::predicate_tests(pset, f);
190 
191  mln_piter(S) p(exact(pset));
192  for_all(p)
193  if (! f(p))
194  return false;
195  return true;
196  }
197 
198  } // end of namespace mln::test::impl
199 
200 
201 
202  // Facades.
203 
204 
205  template <typename I, typename F>
206  inline
207  bool predicate(const Image<I>& ima, const Function_v2b<F>& f)
208  {
209  trace::entering("test::predicate");
210 
211  internal::predicate_tests(ima, f);
212  bool res = impl::predicate_(mln_trait_image_speed(I)(), exact(ima),
213  exact(f));
214 
215  trace::exiting("test::predicate");
216  return res;
217  }
218 
219 
220  template <typename I, typename J, typename F>
221  inline
222  bool predicate(const Image<I>& lhs_, const Image<J>& rhs_, const Function_vv2b<F>& f)
223  {
224  trace::entering("test::predicate");
225 
226  const I& lhs = exact(lhs_);
227  const J& rhs = exact(rhs_);
228 
229  internal::predicate_tests(lhs_, rhs_, f);
230 
231  bool res = impl::predicate_(mln_trait_image_speed(I)(),
232  mln_trait_image_speed(J)(),
233  lhs, rhs,
234  exact(f));
235 
236  trace::exiting("test::predicate");
237  return res;
238  }
239 
240  template <typename S, typename F>
241  inline
242  bool predicate(const Site_Set<S>& pset, const Function_v2b<F>& f)
243  {
244  trace::entering("test::predicate");
245 
246  internal::predicate_tests(pset, f);
247 
248  bool res = impl::predicate_(exact(pset), exact(f));
249 
250  trace::exiting("test::predicate");
251  return res;
252  }
253 
254 # endif // ! MLN_INCLUDE_ONLY
255 
256  } // end of namespace mln::test
257 
258 } // end of namespace mln
259 
260 
261 #endif // ! MLN_TEST_PREDICATE_HH