gwenhywfar  4.20.2
tree.h
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Fri Jan 02 2009
3  copyright : (C) 2009 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 
27 #include <gwenhywfar/types.h>
28 #include <assert.h>
29 
30 
31 #ifndef GWEN_DUMMY_EMPTY_ARG
32 
34 # define GWEN_DUMMY_EMPTY_ARG
35 #endif
36 
37 
38 #ifndef GWEN_TREE_H
39 #define GWEN_TREE_H
40 
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 
146 
147 
155 typedef struct GWEN_TREE GWEN_TREE;
157 
158 
161 GWEN_TREE *GWEN_Tree_new(void);
162 
167 void GWEN_Tree_free(GWEN_TREE *l);
168 
173 int GWEN_Tree_GetCount(const GWEN_TREE *l);
174 
179 
185 
192 
199 void GWEN_Tree_AddList(GWEN_TREE *dest, GWEN_TREE *l);
200 
204 
208 
209 
212 void *GWEN_Tree_GetFirst(const GWEN_TREE *l);
213 
216 void *GWEN_Tree_GetLast(const GWEN_TREE *l);
217 
218 
219 
223 
227 
233 
239 
250 
254 
258 
261 
265 
273 
278 #define GWEN_TREE_ELEMENT(t) \
279 GWEN_TREE_ELEMENT *_tree_element;
280 
287 #define GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
288  typedef GWEN_TREE t##_TREE; \
289  \
290  decl t* pr##_Tree_GetFirst(const t##_TREE *l); \
291  decl t* pr##_Tree_GetLast(const t##_TREE *l); \
292  decl t* pr##_Tree_GetNext(const t *element); \
293  decl t* pr##_Tree_GetPrevious(const t *element); \
294  decl t* pr##_Tree_GetBelow(const t *element); \
295  decl uint32_t pr##_Tree_GetCount(const t##_TREE *l); \
296  decl int pr##_Tree_HasElement(const t##_TREE *l, const t *element); \
297  decl t* pr##_Tree_GetFirstChild(const t *element); \
298  decl t* pr##_Tree_GetLastChild(const t *element); \
299  decl uint32_t pr##_Tree_GetChildrenCount(const t *element); \
300  decl t* pr##_Tree_GetParent(const t *element);
301 
302 
303 #define GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl) \
304  typedef GWEN_TREE_ELEMENT t##_TREE_ELEMENT; \
305  \
306  decl void pr##_Tree_Clear(t##_TREE *l); \
307  decl t##_TREE* pr##_Tree_new(); \
308  decl void pr##_Tree_free(t##_TREE *l); \
309  decl void pr##_Tree_AddList(t##_TREE *dst, t##_TREE *l); \
310  decl void pr##_Tree_Add(t##_TREE *list, t *element); \
311  decl void pr##_Tree_Insert(t##_TREE *list, t *element); \
312  decl void pr##_Tree_Del(t *element); \
313  \
314  decl void pr##_Tree_AddChild(t *where, t *element); \
315  decl void pr##_Tree_InsertChild(t *where, t *element); \
316  \
317  decl int pr##_Tree_HasChildElement(const t *who, const t *element); \
318  decl void pr##_Tree_ClearChildren(t *element); \
319 
320 
321 #define GWEN_TREE_FUNCTION_DEFS_CONST(t, pr) \
322  GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
323 
324 #define GWEN_TREE_FUNCTION_DEFS_NOCONST(t, pr) \
325  GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
326 
327 
375 #define GWEN_TREE_FUNCTION_LIB_DEFS(t, pr, decl) \
376  GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
377  GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl)
378 
379 
384 #define GWEN_TREE_FUNCTION_DEFS(t, pr) \
385  GWEN_TREE_FUNCTION_LIB_DEFS(t, pr, GWEN_DUMMY_EMPTY_ARG)
386 
387 
393 #define GWEN_TREE_FUNCTIONS(t, pr) \
394  \
395  void pr##_Tree_Add(t##_TREE *l, t *element) { \
396  assert(element); \
397  assert(element->_tree_element);\
398  GWEN_Tree_Add(l, element->_tree_element); \
399  } \
400  \
401  void pr##_Tree_AddList(t##_TREE *dst, t##_TREE *l) { \
402  GWEN_Tree_AddList(dst, l); \
403  } \
404  \
405  void pr##_Tree_Insert(t##_TREE *l, t *element) { \
406  assert(element); \
407  assert(element->_tree_element);\
408  GWEN_Tree_Insert(l, element->_tree_element); \
409  } \
410  \
411  void pr##_Tree_Del(t *element){ \
412  assert(element); \
413  assert(element->_tree_element);\
414  GWEN_Tree_Del(element->_tree_element); \
415  }\
416  \
417  t* pr##_Tree_GetFirst(const t##_TREE *l) { \
418  if (l) return (t*)GWEN_Tree_GetFirst(l);\
419  else return 0; \
420  } \
421  \
422  t* pr##_Tree_GetLast(const t##_TREE *l) { \
423  if (l) return (t*) GWEN_Tree_GetLast(l);\
424  else return 0; \
425  } \
426  \
427  void pr##_Tree_Clear(t##_TREE *l) { \
428  t* el; \
429  while( (el=GWEN_Tree_GetFirst(l)) ) {\
430  pr##_Tree_Del(el);\
431  pr##_Tree_ClearChildren(el); \
432  pr##_free(el);\
433  } /* while */ \
434  } \
435  \
436  int pr##_Tree_HasElement(const t##_TREE *l, const t *element) { \
437  const t* el; \
438  el=(t*)GWEN_Tree_GetFirst(l); \
439  while(el) {\
440  if (el==element) \
441  return 1; \
442  el=(const t*)GWEN_TreeElement_GetBelow(el->_tree_element); \
443  } /* while */ \
444  return 0; \
445  } \
446  \
447  t##_TREE* pr##_Tree_new(){\
448  return (t##_TREE*)GWEN_Tree_new(); \
449  }\
450  \
451  void pr##_Tree_free(t##_TREE *l) {\
452  if (l) { \
453  pr##_Tree_Clear(l);\
454  GWEN_Tree_free(l); \
455  }\
456  } \
457  \
458  t* pr##_Tree_GetNext(const t *element) { \
459  assert(element); \
460  assert(element->_tree_element);\
461  return (t*)GWEN_TreeElement_GetNext(element->_tree_element);\
462  } \
463  \
464  t* pr##_Tree_GetPrevious(const t *element) { \
465  assert(element); \
466  assert(element->_tree_element);\
467  return (t*)GWEN_TreeElement_GetPrevious(element->_tree_element);\
468  } \
469  \
470  t* pr##_Tree_GetBelow(const t *element) { \
471  assert(element); \
472  assert(element->_tree_element);\
473  return (t*)GWEN_TreeElement_GetBelow(element->_tree_element);\
474  } \
475  \
476  uint32_t pr##_Tree_GetCount(const t##_TREE *l){\
477  return GWEN_Tree_GetCount(l);\
478  } \
479  \
480  int pr##_Tree_HasChildElement(const t *who, const t *element) { \
481  const t* el; \
482  el=(const t*)GWEN_TreeElement_GetFirstChild(who->_tree_element); \
483  while(el) {\
484  if (el==element) \
485  return 1; \
486  el=(const t*)GWEN_TreeElement_GetNext(el->_tree_element); \
487  } /* while */ \
488  return 0; \
489  } \
490  \
491  void pr##_Tree_AddChild(t *where, t *element) { \
492  assert(where); \
493  assert(where->_tree_element);\
494  assert(element); \
495  assert(element->_tree_element);\
496  GWEN_Tree_AddChild(where->_tree_element, element->_tree_element); \
497  } \
498  \
499  void pr##_Tree_InsertChild(t *where, t *element) { \
500  assert(where); \
501  assert(where->_tree_element);\
502  assert(element); \
503  assert(element->_tree_element);\
504  GWEN_Tree_InsertChild(where->_tree_element, element->_tree_element); \
505  } \
506  \
507  void pr##_Tree_ClearChildren(t *element) { \
508  t* c; \
509  while( (c=GWEN_TreeElement_GetFirstChild(element->_tree_element)) ) {\
510  pr##_Tree_ClearChildren(c);\
511  pr##_Tree_Del(c);\
512  pr##_free(c);\
513  } /* while */ \
514  } \
515  \
516  t* pr##_Tree_GetFirstChild(const t *element) { \
517  assert(element); \
518  assert(element->_tree_element);\
519  return (t*)GWEN_TreeElement_GetFirstChild(element->_tree_element);\
520  } \
521  \
522  t* pr##_Tree_GetLastChild(const t *element) { \
523  assert(element); \
524  assert(element->_tree_element);\
525  return (t*)GWEN_TreeElement_GetLastChild(element->_tree_element);\
526  } \
527  \
528  uint32_t pr##_Tree_GetChildrenCount(const t *element){\
529  return GWEN_TreeElement_GetChildrenCount(element->_tree_element);\
530  } \
531  \
532  t* pr##_Tree_GetParent(const t *element) { \
533  assert(element); \
534  assert(element->_tree_element);\
535  return (t*)GWEN_TreeElement_GetParent(element->_tree_element);\
536  } \
537  \
538 
539 
545 #define GWEN_TREE_INIT(t, element) \
546  element->_tree_element=GWEN_TreeElement_new(element);
547 
548 
554 #define GWEN_TREE_FINI(t, element) \
555  if (element && element->_tree_element) { \
556  GWEN_TreeElement_free(element->_tree_element); \
557  element->_tree_element=0; \
558  }
559 
562  /* defgroup */
563 
564 
565 #ifdef __cplusplus
566 }
567 #endif
568 
569 
570 #endif
571 
572 
GWENHYWFAR_API void * GWEN_TreeElement_GetLastChild(const GWEN_TREE_ELEMENT *el)
struct GWEN_TREE_ELEMENT GWEN_TREE_ELEMENT
Definition: tree.h:156
GWENHYWFAR_API void * GWEN_Tree_GetFirst(const GWEN_TREE *l)
GWENHYWFAR_API void * GWEN_TreeElement_GetParent(const GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void GWEN_Tree_AddList(GWEN_TREE *dest, GWEN_TREE *l)
GWENHYWFAR_API int GWEN_Tree_GetCount(const GWEN_TREE *l)
GWENHYWFAR_API void GWEN_Tree_free(GWEN_TREE *l)
GWENHYWFAR_API void * GWEN_TreeElement_GetFirstChild(const GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void GWEN_Tree_InsertChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void * GWEN_Tree_GetLast(const GWEN_TREE *l)
GWENHYWFAR_API void GWEN_Tree_Del(GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void * GWEN_TreeElement_GetNext(const GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void * GWEN_TreeElement_GetPrevious(const GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API GWEN_TREE_ELEMENT * GWEN_TreeElement_new(void *d)
GWENHYWFAR_API void GWEN_Tree_AddChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API uint32_t GWEN_TreeElement_GetChildrenCount(const GWEN_TREE_ELEMENT *el)
#define GWENHYWFAR_API
Definition: gwenhywfarapi.h:67
GWENHYWFAR_API GWEN_TREE * GWEN_Tree_new(void)
GWENHYWFAR_API void GWEN_Tree_Insert(GWEN_TREE *l, GWEN_TREE_ELEMENT *el)
struct GWEN_TREE GWEN_TREE
Definition: tree.h:155
GWENHYWFAR_API void * GWEN_TreeElement_GetBelow(const GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void GWEN_TreeElement_free(GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void GWEN_Tree_Add(GWEN_TREE *l, GWEN_TREE_ELEMENT *el)