
https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Nicolas Ballas <ballas@lrde.epita.fr> Sandbox: Site/Psite refactorization: finalize the example. * sandbox/ballas/refactorization/test/test.cc: Remove this tes. * sandbox/ballas/refactorization/test/rle.cc, * sandbox/ballas/refactorization/test/image2d.cc: update tests. * sandbox/ballas/refactorization/image2d.hh: update values_. * sandbox/ballas/refactorization/concept.hh: u. * sandbox/ballas/refactorization/internal/impl.hh, * sandbox/ballas/refactorization/internal/force_exact.hh, * sandbox/ballas/refactorization/internal/piter_base.hh, * sandbox/ballas/refactorization/internal/psite_base.hh, * sandbox/ballas/refactorization/point2d_impl.hh: Impl recovering. concept.hh | 2 - image2d.hh | 23 ++++++++++---- internal/force_exact.hh | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ internal/impl.hh | 2 - internal/piter_base.hh | 2 - internal/psite_base.hh | 2 - point2d_impl.hh | 11 +++--- test/image2d.cc | 11 ++++++ test/rle.cc | 43 +++++++++++++++++++------- 9 files changed, 146 insertions(+), 27 deletions(-) Index: sandbox/ballas/refactorization/test/rle.cc --- sandbox/ballas/refactorization/test/rle.cc (revision 1784) +++ sandbox/ballas/refactorization/test/rle.cc (working copy) @@ -5,6 +5,7 @@ //# include <p_run.hh> //# include <rle_psite.hh> # include <rle_image.hh> +# include <image2d.hh> int main() @@ -13,20 +14,15 @@ typedef point2d<int> point; - point p1(5, 5), p2(12, 2), p3(7, 6); + point p1(5, 5), p3(12, 6), p2(7, 6); + std::cout << "p1: " << p1 << std::endl; + std::cout << "p2: " << p2 << std::endl; + std::cout << "p3: " << p3 << std::endl; + p_run<point> prun1(p1, 5); p_run<point> prun2(p2, 12); p_run<point> prun3(p3, 4); - rle_pset<point> pset; -// pset.insert(prun1); -// pset.insert(prun2); -// pset.insert(prun3); - -// rle_pset<point>::piter p(pset); -// for (p.start(); p.is_valid(); p.next()) -// std::cout << (point) p << std::endl; - rle_image<point, int> ima; ima.insert(prun1, 1); @@ -35,7 +31,32 @@ rle_image<point, int>::piter p(ima.domain()); for (p.start(); p.is_valid(); p.next()) - std::cout << ima(p) << std::endl; + { + std::cout << "coord: " << p[0] << "," + << p[1] << std::endl; + std::cout << "ima: "<< ima(p) << std::endl; + } + + + /// Interoperability + std::cout << "------------------------------------------------" << std::endl; + int i = 0; + point2d<int> p4(12, 17); + box2d<int> bb(p1, p4); + image2d<int> ima2d(bb); + image2d<int>::fwd_piter piter2(ima2d.domain()); + for (piter2.start(); piter2.is_valid(); piter2.next()) + { + ++i; + ima2d(piter2) = i; + } + for (p.start(); p.is_valid(); p.next()) + { + std::cout << "coord: " << p[0] << "," + << p[1] << std::endl; + std::cout << "ima: "<< ima(p) << std::endl; + std::cout << "ima2d: "<< ima2d(p) << std::endl; + } } Index: sandbox/ballas/refactorization/test/image2d.cc --- sandbox/ballas/refactorization/test/image2d.cc (revision 1784) +++ sandbox/ballas/refactorization/test/image2d.cc (working copy) @@ -5,13 +5,22 @@ { using namespace mln; - point2d<int> pmin(0, 0), pmax(5, 5); + point2d<int> pmin(0, 0), pmax(0, 5); box2d<int> b(pmin, pmax); image2d<int> ima(b); ima(pmin) = 5; + int i = 0; image2d<int>::fwd_piter p(ima.domain()); for (p.start(); p.is_valid(); p.next()) + { + ++i; + ima(p) = i; + } + + + + for (p.start(); p.is_valid(); p.next()) std::cout << ima(p) << std::endl; } Index: sandbox/ballas/refactorization/image2d.hh --- sandbox/ballas/refactorization/image2d.hh (revision 1784) +++ sandbox/ballas/refactorization/image2d.hh (working copy) @@ -26,6 +26,7 @@ image2d(int nrows, int ncols); image2d(const box2d<int>& b); + ~image2d(); ///typedef mln::value::set<T> vset; // const vset& values() const; @@ -39,7 +40,7 @@ private: - T value_; + T* values_; box2d<int> b_; bool is_ready_; }; @@ -51,6 +52,8 @@ b_(nrows, ncols), is_ready_(false) { + values_ = new T[nrows * ncols]; + is_ready_ = true; } template <typename T> @@ -58,6 +61,15 @@ b_(b), is_ready_(false) { + values_ = new T[(b.pmax()[0] - b.pmin()[0] + 1) * + (b.pmax()[1] - b.pmin()[1] + 1)]; + is_ready_ = true; + } + + template <typename T> + image2d<T>::~image2d() + { + delete[] values_; } template <typename T> @@ -69,17 +81,16 @@ template <typename T> const T& - image2d<T>::operator()(const point2d<int>&) const + image2d<T>::operator()(const point2d<int>& p) const { - return value_; + return values_[p[0] * b_.pmax()[1] + p[1]]; } template <typename T> T& - image2d<T>::operator()(const point2d<int>&) + image2d<T>::operator()(const point2d<int>& p) { - is_ready_ = true; - return value_; + return values_[p[0] * b_.pmax()[1] + p[1]]; } template <typename T> Index: sandbox/ballas/refactorization/concept.hh --- sandbox/ballas/refactorization/concept.hh (revision 1784) +++ sandbox/ballas/refactorization/concept.hh (working copy) @@ -67,7 +67,7 @@ }; template <typename E> - struct Dpsite : public Object<E> + struct Dpsite : public Dsite<E> { /* typedef dpsite; Index: sandbox/ballas/refactorization/internal/impl.hh --- sandbox/ballas/refactorization/internal/impl.hh (revision 1784) +++ sandbox/ballas/refactorization/internal/impl.hh (working copy) @@ -4,7 +4,7 @@ namespace mln { - template <typename S> + template <typename E, typename S> struct impl { /// empty implementation Index: sandbox/ballas/refactorization/internal/force_exact.hh --- sandbox/ballas/refactorization/internal/force_exact.hh (revision 0) +++ sandbox/ballas/refactorization/internal/force_exact.hh (revision 0) @@ -0,0 +1,77 @@ +// Copyright (C) 2007 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_CORE_INTERNAL_FORCE_EXACT_HH +# define MLN_CORE_INTERNAL_FORCE_EXACT_HH + +/*! \file mln/core/internal/force_exact.hh + * + * \brief Definition of a violent cast for internal use only. + * + */ + + + +namespace mln +{ + + namespace internal + { + + /*! \internal Violent cast. + * This cast is an alternative to the mln::exact cast. + * It is used for objects that do not derive from + * mln::Object. + * Warning Do not to use this cast! + * see mln::exact + */ + template <typename E, typename T> + E& force_exact(const T& ref) + { + /* + static const E exact_obj; + static const Type& exact_obj_ref = exact_obj; + static const int exact_offset = + (const char*)(void*)(&exact_obj_ref) + - (const char*)(void*)(&exact_obj); + return *(E*)((char*)(this_) - exact_offset); + */ + static const E* exact_obj; + static const T& exact_obj_ref = *exact_obj; + static const int exact_offset = + (const char*)(void*)(&exact_obj_ref) + - (const char*)(void*)( exact_obj); + + return *(E*)((char*)(&ref) - exact_offset); + } + + } // end of namespace mln::internal + +} // end of namespace mln + + +#endif // ! MLN_CORE_INTERNAL_FORCE_EXACT_HH Index: sandbox/ballas/refactorization/internal/piter_base.hh --- sandbox/ballas/refactorization/internal/piter_base.hh (revision 1784) +++ sandbox/ballas/refactorization/internal/piter_base.hh (working copy) @@ -34,7 +34,7 @@ /// Piter base template <typename E, typename Site, typename Psite> struct piter_base_ : - public impl<Site>, + public impl<E, Site>, public piter_base_site_cast<E, Site, typename mlc_equal(Site, Psite)::eval> { Index: sandbox/ballas/refactorization/internal/psite_base.hh --- sandbox/ballas/refactorization/internal/psite_base.hh (revision 1784) +++ sandbox/ballas/refactorization/internal/psite_base.hh (working copy) @@ -11,7 +11,7 @@ template <typename E, typename P> struct psite_base_ : public Psite<E>, - public impl<P> + public impl<E, P> { operator E() const; Index: sandbox/ballas/refactorization/point2d_impl.hh --- sandbox/ballas/refactorization/point2d_impl.hh (revision 1784) +++ sandbox/ballas/refactorization/point2d_impl.hh (working copy) @@ -2,6 +2,7 @@ # define POINT2D_IMPL_HH_ # include <internal/impl.hh> +# include <internal/force_exact.hh> namespace mln @@ -11,8 +12,8 @@ template <typename C> struct point2d; - template <typename C> - struct impl< point2d<C> > + template <typename E, typename C> + struct impl< E, point2d<C> > { C operator[](unsigned i) const; @@ -26,11 +27,11 @@ # ifndef MLN_INCLUDE_ONLY - template <typename C> + template <typename E, typename C> C - impl< point2d<C> >::operator[] (unsigned i) const + impl< E, point2d<C> >::operator[] (unsigned i) const { - return this->to_site()[i]; + return internal::force_exact<E>(*this).to_site()[i]; } # endif // ! MLN_INCLUDE_ONLY