tree.h
Go to the documentation of this file.
/***************************************************************************
begin : Fri Jan 02 2009
copyright : (C) 2009 by Martin Preuss
email : martin@libchipcard.de
***************************************************************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
* MA 02111-1307 USA *
* *
***************************************************************************/
#include <gwenhywfar/gwenhywfarapi.h>
#include <gwenhywfar/types.h>
#include <assert.h>
#ifndef GWEN_DUMMY_EMPTY_ARG
# define GWEN_DUMMY_EMPTY_ARG
#endif
#ifndef GWEN_TREE_H
#define GWEN_TREE_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct GWEN_TREE GWEN_TREE;
typedef struct GWEN_TREE_ELEMENT GWEN_TREE_ELEMENT;
GWENHYWFAR_API
GWEN_TREE *GWEN_Tree_new(void);
GWENHYWFAR_API
void GWEN_Tree_free(GWEN_TREE *l);
GWENHYWFAR_API
int GWEN_Tree_GetCount(const GWEN_TREE *l);
GWENHYWFAR_API
void GWEN_Tree_Add(GWEN_TREE *l, GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void GWEN_Tree_Insert(GWEN_TREE *l, GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void GWEN_Tree_Del(GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void GWEN_Tree_Replace(GWEN_TREE_ELEMENT *elToReplace, GWEN_TREE_ELEMENT *elReplacement);
GWENHYWFAR_API
void GWEN_Tree_AddList(GWEN_TREE *dest, GWEN_TREE *l);
GWENHYWFAR_API
void GWEN_Tree_AddChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void GWEN_Tree_InsertChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void *GWEN_Tree_GetFirst(const GWEN_TREE *l);
GWENHYWFAR_API
void *GWEN_Tree_GetLast(const GWEN_TREE *l);
GWENHYWFAR_API
GWEN_TREE_ELEMENT *GWEN_TreeElement_new(void *d);
GWENHYWFAR_API
void GWEN_TreeElement_free(GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void *GWEN_TreeElement_GetPrevious(const GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void *GWEN_TreeElement_GetNext(const GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void *GWEN_TreeElement_GetBelow(const GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void *GWEN_TreeElement_GetFirstChild(const GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void *GWEN_TreeElement_GetLastChild(const GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
void *GWEN_TreeElement_GetParent(const GWEN_TREE_ELEMENT *el);
GWENHYWFAR_API
uint32_t GWEN_TreeElement_GetChildrenCount(const GWEN_TREE_ELEMENT *el);
#define GWEN_TREE_ELEMENT(t) \
GWEN_TREE_ELEMENT *_tree_element;
#define GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
typedef GWEN_TREE t##_TREE; \
\
decl t* pr##_Tree_GetFirst(const t##_TREE *l); \
decl t* pr##_Tree_GetLast(const t##_TREE *l); \
decl t* pr##_Tree_GetNext(const t *element); \
decl t* pr##_Tree_GetPrevious(const t *element); \
decl t* pr##_Tree_GetBelow(const t *element); \
decl uint32_t pr##_Tree_GetCount(const t##_TREE *l); \
decl int pr##_Tree_HasElement(const t##_TREE *l, const t *element); \
decl t* pr##_Tree_GetFirstChild(const t *element); \
decl t* pr##_Tree_GetLastChild(const t *element); \
decl uint32_t pr##_Tree_GetChildrenCount(const t *element); \
decl t* pr##_Tree_GetParent(const t *element);
#define GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl) \
typedef GWEN_TREE_ELEMENT t##_TREE_ELEMENT; \
\
decl void pr##_Tree_Clear(t##_TREE *l); \
decl t##_TREE* pr##_Tree_new(); \
decl void pr##_Tree_free(t##_TREE *l); \
decl void pr##_Tree_AddList(t##_TREE *dst, t##_TREE *l); \
decl void pr##_Tree_Add(t##_TREE *list, t *element); \
decl void pr##_Tree_Insert(t##_TREE *list, t *element); \
decl void pr##_Tree_Del(t *element); \
decl void pr##_Tree_Replace(t *elToReplace, t *elReplacement); \
\
decl void pr##_Tree_AddChild(t *where, t *element); \
decl void pr##_Tree_InsertChild(t *where, t *element); \
\
decl int pr##_Tree_HasChildElement(const t *who, const t *element); \
decl void pr##_Tree_ClearChildren(t *element); \
#define GWEN_TREE_FUNCTION_DEFS_CONST(t, pr) \
GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
#define GWEN_TREE_FUNCTION_DEFS_NOCONST(t, pr) \
GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
#define GWEN_TREE_FUNCTION_LIB_DEFS(t, pr, decl) \
GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl)
#define GWEN_TREE_FUNCTION_DEFS(t, pr) \
GWEN_TREE_FUNCTION_LIB_DEFS(t, pr, GWEN_DUMMY_EMPTY_ARG)
#define GWEN_TREE_FUNCTIONS(t, pr) \
\
void pr##_Tree_Add(t##_TREE *l, t *element) { \
assert(element); \
assert(element->_tree_element);\
GWEN_Tree_Add(l, element->_tree_element); \
} \
\
void pr##_Tree_AddList(t##_TREE *dst, t##_TREE *l) { \
GWEN_Tree_AddList(dst, l); \
} \
\
void pr##_Tree_Insert(t##_TREE *l, t *element) { \
assert(element); \
assert(element->_tree_element);\
GWEN_Tree_Insert(l, element->_tree_element); \
} \
\
void pr##_Tree_Del(t *element){ \
assert(element); \
assert(element->_tree_element);\
GWEN_Tree_Del(element->_tree_element); \
}\
\
void pr##_Tree_Replace(t *elToReplace, t *elReplacement) { \
assert(elToReplace); \
assert(elToReplace->_tree_element);\
assert(elReplacement); \
assert(elReplacement->_tree_element);\
GWEN_Tree_Replace(elToReplace->_tree_element, elReplacement->_tree_element); \
} \
\
t* pr##_Tree_GetFirst(const t##_TREE *l) { \
if (l) return (t*)GWEN_Tree_GetFirst(l);\
else return 0; \
} \
\
t* pr##_Tree_GetLast(const t##_TREE *l) { \
if (l) return (t*) GWEN_Tree_GetLast(l);\
else return 0; \
} \
\
void pr##_Tree_Clear(t##_TREE *l) { \
t* el; \
while( (el=GWEN_Tree_GetFirst(l)) ) {\
pr##_Tree_Del(el);\
pr##_Tree_ClearChildren(el); \
pr##_free(el);\
} /* while */ \
} \
\
int pr##_Tree_HasElement(const t##_TREE *l, const t *element) { \
const t* el; \
el=(t*)GWEN_Tree_GetFirst(l); \
while(el) {\
if (el==element) \
return 1; \
el=(const t*)GWEN_TreeElement_GetBelow(el->_tree_element); \
} /* while */ \
return 0; \
} \
\
t##_TREE* pr##_Tree_new(){\
return (t##_TREE*)GWEN_Tree_new(); \
}\
\
void pr##_Tree_free(t##_TREE *l) {\
if (l) { \
pr##_Tree_Clear(l);\
GWEN_Tree_free(l); \
}\
} \
\
t* pr##_Tree_GetNext(const t *element) { \
assert(element); \
assert(element->_tree_element);\
return (t*)GWEN_TreeElement_GetNext(element->_tree_element);\
} \
\
t* pr##_Tree_GetPrevious(const t *element) { \
assert(element); \
assert(element->_tree_element);\
return (t*)GWEN_TreeElement_GetPrevious(element->_tree_element);\
} \
\
t* pr##_Tree_GetBelow(const t *element) { \
assert(element); \
assert(element->_tree_element);\
return (t*)GWEN_TreeElement_GetBelow(element->_tree_element);\
} \
\
uint32_t pr##_Tree_GetCount(const t##_TREE *l){\
return GWEN_Tree_GetCount(l);\
} \
\
int pr##_Tree_HasChildElement(const t *who, const t *element) { \
const t* el; \
el=(const t*)GWEN_TreeElement_GetFirstChild(who->_tree_element); \
while(el) {\
if (el==element) \
return 1; \
el=(const t*)GWEN_TreeElement_GetNext(el->_tree_element); \
} /* while */ \
return 0; \
} \
\
void pr##_Tree_AddChild(t *where, t *element) { \
assert(where); \
assert(where->_tree_element);\
assert(element); \
assert(element->_tree_element);\
GWEN_Tree_AddChild(where->_tree_element, element->_tree_element); \
} \
\
void pr##_Tree_InsertChild(t *where, t *element) { \
assert(where); \
assert(where->_tree_element);\
assert(element); \
assert(element->_tree_element);\
GWEN_Tree_InsertChild(where->_tree_element, element->_tree_element); \
} \
\
void pr##_Tree_ClearChildren(t *element) { \
t* c; \
while( (c=GWEN_TreeElement_GetFirstChild(element->_tree_element)) ) {\
pr##_Tree_ClearChildren(c);\
pr##_Tree_Del(c);\
pr##_free(c);\
} /* while */ \
} \
\
t* pr##_Tree_GetFirstChild(const t *element) { \
assert(element); \
assert(element->_tree_element);\
return (t*)GWEN_TreeElement_GetFirstChild(element->_tree_element);\
} \
\
t* pr##_Tree_GetLastChild(const t *element) { \
assert(element); \
assert(element->_tree_element);\
return (t*)GWEN_TreeElement_GetLastChild(element->_tree_element);\
} \
\
uint32_t pr##_Tree_GetChildrenCount(const t *element){\
return GWEN_TreeElement_GetChildrenCount(element->_tree_element);\
} \
\
t* pr##_Tree_GetParent(const t *element) { \
assert(element); \
assert(element->_tree_element);\
return (t*)GWEN_TreeElement_GetParent(element->_tree_element);\
} \
\
#define GWEN_TREE_INIT(t, element) \
element->_tree_element=GWEN_TreeElement_new(element);
#define GWEN_TREE_FINI(t, element) \
if (element && element->_tree_element) { \
GWEN_TreeElement_free(element->_tree_element); \
element->_tree_element=0; \
}
/* defgroup */
#ifdef __cplusplus
}
#endif
#endif
gwenhywfarapi.h
GWENHYWFAR_API
#define GWENHYWFAR_API
Definition gwenhywfarapi.h:67
GWEN_Tree_Add
GWENHYWFAR_API void GWEN_Tree_Add(GWEN_TREE *l, GWEN_TREE_ELEMENT *el)
GWEN_TreeElement_GetChildrenCount
GWENHYWFAR_API uint32_t GWEN_TreeElement_GetChildrenCount(const GWEN_TREE_ELEMENT *el)
GWEN_TreeElement_GetNext
GWENHYWFAR_API void * GWEN_TreeElement_GetNext(const GWEN_TREE_ELEMENT *el)
GWEN_Tree_Replace
GWENHYWFAR_API void GWEN_Tree_Replace(GWEN_TREE_ELEMENT *elToReplace, GWEN_TREE_ELEMENT *elReplacement)
GWEN_Tree_Del
GWENHYWFAR_API void GWEN_Tree_Del(GWEN_TREE_ELEMENT *el)
GWEN_TREE
struct GWEN_TREE GWEN_TREE
Definition tree.h:155
GWEN_TreeElement_GetLastChild
GWENHYWFAR_API void * GWEN_TreeElement_GetLastChild(const GWEN_TREE_ELEMENT *el)
GWEN_Tree_GetLast
GWENHYWFAR_API void * GWEN_Tree_GetLast(const GWEN_TREE *l)
GWEN_Tree_AddList
GWENHYWFAR_API void GWEN_Tree_AddList(GWEN_TREE *dest, GWEN_TREE *l)
GWEN_TreeElement_GetPrevious
GWENHYWFAR_API void * GWEN_TreeElement_GetPrevious(const GWEN_TREE_ELEMENT *el)
GWEN_TreeElement_GetBelow
GWENHYWFAR_API void * GWEN_TreeElement_GetBelow(const GWEN_TREE_ELEMENT *el)
GWEN_TREE_ELEMENT
#define GWEN_TREE_ELEMENT(t)
Definition tree.h:286
GWEN_Tree_InsertChild
GWENHYWFAR_API void GWEN_Tree_InsertChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el)
GWEN_Tree_GetCount
GWENHYWFAR_API int GWEN_Tree_GetCount(const GWEN_TREE *l)
GWEN_TreeElement_free
GWENHYWFAR_API void GWEN_TreeElement_free(GWEN_TREE_ELEMENT *el)
GWEN_Tree_free
GWENHYWFAR_API void GWEN_Tree_free(GWEN_TREE *l)
GWEN_Tree_GetFirst
GWENHYWFAR_API void * GWEN_Tree_GetFirst(const GWEN_TREE *l)
GWEN_Tree_new
GWENHYWFAR_API GWEN_TREE * GWEN_Tree_new(void)
GWEN_TreeElement_GetFirstChild
GWENHYWFAR_API void * GWEN_TreeElement_GetFirstChild(const GWEN_TREE_ELEMENT *el)
GWEN_Tree_Insert
GWENHYWFAR_API void GWEN_Tree_Insert(GWEN_TREE *l, GWEN_TREE_ELEMENT *el)
GWEN_TreeElement_GetParent
GWENHYWFAR_API void * GWEN_TreeElement_GetParent(const GWEN_TREE_ELEMENT *el)
GWEN_Tree_AddChild
GWENHYWFAR_API void GWEN_Tree_AddChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el)
GWEN_TreeElement_new
GWENHYWFAR_API GWEN_TREE_ELEMENT * GWEN_TreeElement_new(void *d)
types.h