eg_eset.h

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 /* ========================================================================= */
00021 /** @defgroup EGeSet EGeSet 
00022  * This is an implementation of the set's defined at 'Data Structures and
00023  * Network Algorithms' of Robert Endre Tarjan. This structures allow to work
00024  * with disjoint setswith a representant (thus an equivalence class) and ask the
00025  * basic question of, given an elment, who is the representant if his class,
00026  * make a set, link two sets (i.e. union if them) and given a representant link
00027  * an element to that class.
00028  *
00029  * @version 0.0.1
00030  * @par History:
00031  * - 2005-06-20
00032  *          - First Implementation.
00033  * */
00034 /** @file 
00035  * @ingroup EGeSet */
00036 /** @addtogroup EGeSet */
00037 /** @{ */
00038 /** @example eg_eset.ex.c */
00039 /* ========================================================================= */
00040 #ifndef __EG_ESET_H__
00041 #define __EG_ESET_H__
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include "eg_config.h"
00045 #include "eg_macros.h"
00046 #ifndef EG_ESET_DLEVEL
00047 #define EG_ESET_DLEVEL 0
00048 #endif
00049 
00050 /* ========================================================================= */
00051 /** @brief this structure holds an element of a set */
00052 typedef struct EGes_t
00053 {
00054   struct EGes_t *father;/**> pointer to the representing element on the 
00055                              class where this element bellong. */
00056   unsigned int rank;    /**> This is a lower bound on the number of 
00057                              elements for wich this is the represenntant */
00058 }
00059 EGes_t;
00060 
00061 /* ========================================================================= */
00062 /** @brief Initialize a set element as a set containing only itsself */
00063 #define EGesInit(elem) ({\
00064   EGes_t*const __EGesElm = (elem);\
00065   *__EGesElm = (EGes_t){__EGesElm,0};})
00066 
00067 /* ========================================================================= */
00068 /** @brief this function find the representant of this element in his 
00069  * equivalence set.
00070  * @param elem pointer to the element to wich we want to find the
00071  * representant.
00072  * @return the representant for this set (EGes_t*). */
00073 #define EGesFind(elem) ({\
00074   EGes_t *_EGesFc = elem, *_EGesFn;\
00075   while(_EGesFc != _EGesFc->father)\
00076   {\
00077     _EGesFn = _EGesFc->father;\
00078     _EGesFc->father = _EGesFn->father;\
00079     _EGesFc = _EGesFn;\
00080   }\
00081   _EGesFc;})
00082 
00083 /* ========================================================================= */
00084 /**@brief Given two representing elements for two clases, joint them into a
00085  * single class.
00086  * @param u first representative.
00087  * @param v second representative.
00088  * @return the representing element of the new class.
00089  * @note This implementation always let the first element to be the 
00090  * representative for the new class. */
00091 #define EGesLink(u,v) ({\
00092   EGes_t *_EGes_lu = (u), *_EGes_lv = (v);\
00093   EXITL(EG_ESET_DLEVEL, _EGes_lu != _EGes_lu->father, #u\
00094         " is not a representant for its class");\
00095   EXITL(EG_ESET_DLEVEL, _EGes_lv != _EGes_lv->father, #v\
00096         " is not a representant for its class");\
00097   EXITL(EG_ESET_DLEVEL, _EGes_lu == _EGes_lv, "same representant v == u");\
00098   if(_EGes_lu->rank <= _EGes_lv->rank) _EGes_lu->rank = _EGes_lv->rank+1;\
00099   _EGes_lv->father = _EGes_lu;\
00100   _EGes_lu;})
00101 
00102 /* ========================================================================= */
00103 /** @} end eg_eset.h */
00104 #endif

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