27 #ifndef MLN_CORE_IMAGE_IMORPH_LAZY_IMAGE_HH
28 # define MLN_CORE_IMAGE_IMORPH_LAZY_IMAGE_HH
37 # include <mln/core/internal/image_identity.hh>
38 # include <mln/core/alias/box2d.hh>
45 template <
typename I,
typename F,
typename B>
struct lazy_image;
53 template <
typename I,
typename F,
typename B>
54 struct data< lazy_image<I,F,B> >
56 data(
const F& fun_,
const B&
box);
58 mutable mln_ch_value(I,mln_result(F)) ima_;
59 mutable mln_ch_value(I,
bool) is_known;
71 template <
typename I,
typename F,
typename B>
72 struct image_< lazy_image<I,F,B> > : default_image_morpher< I, mln_value(I),
75 typedef trait::image::category::domain_morpher category;
76 typedef trait::image::value_io::read_only value_io;
94 template <
typename I,
typename F,
typename B>
97 mln_domain(I), lazy_image<I, F,B> >
123 void init_(const F& fun, const B& box);
127 const box<mln_psite(I)>& domain() const;
130 bool has(const mln_psite(I)&) const;
133 mln_result(F) operator()(const typename F::input& x) const;
136 mln_result(F) operator()(const typename F::input& x);
139 rvalue operator()(const mln_psite(I)&
p) const;
142 lvalue operator()(const mln_psite(I)&
p);
148 # ifndef MLN_INCLUDE_ONLY
155 template <
typename I,
typename F,
typename B>
158 : ima_(box), is_known(box), fun(fun), bb_(box)
164 template <
typename I,
typename F,
typename B>
168 this->data_ =
new internal::data< lazy_image<I,F,B> >(fun,
box);
171 template <
typename I,
typename F,
typename B>
175 this->data_ =
new internal::data< lazy_image<I,F,B> >(fun,
box);
178 template <
typename I,
typename F,
typename B>
180 bool lazy_image<I,F,B>::has(
const mln_psite(I)& p)
const
182 return this->data_->ima_.has(p);
185 template <
typename I,
typename F,
typename B>
188 lazy_image<I,F,B>::operator()(const typename F::input& p)
const
190 mln_assertion(this->
has(p));
191 if (this->data_->is_known(p))
192 return this->data_->ima_(p);
193 this->data_->ima_(p) = this->data_->fun(p);
194 this->data_->is_known(p) =
true;
195 return this->data_->ima_(p);
199 template <
typename I,
typename F,
typename B>
202 lazy_image<I,F,B>::operator()(const typename F::input& p)
204 mln_assertion(this->
has(p));
205 if (this->data_->is_known(p))
206 return this->data_->ima_(p);
207 this->data_->ima_(p) = this->data_->fun(p);
208 this->data_->is_known(p) =
true;
209 return this->data_->ima_(p);
212 template <
typename I,
typename F,
typename B>
214 typename lazy_image<I,F,B>::rvalue
215 lazy_image<I,F,B>::operator()(
const mln_psite(I)& p)
const
217 return (*this).operator()(convert::to< typename F::input >(
p));
220 template <
typename I,
typename F,
typename B>
222 typename lazy_image<I,F,B>::lvalue
223 lazy_image<I,F,B>::operator()(
const mln_psite(I)& p)
225 return (*this).operator()(convert::to< typename F::input >(
p));
228 template <
typename I,
typename F,
typename B>
230 const box<mln_psite(I)>&
231 lazy_image<I,F,B>::domain()
const
233 return this->data_->bb_;
236 # endif // ! MLN_INCLUDE_ONLY
241 #endif // ! MLN_CORE_IMAGE_IMORPH_LAZY_IMAGE_HH