union.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_TARJAN_UNION_HH
00029 # define OLENA_TOPO_TARJAN_UNION_HH
00030 
00031 #include <oln/basics2d.hh>
00032 
00033 #include <oln/level/fill.hh>
00034 
00035 namespace oln {
00036 
00037   namespace topo {
00039     namespace tarjan {
00040 
00041       struct empty_class
00042       {};
00043 
00048       template< class I, class aux_data_type>
00049       struct tarjan_set
00050       {
00051         typedef oln_point_type(I) point_type;
00052         typedef oln_value_type(I) data_type;
00053         typedef oln_concrete_type(I) image_type;
00054 
00055         typedef typename mute<I, point_type>::ret ima_parent_type;
00056         typedef typename mute<I, aux_data_type>::ret ima_aux_data_type;
00057 
00058       public:
00059         tarjan_set(const image_type & ima) : input(ima)
00060         {
00061           parent = typename mute<I, point_type>::ret(ima.size());
00062           level::fill(parent, inactive());
00063 
00064           parent.border_adapt_assign(1, inactive());
00065         }
00066 
00067         // FIXME: these macros do not respect the coding style!
00068 
00069         // active and inactive are defined with a hook to be static
00070         // and initialized only once.
00071         static const point_type&
00072         active()
00073         {
00074           static struct foo_def{
00075             point_type elt;
00076             foo_def()
00077             {
00078               const unsigned dim = point_type::dim;
00079               for (unsigned i = 0; i < dim; ++i )
00080                 elt.nth(i) = -1;
00081             }
00082           } tmp;
00083 
00084           return tmp.elt;
00085         }
00086 
00087         static const point_type&
00088         inactive()
00089         {
00090           static struct foo_def {
00091             point_type elt;
00092             foo_def() {
00093               const unsigned dim = point_type::dim;
00094               for (unsigned i = 0; i < dim; ++i )
00095                 elt.nth(i) = -2;
00096             }
00097           } tmp;
00098 
00099           return tmp.elt;
00100         }
00101 
00102         void
00103         make_set(const point_type& x)
00104         {
00105           precondition(parent[x] == inactive());
00106           parent[x] = active();
00107         }
00108 
00109         void
00110         link(const point_type& x, const point_type& y)
00111         {
00112           parent[x] = y;
00113         }
00114 
00115         unsigned int
00116         attribute(const point_type& x)
00117         {
00118           precondition(parent[x] == active());
00119           precondition(aux_data[x] != 0);
00120           return aux_data[x];
00121         }
00122 
00123         bool
00124         equiv(const point_type& x, const point_type& y)
00125         {
00126           return ( (input[x] == input[y]));
00127         }
00128 
00129 
00130         void
00131         uni(const point_type& n, const point_type& p)
00132         {
00133           point_type r = find_root(n);
00134           if (r != p && equiv(r,p))
00135             link(r,p);
00136         }
00137 
00138         const point_type&
00139         find_root(const point_type& x)
00140         {
00141           //FIXME do it iteratively
00142           if ((parent[x] != active()) && (parent[x] != inactive()))
00143             {
00144               parent[x] = find_root(parent[x]);
00145               return parent[x];
00146             }
00147           else
00148             return x;
00149         }
00150 
00151         const point_type&
00152         sure_find_root(const point_type& x)
00153         {
00154           if ((parent[x] != active()) && (parent[x] != inactive()))
00155             return parent[x];
00156           else
00157             return x;
00158         }
00159 
00160         const bool
00161         is_root(const point_type& x)
00162         {
00163           if ((parent[x] != active()) && (parent[x] != inactive()))
00164             return false;
00165           return true;
00166         }
00167 
00168         const image_type &input;
00169         ima_parent_type parent;
00170         ima_aux_data_type aux_data;
00171       };
00172 
00173     } // end of namespace tarjan
00174 
00175   } // end of namespace topo
00176 
00177 } // end of namespace oln
00178 
00179 #endif // ! OLENA_TOPO_TARJAN_UNION_HH

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