cleanup-2008 2016: Make windows work with index; test on p_array.

https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> Make windows work with index; test on p_array. * doc/tutorial/examples/p_array.2.cc: New. * mln/core/p_array.hh (has): New overload for index. (operator[]): Likewise. * mln/core/concept/proxy.hh (get_adr): New overload to handle identity. * mln/core/concept/pseudo_site.hh (change_target): Upgrade. Now it can handle non-Objects. * mln/core/concept/site_proxy.hh: Likewise. * mln/util/index.hh: New. doc/tutorial/examples/p_array.2.cc | 81 ++++++++++++++++++++++++++++++ mln/core/concept/proxy.hh | 6 ++ mln/core/concept/pseudo_site.hh | 57 ++++++++++++++++++++- mln/core/concept/site_proxy.hh | 51 ++++++++++++++----- mln/core/p_array.hh | 17 ++++++ mln/util/index.hh | 99 +++++++++++++++++++++++++++++++++++++ 6 files changed, 297 insertions(+), 14 deletions(-) Index: doc/tutorial/examples/p_array.2.cc --- doc/tutorial/examples/p_array.2.cc (revision 0) +++ doc/tutorial/examples/p_array.2.cc (revision 0) @@ -0,0 +1,81 @@ +# include <mln/core/image2d.hh> +# include <mln/core/p_array.hh> +# include <mln/core/window.hh> + +# include <mln/debug/println.hh> +# include <mln/level/fill.hh> + + +template <typename A> +mln::image2d<char> picture(const A& arr) +{ + using namespace mln; + + image2d<char> ima(5, 5); + level::fill(ima, '-'); + + unsigned i = 0; + mln_piter(A) p(arr); + for_all(p) + ima(p) = '0' + i++; + + debug::println(ima); + return ima; +} + + +template <typename I, typename W, typename P> +void picture(const I& ima, const W& win, const P& p) +{ + std::cout << ima(p) << ": "; + mln_qiter(W) q(win, p); + for_all(q) + if (ima.has(q)) + std::cout << ima(q) << ' '; + else + std::cout << "- "; + std::cout << std::endl; +} + + +int main() +{ + using namespace mln; + + typedef p_array<point2d> Arr; + Arr arr; + + point2d p(1,1); + arr.append(p); + dpoint2d dp[] = { right, right, down, down, left, left, up }; + for (unsigned i = 0; i < 7; ++i) + p += dp[i], arr.append(p); + + std::cout << "arr = " << arr << std::endl; + image2d<char> ima = picture(arr); + + typedef window<util::dindex> W; + W win; + win.insert(-1).insert(0).insert(1); + std::cout << "win = " << win << std::endl + << std::endl; + + util::index i; + mln_qiter_(W) j(win, i); + for (i = 0; i < arr.nsites(); i = i + 1) + { + std::cout << "i=" << i << ": "; + for_all(j) + if (arr.has(j)) + { + mln_invariant( ima(arr[j]) == '0' + j.to_site()); + std::cout << j << ' '; + } + else + std::cout << "- "; + std::cout << std::endl; + } + + // FIXME: j does NOT convert to int because index is NOT a proxy + // NOTA: make index be a proxy, so equip it with every op it needs. +} Index: mln/core/p_array.hh --- mln/core/p_array.hh (revision 2015) +++ mln/core/p_array.hh (working copy) @@ -38,6 +38,7 @@ # include <mln/core/internal/site_set_base.hh> # include <mln/core/internal/pseudo_site_base.hh> # include <mln/accu/bbox.hh> +# include <mln/util/index.hh> namespace mln @@ -94,6 +95,9 @@ bool is_valid() const; + operator util::index() const { return i_; } + + private: const p_array<P>* arr_; @@ -162,6 +166,19 @@ /// Test is \p p belongs to this site set. bool has(const psite& p) const; + /// Test is \p i belongs to this site set. + bool has(const util::index& i) const + { + return i >= 0 && i < int(vect_.size()); + } + + /// Give the i-th element. + const P& operator[](const util::index& i) const + { + mln_precondition(has(i)); + return vect_[i]; + } + /// Test is index \p i belongs to this site set. // FIXME: Add an overload "has(index)". bool has_index(int i) const; Index: mln/core/concept/proxy.hh --- mln/core/concept/proxy.hh (revision 2015) +++ mln/core/concept/proxy.hh (working copy) @@ -148,6 +148,12 @@ ptr = & exact(obj); } + template <typename T> + void get_adr( T *& ptr, T& obj) + { + ptr = & obj; + } + // Case 3: Fail to found! template <typename T, typename O> Index: mln/core/concept/pseudo_site.hh --- mln/core/concept/pseudo_site.hh (revision 2015) +++ mln/core/concept/pseudo_site.hh (working copy) @@ -99,17 +99,70 @@ namespace if_possible { + namespace internal + { + + template <bool b> struct helper; + + template <> + struct helper< /* is an Object */ true > + { + template <typename P> - void change_target(Pseudo_Site<P>& p, const typename P::target_t& new_target) + void change_target(Pseudo_Site<P>& p, const typename P::target_t& new_target) const { exact(p).target() = & new_target; } template <typename O, typename D> - void change_target(Object<O>&, const D&) + void change_target(Object<O>&, const D&) const + { + // No-op. + } + + }; + + template <> + struct helper< /* NOT an Object */ false > + { + template <typename O, typename D> + void change_target(O&, const D&) const { // No-op. } + }; + + } // namespace mln::if_possible::internal + + + + // FIXME: Was: + +// template <typename P> +// void change_target(Pseudo_Site<P>& p, const typename P::target_t& new_target) +// { +// exact(p).target() = & new_target; +// } + +// template <typename O, typename D> +// void change_target(Object<O>&, const D&) +// { +// // No-op. +// } + +// template <typename P> +// void change_target(P& p, const typename P::target_t& new_target) +// { +// exact(p).target() = & new_target; +// } + + + template <typename O, typename D> + void change_target(O& o, const D& d) + { + enum { is_object = mlc_is_a(O, Object)::value }; + mln::if_possible::internal::helper< is_object >().change_target(o, d); + } } // end of namespace mln::if_possible Index: mln/core/concept/site_proxy.hh --- mln/core/concept/site_proxy.hh (revision 2015) +++ mln/core/concept/site_proxy.hh (working copy) @@ -109,9 +109,9 @@ // Access to site reference. - template <typename P> - const typename site_from<P>::ret& - to_site(const Object<P>& p); + template <typename O> + const typename site_from<O>::ret& + to_site(const O& obj); } // end of namespace internal @@ -174,27 +174,54 @@ namespace deep { + template <bool b> struct to_site_; + + template <> + struct to_site_< /* is proxy = */ true > + { template <typename P> const typename site_from<P>::ret& - to_site(const Site_Proxy<P>& p) + doit(const Site_Proxy<P>& p) const { return exact(p).to_site(); } + }; - template <typename P> - const typename site_from<P>::ret& - to_site(const Object<P>& p) + template <> + struct to_site_< /* is proxy = */ false > + { + template <typename O> + const O& + doit(const O& obj) const { - return exact(p); + return obj; } + }; + + // FIXME: was: + +// template <typename P> +// const typename site_from<P>::ret& +// to_site(const Site_Proxy<P>& p) +// { +// return exact(p).to_site(); +// } + +// template <typename P> +// const typename site_from<P>::ret& +// to_site(const Object<P>& p) +// { +// return exact(p); +// } } // end of namespace internal::deep - template <typename P> - const typename site_from<P>::ret& - to_site(const Object<P>& p) + template <typename O> + const typename site_from<O>::ret& + to_site(const O& obj) { - return deep::to_site(exact(p)); + enum { is_proxy = mlc_is_a(O, Site_Proxy)::value }; + return deep::to_site_< is_proxy >().doit(obj); } } // end of namespace mln::internal Index: mln/util/index.hh --- mln/util/index.hh (revision 0) +++ mln/util/index.hh (revision 0) @@ -0,0 +1,99 @@ +// Copyright (C) 2008 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef MLN_UTIL_INDEX_HH +# define MLN_UTIL_INDEX_HH + +/*! \file mln/util/index.hh + * + * \brief Definition of an "index" type. + */ + +# include <mln/core/concept/object.hh> + + +namespace mln +{ + + namespace util + { + + // Fwd decl. + template <typename Tag> struct dindex_; + + + /*! \brief Index structure. + * + */ + template <typename Tag = void> + struct index_ // : public Object< index_<Tag> > + { + typedef dindex_<Tag> dpsite; + typedef int coord; + + int i_; + + index_() {} + index_(int i) : i_(i) {} + + operator int() const { return i_; } + }; + + typedef index_<void> index; + + + template <typename Tag = void> + struct dindex_ // : public Object< dindex_<Tag> > + { + typedef index_<Tag> psite; + typedef index_<Tag> site; + typedef int coord; + + int i_; + + dindex_() {} + dindex_(int i) : i_(i) {} + + operator int() const { return i_; } + }; + + typedef dindex_<void> dindex; + + + +# ifndef MLN_INCLUDE_ONLY + + // FIXME + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::util + +} // end of namespace mln + + +#endif // ! MLN_UTIL_INDEX_HH
participants (1)
-
Thierry Geraud