Commit b5211ced authored by Thierry Geraud's avatar Thierry Geraud
Browse files

Introduce accumulators and point-wise operations in mln.

	* tests/pw_value.cc: New.
	* mln/estim/mean.hh: New.
	* mln/fun/chess.hh (result): New.
	* mln/fun/ops.hh: New.
	* mln/fun/val.hh: New.
	* mln/fun/pw_value.hh: New.
	* mln/fun/var.hh: New.
	* mln/core/concept/doc/accumulator.hh: New.
	* mln/core/concept/accumulator.hh: New.
	* mln/core/concept/function.hh (Function_i2v): New.
	* mln/core/macros.hh (mln_result): New.
	* mln/arith: New.
	* mln/arith/plus.hh: New.
	* mln/level/apply.hh: New.
	* mln/level/transform.hh: New.
	* mln/accu/counter.hh: New.
	* mln/accu/mean.hh: New.
	* mln/value/kind.hh: New.
	* mln/value/props.hh (mln_kind, kind): New.

	* mln/estim/count.hh: Remove; useless.
	* mln/fun/all.hh (all): Change inheritance.
	* mln/core/point.hh (point_): Strenghten sigs.
	* mln/core/dpoint.hh (dpoint_): Likewise.
	* mln/accu/histo.hh: Add inheritance.
	* mln/accu/median.hh: Likewise.


