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 /* ========================================================================= */ 00021 /* This is an implementation of the set's defined at 'Data Structures and 00022 * Network Algorithms' of Robert Endre Tarjan. This structures allow to work 00023 * with disjoint setswith a representant (thus an equivalence class) and ask the 00024 * basic question of, given an elment, who is the representant if his class, 00025 * make a set, link two sets (i.e. union if them) and given a representant link 00026 * an element to that class. 00027 * 00028 * - 2003-06-24 00029 * - First Implementation. 00030 * 00031 * */ 00032 /* ========================================================================= */ 00033 #ifndef __EG_EQUISET_H__ 00034 #define __EG_EQUISET_H__ 00035 #include <stdio.h> 00036 #include <stdlib.h> 00037 #include "eg_config.h" 00038 #include "eg_macros.h" 00039 #include "eg_mempool.h" 00040 #ifndef EG_EQUISET_DLEVEL 00041 #define EG_EQUISET_DLEVEL 10 00042 #endif 00043 00044 /* ========================================================================= */ 00045 /* this structure holds an element of a set */ 00046 typedef struct EGequiSetElem_t__ 00047 { 00048 struct EGequiSetElem_t__ *father; 00049 unsigned int rank; 00050 void *this; 00051 } 00052 EGequiSetElem_t; 00053 00054 /* ========================================================================= */ 00055 /* this procedure make a new set */ 00056 extern inline EGequiSetElem_t *EGnewEquiSet (EGmemPool_t * mem, 00057 void *this) 00058 { 00059 /* local variables */ 00060 EGequiSetElem_t *v; 00061 00062 /* basic set-up */ 00063 v = (EGequiSetElem_t *) EGmemPoolMalloc (mem, sizeof (EGequiSetElem_t)); 00064 v->rank = 0; 00065 v->father = v; 00066 v->this = this; 00067 00068 /* ending */ 00069 return v; 00070 } 00071 00072 /* ========================================================================= */ 00073 /* this function find the representant of this element in his equivalence set */ 00074 EGequiSetElem_t *EGequiSetFind (EGequiSetElem_t * w); 00075 00076 /* ========================================================================= */ 00077 /* this functionlink two sets or equivalence clases */ 00078 extern inline EGequiSetElem_t *EGequiSetLink (EGequiSetElem_t * u, 00079 EGequiSetElem_t * v) 00080 { 00081 /* local variables */ 00082 EGequiSetElem_t *w; 00083 00084 /* some tests */ 00085 EXITL (EG_EQUISET_DLEVEL, v != v->father, 00086 "v is not a representant for its class"); 00087 EXITL (EG_EQUISET_DLEVEL, u != u->father, 00088 "u is not a representant for its class"); 00089 00090 /* pick the biggest one */ 00091 if (u->rank < v->rank) 00092 { 00093 w = u; 00094 u = v; 00095 v = w; 00096 } 00097 00098 if (u->rank == v->rank) 00099 { 00100 u->rank++; 00101 } 00102 00103 /* now we know that rank->u > rank->v */ 00104 v->father = u; 00105 00106 /* ending */ 00107 return u; 00108 } 00109 00110 /* ========================================================================= */ 00111 /* this function reset a set element to a singleton with the new parameter */ 00112 extern inline void EGequiSetReset (EGequiSetElem_t * u, 00113 void *elem) 00114 { 00115 u->father = u; 00116 u->rank = 0; 00117 u->this = elem; 00118 return; 00119 } 00120 00121 /* ========================================================================= */ 00122 /* destructor for the structure, note that his only eliminate the given element 00123 * in the set, but no the whole structure */ 00124 extern inline void EGfreeEquiSetElem (void *elem, 00125 EGmemPool_t * mem) 00126 { 00127 EGmemPoolFree (elem, sizeof (EGequiSetElem_t), mem); 00128 } 00129 00130 /* ========================================================================= */ 00131 /* this is an example */ 00132 00133 /* end eg_equiset.h */ 00134 #endif
1.4.5