URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2008-01-21 Matthieu Garrigues <garrigues(a)lrde.epita.fr>
Add accu::rank.
* mln/accu/rank.hh: New, accumulator for the rank filters.
* mln/accu/rank_bool.hh: New. Specialisation of accu::rank<bool>.
* tests/accu/rank.cc: New.
---
mln/accu/rank.hh | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++
mln/accu/rank_bool.hh | 133 +++++++++++++++++++++++++++++++++++++
tests/accu/rank.cc | 55 +++++++++++++++
3 files changed, 364 insertions(+)
Index: trunk/milena/tests/accu/rank.cc
===================================================================
--- trunk/milena/tests/accu/rank.cc (revision 0)
+++ trunk/milena/tests/accu/rank.cc (revision 1675)
@@ -0,0 +1,55 @@
+// 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
+// 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 tests/accu/rank.cc
+ *
+ * \brief Tests on mln::accu::rank.
+ */
+
+#include <mln/accu/rank.hh>
+
+int main()
+{
+ using namespace mln;
+
+ accu::rank_<int> accu(3, 5);
+
+ accu.take(3);
+ accu.take(1);
+ accu.take(4);
+ accu.take(5);
+ accu.take(2);
+ mln_assertion(accu.to_result() == 4);
+
+ accu::rank_<bool> accu_bool(1, 5);
+ accu_bool.take(true);
+ accu_bool.take(true);
+ accu_bool.take(true);
+ accu_bool.take(true);
+ accu_bool.take(false);
+ mln_assertion(accu_bool == true);
+}
Index: trunk/milena/mln/accu/rank.hh
===================================================================
--- trunk/milena/mln/accu/rank.hh (revision 0)
+++ trunk/milena/mln/accu/rank.hh (revision 1675)
@@ -0,0 +1,176 @@
+// 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
+// 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_RANK_HH
+# define MLN_ACCU_RANK_HH
+
+/*! \file mln/accu/rank.hh
+ *
+ * \brief Define an rank accumulator.
+ */
+
+# include <vector>
+# include <mln/accu/internal/base.hh>
+# include <mln/core/concept/meta_accumulator.hh>
+# include <mln/trait/value_.hh>
+# include <mln/util/pix.hh>
+# include <mln/core/inplace.hh>
+
+namespace mln
+{
+
+ namespace accu
+ {
+
+
+ /*! \brief Generic rank accumulator class.
+ *
+ * The parameter \c T is the type of values.
+ */
+ template <typename T>
+ struct rank_ : public mln::accu::internal::base_< T, rank_<T> >
+ {
+ typedef T argument;
+ typedef T result;
+
+ rank_(unsigned k, unsigned n);
+
+ void init();
+ void take_as_init(const argument& t);
+ void take(const argument& t);
+ void take(const rank_<T>& other);
+ void sort();
+
+ T to_result() const;
+
+ protected:
+
+ std::vector<T> elts_;
+ bool is_sorted_;
+ unsigned k_; // 0 <= k_ < n
+ unsigned n_;
+ };
+
+
+ template <typename I> struct rank_< util::pix<I> >;
+
+
+ /*!
+ * \brief Meta accumulator for rank.
+ */
+ struct rank : public Meta_Accumulator< rank >
+ {
+ template <typename T>
+ struct with
+ {
+ typedef rank_<T> ret;
+ };
+ };
+
+
+
+
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename T>
+ inline
+ rank_<T>::rank_(unsigned k, unsigned n)
+ : k_(k),
+ n_(n),
+ is_sorted_(false)
+ {
+ mln_assertion(k_ < n_);
+ init();
+ }
+
+ template <typename T>
+ inline
+ void
+ rank_<T>::init()
+ {
+ }
+
+ template <typename T>
+ inline
+ void rank_<T>::take_as_init(const argument& t)
+ {
+ elts_.push_back(t);
+ is_sorted_ = false;
+ }
+
+ template <typename T>
+ inline
+ void rank_<T>::take(const argument& t)
+ {
+ elts_.push_back(t);
+ is_sorted_ = false;
+ }
+
+ template <typename T>
+ inline
+ void
+ rank_<T>::take(const rank_<T>& other)
+ {
+ elts_.insert(elts_.end(),
+ other.elts_.begin(),
+ other.elts_.end());
+ is_sorted_ = false;
+ }
+
+ template <typename T>
+ inline
+ T
+ rank_<T>::to_result() const
+ {
+ mln_assertion(n_ == elts_.size());
+ inplace(*this).sort();
+ return elts_[k_];
+ }
+
+ template <typename T>
+ inline
+ void
+ rank_<T>::sort()
+ {
+ if (!is_sorted_)
+ {
+ is_sorted_ = true;
+ std::sort(elts_.begin(), elts_.end());
+ }
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::accu
+
+} // end of namespace mln
+
+#include <mln/accu/rank_bool.hh>
+
+#endif // ! MLN_ACCU_RANK_HH
Index: trunk/milena/mln/accu/rank_bool.hh
===================================================================
--- trunk/milena/mln/accu/rank_bool.hh (revision 0)
+++ trunk/milena/mln/accu/rank_bool.hh (revision 1675)
@@ -0,0 +1,133 @@
+// 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
+// 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_RANK_BOOL_HH
+# define MLN_ACCU_RANK_BOOL_HH
+
+/*! \file mln/accu/rank_bool.hh
+ *
+ * \brief Define an rank accumulator.
+ */
+
+# include <vector>
+# include <mln/accu/internal/base.hh>
+# include <mln/core/concept/meta_accumulator.hh>
+# include <mln/trait/value_.hh>
+# include <mln/util/pix.hh>
+# include <mln/core/inplace.hh>
+
+namespace mln
+{
+
+ namespace accu
+ {
+
+ // Fwd declaration.
+ template <typename T> struct rank_;
+
+ /*! \brief rank accumulator class for boolean.
+ *
+ */
+ template <>
+ struct rank_<bool> : public mln::accu::internal::base_< bool,
rank_<bool> >
+ {
+ typedef bool argument;
+ typedef bool result;
+
+ rank_(unsigned k, unsigned n);
+
+ void init();
+ void take_as_init(const argument& t);
+ void take(const argument& t);
+ void take(const rank_<bool>& other);
+
+ bool to_result() const;
+
+ protected:
+ unsigned nfalse_;
+ unsigned k_; // 0 <= k_ < n
+ unsigned n_;
+ };
+
+# ifndef MLN_INCLUDE_ONLY
+
+ inline
+ rank_<bool>::rank_(unsigned k, unsigned n)
+ : k_(k),
+ n_(n),
+ nfalse_(0)
+ {
+ mln_assertion(k_ < n_);
+ init();
+ }
+
+
+ inline
+ void
+ rank_<bool>::init()
+ {
+ }
+
+
+ inline
+ void rank_<bool>::take_as_init(const argument& t)
+ {
+ nfalse_ += !t;
+ }
+
+
+ inline
+ void rank_<bool>::take(const argument& t)
+ {
+ nfalse_ += !t;
+ }
+
+
+ inline
+ void
+ rank_<bool>::take(const rank_<bool>& other)
+ {
+ nfalse_ += other.nfalse_;
+ }
+
+
+ inline
+ bool
+ rank_<bool>::to_result() const
+ {
+ mln_assertion(nfalse_ <= n_);
+ return k_ >= nfalse_;
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::accu
+
+} // end of namespace mln
+
+
+#endif // ! MLN_ACCU_RANK_BOOL_HH