1306: Use more literals.

https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> Use more literals. * tests/value_int_u8.cc: Augment. * mln/literal/origin.hh: New. * tests/point2d.cc (zero): Replace by... (origin): ...this. * mln/core/point.hh, * mln/core/dpoint.hh (operator=): Handle literals. Update. * mln/core/ops.hh: Add FIXMEs. * mln/literal/all.hh: Update. * mln/literal/zero.hh: Fix typo in doc. * mln/value/int_s.hh: Handle literals. * mln/value/int_u.hh (zero, one): Remove variables. * mln/value/concept/scalar.hh, * mln/make/w_window_line.hh, * mln/morpho/hit_or_miss.hh: Update with literals. mln/core/dpoint.hh | 32 ++++++++++++++++------- mln/core/ops.hh | 2 + mln/core/point.hh | 45 ++++++++++++++++++++++++++++++--- mln/literal/all.hh | 2 + mln/literal/origin.hh | 60 ++++++++++++++++++++++++++++++++++++++++++++ mln/literal/zero.hh | 1 mln/make/w_window_line.hh | 3 +- mln/morpho/hit_or_miss.hh | 9 +++--- mln/value/concept/scalar.hh | 5 ++- mln/value/int_s.hh | 39 +++++++++++++++++++++++++++- mln/value/int_u.hh | 12 -------- tests/point2d.cc | 4 +- tests/value_int_u8.cc | 6 +++- 13 files changed, 184 insertions(+), 36 deletions(-) Index: tests/value_int_u8.cc --- tests/value_int_u8.cc (revision 1305) +++ tests/value_int_u8.cc (working copy) @@ -159,7 +159,11 @@ mln_assertion(-i = -234.f); // FIXME: new better version mln_assertion(i * -2 != 0.f); - std::cout << (i) << " " << (i * -2) << " " << (-2 * int(i)) << std::endl; + std::cout << (i) << " * -2 = " + << (i * -2) << ' ' + << (-2 * i) << ' ' + << (-2 * int(i)) + << std::endl; } // Multiplication Index: tests/point2d.cc --- tests/point2d.cc (revision 1305) +++ tests/point2d.cc (working copy) @@ -65,9 +65,9 @@ } { point2d O(0,0); - point2d O_ = literal::zero; + point2d O_ = literal::origin; mln_assertion(O_ = O); - mln_assertion(O = literal::zero); + mln_assertion(O = literal::origin); } q.set_all(0); Index: mln/core/point.hh --- mln/core/point.hh (revision 1305) +++ mln/core/point.hh (working copy) @@ -51,6 +51,7 @@ namespace literal { struct zero_t; struct one_t; + struct origin_t; } /// \} @@ -115,9 +116,14 @@ point_(C sli, C row, C col); /// \} - /// \{ Constructors with literals. + /// \{ Constructors/assignments with literals. + point_(const literal::origin_t&); + point_<M,C>& operator=(const literal::origin_t&); + // Works only in 1D: point_(const literal::zero_t&); - point_(const literal::one_t&); // Works only in 1D. + point_<M,C>& operator=(const literal::zero_t&); + point_(const literal::one_t&); + point_<M,C>& operator=(const literal::one_t&); /// \} /// Constructor; coordinates are set by function \p f. @@ -209,9 +215,33 @@ } template <typename M, typename C> - point_<M,C>::point_(const literal::zero_t&) + point_<M,C>::point_(const literal::origin_t&) + { + coord_.set_all(0); + } + + template <typename M, typename C> + point_<M,C>& + point_<M,C>::operator=(const literal::origin_t&) { coord_.set_all(0); + return *this; + } + + template <typename M, typename C> + point_<M,C>::point_(const literal::zero_t&) + { + metal::bool_<(dim = 1)>::check(); + coord_[0] = 1; + } + + template <typename M, typename C> + point_<M,C>& + point_<M,C>::operator=(const literal::zero_t&) + { + metal::bool_<(dim = 1)>::check(); + coord_[0] = 1; + return *this; } template <typename M, typename C> @@ -222,6 +252,15 @@ } template <typename M, typename C> + point_<M,C>& + point_<M,C>::operator=(const literal::one_t&) + { + metal::bool_<(dim = 1)>::check(); + coord_[0] = 1; + return *this; + } + + template <typename M, typename C> void point_<M,C>::set_all(C c) { coord_.set_all(c); Index: mln/core/ops.hh --- mln/core/ops.hh (revision 1305) +++ mln/core/ops.hh (working copy) @@ -174,6 +174,8 @@ mln_trait_op_times(O, int) operator*(int lhs, const Object<O>& rhs) { + // FIXME HERE: Activate: std::cout << "call(int * Object)" << std::endl; + // FIXME HERE: Change below to '* value::scalar(lhs)': return exact(rhs) * lhs; } Index: mln/core/dpoint.hh --- mln/core/dpoint.hh (revision 1305) +++ mln/core/dpoint.hh (working copy) @@ -98,9 +98,12 @@ dpoint_(C sli, C row, C col); /// \} - /// \{ Constructors with literals. + /// \{ Constructors/assignments with literals. dpoint_(const literal::zero_t&); - dpoint_(const literal::one_t&); // Works only in 1D. + dpoint_<M,C>& operator=(const literal::zero_t&); + // Works only in 1D: + dpoint_(const literal::one_t&); + dpoint_<M,C>& operator=(const literal::one_t&); /// \} /// Constructor; coordinates are set by function \p f. @@ -110,9 +113,6 @@ /// Set all coordinates to the value \p c. void set_all(C c); - /// Zero delta-point. - static const dpoint_<M,C> zero; - /// Conversion towards a metal::vec. template <typename Q> operator metal::vec<M::dim, Q>() const; @@ -177,6 +177,14 @@ } template <typename M, typename C> + dpoint_<M,C>& + dpoint_<M,C>::operator=(const literal::zero_t&) + { + coord_.set_all(0); + return *this; + } + + template <typename M, typename C> dpoint_<M,C>::dpoint_(const literal::one_t&) { metal::bool_<(dim = 1)>::check(); @@ -184,6 +192,15 @@ } template <typename M, typename C> + dpoint_<M,C>& + dpoint_<M,C>::operator=(const literal::one_t&) + { + metal::bool_<(dim = 1)>::check(); + coord_[0] = 1; + return *this; + } + + template <typename M, typename C> template <typename F> dpoint_<M,C>::dpoint_(const Function_i2v<F>& f_) { @@ -201,10 +218,6 @@ } template <typename M, typename C> - const dpoint_<M,C> - dpoint_<M,C>::zero = all(0); - - template <typename M, typename C> template <typename Q> dpoint_<M,C>::operator metal::vec<M::dim, Q> () const { @@ -218,7 +231,6 @@ return coord_; } - # endif // ! MLN_INCLUDE_ONLY } // end of namespace mln Index: mln/literal/origin.hh --- mln/literal/origin.hh (revision 0) +++ mln/literal/origin.hh (revision 0) @@ -0,0 +1,60 @@ +// 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_LITERAL_ORIGIN_HH +# define MLN_LITERAL_ORIGIN_HH + +/*! \file mln/literal/origin.hh + * + * \brief Definition of the literal of mln::origin. + */ + +# include <mln/core/concept/literal.hh> + + +namespace mln +{ + + namespace literal + { + + /// Type of literal origin. + struct origin_t : public Literal<origin_t> + { + }; + + + /// Literal origin. + static origin_t origin = origin_t(); + + + } // end of namespace mln::literal + +} // end of namespace mln + + +#endif // ! MLN_LITERAL_ORIGIN_HH Index: mln/literal/all.hh --- mln/literal/all.hh (revision 1305) +++ mln/literal/all.hh (working copy) @@ -45,6 +45,8 @@ # include <mln/literal/zero.hh> # include <mln/literal/one.hh> +# include <mln/literal/origin.hh> + // FIXME: Add: // # include <mln/literal/white.hh> // # include <mln/literal/black.hh> Index: mln/literal/zero.hh --- mln/literal/zero.hh (revision 1305) +++ mln/literal/zero.hh (working copy) @@ -29,6 +29,7 @@ # define MLN_LITERAL_ZERO_HH /*! \file mln/literal/zero.hh + * * \brief Definition of the literal of mln::zero. * * \todo Macro-ification of similar code (when no trivial conversion ops are involved). Index: mln/value/int_s.hh --- mln/value/int_s.hh (revision 1305) +++ mln/value/int_s.hh (working copy) @@ -46,8 +46,10 @@ { - // Fwd decl. + /// \{ Fwd decls. namespace value { template <unsigned n> struct int_s; } + namespace literal { struct zero_t; struct one_t; } + /// \} @@ -110,6 +112,13 @@ /// Constructor from an integer. int_s(int i); + /// \{ Constructors/assignments with literals. + int_s(const literal::zero_t&); + int_s& operator=(const literal::zero_t&); + int_s(const literal::one_t&); + int_s& operator=(const literal::one_t&); + /// \} + /// Conversion to an integer. operator int() const; @@ -195,6 +204,34 @@ } template <unsigned n> + int_s<n>::int_s(const literal::zero_t&) + { + this->v_ = 0; + } + + template <unsigned n> + int_s<n>& + int_s<n>::operator=(const literal::zero_t&) + { + this->v_ = 0; + return *this; + } + + template <unsigned n> + int_s<n>::int_s(const literal::one_t&) + { + this->v_ = 1; + } + + template <unsigned n> + int_s<n>& + int_s<n>::operator=(const literal::one_t&) + { + this->v_ = 1; + return *this; + } + + template <unsigned n> int_s<n> int_s<n>::operator-() const { Index: mln/value/int_u.hh --- mln/value/int_u.hh (revision 1305) +++ mln/value/int_u.hh (working copy) @@ -148,12 +148,6 @@ /// Assignment from an integer. int_u<n>& operator=(int i); - - /// Zero value. - static const int_u<n> zero; - - /// Unit value. - static const int_u<n> one; }; @@ -256,12 +250,6 @@ } template <unsigned n> - const int_u<n> int_u<n>::zero = 0; - - template <unsigned n> - const int_u<n> int_u<n>::one = 1; - - template <unsigned n> std::ostream& operator<<(std::ostream& ostr, const int_u<n>& i) { // FIXME: This code could be factored for almost every Value<*>... Index: mln/value/concept/scalar.hh --- mln/value/concept/scalar.hh (revision 1305) +++ mln/value/concept/scalar.hh (working copy) @@ -34,6 +34,7 @@ */ # include <mln/core/concept/value.hh> +# include <mln/literal/one.hh> namespace mln @@ -106,14 +107,14 @@ template <typename S> S& operator++(value::Scalar<S>& rhs) { - exact(rhs) += 1; // FIXME: literal::one? + exact(rhs) += S(literal::one); return exact(rhs); } template <typename S> S& operator--(value::Scalar<S>& rhs) { - exact(rhs) -= 1; // FIXME: literal::one? + exact(rhs) -= S(literal::one); return exact(rhs); } Index: mln/make/w_window_line.hh --- mln/make/w_window_line.hh (revision 1305) +++ mln/make/w_window_line.hh (working copy) @@ -34,6 +34,7 @@ */ # include <mln/core/w_window.hh> +# include <mln/literal/zero.hh> namespace mln @@ -62,7 +63,7 @@ { mln_precondition(L % 2 = 1); mln::w_window<D,W> w_win; - D dp = D::zero; + D dp = literal::zero; for (unsigned i = 0; i < L; ++i) { dp[D::dim - 1] = i - L / 2; Index: mln/morpho/hit_or_miss.hh --- mln/morpho/hit_or_miss.hh (revision 1305) +++ mln/morpho/hit_or_miss.hh (working copy) @@ -38,6 +38,7 @@ # include <mln/morpho/includes.hh> # include <mln/pw/all.hh> # include <mln/fun/p2v/ternary.hh> +# include <mln/literal/zero.hh> namespace mln @@ -153,7 +154,7 @@ fun::p2v::ternary(pw::value(input) = pw::value(ero_fg) && pw::value(dil_bg) < pw::value(input), pw::value(input) - pw::value(dil_bg), - pw::cst(V::zero))); + pw::cst( V(literal::zero) ))); // FIXME: Replace 'pw::cst(V::zero)' by 'pw::cst(V(literal::zero))' // FIXME: and then by 'literal::zero'! @@ -167,10 +168,10 @@ fun::p2v::ternary(pw::value(input) = pw::value(dil_fg) && pw::value(ero_bg) > pw::value(input), pw::value(ero_bg) - pw::value(input), - pw::cst(V::zero))); + pw::cst( V(literal::zero) ))); } else - level::fill(output, V::zero); + level::fill(output, V(literal::zero)); } else // Unconstrained: UHMT. { @@ -180,7 +181,7 @@ level::fill(output, fun::p2v::ternary(pw::value(dil) < pw::value(ero), pw::value(ero) - pw::value(dil), - pw::cst(V::zero))); + pw::cst( V(literal::zero) ))); } }
participants (1)
-
Thierry Geraud