678: Introduce ids for images and better oln::morpher::count_rw_.

2006-10-25 Thierry GERAUD <theo@tegucigalpa.lrde.epita.fr> Introduce ids for images and better oln::morpher::count_rw_. * tests/morphers/count_rw_morpher.cc: Add sample code in comments. * oln/core/abstract/iterator_on_points.hh (operator<<): New. * oln/core/abstract/image.hh (image, operator=): New. (id_, id): New. * oln/core/gen/piter_isubset.hh (isubset_): Remove &. * oln/core/gen/pw_value.hh (ima_): Remove &. (impl_calc): Fix static check. * oln/core/gen/topo_add_isubset.hh (isubset_): Add comment. * oln/core/gen/fwd_piter_bbox.hh (operator<<): Remove; obsolete. * oln/core/gen/bkd_piter_bbox.hh: Likewise. * oln/morpher/add_isubset.hh (operator|): New overloading. (add_isubset): Relax static check. * oln/morpher/internal/image_value_morpher.hh (image_): Remove &. * oln/morpher/internal/image_extension.hh (image_): Remove &. (image_extension): New cpy ctor. * oln/morpher/count_rw.hh (count_rw): Rename the class as... (count_rw_): ...this. (count_rw): New overloading. * oln/value/rw_counter.hh (counter_rw): Rewrite. (rw_counter): Remove & from ima_ type. (ro_counter): Likewise. Index: tests/morphers/count_rw_morpher.cc =================================================================== --- tests/morphers/count_rw_morpher.cc (revision 677) +++ tests/morphers/count_rw_morpher.cc (working copy) @@ -38,7 +38,7 @@ typedef image2d<int> image_t; image_t ima(3,3); - morpher::count_rw<image_t> ima2(ima); + morpher::count_rw_<image_t> ima2(ima); point2d p(0,0); ima2(p); @@ -46,8 +46,84 @@ int i = ima2(p); i = 0; // suppress "unused i" warning - assert(value::counter::n_readwrite_calls(ima) == 3); - assert(value::counter::n_readonly_calls(ima) == 0); - assert(value::counter::n_reads(ima) == 1); - assert(value::counter::n_writes(ima) == 1); + assert(value::counter_rw::n_readwrite_calls(ima) == 3); + assert(value::counter_rw::n_readonly_calls(ima) == 0); + assert(value::counter_rw::n_reads(ima) == 1); + assert(value::counter_rw::n_writes(ima) == 1); } + + +/* + +// another piece of nice code: + + +#include <cassert> + +#include <oln/basics2d.hh> +#include <oln/core/gen/pw_value.hh> + +#include <oln/morpher/add_isubset.hh> +#include <oln/morpher/add_neighborhood.hh> +#include <oln/morpher/count_rw.hh> + +#include <oln/debug/print.hh> +#include <oln/level/fill.hh> + + +template <typename I> +void test(const oln::abstract::image<I>& input) +{ + oln_value(I) dev_null; + oln_piter(I) p(input.topo()); + oln_niter(I) n(p, input.topo()); + for_all(p) + { + std::cout << p << ": "; + for_all(n) + { + std::cout << n << ' '; + dev_null = input(n); // read + } + std::cout << std::endl; + } +} + +int main() +{ + using namespace oln; + typedef image2d<int> image_t; + + image_t ima(5, 5); + int values[] = + { 3, 7, 1, 1, 7, + 9, 3, 2, 1, 1, + 1, 6, 5, 4, 3, + 7, 9, 8, 1, 3, + 3, 3, 9, 7, 1 }; + ima << values; + +# define pred (pw_value(ima) % 2 == 0 or pw_value(ima) == 5) + + debug::print(ima | pred); + test(count_rw((ima + c4) | pred)); + + value::counter_rw::print(); + +// gives: +// - - - - - +// - - 2 - - +// - 6 5 4 - +// - - 8 - - +// - - - - - +// (1, 2): (2, 2) +// (2, 1): (2, 2) +// (2, 2): (1, 2) (2, 1) (2, 3) (3, 2) +// (2, 3): (2, 2) +// (3, 2): (2, 2) +// n_readwrite_calls = 0 +// n_readonly_calls = 8 +// n_reads = 8 +// n_writes = 0 + +*/ Index: oln/core/abstract/iterator_on_points.hh =================================================================== --- oln/core/abstract/iterator_on_points.hh (revision 677) +++ oln/core/abstract/iterator_on_points.hh (working copy) @@ -91,7 +91,10 @@ }; // end of class oln::abstract::iterator_on_points<E> + template <typename E> + std::ostream& operator<<(std::ostream& ostr, const iterator_on_points<E>& pit); + # ifndef OLN_INCLUDE_ONLY template <typename E> @@ -130,6 +133,13 @@ } + template <typename E> + std::ostream& operator<<(std::ostream& ostr, const iterator_on_points<E>& pit) + { + return ostr << pit.to_point(); + } + + # endif } // end of namespace oln::abstract Index: oln/core/abstract/image.hh =================================================================== --- oln/core/abstract/image.hh (revision 677) +++ oln/core/abstract/image.hh (working copy) @@ -40,6 +40,12 @@ namespace oln { + + // Fwd decl. + static unsigned& current_image_id(); + + + /*! \namespace oln::abstract ** \brief oln::abstract namespace. */ @@ -108,17 +114,26 @@ oln_rvalue(E) operator()(const oln_psite(E)& p) const; + unsigned id() const; + protected: - /*! \brief Constructor (protected, empty). + /*! \brief Constructors (protected). */ image(); - image(const image&); + image(const image& rhs); + /*! \brief Assignment (protected). + */ + E& operator=(const image& rhs); + /*! \brief Destructor. */ virtual ~image(); + private: + + const unsigned id_; }; @@ -139,17 +154,26 @@ template <typename E> image<E>::image() + : id_(++current_image_id()) { ++debug::n_images; } template <typename E> - image<E>::image(const image&) + image<E>::image(const image& rhs) + : id_(rhs.id_) { ++debug::n_images; } template <typename E> + E& image<E>::operator=(const image<E>& rhs) + { + this->id_ = rhs.id_; + return this->exact(); + } + + template <typename E> image<E>::~image() { decl(); @@ -157,6 +181,13 @@ } template <typename E> + unsigned + image<E>::id() const + { + return id_; + } + + template <typename E> const oln_topo(E)& image<E>::topo() const { @@ -176,6 +207,19 @@ } // end of namespace oln::abstract + + +# ifndef OLN_INCLUDE_ONLY + + static unsigned& current_image_id() + { + static unsigned id_ = 0; + return id_; + } + +# endif + + } // end of namespace oln Index: oln/core/gen/piter_isubset.hh =================================================================== --- oln/core/gen/piter_isubset.hh (revision 677) +++ oln/core/gen/piter_isubset.hh (working copy) @@ -97,7 +97,7 @@ protected: piter_t p_; - const isubset_t& isubset_; + isubset_t isubset_; // Cpy. }; // end of class oln::piter_isubset_<point> Index: oln/core/gen/pw_value.hh =================================================================== --- oln/core/gen/pw_value.hh (revision 677) +++ oln/core/gen/pw_value.hh (working copy) @@ -61,7 +61,7 @@ protected: - const I& ima_; + const I ima_; }; @@ -72,7 +72,8 @@ oln_rvalue(I) pw_value_type<I>::impl_calc(const A& a) const { - mlc::assert_< mlc_is_a(A, oln::abstract::point) >::check(); + mlc::assert_< mlc::or_< mlc_is_a(A, oln::abstract::point), + mlc_is_a(A, oln::abstract::iterator_on_points) > >::check(); return ima_(a); } Index: oln/core/gen/topo_add_isubset.hh =================================================================== --- oln/core/gen/topo_add_isubset.hh (revision 677) +++ oln/core/gen/topo_add_isubset.hh (working copy) @@ -84,7 +84,7 @@ protected: topo_t topo_; - isubset_t isubset_; + isubset_t isubset_; // Cpy. }; Index: oln/core/gen/fwd_piter_bbox.hh =================================================================== --- oln/core/gen/fwd_piter_bbox.hh (revision 677) +++ oln/core/gen/fwd_piter_bbox.hh (working copy) @@ -79,11 +79,7 @@ }; // end of class oln::fwd_piter_bbox_<point> - template <typename point_t> - std::ostream& operator<<(std::ostream& ostr, const fwd_piter_bbox_<point_t>& t); - - # ifndef OLN_INCLUDE_ONLY template <typename point_t> @@ -100,14 +96,6 @@ << " }"; } - template <typename point_t> - std::ostream& operator<<(std::ostream& ostr, const fwd_piter_bbox_<point_t>& t) - { - t.print(ostr); - return ostr; - } - - # endif } // end of namespace oln Index: oln/core/gen/bkd_piter_bbox.hh =================================================================== --- oln/core/gen/bkd_piter_bbox.hh (revision 677) +++ oln/core/gen/bkd_piter_bbox.hh (working copy) @@ -79,11 +79,7 @@ }; // end of class oln::bkd_piter_bbox_<point_t> - template <typename point_t> - std::ostream& operator<<(std::ostream& ostr, const bkd_piter_bbox_<point_t>& t); - - # ifndef OLN_INCLUDE_ONLY template <typename point_t> @@ -100,13 +96,6 @@ << " }"; } - template <typename point_t> - std::ostream& operator<<(std::ostream& ostr, const bkd_piter_bbox_<point_t>& t) - { - t.print(ostr); - return ostr; - } - # endif } // end of namespace oln Index: oln/morpher/add_isubset.hh =================================================================== --- oln/morpher/add_isubset.hh (revision 677) +++ oln/morpher/add_isubset.hh (working copy) @@ -34,7 +34,10 @@ # include <oln/core/gen/topo_add_isubset.hh> # include <oln/core/gen/piter_isubset.hh> +# include <xtd/abstract/fun_nary_expr.hh> +# include <oln/core/gen/pw_value.hh> + namespace oln { @@ -124,7 +127,7 @@ super_t(image), topo_(image.topo(), isubset) { - mlc::assert_equal_<oln_type_of(Image, grid), oln_type_of(Isubset, grid)>::check(); + // mlc::assert_equal_<oln_type_of(Image, grid), oln_type_of(Isubset, grid)>::check(); // FIXME: check that Image is without a isubset } @@ -151,6 +154,13 @@ // FIXME: Add mutable version. + template <typename I, typename S> + morpher::add_isubset<I, S> + operator | (const abstract::image<I>& image, + const xtd::abstract::fun_nary_expr_<1,S>& fsubset); + + + # ifndef OLN_INCLUDE_ONLY template <typename I, typename S> @@ -164,6 +174,15 @@ return tmp; } + template <typename I, typename S> + morpher::add_isubset<I, S> + operator | (const abstract::image<I>& image, + const xtd::abstract::fun_nary_expr_<1,S>& fsubset) + { + morpher::add_isubset<I, S> tmp(image.exact(), fsubset.exact_()); + return tmp; + } + # endif } // end of namespace oln Index: oln/morpher/internal/image_value_morpher.hh =================================================================== --- oln/morpher/internal/image_value_morpher.hh (revision 677) +++ oln/morpher/internal/image_value_morpher.hh (working copy) @@ -96,7 +96,7 @@ Image& delegate(); protected: - Image& image_; + Image image_; }; Index: oln/morpher/internal/image_extension.hh =================================================================== --- oln/morpher/internal/image_extension.hh (revision 677) +++ oln/morpher/internal/image_extension.hh (working copy) @@ -86,6 +86,7 @@ // FIXME: Handle the constness. image_extension(const Image& image); + image_extension(Image& image); const Image& delegate() const; Image& delegate(); @@ -100,6 +101,12 @@ template <typename Image, typename Exact> image_extension<Image, Exact>::image_extension(const Image& image) : + image_(const_cast<Image&>(image)) + { + } + + template <typename Image, typename Exact> + image_extension<Image, Exact>::image_extension(Image& image) : image_(image) { } Index: oln/morpher/count_rw.hh =================================================================== --- oln/morpher/count_rw.hh (revision 677) +++ oln/morpher/count_rw.hh (working copy) @@ -39,22 +39,22 @@ namespace morpher { // Forward declaration. - template <typename Image> struct count_rw; + template <typename Image> struct count_rw_; } // end of namespace oln::morpher /// Super type. template <typename Image> - struct set_super_type< morpher::count_rw<Image> > + struct set_super_type< morpher::count_rw_<Image> > { - typedef morpher::count_rw<Image> self_t; + typedef morpher::count_rw_<Image> self_t; typedef morpher::internal::image_value_morpher<Image, self_t> ret; }; template <typename Image> - struct vtypes< morpher::count_rw<Image> > + struct vtypes< morpher::count_rw_<Image> > { public: typedef mlc::true_ is_computed_type; @@ -64,7 +64,7 @@ }; template <typename Image> - struct single_vtype< morpher::count_rw<Image>, typedef_::rvalue_type > + struct single_vtype< morpher::count_rw_<Image>, typedef_::rvalue_type > { typedef value::ro_counter<Image> ret; }; @@ -76,20 +76,20 @@ /// 'Image thru Function' morpher. template <typename Image> - class count_rw : public internal::image_value_morpher< Image, - morpher::count_rw<Image> > + class count_rw_ : public internal::image_value_morpher< Image, + morpher::count_rw_<Image> > { private: - typedef count_rw<Image> self_t; + typedef count_rw_<Image> self_t; typedef internal::image_value_morpher<Image, self_t> super_t; using super_t::image_; public: - count_rw(Image& image); - count_rw(oln::abstract::mutable_image<Image>& image); + count_rw_(Image& image); + count_rw_(oln::abstract::mutable_image<Image>& image); oln_rvalue(self_t) impl_op_read(const oln_psite(self_t)& p) const; oln_lvalue(self_t) impl_op_readwrite(const oln_psite(self_t)& p); @@ -109,28 +109,28 @@ // public template <typename Image> - count_rw<Image>::count_rw(Image& image) : + count_rw_<Image>::count_rw_(Image& image) : super_t(image) { } template <typename Image> - count_rw<Image>::count_rw(oln::abstract::mutable_image<Image>& image) : + count_rw_<Image>::count_rw_(oln::abstract::mutable_image<Image>& image) : super_t(image.exact()) { } template <typename Image> - oln_rvalue(count_rw<Image>) - count_rw<Image>::impl_op_read(const oln_psite(count_rw<Image>)& p) const + oln_rvalue(count_rw_<Image>) + count_rw_<Image>::impl_op_read(const oln_psite(count_rw_<Image>)& p) const { value::ro_counter<Image> tmp(image_, p); return tmp; } template <typename Image> - oln_lvalue(count_rw<Image>) - count_rw<Image>::impl_op_readwrite(const oln_psite(count_rw<Image>)& p) + oln_lvalue(count_rw_<Image>) + count_rw_<Image>::impl_op_readwrite(const oln_psite(count_rw_<Image>)& p) { value::rw_counter<Image> tmp(image_, p); return tmp; @@ -142,14 +142,24 @@ template <typename I> - morpher::count_rw<I> + morpher::count_rw_<I> count_rw(oln::abstract::mutable_image<I>& input) { - morpher::count_rw<I> tmp(input.exact()); + morpher::count_rw_<I> tmp(input.exact()); return tmp; } + template <typename I> + morpher::count_rw_<I> + count_rw(const oln::abstract::mutable_image<I>& input) + { + // FIXME: Hack. + I& input_ = const_cast<I&>(input.exact()); + morpher::count_rw_<I> tmp(input_); + return tmp; + } + } // end of namespace oln Index: oln/value/rw_counter.hh =================================================================== --- oln/value/rw_counter.hh (revision 677) +++ oln/value/rw_counter.hh (working copy) @@ -30,6 +30,7 @@ # include <iostream> # include <map> +# include <set> # include <oln/value/proxy.hh> # include <oln/core/abstract/image/all.hh> @@ -42,45 +43,80 @@ { - struct counter + struct counter_rw { - typedef std::map<void*, unsigned> map_t; + typedef std::map<unsigned, unsigned> map_t; + typedef std::set<unsigned> set_t; + static map_t n_rwc_; + static map_t n_roc_; + static map_t n_r_; + static map_t n_w_; + static set_t ids_; // read-write call - static unsigned& n_readwrite_calls(const oln::type& ima) + template <typename I> + static unsigned& n_readwrite_calls(const oln::abstract::image<I>& ima) { - static map_t n; - return n[(void*)(&ima)]; + ids_.insert(ima.id()); + return n_rwc_[ima.id()]; } // read-only call - static unsigned& n_readonly_calls(const oln::type& ima) + template <typename I> + static unsigned& n_readonly_calls(const oln::abstract::image<I>& ima) { - static map_t n; - return n[(void*)(&ima)]; + ids_.insert(ima.id()); + return n_roc_[ima.id()]; } // read (effective) - static unsigned& n_reads(const oln::type& ima) + template <typename I> + static unsigned& n_reads(const oln::abstract::image<I>& ima) { - static map_t n; - return n[(void*)(&ima)]; + ids_.insert(ima.id()); + return n_r_[ima.id()]; } // write (effective) - static unsigned& n_writes(const oln::type& ima) + template <typename I> + static unsigned& n_writes(const oln::abstract::image<I>& ima) { - static map_t n; - return n[(void*)(&ima)]; + ids_.insert(ima.id()); + return n_w_[ima.id()]; } - static void print(const oln::type& ima) + static void print(unsigned id) { - std::cout << "n_readwrite_calls = " << n_readwrite_calls(ima) << std::endl - << "n_readonly_calls = " << n_readonly_calls(ima) << std::endl - << "n_reads = " << n_reads(ima) << std::endl - << "n_writes = " << n_writes(ima) << std::endl; + ids_.insert(id); + std::cout << "n_readwrite_calls = " << n_rwc_[id] << std::endl + << "n_readonly_calls = " << n_roc_[id] << std::endl + << "n_reads = " << n_r_[id] << std::endl + << "n_writes = " << n_w_[id] << std::endl; } + + template <typename I> + static void print(const oln::abstract::image<I>& ima) + { + print(ima.id()); + } + + static void print() + { + set_t::const_iterator it; + for (it = ids_.begin(); it != ids_.end(); ++it) + print(*it); + } }; +# ifndef OLN_INCLUDE_ONLY + + counter_rw::map_t counter_rw::n_rwc_; + counter_rw::map_t counter_rw::n_roc_; + counter_rw::map_t counter_rw::n_r_; + counter_rw::map_t counter_rw::n_w_; + counter_rw::set_t counter_rw::ids_; + +# endif + + template <typename I> class rw_counter : public abstract::proxy< rw_counter<I> > { @@ -104,7 +140,7 @@ rw_counter<I>& operator=(const V& value); protected: - I& ima_; + I ima_; const oln_psite(I)& p_; }; @@ -132,7 +168,7 @@ oln_rvalue(I) value() const; protected: - const I& ima_; + const I ima_; const oln_psite(I)& p_; }; @@ -151,7 +187,7 @@ : ima_(ima.exact()), p_(p) { - ++counter::n_readwrite_calls(ima_); + ++counter_rw::n_readwrite_calls(ima_); } // Read. @@ -159,7 +195,7 @@ template <typename V> rw_counter<I>::operator V() const { - ++counter::n_reads(ima_); + ++counter_rw::n_reads(ima_); V tmp = ima_(p_); return tmp; } @@ -169,7 +205,7 @@ oln_rvalue(I) rw_counter<I>::value() const { - ++counter::n_reads(ima_); + ++counter_rw::n_reads(ima_); return ima_(p_); } @@ -181,7 +217,7 @@ rw_counter<I>::operator=(const V& value) { ima_(p_) = value; - ++counter::n_writes(ima_); + ++counter_rw::n_writes(ima_); return *this; } @@ -201,7 +237,7 @@ : ima_(ima.exact()), p_(p) { - ++counter::n_readonly_calls(ima_); + ++counter_rw::n_readonly_calls(ima_); } // Read. @@ -209,7 +245,7 @@ template <typename V> ro_counter<I>::operator V() const { - ++counter::n_reads(ima_); + ++counter_rw::n_reads(ima_); V tmp = ima_(p_); return tmp; } @@ -219,7 +255,7 @@ oln_rvalue(I) ro_counter<I>::value() const { - ++counter::n_reads(ima_); + ++counter_rw::n_reads(ima_); return ima_(p_); }
participants (1)
-
Thierry GERAUD