gwenhywfar  5.11.1beta
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
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;
156 typedef struct GWEN_TREE_ELEMENT GWEN_TREE_ELEMENT;
157 
158 
162 
168 
174 
179 
185 
192 
193 
199 void GWEN_Tree_Replace(GWEN_TREE_ELEMENT *elToReplace, GWEN_TREE_ELEMENT *elReplacement);
200 
208 
212 
216 
217 
221 
224 void *GWEN_Tree_GetLast(const GWEN_TREE *l);
225 
226 
227 
231 
235 
241 
247 
258 
262 
266 
269 
273 
281 
286 #define GWEN_TREE_ELEMENT(t) \
287 GWEN_TREE_ELEMENT *_tree_element;
288 
295 #define GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
296  typedef GWEN_TREE t##_TREE; \
297  \
298  decl t* pr##_Tree_GetFirst(const t##_TREE *l); \
299  decl t* pr##_Tree_GetLast(const t##_TREE *l); \
300  decl t* pr##_Tree_GetNext(const t *element); \
301  decl t* pr##_Tree_GetPrevious(const t *element); \
302  decl t* pr##_Tree_GetBelow(const t *element); \
303  decl uint32_t pr##_Tree_GetCount(const t##_TREE *l); \
304  decl int pr##_Tree_HasElement(const t##_TREE *l, const t *element); \
305  decl t* pr##_Tree_GetFirstChild(const t *element); \
306  decl t* pr##_Tree_GetLastChild(const t *element); \
307  decl uint32_t pr##_Tree_GetChildrenCount(const t *element); \
308  decl t* pr##_Tree_GetParent(const t *element);
309 
310 
311 #define GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl) \
312  typedef GWEN_TREE_ELEMENT t##_TREE_ELEMENT; \
313  \
314  decl void pr##_Tree_Clear(t##_TREE *l); \
315  decl t##_TREE* pr##_Tree_new(); \
316  decl void pr##_Tree_free(t##_TREE *l); \
317  decl void pr##_Tree_AddList(t##_TREE *dst, t##_TREE *l); \
318  decl void pr##_Tree_Add(t##_TREE *list, t *element); \
319  decl void pr##_Tree_Insert(t##_TREE *list, t *element); \
320  decl void pr##_Tree_Del(t *element); \
321  decl void pr##_Tree_Replace(t *elToReplace, t *elReplacement); \
322  \
323  decl void pr##_Tree_AddChild(t *where, t *element); \
324  decl void pr##_Tree_InsertChild(t *where, t *element); \
325  \
326  decl int pr##_Tree_HasChildElement(const t *who, const t *element); \
327  decl void pr##_Tree_ClearChildren(t *element); \
328 
329 
330 #define GWEN_TREE_FUNCTION_DEFS_CONST(t, pr) \
331  GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
332 
333 #define GWEN_TREE_FUNCTION_DEFS_NOCONST(t, pr) \
334  GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, GWEN_DUMMY_EMPTY_ARG)
335 
336 
384 #define GWEN_TREE_FUNCTION_LIB_DEFS(t, pr, decl) \
385  GWEN_TREE_FUNCTION_LIB_DEFS_CONST(t, pr, decl) \
386  GWEN_TREE_FUNCTION_LIB_DEFS_NOCONST(t, pr, decl)
387 
388 
393 #define GWEN_TREE_FUNCTION_DEFS(t, pr) \
394  GWEN_TREE_FUNCTION_LIB_DEFS(t, pr, GWEN_DUMMY_EMPTY_ARG)
395 
396 
402 #define GWEN_TREE_FUNCTIONS(t, pr) \
403  \
404  void pr##_Tree_Add(t##_TREE *l, t *element) { \
405  assert(element); \
406  assert(element->_tree_element);\
407  GWEN_Tree_Add(l, element->_tree_element); \
408  } \
409  \
410  void pr##_Tree_AddList(t##_TREE *dst, t##_TREE *l) { \
411  GWEN_Tree_AddList(dst, l); \
412  } \
413  \
414  void pr##_Tree_Insert(t##_TREE *l, t *element) { \
415  assert(element); \
416  assert(element->_tree_element);\
417  GWEN_Tree_Insert(l, element->_tree_element); \
418  } \
419  \
420  void pr##_Tree_Del(t *element){ \
421  assert(element); \
422  assert(element->_tree_element);\
423  GWEN_Tree_Del(element->_tree_element); \
424  }\
425  \
426  void pr##_Tree_Replace(t *elToReplace, t *elReplacement) { \
427  assert(elToReplace); \
428  assert(elToReplace->_tree_element);\
429  assert(elReplacement); \
430  assert(elReplacement->_tree_element);\
431  GWEN_Tree_Replace(elToReplace->_tree_element, elReplacement->_tree_element); \
432  } \
433  \
434  t* pr##_Tree_GetFirst(const t##_TREE *l) { \
435  if (l) return (t*)GWEN_Tree_GetFirst(l);\
436  else return 0; \
437  } \
438  \
439  t* pr##_Tree_GetLast(const t##_TREE *l) { \
440  if (l) return (t*) GWEN_Tree_GetLast(l);\
441  else return 0; \
442  } \
443  \
444  void pr##_Tree_Clear(t##_TREE *l) { \
445  t* el; \
446  while( (el=GWEN_Tree_GetFirst(l)) ) {\
447  pr##_Tree_Del(el);\
448  pr##_Tree_ClearChildren(el); \
449  pr##_free(el);\
450  } /* while */ \
451  } \
452  \
453  int pr##_Tree_HasElement(const t##_TREE *l, const t *element) { \
454  const t* el; \
455  el=(t*)GWEN_Tree_GetFirst(l); \
456  while(el) {\
457  if (el==element) \
458  return 1; \
459  el=(const t*)GWEN_TreeElement_GetBelow(el->_tree_element); \
460  } /* while */ \
461  return 0; \
462  } \
463  \
464  t##_TREE* pr##_Tree_new(){\
465  return (t##_TREE*)GWEN_Tree_new(); \
466  }\
467  \
468  void pr##_Tree_free(t##_TREE *l) {\
469  if (l) { \
470  pr##_Tree_Clear(l);\
471  GWEN_Tree_free(l); \
472  }\
473  } \
474  \
475  t* pr##_Tree_GetNext(const t *element) { \
476  assert(element); \
477  assert(element->_tree_element);\
478  return (t*)GWEN_TreeElement_GetNext(element->_tree_element);\
479  } \
480  \
481  t* pr##_Tree_GetPrevious(const t *element) { \
482  assert(element); \
483  assert(element->_tree_element);\
484  return (t*)GWEN_TreeElement_GetPrevious(element->_tree_element);\
485  } \
486  \
487  t* pr##_Tree_GetBelow(const t *element) { \
488  assert(element); \
489  assert(element->_tree_element);\
490  return (t*)GWEN_TreeElement_GetBelow(element->_tree_element);\
491  } \
492  \
493  uint32_t pr##_Tree_GetCount(const t##_TREE *l){\
494  return GWEN_Tree_GetCount(l);\
495  } \
496  \
497  int pr##_Tree_HasChildElement(const t *who, const t *element) { \
498  const t* el; \
499  el=(const t*)GWEN_TreeElement_GetFirstChild(who->_tree_element); \
500  while(el) {\
501  if (el==element) \
502  return 1; \
503  el=(const t*)GWEN_TreeElement_GetNext(el->_tree_element); \
504  } /* while */ \
505  return 0; \
506  } \
507  \
508  void pr##_Tree_AddChild(t *where, t *element) { \
509  assert(where); \
510  assert(where->_tree_element);\
511  assert(element); \
512  assert(element->_tree_element);\
513  GWEN_Tree_AddChild(where->_tree_element, element->_tree_element); \
514  } \
515  \
516  void pr##_Tree_InsertChild(t *where, t *element) { \
517  assert(where); \
518  assert(where->_tree_element);\
519  assert(element); \
520  assert(element->_tree_element);\
521  GWEN_Tree_InsertChild(where->_tree_element, element->_tree_element); \
522  } \
523  \
524  void pr##_Tree_ClearChildren(t *element) { \
525  t* c; \
526  while( (c=GWEN_TreeElement_GetFirstChild(element->_tree_element)) ) {\
527  pr##_Tree_ClearChildren(c);\
528  pr##_Tree_Del(c);\
529  pr##_free(c);\
530  } /* while */ \
531  } \
532  \
533  t* pr##_Tree_GetFirstChild(const t *element) { \
534  assert(element); \
535  assert(element->_tree_element);\
536  return (t*)GWEN_TreeElement_GetFirstChild(element->_tree_element);\
537  } \
538  \
539  t* pr##_Tree_GetLastChild(const t *element) { \
540  assert(element); \
541  assert(element->_tree_element);\
542  return (t*)GWEN_TreeElement_GetLastChild(element->_tree_element);\
543  } \
544  \
545  uint32_t pr##_Tree_GetChildrenCount(const t *element){\
546  return GWEN_TreeElement_GetChildrenCount(element->_tree_element);\
547  } \
548  \
549  t* pr##_Tree_GetParent(const t *element) { \
550  assert(element); \
551  assert(element->_tree_element);\
552  return (t*)GWEN_TreeElement_GetParent(element->_tree_element);\
553  } \
554  \
555 
556 
562 #define GWEN_TREE_INIT(t, element) \
563  element->_tree_element=GWEN_TreeElement_new(element);
564 
565 
571 #define GWEN_TREE_FINI(t, element) \
572  if (element && element->_tree_element) { \
573  GWEN_TreeElement_free(element->_tree_element); \
574  element->_tree_element=0; \
575  }
576  /* defgroup */
580 
581 
582 #ifdef __cplusplus
583 }
584 #endif
585 
586 
587 #endif
588 
589 
#define GWENHYWFAR_API
Definition: gwenhywfarapi.h:67
GWENHYWFAR_API void GWEN_Tree_Add(GWEN_TREE *l, GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void * GWEN_TreeElement_GetPrevious(const GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API uint32_t GWEN_TreeElement_GetChildrenCount(const GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API GWEN_TREE * GWEN_Tree_new(void)
GWENHYWFAR_API void GWEN_Tree_Replace(GWEN_TREE_ELEMENT *elToReplace, GWEN_TREE_ELEMENT *elReplacement)
GWENHYWFAR_API void GWEN_Tree_Del(GWEN_TREE_ELEMENT *el)
struct GWEN_TREE_ELEMENT GWEN_TREE_ELEMENT
Definition: tree.h:156
GWENHYWFAR_API void * GWEN_TreeElement_GetLastChild(const GWEN_TREE_ELEMENT *el)
struct GWEN_TREE GWEN_TREE
Definition: tree.h:155
GWENHYWFAR_API void * GWEN_TreeElement_GetNext(const GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void GWEN_Tree_AddList(GWEN_TREE *dest, 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 int GWEN_Tree_GetCount(const GWEN_TREE *l)
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_free(GWEN_TREE *l)
GWENHYWFAR_API void * GWEN_TreeElement_GetParent(const GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void * GWEN_Tree_GetLast(const GWEN_TREE *l)
GWENHYWFAR_API GWEN_TREE_ELEMENT * GWEN_TreeElement_new(void *d)
GWENHYWFAR_API void GWEN_Tree_Insert(GWEN_TREE *l, GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void GWEN_Tree_AddChild(GWEN_TREE_ELEMENT *where, GWEN_TREE_ELEMENT *el)
GWENHYWFAR_API void * GWEN_Tree_GetFirst(const GWEN_TREE *l)