level.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_TOPO_COMBINATORIAL_MAP_INTERNAL_LEVEL_HH
00029 # define OLENA_TOPO_COMBINATORIAL_MAP_INTERNAL_LEVEL_HH
00030 
00031 # include <iostream>
00032 # include <vector>
00033 
00034 namespace oln {
00035 
00036   namespace topo {
00037 
00038     namespace combinatorial_map {
00039 
00040       namespace internal {
00041         /* Node of a tree
00042         **
00043         ** \var node::lb left brother.
00044         ** \var node::rb right brother.
00045         ** \var node::fchild first child.
00046         ** \var node::father father.
00047         */
00048         template <class U>
00049         struct node
00050         {
00051           node() : fchild(0), lb(0), rb(0), father(0) {}
00052 
00053           U fchild, lb, rb, father;
00054         };
00055 
00056         /* Level function.
00057         **
00058         ** \todo FIXME: add doc.
00059         */
00060         template <class U>
00061         class level
00062         {
00063         public:
00064           level(unsigned n) : tree_(n+1)
00065           {
00066             assertion(n);
00067           }
00068 
00069           U father(const U & child) const
00070           {
00071             assertion(child < tree_.size());
00072 
00073             return tree_[child].father;
00074           }
00075 
00076           U children(const U & father) const
00077           {
00078             static unsigned f = 0;
00079             static unsigned last = 0;
00080 
00081             assertion(father < tree_.size());
00082 
00083             if (f != father)
00084               {
00085                 f = father;
00086                 last = tree_[f].fchild;
00087               }
00088             else
00089               last = (!last || tree_[last].rb == tree_[f].fchild) ?
00090                 0 : tree_[last].rb;
00091 
00092             return last;
00093           }
00094 
00095           void insert(const U & father, const U & child)
00096           {
00097             node<U> & f = tree_[father];
00098             node<U> & c = tree_[child];
00099 
00100             if (!f.lb)
00101               f.lb = f.rb = father;
00102 
00103             unsigned fchild = f.fchild;
00104 
00105             if (fchild)
00106               {
00107                 unsigned tail = tree_[fchild].lb;
00108 
00109                 tree_[tail].rb = child;
00110                 c.lb = tail;
00111                 tree_[fchild].lb = child;
00112                 c.rb = fchild;
00113               }
00114             else
00115               f.fchild = c.lb = c.rb = child;
00116 
00117             c.father = father;
00118           }
00119 
00120           void merge(const U & l1, const U & l2)
00121           {
00122             node<U> & a = tree_[l1];
00123             node<U> & b = tree_[l2];
00124 
00125             // assert l2 is not the first child of its father
00126             if (tree_[b.father].fchild == l2)
00127               tree_[b.father].fchild = (l2 != b.rb) ? b.rb : 0;
00128 
00129             // append l2' children to l1 ones
00130             if (b.fchild)
00131               {
00132                 // FIXME: find a way to get this constant time
00133                 unsigned l = 0;
00134                 while ((l = children(l2)))
00135                   tree_[l].father = l1;
00136 
00137                 if (a.fchild)
00138                   {
00139                     tree_[tree_[a.fchild].lb].rb = b.fchild;
00140                     tree_[b.fchild].lb = tree_[a.fchild].lb;
00141 
00142                     tree_[a.fchild].lb = tree_[b.fchild].lb;
00143                     tree_[b.fchild].rb = a.fchild;
00144                   }
00145                 else
00146                   a.fchild = b.fchild;
00147               }
00148 
00149             // pop up l2 from the list child
00150             tree_[b.rb].lb = b.lb;
00151             tree_[b.lb].rb = b.rb;
00152 
00153             b.fchild = b.lb = b.rb = b.father = 0;
00154           }
00155 
00156         public:
00157           std::ostream & print(std::ostream & ostr) const
00158           {
00159             for (unsigned i = 1; i < tree_.size(); ++i)
00160               ostr << "father(" << i << ") = " << father(i) << std::endl;
00161 
00162             ostr << std::endl;
00163 
00164             for (unsigned i = 1; i < tree_.size(); ++i)
00165               {
00166                 ostr << "children(" << i << ") = ";
00167 
00168                 unsigned l = children(i);
00169 
00170                 if (l)
00171                   {
00172                     ostr << l;
00173                     while ((l = children(i)))
00174                       ostr << ", " << l;
00175                     ostr << std::endl;
00176                   }
00177                 else
00178                   ostr << "none" << std::endl;
00179               }
00180 
00181             return ostr;
00182           }
00183 
00184         private:
00185           std::vector< node<U> > tree_;
00186         };
00187 
00188       } // end of namespace internal
00189 
00190     } // end of namespace combinatorial_map
00191 
00192   } // end of namespace topo
00193 
00194 } // end of namespace oln
00195 
00196 template<class U>
00197 inline std::ostream &
00198 operator<<(std::ostream & ostr,
00199            const oln::topo::combinatorial_map::internal::level<U> & l)
00200 {
00201   return l.print(ostr);
00202 }
00203 
00204 #endif // ! OLENA_TOPO_COMBINATORIAL_MAP_INTERNAL_LEVEL_HH

Generated on Thu Apr 15 20:13:13 2004 for Olena by doxygen 1.3.6-20040222