
https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> Add a tutorial example of use of p_array. * tests/core/p_array.cc: Clean up (no more stdout). * doc/tutorial/examples: New directory. * doc/tutorial/examples/p_array.cc: New. * mln/core/internal/image_base.hh (mesh, bbox): Remove. (point, npoints): Rename as... (site, nsites): ...these. * mln/core/concept/image.hh: Likewise. * mln/core/p_array_piter.hh (index): New method. (index_of_in): New overload. * mln/core/concept/proxy.hh (get_adr): Make it work when no match is found. (operator Subject): Change into a more reliable version. * mln/core/p_array.hh (index_of_in): New procedures. (insert, change): New methods. (print): Remove. * mln/core/image2d.hh (bbox): New. (npoints): Rename as... (nsites): ...this. doc/tutorial/examples/p_array.cc | 68 +++++++++++++++++++++++++++++++++++++++ mln/core/concept/image.hh | 8 ---- mln/core/concept/proxy.hh | 17 ++++++--- mln/core/image2d.hh | 14 +++++++- mln/core/internal/image_base.hh | 33 +++++------------- mln/core/p_array.hh | 55 +++++++++++++++++++++++++------ mln/core/p_array_piter.hh | 19 ++++++++++ tests/core/p_array.cc | 36 +++++--------------- 8 files changed, 177 insertions(+), 73 deletions(-) Index: tests/core/p_array.cc --- tests/core/p_array.cc (revision 1980) +++ tests/core/p_array.cc (working copy) @@ -30,44 +30,26 @@ * \brief Tests on mln::p_array. */ -#include <iterator> - #include <mln/core/point2d.hh> #include <mln/core/p_array.hh> - int main() { using namespace mln; typedef p_array<point2d> Arr; - Arr pa; - pa - .append(make::point2d(6, 9)) - .append(make::point2d(4, 2)) - .append(make::point2d(4, 2)) - .append(make::point2d(5, 1)); - mln_assertion(pa.nsites() == 4); - - mlc_equal( mln_psite_(Arr)::site, point2d )::check(); - - mln_psite_(Arr) p(pa, 0); - std::cout << p.to_site() << ' ' - << p.row() << ' ' - << point2d(p) << ' ' - << std::endl; + point2d x(5, 1); + Arr arr; + arr.append(x).append(x); + mln_assertion(arr.nsites() == 2); - std::copy(pa.vect().begin(), pa.vect().end(), - std::ostream_iterator<point2d>(std::cout, " ")); - std::cout << std::endl; + mlc_equal( mln_psite_(Arr)::site, point2d )::check(); - { - mln_piter_(Arr) p(pa); - for_all(p) - std::cout << p << ' '; - std::cout << std::endl; - } + mln_psite_(Arr) p(arr, 0); + mln_assertion(p.to_site() == x); + mln_assertion(p.row() == 5); + mln_assertion(point2d(p) == x); } Index: doc/tutorial/examples/p_array.cc --- doc/tutorial/examples/p_array.cc (revision 0) +++ doc/tutorial/examples/p_array.cc (revision 0) @@ -0,0 +1,68 @@ +# include <mln/core/image2d.hh> +# include <mln/core/p_array.hh> +# include <mln/debug/println.hh> +# include <mln/level/fill.hh> +# include <mln/value/int_u8.hh> + + +template <typename A> +void 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); +} + + + +int main() +{ + using namespace mln; + + typedef p_array<point2d> Arr1; + Arr1 arr1; + + image2d<value::int_u8> ima(5, 5); + + { + point2d p(1,1); + dpoint2d dp[] = { right, right, down, down, left, left, up }; + arr1.append(p); + for (unsigned i = 0; i < 7; ++i) + p += dp[i], arr1.append(p); + + std::cout << "arr1 = " << arr1 << std::endl; + picture(arr1); + } + + { + typedef p_array< mln_psite_(Arr1) > Arr2; + Arr2 arr2; + + mln_piter_(Arr1) p(arr1); + for_all(p) + if (p.row() % 2 && p.col() % 2) + arr2.append(p); + + std::cout << "arr2 = " << arr2 << std::endl; + picture(arr2); + + { + mln_piter_(Arr2) p(arr2); + for_all(p) + std::cout << "point " << p << ": #" + << index_of_in(p, arr2) << " in arr2, #" + << index_of_in(p, arr1) << " in arr1" << std::endl; + } + + } + +} Index: mln/core/internal/image_base.hh --- mln/core/internal/image_base.hh (revision 1980) +++ mln/core/internal/image_base.hh (working copy) @@ -100,27 +100,23 @@ /// Site_Set associated type. typedef S pset; - - /// Mesh associated type. - typedef mln_mesh(S) mesh; - /// Point_Site associated type. typedef mln_psite(S) psite; /// Point associated type. - typedef mln_point(S) point; + typedef mln_site(S) site; /// Dpoint associated type. - typedef mln_dpoint(point) dpoint; + typedef mln_dpoint(site) dpoint; /// Coordinate associated type. - typedef mln_coord(point) coord; + typedef mln_coord(site) coord; - /// Forward Point_Iterator associated type. + /// Forward Site_Iterator associated type. typedef mln_fwd_piter(S) fwd_piter; - /// Backward Point_Iterator associated type. + /// Backward Site_Iterator associated type. typedef mln_bkd_piter(S) bkd_piter; @@ -131,10 +127,10 @@ bool owns_(const psite& p) const; /// Give a bounding box of the image domain. - const box_<point>& bbox() const; + const box_<site>& bbox() const; - /// Give the number of points of the image domain. - std::size_t npoints() const; + /// Give the number of sites of the image domain. + std::size_t nsites() const; /// Test if this image has been initialized; default impl. bool has_data() const; @@ -221,20 +217,11 @@ template <typename S, typename E> inline - const box_<mln_point(S)>& - image_base_<S,E>::bbox() const - { - mln_precondition(exact(this)->has_data()); - return exact(this)->domain().bbox(); - } - - template <typename S, typename E> - inline std::size_t - image_base_<S,E>::npoints() const + image_base_<S,E>::nsites() const { mln_precondition(exact(this)->has_data()); - return exact(this)->domain().npoints(); + return exact(this)->domain().nsites(); } template <typename S, typename E> Index: mln/core/p_array_piter.hh --- mln/core/p_array_piter.hh (revision 1980) +++ mln/core/p_array_piter.hh (working copy) @@ -75,15 +75,25 @@ const p_array_psite<P>& unproxy() const; // As a Site_Proxy: + typedef typename super::site site; const site& to_site() const; + /// Return the current index. + int index() const; + protected: p_array_psite<P> p_; }; + template <typename P, typename A> + int index_of_in(const p_array_fwd_piter_<P>& p, const A& arr) + { + return index_of_in(p.unproxy(), arr); + } + // /// \brief Backward iterator on points of a p_array<P>. // template <typename P> @@ -222,6 +232,15 @@ return p_.to_site(); } + template <typename P> + inline + int + p_array_fwd_piter_<P>::index() const + { + mln_precondition(p_.target() != 0); + return p_.index(); + } + /*------------------------. | p_array_bkd_piter_<P>. | `------------------------*/ Index: mln/core/concept/proxy.hh --- mln/core/concept/proxy.hh (revision 1980) +++ mln/core/concept/proxy.hh (working copy) @@ -150,12 +150,15 @@ // Case 3: Fail to found! template <typename T, typename O> - void get_adr(const T *& ptr, const Object<O>& obj); + void get_adr(const T *& ptr, const Object<O>& obj) + { + ptr = 0; + } template <typename T, typename O> void get_adr( T *& ptr, Object<O>& obj) { - return 0; + ptr = 0; } @@ -183,9 +186,13 @@ { operator Subject() const { - const Subject* adr; - get_adr(adr, mln::internal::force_exact<const E>(*this)); - return *adr; + return mln::internal::force_exact<const E>(*this).unproxy(); + // The code above seems more effective than the one below: + // + // const Subject* adr; + // get_adr(adr, mln::internal::force_exact<const E>(*this)); + // mln_postcondition(adr != 0); + // return *adr; } }; Index: mln/core/concept/image.hh --- mln/core/concept/image.hh (revision 1980) +++ mln/core/concept/image.hh (working copy) @@ -106,8 +106,7 @@ typedef bkd_piter; bool has(const psite& p) const; - const box_<point>& bbox() const; - std::size_t npoints() const; + std::size_t nsites() const; bool has_data() const; */ @@ -137,14 +136,9 @@ bool (E::*m1)(const psite& p) const = & E::has; m1 = 0; - const box_<point>& (E::*m2)() const = & E::bbox; - m2 = 0; // to be provided in concrete image classes: - typedef mln_mesh(E) mesh; - metal::is_a<mesh, Mesh>::check(); // FIXME: Add other checks. - typedef mln_value(E) value; typedef mln_rvalue(E) rvalue; typedef mln_lvalue(E) lvalue; Index: mln/core/p_array.hh --- mln/core/p_array.hh (revision 1980) +++ mln/core/p_array.hh (working copy) @@ -85,8 +85,6 @@ void change_target(const p_array<P>& arr); - void print() const; - private: const p_array<P>* arr_; @@ -94,6 +92,11 @@ }; + template <typename P, typename A> + int index_of_in(const P&, const A&); + + template <typename P, typename A> + int index_of_in(const p_array_psite<P>& p, const A& arr); namespace trait @@ -149,6 +152,9 @@ // FIXME: Add an overload "has(index)". + /// Change site \p p into \p new_p. + void change(const psite& p, const P& new_p); + /// Give the number of sites. std::size_t nsites() const; @@ -158,6 +164,9 @@ /// Append an array \p other of points. p_array<P>& append(const p_array<P>& other); + /// Insert a point \p p (equivalent as 'append'). + void insert(const P& p); + /// Clear this set. void clear(); @@ -235,6 +244,14 @@ template <typename P> inline + void + p_array<P>::insert(const P& p) + { + vect_.push_back(p); + } + + template <typename P> + inline p_array<P>& p_array<P>::append(const p_array<P>& other) { @@ -277,6 +294,14 @@ return vect_[i]; } + template <typename P> + inline + void + p_array<P>::change(const psite& p, const P& new_p) + { + mln_precondition(has(p)); + vect_[p.index()] = new_p; + } // p_array_psite<P> @@ -340,14 +365,6 @@ template <typename P> inline - void - p_array_psite<P>::print() const - { - std::cout << i_ << "-th site of " << arr_ << " => site " << to_site() << std::endl; - } - - template <typename P> - inline const P& p_array_psite<P>::unproxy() const { @@ -355,6 +372,24 @@ return (*arr_)[i_]; } + + // Procedures + + template <typename P, typename A> + int index_of_in(const P&, const A&) + { + return -1; + } + + template <typename P, typename A> + int index_of_in(const p_array_psite<P>& p, const A& arr) + { + if ((void*)(p.target()) == (void*)(&arr)) + return p.index(); + else + return index_of_in(p.unproxy(), arr); + } + # endif // ! MLN_INCLUDE_ONLY } // end of namespace mln Index: mln/core/image2d.hh --- mln/core/image2d.hh (revision 1980) +++ mln/core/image2d.hh (working copy) @@ -168,6 +168,9 @@ /// Give the definition domain. const box2d& domain() const; + /// Give the bounding box domain. + const box2d& bbox() const; + /// Give the border thickness. unsigned border() const; @@ -391,6 +394,15 @@ template <typename T> inline + const box2d& + image2d<T>::bbox() const + { + mln_precondition(this->has_data()); + return this->data_->b_; + } + + template <typename T> + inline unsigned image2d<T>::border() const { @@ -404,7 +416,7 @@ image2d<T>::ncells() const { mln_precondition(this->has_data()); - return this->data_->vb_.npoints(); + return this->data_->vb_.nsites(); } template <typename T>