git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@1015 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent 618f8aa5
2007-07-17 Thierry Geraud <thierry.geraud@lrde.epita.fr>
Introduce accumulators and point-wise operations in mln.
* tests/pw_value.cc: New.
* mln/estim/mean.hh: New.
* mln/fun/chess.hh (result): New.
* mln/fun/ops.hh: New.
* mln/fun/val.hh: New.
* mln/fun/pw_value.hh: New.
* mln/fun/var.hh: New.
* mln/core/concept/doc/accumulator.hh: New.
* mln/core/concept/accumulator.hh: New.
* mln/core/concept/function.hh (Function_i2v): New.
* mln/core/macros.hh (mln_result): New.
* mln/arith: New.
* mln/arith/plus.hh: New.
* mln/level/apply.hh: New.
* mln/level/transform.hh: New.
* mln/accu/counter.hh: New.
* mln/accu/mean.hh: New.
* mln/value/kind.hh: New.
* mln/value/props.hh (mln_kind, kind): New.
* mln/estim/count.hh: Remove; useless.
* mln/fun/all.hh (all): Change inheritance.
* mln/core/point.hh (point_): Strenghten sigs.
* mln/core/dpoint.hh (dpoint_): Likewise.
* mln/accu/histo.hh: Add inheritance.
* mln/accu/median.hh: Likewise.
2007-07-13 Thierry Geraud <thierry.geraud@lrde.epita.fr>
Fix make doc, handle todos, and pass tests with g++-2.95.
......
// 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_ACCU_COUNTER_HH
# define MLN_ACCU_COUNTER_HH
/*! \file mln/accu/counter.hh
*
* \brief Define an accumulator that counts.
*/
# include <mln/core/concept/accumulator.hh>
namespace mln
{
namespace accu
{
/*! Generic counter accumulator class.
*/
template <typename V>
struct counter : public Accumulator< counter<V> >
{
typedef V value;
counter();
void take(const value&);
void init();
operator std::size_t() const;
std::size_t to_value() const;
protected:
std::size_t count_;
};
# ifndef MLN_INCLUDE_ONLY
template <typename V>
counter<V>::counter()
{
init();
}
template <typename V>
void
counter<V>::take(const value&)
{
++count_;
}
template <typename V>
void
counter<V>::init()
{
count_ = 0;
}
template <typename V>
counter<V>::operator std::size_t() const
{
return to_value();
}
template <typename V>
std::size_t
counter<V>::to_value() const
{
return count_;
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::accu
} // end of namespace mln
#endif // ! MLN_ACCU_COUNTER_HH
......@@ -37,6 +37,7 @@
# include <algorithm>
# include <mln/core/concept/value_set.hh>
# include <mln/core/concept/accumulator.hh>
# include <mln/value/set.hh>
......@@ -50,7 +51,7 @@ namespace mln
/*! Generic histogram class over a value set with type \c S.
*/
template <typename S>
struct histo_on_set
struct histo_on_set : public Accumulator< histo_on_set<S> >
{
histo_on_set(const Value_Set<S>& s);
......@@ -84,6 +85,9 @@ namespace mln
/*! Generic histogram class over the set of values of type \c T.
*
* \todo Inheritance is badly formed since this concrete class
* derives from another concrete class.
*/
template <typename T>
struct histo_on_type : public histo_on_set< value::set_<T> >
......
// 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_ACCU_MEAN_HH
# define MLN_ACCU_MEAN_HH
/*! \file mln/accu/mean.hh
*
* \brief Define an accumulator that computes a mean.
*/
# include <mln/core/concept/accumulator.hh>
namespace mln
{
namespace accu
{
/*! Generic mean accumulator class.
*
* Parameter \c V is the type of values that we sum. Parameter \c
* S is the type to store the sum of values; the default type of
* \c S is \c V. Parameter \c M is the type of the mean value;
* the default type of \c M is \c S.
*/
template <typename V, typename S = V, typename M = S>
struct mean : public Accumulator< mean<V,S,M> >
{
typedef V value;
mean();
void take(const value& v);
void init();
operator M() const;
M to_value() const;
protected:
std::size_t count_;
S sum_;
};
# ifndef MLN_INCLUDE_ONLY
template <typename V, typename S, typename M>
mean<V,S,M>::mean()
{
init();
}
template <typename V, typename S, typename M>
void mean<V,S,M>::take(const value& v)
{
++count_;
sum_ += v;
}
template <typename V, typename S, typename M>
void
mean<V,S,M>::init()
{
count_ = 0;
sum_ = 0;
}
template <typename V, typename S, typename M>
mean<V,S,M>::operator M() const
{
return to_value;
}
template <typename V, typename S, typename M>
M
mean<V,S,M>::to_value() const
{
return sum_ / count_;
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::accu
} // end of namespace mln
#endif // ! MLN_ACCU_MEAN_HH
......@@ -33,6 +33,7 @@
* \brief Define FIXME
*/
# include <mln/core/concept/accumulator.hh>
# include <mln/accu/histo.hh>
......@@ -47,7 +48,7 @@ namespace mln
* with type \c S.
*/
template <typename S>
struct median
struct median : public Accumulator< median<S> >
{
typedef mln_value(S) value;
......@@ -91,6 +92,11 @@ namespace mln
};
/*! Generic median class over the set of values of type \c T.
*
* \todo Inheritance is badly formed since this concrete class
* derives from another concrete class.
*/
template <typename T>
struct median_on : public median< value::set_<T> >
{
......
// 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_ARITH_PLUS_HH
# define MLN_ARITH_PLUS_HH
/*! \file mln/arith/plus.hh
*
* \brief Point-wise addition between images.
*/
# include <mln/core/concept/image.hh>
namespace mln
{
namespace arith
{
/*! Point-wise addition of images \p lhs and \p rhs.
*
* \param[in] lhs First operand image.
* \param[in] rhs Second operand image.
* \param[out] output The result image.
*
* \pre \p output.domain == \p lhs.domain == \p rhs.domain
*/
template <typename L, typename R, typename O>
void plus(const Image<L>& lhs, const Image<R>& rhs, Image<O>& output);
# ifndef MLN_INCLUDE_ONLY
template <typename L, typename R, typename O>
void plus(const Image<L>& lhs_, const Image<R>& rhs_, Image<O>& output_)
{
const L& lhs = exact(lhs_);
const R& rhs = exact(rhs_);
O& output = exact(output_);
mln_precondition(rhs.domain() == lhs.domain());
mln_precondition(output.domain() == lhs.domain());
mln_piter(I) p(output.domain());
for_all(p)
output(p) = lhs(p) + rhs(p);
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::arith
} // end of namespace mln
#endif // ! MLN_ARITH_PLUS_HH
// 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_CORE_CONCEPT_ACCUMULATOR_HH
# define MLN_CORE_CONCEPT_ACCUMULATOR_HH
/*! \file mln/core/concept/accumulator.hh
*
* \brief Definition of the concept of mln::Accumulator.
*/
# include <mln/core/concept/object.hh>
namespace mln
{
/*! \brief Base class for implementation of accumulators.
*
* The parameter \a E is the exact type.
*
* \see mln::doc::Accumulator for a complete documentation of this
* class contents.
*/
template <typename E>
struct Accumulator : public Object<E>
{
/*
typedef value;
void init();
void take(const value& v);
*/
protected:
Accumulator();
};
# ifndef MLN_INCLUDE_ONLY
template <typename E>
Accumulator<E>::Accumulator()
{
typedef mln_value(E) value;
void (E::*m1)() = & E::init;
m1 = 0;
void (E::*m2)(const value&) = & E::take;
m2 = 0;
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln
#endif // ! MLN_CORE_CONCEPT_ACCUMULATOR_HH
// 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.
/*! \file mln/core/concept/doc/accumulator.hh
* \brief This file documents the concept of mln::Accumulator.
*/
namespace mln
{
namespace doc
{
/*! \brief Documentation class for mln::Accumulator.
* \see mln::Accumulator
*/
template <typename E>
struct Accumulator
{
/// The value type of elements to accumulate.
typedef void value;
/// Initialize the accumulator.
void init();
/// Take into account a value \p v (an element).
void take(const value& v);
};
} // end of namespace mln::doc
} // end of namespace mln
......@@ -68,6 +68,18 @@ namespace mln
};
// Index -> Value.
/// Base class for implementation of function-objects from index to
/// value.
template <typename E>
struct Function_i2v : public Function_v2v<E>
{
protected:
Function_i2v();
};
// Point -> Value.
/// Base class for implementation of function-objects from point to
......@@ -110,6 +122,7 @@ namespace mln
template <typename E>
Function<E>::Function()
{
typedef mln_result(E) result;
}
template <typename E>
......@@ -117,6 +130,11 @@ namespace mln
{
}
template <typename E>
Function_i2v<E>::Function_i2v()
{
}
template <typename E>
Function_p2v<E>::Function_p2v()
{
......
......@@ -87,7 +87,7 @@ namespace mln
/*! \brief Constructor; coordinates are set by function \p f.
*/
template <typename F>
dpoint_(const Function<F>& f);
dpoint_(const Function_i2v<F>& f);
/*! \brief Set all coordinates to the value \p c.
*/
......@@ -124,7 +124,7 @@ namespace mln
template <unsigned n, typename C>
template <typename F>
dpoint_<n,C>::dpoint_(const Function<F>& f_)
dpoint_<n,C>::dpoint_(const Function_i2v<F>& f_)
{
const F& f = exact(f_);
for (unsigned i = 0; i < n; ++i)
......
......@@ -138,6 +138,9 @@
// r
/// Shortcut to access the result type associated to T.
# define mln_result(T) typename T::result
/// Shortcut to access the rvalue type associated to T.
# define mln_rvalue(T) typename T::rvalue
......
......@@ -87,7 +87,7 @@ namespace mln
/*! \brief Constructor; coordinates are set by function \p f.
*/
template <typename F>
point_(const Function<F>& f);
point_(const Function_i2v<F>& f);
/*! \brief Set all coordinates to the value \p c.
*/
......@@ -124,7 +124,7 @@ namespace mln
template <unsigned n, typename C>
template <typename F>
point_<n,C>::point_(const Function<F>& f_)
point_<n,C>::point_(const Function_i2v<F>& f_)
{
const F& f = exact(f_);
for (unsigned i = 0; i < n; ++i)
......
......@@ -25,14 +25,17 @@
// reasons why the executable file might be covered by the GNU General
// Public License.
#ifndef MLN_ESTIM_COUNT_HH
# define MLN_ESTIM_COUNT_HH
#ifndef MLN_ESTIM_MEAN_HH
# define MLN_ESTIM_MEAN_HH