tree.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  begin       : Fri Jan 02 2009
00003  copyright   : (C) 2009 by Martin Preuss
00004  email       : martin@libchipcard.de
00005 
00006  ***************************************************************************
00007  *                                                                         *
00008  *   This library is free software; you can redistribute it and/or         *
00009  *   modify it under the terms of the GNU Lesser General Public            *
00010  *   License as published by the Free Software Foundation; either          *
00011  *   version 2.1 of the License, or (at your option) any later version.    *
00012  *                                                                         *
00013  *   This library is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00016  *   Lesser General Public License for more details.                       *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU Lesser General Public      *
00019  *   License along with this library; if not, write to the Free Software   *
00020  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00021  *   MA  02111-1307  USA                                                   *
00022  *                                                                         *
00023  ***************************************************************************/
00024 
00025 
00026 #include <gwenhywfar/gwenhywfarapi.h>
00027 #include <gwenhywfar/types.h>
00028 #include <assert.h>
00029 
00030 
00031 #ifndef GWEN_DUMMY_EMPTY_ARG
00032 
00034 # define GWEN_DUMMY_EMPTY_ARG
00035 #endif
00036 
00037 
00038 #ifndef GWEN_TREE_H
00039 #define GWEN_TREE_H
00040 
00041 
00042 #ifdef __cplusplus
00043 extern "C" {
00044 #endif
00045 
00046 
00146 
00147 
00155 typedef struct GWEN_TREE GWEN_TREE;
00156 typedef struct GWEN_TREE_ELEMENT GWEN_TREE_ELEMENT;
00157 
00158 
00160 GWENHYWFAR_API
00161 GWEN_TREE *GWEN_Tree_new();
00162 
00166 GWENHYWFAR_API
00167 void GWEN_Tree_free(GWEN_TREE *l);
00168 
00172 GWENHYWFAR_API
00173 int GWEN_Tree_GetCount(const GWEN_TREE *l);
00174 
00177 GWENHYWFAR_API
00178 void GWEN_Tree_Add(GWEN_TREE *l, GWEN_TREE_ELEMENT *el);
00179 
00183 GWENHYWFAR_API
00184 void GWEN_Tree_Insert(GWEN_TREE *l, GWEN_TREE_ELEMENT *el);
00185 
00190 GWENHYWFAR_API
00191 void GWEN_Tree_Del(GWEN_TREE_ELEMENT *el);
00192 
00196 GWENHYWFAR_API
00197 void GWEN_Tree_AddList(GWEN_TREE *dest, GWEN_TREE *l);
00198 
00200 GWENHYWFAR_API
00201 void GWEN_Tree_AddChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el);
00202 
00204 GWENHYWFAR_API
00205 void GWEN_Tree_InsertChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el);
00206 
00207 
00209 GWENHYWFAR_API
00210 void *GWEN_Tree_GetFirst(const GWEN_TREE *l);
00211 
00213 GWENHYWFAR_API
00214 void *GWEN_Tree_GetLast(const GWEN_TREE *l);
00215 
00216 
00217 
00219 GWENHYWFAR_API
00220 GWEN_TREE_ELEMENT *GWEN_TreeElement_new(void *d);
00221 
00223 GWENHYWFAR_API
00224 void GWEN_TreeElement_free(GWEN_TREE_ELEMENT *el);
00225 
00229 GWENHYWFAR_API
00230 void *GWEN_TreeElement_GetPrevious(const GWEN_TREE_ELEMENT *el);
00231 
00235 GWENHYWFAR_API
00236 void *GWEN_TreeElement_GetNext(const GWEN_TREE_ELEMENT *el);
00237 
00246 GWENHYWFAR_API
00247 void *GWEN_TreeElement_GetBelow(const GWEN_TREE_ELEMENT *el);
00248 
00250 GWENHYWFAR_API
00251 void *GWEN_TreeElement_GetFirstChild(const GWEN_TREE_ELEMENT *el);
00252 
00254 GWENHYWFAR_API
00255 void *GWEN_TreeElement_GetLastChild(const GWEN_TREE_ELEMENT *el);
00256 
00257 GWENHYWFAR_API
00258 void *GWEN_TreeElement_GetParent(const GWEN_TREE_ELEMENT *el);
00259 
00261 GWENHYWFAR_API
00262 uint32_t GWEN_TreeElement_GetChildrenCount(const GWEN_TREE_ELEMENT *el);
00263 
00271 
00276 #define GWEN_TREE_ELEMENT(t) \
00277 GWEN_TREE_ELEMENT *_tree_element;
00278 
00285 #define GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
00286   typedef GWEN_TREE t##_TREE; \
00287   \
00288   decl t* pr##_Tree_GetFirst(const t##_TREE *l); \
00289   decl t* pr##_Tree_GetLast(const t##_TREE *l); \
00290   decl t* pr##_Tree_GetNext(const t *element); \
00291   decl t* pr##_Tree_GetPrevious(const t *element); \
00292   decl t* pr##_Tree_GetBelow(const t *element); \
00293   decl uint32_t pr##_Tree_GetCount(const t##_TREE *l); \
00294   decl int pr##_Tree_HasElement(const t##_TREE *l, const t *element); \
00295   decl t* pr##_Tree_GetFirstChild(const t *element); \
00296   decl t* pr##_Tree_GetLastChild(const t *element); \
00297   decl uint32_t pr##_Tree_GetChildrenCount(const t *element); \
00298   decl t* pr##_Tree_GetParent(const t *element);
00299 
00300 
00301 #define GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl) \
00302   typedef GWEN_TREE_ELEMENT t##_TREE_ELEMENT; \
00303   \
00304   decl void pr##_Tree_Clear(t##_TREE *l); \
00305   decl t##_TREE* pr##_Tree_new(); \
00306   decl void pr##_Tree_free(t##_TREE *l); \
00307   decl void pr##_Tree_AddList(t##_TREE *dst, t##_TREE *l); \
00308   decl void pr##_Tree_Add(t##_TREE *list, t *element); \
00309   decl void pr##_Tree_Insert(t##_TREE *list, t *element); \
00310   decl void pr##_Tree_Del(t *element); \
00311   \
00312   decl void pr##_Tree_AddChild(t *where, t *element); \
00313   decl void pr##_Tree_InsertChild(t *where, t *element); \
00314   \
00315   decl int pr##_Tree_HasChildElement(const t *who, const t *element); \
00316   decl void pr##_Tree_ClearChildren(t *element); \
00317 
00318 
00319 #define GWEN_TREE_FUNCTION_DEFS_CONST(t, pr) \
00320   GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
00321 
00322 #define GWEN_TREE_FUNCTION_DEFS_NOCONST(t, pr) \
00323   GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
00324 
00325 
00373 #define GWEN_TREE_FUNCTION_LIB_DEFS(t, pr, decl) \
00374   GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
00375   GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl)
00376 
00377 
00382 #define GWEN_TREE_FUNCTION_DEFS(t, pr) \
00383   GWEN_TREE_FUNCTION_LIB_DEFS(t, pr, GWEN_DUMMY_EMPTY_ARG)
00384 
00385 
00391 #define GWEN_TREE_FUNCTIONS(t, pr) \
00392   \
00393   void pr##_Tree_Add(t##_TREE *l, t *element) { \
00394     assert(element); \
00395     assert(element->_tree_element);\
00396     GWEN_Tree_Add(l, element->_tree_element); \
00397   } \
00398   \
00399   void pr##_Tree_AddList(t##_TREE *dst, t##_TREE *l) { \
00400     GWEN_Tree_AddList(dst, l); \
00401   } \
00402   \
00403   void pr##_Tree_Insert(t##_TREE *l, t *element) { \
00404     assert(element); \
00405     assert(element->_tree_element);\
00406     GWEN_Tree_Insert(l, element->_tree_element); \
00407   } \
00408   \
00409   void pr##_Tree_Del(t *element){ \
00410     assert(element); \
00411     assert(element->_tree_element);\
00412     GWEN_Tree_Del(element->_tree_element); \
00413   }\
00414   \
00415   t* pr##_Tree_GetFirst(const t##_TREE *l) { \
00416     if (l) return (t*)GWEN_Tree_GetFirst(l);\
00417     else return 0; \
00418   } \
00419   \
00420   t* pr##_Tree_GetLast(const t##_TREE *l) { \
00421     if (l) return (t*) GWEN_Tree_GetLast(l);\
00422     else return 0; \
00423   } \
00424   \
00425   void pr##_Tree_Clear(t##_TREE *l) { \
00426     t* el; \
00427     while( (el=GWEN_Tree_GetFirst(l)) ) {\
00428       pr##_Tree_Del(el);\
00429       pr##_Tree_ClearChildren(el); \
00430       pr##_free(el);\
00431     } /* while */ \
00432   } \
00433   \
00434   int pr##_Tree_HasElement(const t##_TREE *l, const t *element) { \
00435     const t* el; \
00436     el=(t*)GWEN_Tree_GetFirst(l); \
00437     while(el) {\
00438       if (el==element) \
00439         return 1; \
00440       el=(const t*)GWEN_TreeElement_GetBelow(el->_tree_element); \
00441     } /* while */ \
00442     return 0; \
00443   } \
00444   \
00445   t##_TREE* pr##_Tree_new(){\
00446     return (t##_TREE*)GWEN_Tree_new(); \
00447   }\
00448   \
00449   void pr##_Tree_free(t##_TREE *l) {\
00450     if (l) { \
00451       pr##_Tree_Clear(l);\
00452       GWEN_Tree_free(l); \
00453     }\
00454   } \
00455   \
00456   t* pr##_Tree_GetNext(const t *element) { \
00457     assert(element); \
00458     assert(element->_tree_element);\
00459     return (t*)GWEN_TreeElement_GetNext(element->_tree_element);\
00460   } \
00461   \
00462   t* pr##_Tree_GetPrevious(const t *element) { \
00463     assert(element); \
00464     assert(element->_tree_element);\
00465     return (t*)GWEN_TreeElement_GetPrevious(element->_tree_element);\
00466   } \
00467   \
00468   t* pr##_Tree_GetBelow(const t *element) { \
00469     assert(element); \
00470     assert(element->_tree_element);\
00471     return (t*)GWEN_TreeElement_GetBelow(element->_tree_element);\
00472   } \
00473   \
00474   uint32_t pr##_Tree_GetCount(const t##_TREE *l){\
00475     return GWEN_Tree_GetCount(l);\
00476   } \
00477   \
00478   int pr##_Tree_HasChildElement(const t *who, const t *element) { \
00479     const t* el; \
00480     el=(const t*)GWEN_TreeElement_GetFirstChild(who->_tree_element); \
00481     while(el) {\
00482       if (el==element) \
00483         return 1; \
00484       el=(const t*)GWEN_TreeElement_GetNext(el->_tree_element); \
00485     } /* while */ \
00486     return 0; \
00487   } \
00488   \
00489   void pr##_Tree_AddChild(t *where, t *element) { \
00490     assert(where); \
00491     assert(where->_tree_element);\
00492     assert(element); \
00493     assert(element->_tree_element);\
00494     GWEN_Tree_AddChild(where->_tree_element, element->_tree_element); \
00495   } \
00496   \
00497   void pr##_Tree_InsertChild(t *where, t *element) { \
00498     assert(where); \
00499     assert(where->_tree_element);\
00500     assert(element); \
00501     assert(element->_tree_element);\
00502     GWEN_Tree_InsertChild(where->_tree_element, element->_tree_element); \
00503   } \
00504   \
00505   void pr##_Tree_ClearChildren(t *element) { \
00506     t* c; \
00507     while( (c=GWEN_TreeElement_GetFirstChild(element->_tree_element)) ) {\
00508       pr##_Tree_ClearChildren(c);\
00509       pr##_Tree_Del(c);\
00510       pr##_free(c);\
00511     } /* while */ \
00512   } \
00513   \
00514   t* pr##_Tree_GetFirstChild(const t *element) { \
00515     assert(element); \
00516     assert(element->_tree_element);\
00517     return (t*)GWEN_TreeElement_GetFirstChild(element->_tree_element);\
00518   } \
00519   \
00520   t* pr##_Tree_GetLastChild(const t *element) { \
00521     assert(element); \
00522     assert(element->_tree_element);\
00523     return (t*)GWEN_TreeElement_GetLastChild(element->_tree_element);\
00524   } \
00525   \
00526   uint32_t pr##_Tree_GetChildrenCount(const t *element){\
00527     return GWEN_TreeElement_GetChildrenCount(element->_tree_element);\
00528   } \
00529   \
00530   t* pr##_Tree_GetParent(const t *element) { \
00531     assert(element); \
00532     assert(element->_tree_element);\
00533     return (t*)GWEN_TreeElement_GetParent(element->_tree_element);\
00534   } \
00535   \
00536 
00537 
00543 #define GWEN_TREE_INIT(t, element) \
00544   element->_tree_element=GWEN_TreeElement_new(element);
00545 
00546 
00552 #define GWEN_TREE_FINI(t, element) \
00553   if (element && element->_tree_element) { \
00554     GWEN_TreeElement_free(element->_tree_element); \
00555     element->_tree_element=0; \
00556   }
00557 
00560  /* defgroup */
00561 
00562 
00563 #ifdef __cplusplus
00564 }
00565 #endif
00566 
00567 
00568 #endif
00569 
00570 

Generated on Sat Jan 2 09:32:36 2010 for gwenhywfar by  doxygen 1.6.1