milena r1371: Update of abr

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena ChangeLog: 2007-10-22 Guillaume Duhamel <guillaume.duhamel@lrde.epita.fr> Update of abr. * mln/util/abr.hh: Update with better coding style. * tests/abr.cc: Add assertion in this test. Sandbox. * sandbox/duhamel/abr.hh: Update. --- mln/util/abr.hh | 72 ++++++++++++++----------------------------------- sandbox/duhamel/abr.hh | 44 ++++++++++++++--------------- tests/abr.cc | 21 ++++++++------ 3 files changed, 56 insertions(+), 81 deletions(-) Index: trunk/milena/tests/abr.cc =================================================================== --- trunk/milena/tests/abr.cc (revision 1370) +++ trunk/milena/tests/abr.cc (revision 1371) @@ -33,22 +33,27 @@ */ #include <mln/util/abr.hh> +#include <mln/core/contract.hh> int main (void) { using namespace mln; + unsigned elt1 = 1; unsigned elt2 = 2; unsigned elt3 = 3; unsigned elt4 = 4; unsigned elt5 = 5; - util::s_abr<unsigned> abr (elt1); - abr.add_son (elt2); - abr.add_son (elt3); - abr.print (); - util::s_abr<unsigned>* abr2 = abr.search (elt2); - abr2->add_son (elt4); - abr2->add_son (elt5); - abr.print (); + util::abr<unsigned> abr(elt1); + abr.add_child(elt2); + abr.add_child(elt3); + util::abr<unsigned>* abr2 = abr.search(elt2); + mln_assertion(abr2); + abr2->add_child(elt4); + abr2->add_child(elt5); + util::abr<unsigned>* abr3 = abr.search(elt4); + mln_assertion(abr3); + abr3 = abr2->search(elt1); + mln_assertion(!abr3); } Index: trunk/milena/mln/util/abr.hh =================================================================== --- trunk/milena/mln/util/abr.hh (revision 1370) +++ trunk/milena/mln/util/abr.hh (revision 1371) @@ -29,17 +29,14 @@ # define MLN_UTIL_ABR_HH # include <vector> -# include <iostream> /*! * \file mln/util/abr.hh * * \brief Definition of a generic general tree. * - * */ - namespace mln { @@ -47,76 +44,52 @@ { template <typename T> - struct s_abr + struct abr { - s_abr (); - s_abr (T& elt); + abr(); + abr(T& elt); - void add_son (T& elt); + void add_child(T& elt); void print_rec (int n) const; void print (void) const; - int search_rec(s_abr<T>** res, T& elt); - s_abr<T>* search(T& elt); + int search_rec(abr<T>** res, T& elt); + abr<T>* search(T& elt); T& elt_; - s_abr<T>* father_; - std::vector< s_abr<T>* > sons_; + abr<T>* parent_; + std::vector< abr<T>* > child_; }; # ifndef MLN_INCLUDE_ONLY template <typename T> - s_abr<T>::s_abr () + abr<T>::abr() : elt_ (0), - father_ (0) + parent_ (0) { } template <typename T> - s_abr<T>::s_abr (T& elt) + abr<T>::abr(T& elt) : elt_ (elt), - father_ (0) - { - } - - - template <typename T> - void - s_abr<T>::add_son (T& elt) + parent_ (0) { - - 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 + abr<T>::add_child(T& elt) { - std::cout << this->elt_ << std::endl; - typename std::vector<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); - } - } + abr<T>* s = new abr<T>(elt); - template <typename T> - void - s_abr<T>::print (void) const - { - this->print_rec(1); + s->parent_ = this; + this->child_.push_back(s); } - template <typename T> int - s_abr<T>::search_rec(s_abr<T>** res, T& elt) + abr<T>::search_rec(abr<T>** res, T& elt) { if (elt == this->elt_) { @@ -125,8 +98,8 @@ } else { - typename std::vector<s_abr<T>* >::iterator it = this->sons_.begin (); - for (; it != this->sons_.end (); ++it) + for (typename std::vector<abr<T>* >::iterator it = this->child_.begin(); + it != this->child_.end(); ++it) { if ((**it).search_rec (res, elt)) return 1; @@ -136,14 +109,13 @@ } template <typename T> - s_abr<T>* - s_abr<T>::search(T& elt) + abr<T>* + abr<T>::search(T& elt) { - s_abr<T>* res = 0; + abr<T>* res = 0; if (search_rec (&res, elt)) return res; - std::cerr << elt << " not found" << std::endl; return 0; } Index: trunk/milena/sandbox/duhamel/abr.hh =================================================================== --- trunk/milena/sandbox/duhamel/abr.hh (revision 1370) +++ trunk/milena/sandbox/duhamel/abr.hh (revision 1371) @@ -39,7 +39,6 @@ * */ - namespace mln { @@ -93,28 +92,6 @@ } 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, T& elt) { @@ -147,6 +124,27 @@ return 0; } +// template <typename T> +// void +// abr<T>::print_rec(int n) const +// { +// std::cout << this->elt_ << std::endl; +// typename std::vector<abr<T>* >::const_iterator it = this->child_.begin(); +// for (; it != this->child_.end(); ++it) +// { +// for (int i = 0; i < n; ++i) +// std::cout << " "; +// (**it).print_rec(n + 1); +// } +// } + +// template <typename T> +// void +// abr<T>::print(void) const +// { +// this->print_rec(1); +// } + # endif // ! MLN_INCLUDE_ONLY } // end of namespace mln::util
participants (1)
-
Guillaume Duhamel