cleanup-2008 2025: Add a default ordering (util::less); update (d)points and vec.

https://svn.lrde.epita.fr/svn/oln/branches/cleanup-2008/milena Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> Add a default ordering (util::less); update (d)points and vec. * mln/core/point.hh (todo): New in doc. (to_vec): New; was missing. (less_than): New specialization. * mln/core/dpoint.hh (to_vec): Avoid copy at return. (less_than): New specialization. * mln/core/box.hh: Rely on util::less. (is_valid): New. (contract): Add pre- and post-conditions. * mln/core/concept/point_site.hh (operator<): Remove. * mln/algebra/vec.hh (less_than): New. * mln/util/set.hh: Rely on util::less. * mln/util/less.hh: New. * mln/util/less_than.hh: New. algebra/vec.hh | 48 +++++++++++++++++++++++++- core/box.hh | 30 +++++++++++++++- core/concept/point_site.hh | 42 ----------------------- core/dpoint.hh | 51 ++++++++++++++++++++++++++-- core/point.hh | 66 ++++++++++++++++++++++++++++++++++++ util/less.hh | 80 ++++++++++++++++++++++++++++++++++++++++++++ util/less_than.hh | 81 +++++++++++++++++++++++++++++++++++++++++++++ util/set.hh | 8 +++- 8 files changed, 356 insertions(+), 50 deletions(-) Index: mln/core/point.hh --- mln/core/point.hh (revision 2024) +++ mln/core/point.hh (working copy) @@ -1,4 +1,4 @@ -// Copyright (C) 2007 EPITA Research and Development Laboratory +// Copyright (C) 2007, 2008 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 @@ -31,6 +31,8 @@ /*! \file mln/core/point.hh * * \brief Definition of the generic point class mln::point_. + * + * \todo Try to generalize less_than with Ml and Mr. */ # include <mln/core/concept/point.hh> @@ -47,6 +49,7 @@ { /// \{ Fwd decls. + template <typename M, typename C> struct point_; template <typename M, typename C> struct dpoint_; namespace literal { struct zero_t; @@ -154,6 +157,9 @@ operator typename internal::point_to_<M, C>::metal_vec () const; operator algebra::vec<M::dim, float> () const; + /// Explicit conversion towards mln::algebra::vec. + const algebra::vec<M::dim, C>& to_vec() const; + /// Transform to point in homogene coordinate system. h_vec<M::dim, C> to_h_vec() const; @@ -162,6 +168,38 @@ }; + namespace util + { + + /*! \brief Ordering "less than" comparison between a couple of + * points. + * + * The ordering is based on a lexicographical ordering over + * coordinates. + * + * Both points have to be defined on the same topology. + * + * \warning In the general case this ordering relationship is \em + * not bound to the way of browsing a domain with a forward point + * iterator. + */ + template <typename M, typename Cl, typename Cr> + struct less_than< point_<M,Cl>, + point_<M,Cr> > + { + /*! \brief Comparison between a couple of points \a lhs and \a + * rhs. + * + * \return True if \p lhs is before \p rhs in the sense of the + * coordinates lexicographic comparison, otherwise false. + */ + bool operator()(const point_<M,Cl>& lhs, + const point_<M,Cr>& rhs) const; + }; + + } // end of namespace mln::util + + # ifndef MLN_INCLUDE_ONLY template <typename M, typename C> @@ -327,6 +365,14 @@ template <typename M, typename C> inline + const algebra::vec<M::dim, C>& + point_<M,C>::to_vec() const + { + return coord_; + } + + template <typename M, typename C> + inline h_vec<M::dim, C> point_<M,C>::to_h_vec() const { h_vec<M::dim, C> tmp; @@ -336,6 +382,24 @@ return tmp; } + + namespace util + { + + template <typename M, typename Cl, typename Cr> + bool + less_than< point_<M,Cl>, + point_<M,Cr> >::operator()(const point_<M,Cl>& lhs, + const point_<M,Cr>& rhs) const + { + enum { n = M::dim }; + typedef less_than< algebra::vec<n,Cl>, algebra::vec<n,Cr> > less_t; + static const less_t op = less_t(); + return op(lhs.to_vec(), rhs.to_vec()); + } + + } // end of namespace mln::util + # endif // ! MLN_INCLUDE_ONLY } // end of namespace mln Index: mln/core/dpoint.hh --- mln/core/dpoint.hh (revision 2024) +++ mln/core/dpoint.hh (working copy) @@ -1,4 +1,4 @@ -// Copyright (C) 2007 EPITA Research and Development Laboratory +// Copyright (C) 2007, 2008 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 @@ -123,13 +123,41 @@ operator algebra::vec<M::dim, Q>() const; /// Explicit conversion. - algebra::vec<M::dim, C> to_vec() const; + const algebra::vec<M::dim, C>& to_vec() const; protected: algebra::vec<M::dim, C> coord_; }; + namespace util + { + + /*! \brief Ordering "less than" comparison between a couple of + * delta-points. + * + * The ordering is based on a lexicographical ordering over + * coordinates. + * + * Both delta-points have to be defined on the same topology. + */ + template <typename M, typename Cl, typename Cr> + struct less_than< dpoint_<M,Cl>, + dpoint_<M,Cr> > + { + /*! \brief Comparison between a couple of delta-points \a lhs + * and \a rhs. + * + * \return True if \p lhs is before \p rhs in the sense of the + * coordinates lexicographic comparison, otherwise false. + */ + bool operator()(const dpoint_<M,Cl>& lhs, + const dpoint_<M,Cr>& rhs) const; + }; + + } // end of namespace mln::util + + # ifndef MLN_INCLUDE_ONLY template <typename M, typename C> @@ -244,12 +272,29 @@ template <typename M, typename C> inline - algebra::vec<M::dim, C> + const algebra::vec<M::dim, C>& dpoint_<M,C>::to_vec() const { return coord_; } + namespace util + { + + template <typename M, typename Cl, typename Cr> + bool + less_than< dpoint_<M,Cl>, + dpoint_<M,Cr> >::operator()(const dpoint_<M,Cl>& lhs, + const dpoint_<M,Cr>& rhs) const + { + enum { n = M::dim }; + typedef less_than< algebra::vec<n,Cl>, algebra::vec<n,Cr> > less_t; + static const less_t op = less_t(); + return op(lhs.to_vec(), rhs.to_vec()); + } + + } // end of namespace mln::util + # endif // ! MLN_INCLUDE_ONLY } // end of namespace mln Index: mln/core/box.hh --- mln/core/box.hh (revision 2024) +++ mln/core/box.hh (working copy) @@ -1,4 +1,4 @@ -// Copyright (C) 2007 EPITA Research and Development Laboratory +// Copyright (C) 2007, 2008 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 @@ -37,6 +37,7 @@ # include <mln/core/internal/box_impl.hh> # include <mln/core/point.hh> # include <mln/literal/origin.hh> +# include <mln/util/less.hh> namespace mln @@ -124,7 +125,12 @@ /// Give a larger box. box_<P> to_larger(unsigned b) const; + /// Test that the box owns valid data, i.e., pmin is 'less-than' + /// pmax. + bool is_valid() const; + protected: + P pmin_, pmax_; }; @@ -146,9 +152,20 @@ template <typename P> inline + bool + box_<P>::is_valid() const + { + typedef util::less<P> less_t; + static const less_t op = less_t(); + return op(pmin_, pmax_); + } + + template <typename P> + inline P box_<P>::pmin() const { + mln_precondition(is_valid()); return pmin_; } @@ -165,6 +182,7 @@ P box_<P>::pmax() const { + mln_precondition(is_valid()); return pmax_; } @@ -188,7 +206,7 @@ : pmin_(pmin), pmax_(pmax) { - + mln_precondition(is_valid()); } template <typename P> @@ -207,6 +225,7 @@ metal::bool_<(dim == 2)>::check(); pmin_ = literal::origin; pmax_ = P(nrows - 1, ncols - 1); + mln_postcondition(is_valid()); } template <typename P> @@ -216,6 +235,7 @@ metal::bool_<(dim == 3)>::check(); pmin_ = literal::origin; pmax_ = P(nslis - 1, nrows - 1, ncols - 1); + mln_postcondition(is_valid()); } template <typename P> @@ -223,6 +243,7 @@ bool box_<P>::has(const P& p) const { + mln_precondition(is_valid()); for (unsigned i = 0; i < P::dim; ++i) if (p[i] < pmin_[i] || p[i] > pmax_[i]) return false; @@ -234,11 +255,13 @@ void box_<P>::enlarge(unsigned b) { + mln_precondition(is_valid()); for (unsigned i = 0; i < P::dim; ++i) { pmin_[i] -= b; pmax_[i] += b; } + mln_postcondition(is_valid()); } @@ -247,6 +270,7 @@ box_<P> box_<P>::to_larger(unsigned b) const { + mln_precondition(is_valid()); box_<P> tmp(*this); for (unsigned i = 0; i < P::dim; ++i) @@ -254,6 +278,7 @@ tmp.pmin_[i] -= b; tmp.pmax_[i] += b; } + mln_postcondition(tmp.is_valid()); return tmp; } @@ -261,6 +286,7 @@ inline std::ostream& operator<<(std::ostream& ostr, const box_<P>& b) { + mln_precondition(b.is_valid()); return ostr << "[" << b.pmin() << ".." << b.pmax() << ']'; } Index: mln/core/concept/point_site.hh --- mln/core/concept/point_site.hh (revision 2024) +++ mln/core/concept/point_site.hh (working copy) @@ -1,4 +1,4 @@ -// Copyright (C) 2007 EPITA Research and Development Laboratory +// Copyright (C) 2007, 2008 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 @@ -145,30 +145,6 @@ bool operator==(const Point_Site<L>& lhs, const Point_Site<R>& rhs); - /*! \brief Ordering "less than" comparison between a couple of - * point sites \p lhs and \p rhs. - * - * \param[in] lhs A first point site. - * \param[in] rhs A second point site. - * - * This test is based on a lexicographical ordering over coordinates. - * - * \warning In the general case this ordering relationship is \em - * not bound to the way of browsing a domain with a forward point - * iterator. - * - * \pre Both \p lhs and \p rhs have to be defined on the same - * topology; otherwise this test does not compile. - * - * \return True if \p lhs is before \p rhs in the sense of the - * coordinates lexicographic comparison, otherwise false. - * - * \relates mln::Point_Site - */ - template <typename L, typename R> - bool operator<(const Point_Site<L>& lhs, const Point_Site<R>& rhs); - - /*! \brief Difference between a couple of point site \p lhs and \p * rhs. * @@ -281,22 +257,6 @@ template <typename L, typename R> inline - bool operator<(const Point_Site<L>& lhs_, const Point_Site<R>& rhs_) - { - mln::metal::bool_<(int(L::dim) == int(R::dim))>::check(); - const L& lhs = exact(lhs_); - const R& rhs = exact(rhs_); - for (unsigned i = 0; i < L::dim; ++i) - { - if (lhs[i] == rhs[i]) - continue; - return lhs[i] < rhs[i]; - } - return false; - } - - template <typename L, typename R> - inline mln_dpoint(L) // FIXME: promote! operator-(const Point_Site<L>& lhs_, const Point_Site<R>& rhs_) { Index: mln/algebra/vec.hh --- mln/algebra/vec.hh (revision 2024) +++ mln/algebra/vec.hh (working copy) @@ -1,4 +1,4 @@ -// Copyright (C) 2006 EPITA Research and Development Laboratory +// Copyright (C) 2006, 2008 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 @@ -42,6 +42,7 @@ # include <mln/trait/all.hh> # include <mln/trait/value_.hh> # include <mln/fun/i2v/all_to.hh> +# include <mln/util/less_than.hh> # include <mln/debug/format.hh> # include <mln/value/ops.hh> @@ -63,6 +64,19 @@ template <unsigned d, typename C> struct h_vec; + namespace util + { + + template <unsigned n, typename Tl, typename Tr> + struct less_than< algebra::vec<n,Tl>, + algebra::vec<n,Tr> > + { + bool operator()(const algebra::vec<n,Tl>& lhs, + const algebra::vec<n,Tr>& rhs) const; + }; + + } // end of namespace mln::util + namespace trait { @@ -326,9 +340,16 @@ vec<3, mln_trait_op_times(T,U)> // FIXME: Sum of product... vprod(const vec<3, T>& lhs, const vec<3, U>& rhs); + } // end of namespace mln::algebra + + + # ifndef MLN_INCLUDE_ONLY + namespace algebra + { + template <unsigned n, typename T> inline vec<n,T>::vec() @@ -561,10 +582,33 @@ return tmp; } -# endif // MLN_INCLUDE_ONLY } // end of namespace mln::algebra + + namespace util + { + + template <unsigned n, typename Tl, typename Tr> + bool + less_than< algebra::vec<n,Tl>, + algebra::vec<n,Tr> >::operator()(const algebra::vec<n,Tl>& lhs, + const algebra::vec<n,Tr>& rhs) const + { + for (unsigned i = 0; i < n; ++i) + { + if (lhs[i] == rhs[i]) + continue; + return lhs[i] < rhs[i]; + } + return false; + } + + } // end of namespace mln::util + + +# endif // MLN_INCLUDE_ONLY + } // end of namespace mln Index: mln/util/set.hh --- mln/util/set.hh (revision 2024) +++ mln/util/set.hh (working copy) @@ -42,6 +42,7 @@ # include <iostream> # include <mln/core/contract.hh> +# include <mln/util/less.hh> namespace mln @@ -66,7 +67,12 @@ * The parameter \c T is the element type, which shall not be * const-qualified. * + * The unicity of set elements is handled by the mln::util::less + * function-object. + * * \todo Add a remove method. + * + * \see mln::util::less */ template <typename T> class set @@ -174,7 +180,7 @@ * * This structure is always up-to-date w.r.t. the set contents. */ - mutable std::set<T> s_; + mutable std::set< T, mln::util::less<T> > s_; /*! \brief Freeze the contents of the set (update \a v_ from \a Index: mln/util/less.hh --- mln/util/less.hh (revision 0) +++ mln/util/less.hh (revision 0) @@ -0,0 +1,80 @@ +// Copyright (C) 2008 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_UTIL_LESS_HH +# define MLN_UTIL_LESS_HH + +/*! \file mln/util/less.hh + * + * \brief Definition of a "less" function-object with one type (T). + * + * \todo Use mln_trait_op_less as return type (?) + */ + +# include <mln/util/less_than.hh> + + +namespace mln +{ + + namespace util + { + + /*! \brief Function-object to define a less-than comparison + * between a couple of objects with the same type \p T. + * + * This implementation relies on mln::util::less_than so prefer to + * overload mln::util::less_than (because it is more general). + * + * \see mln::util::less_than + */ + template <typename T> + struct less + { + bool operator()(const T& lhs, const T& rhs) const; + }; + + +# ifndef MLN_INCLUDE_ONLY + + template <typename T> + inline + bool + less<T>::operator()(const T& lhs, const T& rhs) const + { + static const less_than<T,T> lt_ = less_than<T,T>(); + return lt_(lhs, rhs); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::util + +} // end of namespace mln + + +#endif // ! MLN_UTIL_LESS_HH Index: mln/util/less_than.hh --- mln/util/less_than.hh (revision 0) +++ mln/util/less_than.hh (revision 0) @@ -0,0 +1,81 @@ +// Copyright (C) 2008 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_UTIL_LESS_THAN_HH +# define MLN_UTIL_LESS_THAN_HH + +/*! \file mln/util/less_than.hh + * + * \brief Definition of a "less-than" function-object with a couple of + * types (L and R). + * + * \todo Use mln_trait_op_less as return type (?) + */ + + +namespace mln +{ + + namespace util + { + + /*! \brief Function-object to define a less-than comparison + * between a couple of objects respectively with types \p L and + * \p R. + * + * This is a default implementation that relies on 'operator<'. + * It shall be overloaded for types that do not have such an + * operator when those types are involved with an ordered + * container. + * + * \see mln::util::less_than + */ + template <typename L, typename R> + struct less_than + { + bool operator()(const L& lhs, const R& rhs) const; + }; + + +# ifndef MLN_INCLUDE_ONLY + + template <typename L, typename R> + inline + bool + less_than<L,R>::operator()(const L& lhs, const R& rhs) const + { + return lhs < rhs; + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::util + +} // end of namespace mln + + +#endif // ! MLN_UTIL_LESS_THAN_HH
participants (1)
-
Thierry Geraud