Tree.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2013 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial implementation and documentation
  15. *******************************************************************************/
  16. #if !defined(TREE_H)
  17. #define TREE_H
  18. #include <stdlib.h> /* for size_t definition */
  19. /*BE
  20. defm defTree(T) // macro to define a tree
  21. def T concat Node
  22. {
  23. n32 ptr T concat Node "parent"
  24. n32 ptr T concat Node "left"
  25. n32 ptr T concat Node "right"
  26. n32 ptr T id2str(T)
  27. n32 suppress "size"
  28. }
  29. def T concat Tree
  30. {
  31. struct
  32. {
  33. n32 ptr T concat Node suppress "root"
  34. n32 ptr DATA suppress "compare"
  35. }
  36. struct
  37. {
  38. n32 ptr T concat Node suppress "root"
  39. n32 ptr DATA suppress "compare"
  40. }
  41. n32 dec "count"
  42. n32 dec suppress "size"
  43. }
  44. endm
  45. defTree(INT)
  46. defTree(STRING)
  47. defTree(TMP)
  48. BE*/
  49. /**
  50. * Structure to hold all data for one list element
  51. */
  52. typedef struct NodeStruct
  53. {
  54. struct NodeStruct *parent, /**< pointer to parent tree node, in case we need it */
  55. *child[2]; /**< pointers to child tree nodes 0 = left, 1 = right */
  56. void* content; /**< pointer to element content */
  57. size_t size; /**< size of content */
  58. unsigned int red : 1;
  59. } Node;
  60. /**
  61. * Structure to hold all data for one tree
  62. */
  63. typedef struct
  64. {
  65. struct
  66. {
  67. Node *root; /**< root node pointer */
  68. int (*compare)(void*, void*, int); /**< comparison function */
  69. } index[2];
  70. int indexes, /**< no of indexes into tree */
  71. count; /**< no of items */
  72. size_t size; /**< heap storage used */
  73. unsigned int heap_tracking : 1; /**< switch on heap tracking for this tree? */
  74. unsigned int allow_duplicates : 1; /**< switch to allow duplicate entries */
  75. } Tree;
  76. Tree* TreeInitialize(int(*compare)(void*, void*, int));
  77. void TreeInitializeNoMalloc(Tree* aTree, int(*compare)(void*, void*, int));
  78. void TreeAddIndex(Tree* aTree, int(*compare)(void*, void*, int));
  79. void* TreeAdd(Tree* aTree, void* content, size_t size);
  80. void* TreeRemove(Tree* aTree, void* content);
  81. void* TreeRemoveKey(Tree* aTree, void* key);
  82. void* TreeRemoveKeyIndex(Tree* aTree, void* key, int index);
  83. void* TreeRemoveNodeIndex(Tree* aTree, Node* aNode, int index);
  84. void TreeFree(Tree* aTree);
  85. Node* TreeFind(Tree* aTree, void* key);
  86. Node* TreeFindIndex(Tree* aTree, void* key, int index);
  87. Node* TreeNextElement(Tree* aTree, Node* curnode);
  88. int TreeIntCompare(void* a, void* b, int);
  89. int TreePtrCompare(void* a, void* b, int);
  90. int TreeStringCompare(void* a, void* b, int);
  91. #endif