705: Add backward value iterators on look-up tables.

https://svn.lrde.epita.fr/svn/oln/trunk/olena Index: ChangeLog from Roland Levillain <roland@lrde.epita.fr> Add backward value iterators on look-up tables. * oln/core/lookup_table.hh (oln::lookup_table::reverse_iterator) (oln::lookup_table::const_reverse_iterator): New typedefs. (oln::lookup_table::rbegin, oln::lookup_table::rend): New methods. * oln/core/typedefs.hh (bkd_viter_type): New virtual type declaration. * oln/core/gen/bkd_viter_lut.hh: New. * oln/core/gen/fwd_viter_lut.hh: Typos. * oln/morpher/with_lut.hh (single_vtype<morpher::with_lut<Image,Lut>, typedef_::bkd_viter_type>): New. * tests/morphers/with_lut.cc (main): Test backward value iterator on look-up table. * oln/Makefile.am (nobase_oln_HEADERS): Add core/gen/bkd_viter_lut.hh. oln/Makefile.am | 1 oln/core/gen/bkd_viter_lut.hh | 182 ++++++++++++++++++++++++++++++++++++++++++ oln/core/gen/fwd_viter_lut.hh | 4 oln/core/lookup_table.hh | 53 +++++++++++- oln/core/typedefs.hh | 2 oln/morpher/with_lut.hh | 13 ++- tests/morphers/with_lut.cc | 29 ++---- 7 files changed, 257 insertions(+), 27 deletions(-) Index: tests/morphers/with_lut.cc --- tests/morphers/with_lut.cc (revision 704) +++ tests/morphers/with_lut.cc (working copy) @@ -151,27 +151,18 @@ oln_type_of_(image_with_lut_t, fwd_viter) fv(ima_with_lut.lut()); std::list<rgb8> fwd_values; std::cout << "values of lut (fwd) =" << std::endl; - for_all (fv) - { // Push the values to the *back* of the list. + for_all (fv) fwd_values.push_back(fv); - std::cout << " " << fv << std::endl; - } - -// FIXME: To be enabled later. - -// // Check bkd_viter_lut. -// oln_type_of_(image_with_lut_t, bkd_viter) bv(ima_with_lut.lut()); -// std::list<rgb8> bkd_values; -// std::cout << "values of lut (bkd) =" << std::endl; -// for_all (bv) -// { -// // Push the values to the *front* of the list. -// bkd_values.push_front(bv); -// std::cout << " " << bv << std::endl; -// } -// // Compare the two lists. -// assert (fwd_values == bkd_values); + // Check bkd_viter_lut. + oln_type_of_(image_with_lut_t, bkd_viter) bv(ima_with_lut.lut()); + std::list<rgb8> bkd_values; + std::cout << "values of lut (bkd) =" << std::endl; + // Push the values to the *front* of the list (i.e., in reverse order). + for_all (bv) + bkd_values.push_front(bv); + // Compare the two lists. + assert (fwd_values == bkd_values); // FIXME: To be enabled later, when oln::level::fill is specialized Index: oln/core/typedefs.hh --- oln/core/typedefs.hh (revision 704) +++ oln/core/typedefs.hh (working copy) @@ -97,7 +97,7 @@ mlc_decl_typedef(bkd_niter_type); mlc_decl_typedef(fwd_viter_type); - // FIXME: bkd_viter_type. + mlc_decl_typedef(bkd_viter_type); /*------------. Index: oln/core/gen/bkd_viter_lut.hh --- oln/core/gen/bkd_viter_lut.hh (revision 0) +++ oln/core/gen/bkd_viter_lut.hh (revision 0) @@ -0,0 +1,182 @@ +// Copyright (C) 2006 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 OLN_CORE_GEN_BKD_VITER_LUT_HH +# define OLN_CORE_GEN_BKD_VITER_LUT_HH + +# include <oln/core/abstract/iterator_on_values.hh> +# include <oln/core/lookup_table.hh> + +namespace oln +{ + + // Forward declaration. + template <typename Lut> class bkd_viter_lut; + + + /// Super type declaration. + template <typename Lut> + struct set_super_type< bkd_viter_lut<Lut> > + { + typedef bkd_viter_lut<Lut> self_t; + typedef abstract::iterator_on_values<self_t> ret; + }; + + + /// Virtual types associated to oln::bkd_viter_lut<Lut>. + template <typename Lut> + struct vtypes< bkd_viter_lut<Lut> > + { + // The look-up table is immutable. + typedef const Lut lut_type; + typedef typename Lut::const_reverse_iterator lut_iter_type; + typedef typename Lut::new_value_type value_type; + }; + + + /// Backward value iterator on look-up table. + template <typename Lut> + class bkd_viter_lut : + public stc_get_supers(bkd_viter_lut<Lut>) + // Check that Lut is a look-up table. + // FIXME: an abstract::lookup_table would be useful. + { + typedef bkd_viter_lut<Lut> self_t; + typedef stc_get_super(self_t) super_t; + typedef oln_type_of(self_t, lut_iter) lut_iter_t; + + public: + typedef oln_type_of(self_t, lut) lut_type; + typedef oln_type_of(self_t, value) value_type; + + public: + // Construct an uninitialized value iterator. + bkd_viter_lut(const Lut& lut); + + /// Iterator manipulators. + /// \{ + void impl_start(); + void impl_next(); + void impl_invalidate(); + bool impl_is_valid() const; + /// \} + + // Get the value pointed by this iterator (const version). + value_type impl_to_value() const; + + void print(std::ostream& ostr) const; + + protected: + /// Look-up table. + lut_type& lut_; + /// Iterator on \a lut_. + lut_iter_t i_; + }; + + + template <typename Lut> + std::ostream& operator<<(std::ostream& ostr, + const bkd_viter_lut<Lut>& t); + + + +# ifndef OLN_INCLUDE_ONLY + + template <typename Lut> + bkd_viter_lut<Lut>::bkd_viter_lut(const Lut& lut) : + super_t(), + lut_(lut), + i_() + { + // Initialize underlying iterator (i.e., \a i_.) + this->invalidate(); + } + + template <typename Exact> + void + bkd_viter_lut<Exact>::impl_start() + { + i_ = lut_.rbegin(); + } + + template <typename Exact> + void + bkd_viter_lut<Exact>::impl_next() + { + /* Iterate until a different key is reached. In fact, + std::multimap might not be the best choice to implement + new_to_orig_map_. Maybe a std::map binding orig_val to a + std::set of new_val's would is better?. */ + value_type val = i_->first; + do + ++i_; + while (i_ != lut_.rend() and i_->first == val); + } + + template <typename Exact> + void + bkd_viter_lut<Exact>::impl_invalidate() + { + i_ = lut_.rend(); + } + + template <typename Exact> + bool + bkd_viter_lut<Exact>::impl_is_valid() const + { + return (i_ != lut_.rend()); + } + + template <typename Lut> + typename bkd_viter_lut<Lut>::value_type + bkd_viter_lut<Lut>::impl_to_value() const + { + precondition(this->is_valid()); + return this->i_->first; + } + + template <typename Lut> + void bkd_viter_lut<Lut>::print(std::ostream& ostr) const + { + precondition(this->is_valid()); + ostr << "{ value = " << this->i_->first << " }"; + } + + + template <typename Lut> + std::ostream& operator<<(std::ostream& ostr, const bkd_viter_lut<Lut>& t) + { + t.print(ostr); + return ostr; + } + +# endif + +} // end of namespace oln + + +#endif // ! OLN_CORE_GEN_BKD_VITER_LUT_HH Index: oln/core/gen/fwd_viter_lut.hh --- oln/core/gen/fwd_viter_lut.hh (revision 704) +++ oln/core/gen/fwd_viter_lut.hh (working copy) @@ -34,8 +34,6 @@ namespace oln { - // FIXME: Merge this file with oln/core/internal/fwd_viter_lut.hh? - // Forward declaration. template <typename Lut> class fwd_viter_lut; @@ -60,7 +58,7 @@ }; - /// Const forward value iterator on look-up table. + /// Forward value iterator on look-up table. template <typename Lut> class fwd_viter_lut : public stc_get_supers(fwd_viter_lut<Lut>) Index: oln/core/lookup_table.hh --- oln/core/lookup_table.hh (revision 704) +++ oln/core/lookup_table.hh (working copy) @@ -52,6 +52,9 @@ typedef typename new_to_orig_map_type::const_iterator const_iterator; typedef typename new_to_orig_map_type::iterator iterator; + typedef typename new_to_orig_map_type::const_reverse_iterator const_reverse_iterator; + typedef typename new_to_orig_map_type::reverse_iterator reverse_iterator; + public: lookup_table(); @@ -59,13 +62,17 @@ const new_value_type operator() (const orig_value_type& orig_value) const; - /// (Internal) iterators. + /// (Internal) iterators bounds. /// \{ const_iterator begin() const; iterator begin(); - const_iterator end() const; iterator end(); + + const_reverse_iterator rbegin() const; + reverse_iterator rbegin(); + const_reverse_iterator rend() const; + reverse_iterator rend(); /// \} const_iterator find(const new_value_type& val) const; @@ -120,6 +127,10 @@ } + /*--------------------------. + | Direct iterators bounds. | + `--------------------------*/ + template <typename Orig_Value, typename New_Value, typename Orig_Value_Compare, typename New_Value_Compare> typename lookup_table<Orig_Value, New_Value, Orig_Value_Compare, New_Value_Compare>::const_iterator @@ -153,6 +164,44 @@ } + /*---------------------------. + | Reverse iterators bounds. | + `---------------------------*/ + + template <typename Orig_Value, typename New_Value, + typename Orig_Value_Compare, typename New_Value_Compare> + typename lookup_table<Orig_Value, New_Value, Orig_Value_Compare, New_Value_Compare>::const_reverse_iterator + lookup_table<Orig_Value, New_Value, Orig_Value_Compare, New_Value_Compare>::rbegin() const + { + return new_to_orig_map_.rbegin(); + } + + template <typename Orig_Value, typename New_Value, + typename Orig_Value_Compare, typename New_Value_Compare> + typename lookup_table<Orig_Value, New_Value, Orig_Value_Compare, New_Value_Compare>::reverse_iterator + lookup_table<Orig_Value, New_Value, Orig_Value_Compare, New_Value_Compare>::rbegin() + { + return new_to_orig_map_.rbegin(); + } + + template <typename Orig_Value, typename New_Value, + typename Orig_Value_Compare, typename New_Value_Compare> + typename lookup_table<Orig_Value, New_Value, Orig_Value_Compare, New_Value_Compare>::const_reverse_iterator + lookup_table<Orig_Value, New_Value, Orig_Value_Compare, New_Value_Compare>::rend() const + { + return new_to_orig_map_.rend(); + } + + template <typename Orig_Value, typename New_Value, + typename Orig_Value_Compare, typename New_Value_Compare> + typename lookup_table<Orig_Value, New_Value, Orig_Value_Compare, New_Value_Compare>::reverse_iterator + lookup_table<Orig_Value, New_Value, Orig_Value_Compare, New_Value_Compare>::rend() + { + return new_to_orig_map_.rend(); + } + + + template <typename Orig_Value, typename New_Value, typename Orig_Value_Compare, typename New_Value_Compare> typename lookup_table<Orig_Value, New_Value, Orig_Value_Compare, New_Value_Compare>::const_iterator Index: oln/Makefile.am --- oln/Makefile.am (revision 704) +++ oln/Makefile.am (working copy) @@ -123,6 +123,7 @@ core/gen/bkd_niter_neighb.hh \ core/gen/bkd_piter_bbox.hh \ core/gen/bkd_qiter_win.hh \ + core/gen/bkd_viter_lut.hh \ core/gen/fwd_decls.hh \ core/gen/fwd_niter_neighb.hh \ core/gen/fwd_piter_bbox.hh \ Index: oln/morpher/with_lut.hh --- oln/morpher/with_lut.hh (revision 704) +++ oln/morpher/with_lut.hh (working copy) @@ -32,6 +32,7 @@ # include <oln/core/lookup_table.hh> # include <oln/value/lut_value_proxy.hh> # include <oln/core/gen/fwd_viter_lut.hh> +# include <oln/core/gen/bkd_viter_lut.hh> namespace oln @@ -80,7 +81,7 @@ typedef value::mutable_lut_value_proxy<Lut> mutable_value_proxy_type; // fwd_viter_type: see below. - // FIXME: implement bkd_viter_type! + // bkd_viter_type: see below. }; // Rvalue. @@ -93,13 +94,21 @@ typedef oln_value(self_t) ret; }; - /// Const forward viter vtype of morpher::with_lut. + /// Forward value iterator virtual type for morpher::with_lut. template <typename Image, typename Lut> struct single_vtype< morpher::with_lut<Image, Lut>, typedef_::fwd_viter_type > { typedef fwd_viter_lut<Lut> ret; }; + + /// Backward value iterator virtual type for morpher::with_lut. + template <typename Image, typename Lut> + struct single_vtype< morpher::with_lut<Image, Lut>, + typedef_::bkd_viter_type > + { + typedef bkd_viter_lut<Lut> ret; + }; /// \}
participants (1)
-
Roland Levillain