eg_equiset.c

Go to the documentation of this file.
00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 #include "eg_equiset.h"
00021 
00022 /* ========================================================================= */
00023 /* interface functions */
00024 EGequiSetElem_t *EGnewEquiSet (EGmemPool_t * mem,
00025                                void *this)
00026 {
00027   /* local variables */
00028   EGequiSetElem_t *v;
00029 
00030   /* basic set-up */
00031   v = (EGequiSetElem_t *) EGmemPoolMalloc (mem, sizeof (EGequiSetElem_t));
00032   v->rank = 0;
00033   v->father = v;
00034   v->this = this;
00035 
00036   /* ending */
00037   return v;
00038 }
00039 
00040 /* ========================================================================= */
00041 EGequiSetElem_t *EGequiSetFind (EGequiSetElem_t * w)
00042 {
00043   /* local variables */
00044   EGequiSetElem_t *p = w->father;
00045 
00046   /* ending */
00047   return (w == p) ? w : (w->father = EGequiSetFind (p));
00048 }
00049 
00050 /* ========================================================================= */
00051 EGequiSetElem_t *EGequiSetLink (EGequiSetElem_t * u,
00052                                 EGequiSetElem_t * v)
00053 {
00054   /* local variables */
00055   EGequiSetElem_t *w;
00056 
00057   /* some tests */
00058   EXITL (3, v != v->father, "v is not a representant for its class");
00059   EXITL (3, u != u->father, "u is not a representant for its class");
00060 
00061   /* pick the biggest one */
00062   if (u->rank < v->rank)
00063   {
00064     w = u;
00065     u = v;
00066     v = w;
00067   }
00068 
00069   if (u->rank == v->rank)
00070   {
00071     u->rank++;
00072   }
00073 
00074   /* now we know that rank->u > rank->v */
00075   v->father = u;
00076 
00077   /* ending */
00078   return u;
00079 }
00080 
00081 /* ========================================================================= */
00082 /* this function reset a set element to a singleton with the new parameter */
00083 void EGequiSetReset (EGequiSetElem_t * u,
00084                      void *elem)
00085 {
00086   u->father = u;
00087   u->rank = 0;
00088   u->this = elem;
00089   return;
00090 }
00091 
00092 /* ========================================================================= */
00093 void EGfreeEquiSetElem (void *elem,
00094                         EGmemPool_t * mem)
00095 {
00096   EGmemPoolFree (elem, sizeof (EGequiSetElem_t), mem);
00097 }

Generated on Mon Jan 30 08:48:53 2006 for EGlib by  doxygen 1.4.5