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(void);
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 
00198 GWENHYWFAR_API
00199 void GWEN_Tree_AddList(GWEN_TREE *dest, GWEN_TREE *l);
00200 
00202 GWENHYWFAR_API
00203 void GWEN_Tree_AddChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el);
00204 
00206 GWENHYWFAR_API
00207 void GWEN_Tree_InsertChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el);
00208 
00209 
00211 GWENHYWFAR_API
00212 void *GWEN_Tree_GetFirst(const GWEN_TREE *l);
00213 
00215 GWENHYWFAR_API
00216 void *GWEN_Tree_GetLast(const GWEN_TREE *l);
00217 
00218 
00219 
00221 GWENHYWFAR_API
00222 GWEN_TREE_ELEMENT *GWEN_TreeElement_new(void *d);
00223 
00225 GWENHYWFAR_API
00226 void GWEN_TreeElement_free(GWEN_TREE_ELEMENT *el);
00227 
00231 GWENHYWFAR_API
00232 void *GWEN_TreeElement_GetPrevious(const GWEN_TREE_ELEMENT *el);
00233 
00237 GWENHYWFAR_API
00238 void *GWEN_TreeElement_GetNext(const GWEN_TREE_ELEMENT *el);
00239 
00248 GWENHYWFAR_API
00249 void *GWEN_TreeElement_GetBelow(const GWEN_TREE_ELEMENT *el);
00250 
00252 GWENHYWFAR_API
00253 void *GWEN_TreeElement_GetFirstChild(const GWEN_TREE_ELEMENT *el);
00254 
00256 GWENHYWFAR_API
00257 void *GWEN_TreeElement_GetLastChild(const GWEN_TREE_ELEMENT *el);
00258 
00259 GWENHYWFAR_API
00260 void *GWEN_TreeElement_GetParent(const GWEN_TREE_ELEMENT *el);
00261 
00263 GWENHYWFAR_API
00264 uint32_t GWEN_TreeElement_GetChildrenCount(const GWEN_TREE_ELEMENT *el);
00265 
00273 
00278 #define GWEN_TREE_ELEMENT(t) \
00279 GWEN_TREE_ELEMENT *_tree_element;
00280 
00287 #define GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
00288   typedef GWEN_TREE t##_TREE; \
00289   \
00290   decl t* pr##_Tree_GetFirst(const t##_TREE *l); \
00291   decl t* pr##_Tree_GetLast(const t##_TREE *l); \
00292   decl t* pr##_Tree_GetNext(const t *element); \
00293   decl t* pr##_Tree_GetPrevious(const t *element); \
00294   decl t* pr##_Tree_GetBelow(const t *element); \
00295   decl uint32_t pr##_Tree_GetCount(const t##_TREE *l); \
00296   decl int pr##_Tree_HasElement(const t##_TREE *l, const t *element); \
00297   decl t* pr##_Tree_GetFirstChild(const t *element); \
00298   decl t* pr##_Tree_GetLastChild(const t *element); \
00299   decl uint32_t pr##_Tree_GetChildrenCount(const t *element); \
00300   decl t* pr##_Tree_GetParent(const t *element);
00301 
00302 
00303 #define GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl) \
00304   typedef GWEN_TREE_ELEMENT t##_TREE_ELEMENT; \
00305   \
00306   decl void pr##_Tree_Clear(t##_TREE *l); \
00307   decl t##_TREE* pr##_Tree_new(); \
00308   decl void pr##_Tree_free(t##_TREE *l); \
00309   decl void pr##_Tree_AddList(t##_TREE *dst, t##_TREE *l); \
00310   decl void pr##_Tree_Add(t##_TREE *list, t *element); \
00311   decl void pr##_Tree_Insert(t##_TREE *list, t *element); \
00312   decl void pr##_Tree_Del(t *element); \
00313   \
00314   decl void pr##_Tree_AddChild(t *where, t *element); \
00315   decl void pr##_Tree_InsertChild(t *where, t *element); \
00316   \
00317   decl int pr##_Tree_HasChildElement(const t *who, const t *element); \
00318   decl void pr##_Tree_ClearChildren(t *element); \
00319 
00320 
00321 #define GWEN_TREE_FUNCTION_DEFS_CONST(t, pr) \
00322   GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
00323 
00324 #define GWEN_TREE_FUNCTION_DEFS_NOCONST(t, pr) \
00325   GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
00326 
00327 
00375 #define GWEN_TREE_FUNCTION_LIB_DEFS(t, pr, decl) \
00376   GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
00377   GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl)
00378 
00379 
00384 #define GWEN_TREE_FUNCTION_DEFS(t, pr) \
00385   GWEN_TREE_FUNCTION_LIB_DEFS(t, pr, GWEN_DUMMY_EMPTY_ARG)
00386 
00387 
00393 #define GWEN_TREE_FUNCTIONS(t, pr) \
00394   \
00395   void pr##_Tree_Add(t##_TREE *l, t *element) { \
00396     assert(element); \
00397     assert(element->_tree_element);\
00398     GWEN_Tree_Add(l, element->_tree_element); \
00399   } \
00400   \
00401   void pr##_Tree_AddList(t##_TREE *dst, t##_TREE *l) { \
00402     GWEN_Tree_AddList(dst, l); \
00403   } \
00404   \
00405   void pr##_Tree_Insert(t##_TREE *l, t *element) { \
00406     assert(element); \
00407     assert(element->_tree_element);\
00408     GWEN_Tree_Insert(l, element->_tree_element); \
00409   } \
00410   \
00411   void pr##_Tree_Del(t *element){ \
00412     assert(element); \
00413     assert(element->_tree_element);\
00414     GWEN_Tree_Del(element->_tree_element); \
00415   }\
00416   \
00417   t* pr##_Tree_GetFirst(const t##_TREE *l) { \
00418     if (l) return (t*)GWEN_Tree_GetFirst(l);\
00419     else return 0; \
00420   } \
00421   \
00422   t* pr##_Tree_GetLast(const t##_TREE *l) { \
00423     if (l) return (t*) GWEN_Tree_GetLast(l);\
00424     else return 0; \
00425   } \
00426   \
00427   void pr##_Tree_Clear(t##_TREE *l) { \
00428     t* el; \
00429     while( (el=GWEN_Tree_GetFirst(l)) ) {\
00430       pr##_Tree_Del(el);\
00431       pr##_Tree_ClearChildren(el); \
00432       pr##_free(el);\
00433     } /* while */ \
00434   } \
00435   \
00436   int pr##_Tree_HasElement(const t##_TREE *l, const t *element) { \
00437     const t* el; \
00438     el=(t*)GWEN_Tree_GetFirst(l); \
00439     while(el) {\
00440       if (el==element) \
00441         return 1; \
00442       el=(const t*)GWEN_TreeElement_GetBelow(el->_tree_element); \
00443     } /* while */ \
00444     return 0; \
00445   } \
00446   \
00447   t##_TREE* pr##_Tree_new(){\
00448     return (t##_TREE*)GWEN_Tree_new(); \
00449   }\
00450   \
00451   void pr##_Tree_free(t##_TREE *l) {\
00452     if (l) { \
00453       pr##_Tree_Clear(l);\
00454       GWEN_Tree_free(l); \
00455     }\
00456   } \
00457   \
00458   t* pr##_Tree_GetNext(const t *element) { \
00459     assert(element); \
00460     assert(element->_tree_element);\
00461     return (t*)GWEN_TreeElement_GetNext(element->_tree_element);\
00462   } \
00463   \
00464   t* pr##_Tree_GetPrevious(const t *element) { \
00465     assert(element); \
00466     assert(element->_tree_element);\
00467     return (t*)GWEN_TreeElement_GetPrevious(element->_tree_element);\
00468   } \
00469   \
00470   t* pr##_Tree_GetBelow(const t *element) { \
00471     assert(element); \
00472     assert(element->_tree_element);\
00473     return (t*)GWEN_TreeElement_GetBelow(element->_tree_element);\
00474   } \
00475   \
00476   uint32_t pr##_Tree_GetCount(const t##_TREE *l){\
00477     return GWEN_Tree_GetCount(l);\
00478   } \
00479   \
00480   int pr##_Tree_HasChildElement(const t *who, const t *element) { \
00481     const t* el; \
00482     el=(const t*)GWEN_TreeElement_GetFirstChild(who->_tree_element); \
00483     while(el) {\
00484       if (el==element) \
00485         return 1; \
00486       el=(const t*)GWEN_TreeElement_GetNext(el->_tree_element); \
00487     } /* while */ \
00488     return 0; \
00489   } \
00490   \
00491   void pr##_Tree_AddChild(t *where, t *element) { \
00492     assert(where); \
00493     assert(where->_tree_element);\
00494     assert(element); \
00495     assert(element->_tree_element);\
00496     GWEN_Tree_AddChild(where->_tree_element, element->_tree_element); \
00497   } \
00498   \
00499   void pr##_Tree_InsertChild(t *where, t *element) { \
00500     assert(where); \
00501     assert(where->_tree_element);\
00502     assert(element); \
00503     assert(element->_tree_element);\
00504     GWEN_Tree_InsertChild(where->_tree_element, element->_tree_element); \
00505   } \
00506   \
00507   void pr##_Tree_ClearChildren(t *element) { \
00508     t* c; \
00509     while( (c=GWEN_TreeElement_GetFirstChild(element->_tree_element)) ) {\
00510       pr##_Tree_ClearChildren(c);\
00511       pr##_Tree_Del(c);\
00512       pr##_free(c);\
00513     } /* while */ \
00514   } \
00515   \
00516   t* pr##_Tree_GetFirstChild(const t *element) { \
00517     assert(element); \
00518     assert(element->_tree_element);\
00519     return (t*)GWEN_TreeElement_GetFirstChild(element->_tree_element);\
00520   } \
00521   \
00522   t* pr##_Tree_GetLastChild(const t *element) { \
00523     assert(element); \
00524     assert(element->_tree_element);\
00525     return (t*)GWEN_TreeElement_GetLastChild(element->_tree_element);\
00526   } \
00527   \
00528   uint32_t pr##_Tree_GetChildrenCount(const t *element){\
00529     return GWEN_TreeElement_GetChildrenCount(element->_tree_element);\
00530   } \
00531   \
00532   t* pr##_Tree_GetParent(const t *element) { \
00533     assert(element); \
00534     assert(element->_tree_element);\
00535     return (t*)GWEN_TreeElement_GetParent(element->_tree_element);\
00536   } \
00537   \
00538 
00539 
00545 #define GWEN_TREE_INIT(t, element) \
00546   element->_tree_element=GWEN_TreeElement_new(element);
00547 
00548 
00554 #define GWEN_TREE_FINI(t, element) \
00555   if (element && element->_tree_element) { \
00556     GWEN_TreeElement_free(element->_tree_element); \
00557     element->_tree_element=0; \
00558   }
00559 
00562  /* defgroup */
00563 
00564 
00565 #ifdef __cplusplus
00566 }
00567 #endif
00568 
00569 
00570 #endif
00571 
00572