1782: sandbox: continuing the psite/site factorization example.

https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Nicolas Ballas <ballas@lrde.epita.fr> sandbox: continuing the psite/site factorization example. * sandbox/ballas/refactorization/test/box2d.cc, * sandbox/ballas/refactorization/test/image2d.cc, * sandbox/ballas/refactorization/test/test.cc: tests. * sandbox/ballas/refactorization/internal/impl.hh: New,base impl class. * sandbox/ballas/refactorization/box2d.hh, * sandbox/ballas/refactorization/point2d.hh, * sandbox/ballas/refactorization/point2d_impl.hh, * sandbox/ballas/refactorization/image2d.hh: New, add a light image2d. * sandbox/ballas/refactorization/concept.hh:, * sandbox/ballas/refactorization/internal/pset_base.hh:, * sandbox/ballas/refactorization/internal/image_base.hh:, * sandbox/ballas/refactorization/internal/piter_base.hh:, * sandbox/ballas/refactorization/internal/psite_base.hh: Update. box2d.hh | 246 +++++++++++++++++++++++++++++++++++++++++++++++++ concept.hh | 53 +++++++++- image2d.hh | 97 +++++++++++++++++++ internal/image_base.hh | 3 internal/impl.hh | 16 +++ internal/piter_base.hh | 16 +-- internal/pset_base.hh | 4 internal/psite_base.hh | 4 point2d.hh | 201 ++++++++++++++++++++++++++++++++++++++++ point2d_impl.hh | 40 +++++++ test/box2d.cc | 22 ++++ test/image2d.cc | 17 +++ test/test.cc | 3 13 files changed, 706 insertions(+), 16 deletions(-) Index: sandbox/ballas/refactorization/test/box2d.cc --- sandbox/ballas/refactorization/test/box2d.cc (revision 0) +++ sandbox/ballas/refactorization/test/box2d.cc (revision 0) @@ -0,0 +1,22 @@ +#include <box2d.hh> + + +int main() +{ + using namespace mln; + + point2d<int> pmin(0, 0), pmax(5, 5); + std::cout << pmin << std::endl; + std::cout << pmax << std::endl; + + box2d<int> b(pmin, pmax); + std::cout << b << std::endl; + + box2d<int>::fwd_piter p(b); + + + for (p.start(); p.is_valid(); p.next()) + { + std::cout << p.to_site() << std::endl; + } +} Index: sandbox/ballas/refactorization/test/test.cc --- sandbox/ballas/refactorization/test/test.cc (revision 1781) +++ sandbox/ballas/refactorization/test/test.cc (working copy) @@ -1,7 +1,6 @@ -#include <internal/image_base.hh> +#include <box2d.hh> int main() { - return 0; } Index: sandbox/ballas/refactorization/test/image2d.cc --- sandbox/ballas/refactorization/test/image2d.cc (revision 0) +++ sandbox/ballas/refactorization/test/image2d.cc (revision 0) @@ -0,0 +1,17 @@ +#include <image2d.hh> + + +int main() +{ + using namespace mln; + + point2d<int> pmin(0, 0), pmax(5, 5); + box2d<int> b(pmin, pmax); + image2d<int> ima(b); + + ima(pmin) = 5; + + image2d<int>::fwd_piter p(ima.domain()); + 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 0) +++ sandbox/ballas/refactorization/image2d.hh (revision 0) @@ -0,0 +1,97 @@ +#ifndef IMAGE2D_HH_ +# define IMAGE2D_HH_ + +# include <internal/image_base.hh> + +# include <box2d.hh> + +namespace mln +{ + + template <typename T> + class image2d : public internal::image_base_< box2d<int>, image2d<T> > + { + public: + + typedef box2d<int> pset; + typedef pset::fwd_piter fwd_piter; + typedef pset::bkd_piter bkd_piter; + + + + typedef T value; + typedef const T& rvalue; + typedef T& lvalue; + + + image2d(int nrows, int ncols); + image2d(const box2d<int>& b); + + ///typedef mln::value::set<T> vset; + // const vset& values() const; + bool is_ready() const; + + bool owns_(const point2d<int>& p) const; + const box2d<int>& domain() const; + + const T& operator()(const point2d<int>& p) const; + T& operator()(const point2d<int>& p); + + + private: + T value_; + box2d<int> b_; + bool is_ready_; + }; + +# ifndef MLN_INCLUDE_ONLY + + template <typename T> + image2d<T>::image2d(int nrows, int ncols) : + b_(nrows, ncols), + is_ready_(false) + { + } + + template <typename T> + image2d<T>::image2d(const box2d<int>& b) : + b_(b), + is_ready_(false) + { + } + + template <typename T> + bool + image2d<T>::is_ready() const + { + return is_ready_; + } + + template <typename T> + const T& + image2d<T>::operator()(const point2d<int>&) const + { + return value_; + } + + template <typename T> + T& + image2d<T>::operator()(const point2d<int>&) + { + is_ready_ = true; + return value_; + } + + template <typename T> + const box2d<int>& + image2d<T>::domain() const + { + return this->b_; + } + +# endif // ! MLN_INCLUDE_ONLY + +} + + +#endif // ! IMAGE2D_HH_ Index: sandbox/ballas/refactorization/concept.hh --- sandbox/ballas/refactorization/concept.hh (revision 1781) +++ sandbox/ballas/refactorization/concept.hh (working copy) @@ -24,18 +24,29 @@ /* typedef site typedef dsite; - - const psite& ref_(); */ protected: Site(); }; + template <typename E> + struct Dsite : public Object<E> + { + /* + typedef dsite; + + const dsite& to_dsite() const; + */ + protected: + Dsite(); + }; + + /// Psite Concept /// Do we need a concept for representing psite? template <typename E> - struct Psite : public Object<E> + struct Psite : public Site<E> { /* // Site must be different from psite, @@ -55,6 +66,18 @@ Psite(); }; + template <typename E> + struct Dpsite : public Object<E> + { + /* + typedef dpsite; + + const dpsite& to_dpsite() const; + */ + protected: + Dpsite(); + }; + /// Piter concept /// Psite iterator /// use template specializaion for psite == site or psite != psite @@ -108,6 +131,7 @@ typedef values; typedef rvalues; typedef lvalues; + //typedef vset; //const vset& destination() const; // const vset& values() const; @@ -120,6 +144,9 @@ bool owns_(const psite&) const; bool has(const psite&) const; + typedef pset; + const pset& domain() const; + typedef fwd_piter; typedef bkd_piter; */ @@ -146,6 +173,20 @@ /// FIXME } + template <typename E> + inline + Dsite<E>::Dsite() + { + /// FIXME + } + + template <typename E> + inline + Dpsite<E>::Dpsite() + { + /// FIXME + } + /// Psite template <typename E> inline @@ -168,6 +209,12 @@ } template <typename E> + Pset<E>::Pset() + { + /// FIXME + } + + template <typename E> Image<E>::Image() { /// FIXME Index: sandbox/ballas/refactorization/internal/impl.hh --- sandbox/ballas/refactorization/internal/impl.hh (revision 0) +++ sandbox/ballas/refactorization/internal/impl.hh (revision 0) @@ -0,0 +1,16 @@ +#ifndef IMPL_HH_ +# define IMPL_HH_ + +namespace mln +{ + + template <typename S> + struct impl + { + /// empty implementation + }; + +} + + +#endif // !IMPL_HH_ Index: sandbox/ballas/refactorization/internal/pset_base.hh --- sandbox/ballas/refactorization/internal/pset_base.hh (revision 1781) +++ sandbox/ballas/refactorization/internal/pset_base.hh (working copy) @@ -2,6 +2,8 @@ # define INTERNAL_PSET_BASE_HH_ +# include <concept.hh> + namespace mln { namespace internal @@ -11,7 +13,7 @@ /// FIXME: psite = site => Do we declare a site /// typedef in site_base class? template <typename P, typename E> - struct pset_base_ + struct pset_base_ : public Pset<E> { typedef typename P::site site; typedef P psite; Index: sandbox/ballas/refactorization/internal/image_base.hh --- sandbox/ballas/refactorization/internal/image_base.hh (revision 1781) +++ sandbox/ballas/refactorization/internal/image_base.hh (working copy) @@ -19,6 +19,8 @@ typedef typename S::fwd_piter fwd_piter; typedef typename S::bkd_piter bkd_piter; + bool has(const psite&) const; + protected: image_base_(); }; @@ -32,6 +34,7 @@ { } + # endif // ! MLN_INCLUDE_ONLY } } Index: sandbox/ballas/refactorization/internal/piter_base.hh --- sandbox/ballas/refactorization/internal/piter_base.hh (revision 1781) +++ sandbox/ballas/refactorization/internal/piter_base.hh (working copy) @@ -30,13 +30,13 @@ }; /// Piter base - template <typename E> + template <typename E, typename Site, typename Psite> struct piter_base_ : public piter_base_site_cast<E, - typename mlc_equal(typename E::site, - typename E::psite)::eval> + typename mlc_equal(Site, + Psite)::eval> { - operator typename E::psite() const; + operator Psite () const; protected: piter_base_(); @@ -52,15 +52,15 @@ } - template <typename E> - piter_base_<E>::operator typename E::psite () const + template <typename E, typename Site, typename Psite> + piter_base_<E, Site, Psite>::operator Psite () const { return exact(this)->to_site(); } - template <typename E> - piter_base_<E>::piter_base_() + template <typename E, typename Site, typename Psite> + piter_base_<E, Site, Psite>::piter_base_() { } Index: sandbox/ballas/refactorization/internal/psite_base.hh --- sandbox/ballas/refactorization/internal/psite_base.hh (revision 1781) +++ sandbox/ballas/refactorization/internal/psite_base.hh (working copy) @@ -10,8 +10,8 @@ { template <typename E> - struct psite_base : public Psite<E> - /// FIXME Get implementation + struct psite_base : public Psite<E>, + public impl<typename E::site> { operator typename E::psite () const; Index: sandbox/ballas/refactorization/box2d.hh --- sandbox/ballas/refactorization/box2d.hh (revision 0) +++ sandbox/ballas/refactorization/box2d.hh (revision 0) @@ -0,0 +1,246 @@ +#ifndef BOX2D_HH_ +# define BOX2D_HH_ + +# include <point2d.hh> +# include <internal/piter_base.hh> +# include <iostream> + +namespace mln +{ + + // fwd decl + template <typename P> + struct box2d_fwd_piter_; + + + /// FIXME Do a concept box + /// FIXME add implementation method + template <typename C> + struct box2d : public Pset< box2d<point2d<C> > > + { + enum { dim = 2 }; + + typedef point2d<C> psite; + typedef point2d<C> site; + + typedef dpoint2d<C> dpoint; + + typedef box2d_fwd_piter_<C> fwd_piter; + typedef box2d_fwd_piter_<C> bkd_piter; // fixme + + point2d<C> pmin() const; + point2d<C>& pmin(); + point2d<C> pmax() const; + point2d<C>& pmax(); + + box2d(); + box2d(const site& pmin, const site& pmax); + + bool has(const site& p) const; + + protected: + point2d<C> pmin_, pmax_; + }; + + + template <typename C> + std::ostream& operator<<(std::ostream& ostr, const box2d<C>& b); + + +# ifndef MLN_INCLUDE_ONLY + + template <typename C> + inline + point2d<C> + box2d<C>::pmin() const + { + return pmin_; + } + + template <typename C> + inline + point2d<C>& + box2d<C>::pmin() + { + return pmin_; + } + + template <typename C> + inline + point2d<C> + box2d<C>::pmax() const + { + return pmax_; + } + + template <typename C> + inline + point2d<C>& + box2d<C>::pmax() + { + return pmax_; + } + + template <typename C> + inline + box2d<C>::box2d() + { + } + + template <typename C> + inline + box2d<C>::box2d(const site& pmin, const psite& pmax) + : pmin_(pmin), + pmax_(pmax) + { + } + + template <typename C> + inline + bool + box2d<C>::has(const site& p) const + { + for (unsigned i = 0; i < C::dim; ++i) + if (p[i] < pmin_[i] || p[i] > pmax_[i]) + return false; + return true; + } + + template <typename C> + inline + std::ostream& operator<<(std::ostream& ostr, const box2d<C>& b) + { + return ostr << "[" << b.pmin() << ".." << b.pmax() << ']'; + } + +# endif // ! MLN_INCLUDE_ONLY + + + /// fwd_piter + template <typename C> + class box2d_fwd_piter_ : public + internal::piter_base_< box2d_fwd_piter_<C>, point2d<C>, point2d<C> > + { + public: + typedef point2d<C> psite; + typedef point2d<C> site; + + + + enum { dim = 2}; + + box2d_fwd_piter_(const box2d<C>& b); + + const point2d<C>& to_site() const; + const point2d<C>& to_psite() const; + + /// Give the i-th coordinate. + C operator[](unsigned i) const; + + /// Test the iterator validity. + bool is_valid() const; + + /// Invalidate the iterator. + void invalidate(); + + /// Start an iteration. + void start(); + + /// Go to the next point. + void next_(); + + private: + const box2d<C>& b_; + site p_, nop_; + }; + + + +# ifndef MLN_INCLUDE_ONLY + + + // box_fwd_piter_<P> + + template <typename C> + inline + box2d_fwd_piter_<C>::box2d_fwd_piter_(const box2d<C>& b) + : b_(b) + { + nop_ = b_.pmax(); + ++nop_[0]; + invalidate(); + } + + template <typename C> + inline + const point2d<C>& + box2d_fwd_piter_<C>::to_site() const + { + return p_; + } + + template <typename C> + inline + const point2d<C>& + box2d_fwd_piter_<C>::to_psite() const + { + return p_; + } + + template <typename C> + inline + C + box2d_fwd_piter_<C>::operator[](unsigned i) const + { + assert(i < dim); + return p_[i]; + } + + template <typename C> + inline + bool + box2d_fwd_piter_<C>::is_valid() const + { + return p_ != nop_; + } + + template <typename C> + inline + void + box2d_fwd_piter_<C>::invalidate() + { + p_ = nop_; + } + + template <typename C> + inline + void + box2d_fwd_piter_<C>::start() + { + p_ = b_.pmin(); + } + + template <typename C> + inline + void + box2d_fwd_piter_<C>::next_() + { + for (int i = dim - 1; i >= 0; --i) + if (p_[i] == b_.pmax()[i]) + p_[i] = b_.pmin()[i]; + else + { + ++p_[i]; + break; + } + if (p_ == b_.pmin()) + p_ = nop_; + } + +# endif // ! MLN_INCLUDE_ONLY + +} // end of namespace mln + + + +#endif // !BOX2D_HH_ Index: sandbox/ballas/refactorization/point2d.hh --- sandbox/ballas/refactorization/point2d.hh (revision 0) +++ sandbox/ballas/refactorization/point2d.hh (revision 0) @@ -0,0 +1,201 @@ +#ifndef POINT2D_HH_ +# define POINT2D_HH_ + +# include <concept.hh> +# include <vector> +# include <point2d_impl.hh> +# include <iostream> + +namespace mln +{ + /// fwd declaration + template <typename C> struct point2d; + + /// Dpoint2d + template <typename C> + struct dpoint2d : public Dsite< dpoint2d<C> > + { + enum { dim = 2 }; + typedef point2d<C> point; + typedef C coord; + + C operator[](unsigned i) const; + C& operator[](unsigned i); + + dpoint2d(); + dpoint2d(C row, C col); + + void set_all(C c); + + protected: + std::vector<C> coord_; + }; + + /// Point2d + template <typename C> + struct point2d : public Site< point2d<C> > + { + + enum { dim = 2 }; + typedef dpoint2d<C> dpoint; + typedef C coord; + + + C operator[](unsigned i) const; + C& operator[](unsigned i); + + point2d(); + point2d(C row, C col); + + void set_all(C c); + + point2d<C>& operator+=(const dpoint& dp); + point2d<C>& operator-=(const dpoint& dp); + + protected: + std::vector<C> coord_; + }; + + template <typename C> + inline + bool operator==(const point2d<C>& lhs_, const point2d<C>& rhs_); + + template <typename C> + inline + bool operator!=(const point2d<C>& lhs, const point2d<C>& rhs); + + +# ifndef MLN_INCLUDE_ONLY + + /// Dpoint2d + template <typename C> + C + dpoint2d<C>::operator[](unsigned i) const + { + assert(i < 2); + return coord_[i]; + } + + template <typename C> + C& + dpoint2d<C>::operator[](unsigned i) + { + assert(i < 2); + return coord_[i]; + } + + + template <typename C> + dpoint2d<C>::dpoint2d() : + coord_(2, 0) + { + } + + template <typename C> + dpoint2d<C>::dpoint2d(C row, C col) : + coord_(0) + { + coord_[0] = row; + coord_[1] = col; + } + + template <typename C> + void + dpoint2d<C>::set_all(C c) + { + coord_[0] = c; + coord_[1] = c; + } + + + /// Point2d + template <typename C> + inline + C point2d<C>::operator[](unsigned i) const + { + assert(i < dim); + return this->coord_[i]; + } + + template <typename C> + inline + C& point2d<C>::operator[](unsigned i) + { + assert(i < dim); + return this->coord_[i]; + } + + // Constructors. + template <typename C> + inline + point2d<C>::point2d() : + coord_(2, 0) + { + } + + template <typename C> + inline + point2d<C>::point2d(C row, C col) : + coord_(2) + { + coord_[0] = row; + coord_[1] = col; + } + + template <typename C> + inline + void point2d<C>::set_all(C c) + { + coord_[0] = c; + coord_[1] = c; + } + + template <typename C> + inline + point2d<C>& + point2d<C>::operator+=(const dpoint& dp) + { + for (unsigned i = 0; i < dim; ++i) + coord_[i] += dp[i]; + return *this; + } + + template <typename C> + inline + point2d<C>& + point2d<C>::operator-=(const dpoint& dp) + { + for (unsigned i = 0; i < dim; ++i) + coord_[i] -= dp[i]; + return *this; + } + + + template <typename C> + inline + bool operator==(const point2d<C>& lhs, const point2d<C>& rhs) + { + for (unsigned i = 0; i < 2; ++i) + if (lhs[i] != rhs[i]) + return false; + return true; + } + + template <typename C> + inline + bool operator!=(const point2d<C>& lhs, const point2d<C>& rhs) + { + return !(lhs == rhs); + } + + template <typename C> + std::ostream& operator<<(std::ostream& ostr, const point2d<C>& p) + { + return ostr << "(" << p[0] << "," << p[1] << ")"; + } + +# endif // ! MLN_INCLUDE_ONLY + +} + +#endif // !POINT2D_HH_ Index: sandbox/ballas/refactorization/point2d_impl.hh --- sandbox/ballas/refactorization/point2d_impl.hh (revision 0) +++ sandbox/ballas/refactorization/point2d_impl.hh (revision 0) @@ -0,0 +1,40 @@ +#ifndef POINT2D_IMPL_HH_ +# define POINT2D_IMPL_HH_ + +# include <internal/impl.hh> + + +namespace mln +{ + + /// fwd declaration + template <typename C> + struct point2d; + + template <typename C> + struct impl< point2d<C> > + { + C operator[](unsigned i) const; + + + // Non const method +// C& operator[](unsigned i); +// void set_all(C c); +// point2d<C>& operator+=(const dpoint& dp); +// point2d<C>& operator-=(const dpoint& dp); + }; + +# ifndef MLN_INCLUDE_ONLY + + template <typename C> + C + impl< point2d<C> >::operator[] (unsigned i) const + { + return this->to_site()[i]; + } + +# endif // ! MLN_INCLUDE_ONLY +} + + +#endif // !POINT2D_IMPL_HH_
participants (1)
-
Nicolas Ballas