URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2007-10-22 Guillaume Duhamel <guillaume.duhamel(a)lrde.epita.fr>
Add a structure of tree in sandbox.
* abr.cc: New test file for mln::util::abr.
* abr.hh: New structure for tree.
---
abr.cc | 49 ++++++++++++++++++++
abr.hh | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 207 insertions(+)
Index: trunk/milena/sandbox/duhamel/abr.hh
===================================================================
--- trunk/milena/sandbox/duhamel/abr.hh (revision 0)
+++ trunk/milena/sandbox/duhamel/abr.hh (revision 1362)
@@ -0,0 +1,158 @@
+// 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.
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+#ifndef MLN_UTIL_ABR_HH
+# define MLN_UTIL_ABR_HH
+
+# include <list>
+# include <iostream>
+
+/*!
+ * \file mln/util/abr.hh
+ *
+ * \brief Definition of a generic general tree.
+ *
+ *
+ */
+
+
+namespace mln
+{
+
+ namespace util
+ {
+
+ template <typename T>
+ struct s_abr
+ {
+ s_abr ();
+ s_abr (const T& elt);
+
+ void add_son (const T& elt);
+ void print_rec (int n) const;
+ void print (void) const;
+ int search_rec(s_abr<T>** res, const T& elt);
+ s_abr<T>* search(const T& elt);
+
+ const T& elt_;
+ s_abr<T>* father_;
+ std::list< s_abr<T>* > sons_;
+ };
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename T>
+ s_abr<T>::s_abr ()
+ : elt_ (0),
+ father_ (0)
+ {
+ }
+
+ template <typename T>
+ s_abr<T>::s_abr (const T& elt)
+ : elt_ (elt),
+ father_ (0)
+ {
+ }
+
+
+ template <typename T>
+ void
+ s_abr<T>::add_son (const T& elt)
+ {
+
+ s_abr<T>* s = new s_abr<T> (elt);
+
+ s->father_ = this;
+ this->sons_.push_back (s);
+ }
+
+ template <typename T>
+ void
+ s_abr<T>::print_rec (int n) const
+ {
+ std::cout << this->elt_ << std::endl;
+ typename std::list<s_abr<T>* >::const_iterator it =
this->sons_.begin ();
+ for (; it != this->sons_.end (); ++it)
+ {
+ for (int i = 0; i < n; ++i)
+ std::cout << " ";
+ (**it).print_rec (n + 1);
+ }
+ }
+
+ template <typename T>
+ void
+ s_abr<T>::print (void) const
+ {
+ this->print_rec(1);
+ }
+
+
+ template <typename T>
+ int
+ s_abr<T>::search_rec(s_abr<T>** res, const T& elt)
+ {
+ if (elt == this->elt_)
+ {
+ *res = this;
+ return 1;
+ }
+ else
+ {
+ typename std::list<s_abr<T>* >::iterator it = this->sons_.begin ();
+ for (; it != this->sons_.end (); ++it)
+ {
+ if ((**it).search_rec (res, elt))
+ return 1;
+ }
+ }
+ return 0;
+ }
+
+ template <typename T>
+ s_abr<T>*
+ s_abr<T>::search(const T& elt)
+ {
+ s_abr<T>* res = 0;
+
+ if (search_rec (&res, elt))
+ return res;
+ std::cerr << elt << " not found" << std::endl;
+ return 0;
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::util
+
+
+} // end of namespace mln
+
+
+#endif // !MLN_UTIL_ABR_HH
Index: trunk/milena/sandbox/duhamel/abr.cc
===================================================================
--- trunk/milena/sandbox/duhamel/abr.cc (revision 0)
+++ trunk/milena/sandbox/duhamel/abr.cc (revision 1362)
@@ -0,0 +1,49 @@
+// 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.
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+/*!
+ * \file tests/abr.cc
+ *
+ * \brief test of mln::util::abr
+ *
+ */
+
+#include "abr.hh"
+
+int main (void)
+{
+ using namespace mln;
+ util::s_abr<unsigned> abr (1);
+
+ abr.add_son (2);
+ abr.add_son (3);
+ abr.print ();
+ util::s_abr<unsigned>* abr2 = abr.search (2);
+ abr2->add_son (4);
+ abr2->add_son (5);
+ abr.print ();
+}