inttree.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #ifndef YASM_INTTREE_H
  2. #define YASM_INTTREE_H
  3. #ifndef YASM_LIB_DECL
  4. #define YASM_LIB_DECL
  5. #endif
  6. /* The interval_tree.h and interval_tree.cc files contain code for
  7. * interval trees implemented using red-black-trees as described in
  8. * the book _Introduction_To_Algorithms_ by Cormen, Leisserson,
  9. * and Rivest.
  10. */
  11. typedef struct IntervalTreeNode {
  12. struct IntervalTreeNode *left, *right, *parent;
  13. void *data;
  14. long low;
  15. long high;
  16. long maxHigh;
  17. int red; /* if red=0 then the node is black */
  18. } IntervalTreeNode;
  19. typedef struct it_recursion_node {
  20. /* This structure stores the information needed when we take the
  21. * right branch in searching for intervals but possibly come back
  22. * and check the left branch as well.
  23. */
  24. IntervalTreeNode *start_node;
  25. unsigned int parentIndex;
  26. int tryRightBranch;
  27. } it_recursion_node;
  28. typedef struct IntervalTree {
  29. /* A sentinel is used for root and for nil. These sentinels are
  30. * created when ITTreeCreate is called. root->left should always
  31. * point to the node which is the root of the tree. nil points to a
  32. * node which should always be black but has aribtrary children and
  33. * parent and no key or info. The point of using these sentinels is so
  34. * that the root and nil nodes do not require special cases in the code
  35. */
  36. IntervalTreeNode *root;
  37. IntervalTreeNode *nil;
  38. /*private:*/
  39. unsigned int recursionNodeStackSize;
  40. it_recursion_node * recursionNodeStack;
  41. unsigned int currentParent;
  42. unsigned int recursionNodeStackTop;
  43. } IntervalTree;
  44. YASM_LIB_DECL
  45. IntervalTree *IT_create(void);
  46. YASM_LIB_DECL
  47. void IT_destroy(IntervalTree *);
  48. YASM_LIB_DECL
  49. void IT_print(const IntervalTree *);
  50. YASM_LIB_DECL
  51. void *IT_delete_node(IntervalTree *, IntervalTreeNode *, long *low,
  52. long *high);
  53. YASM_LIB_DECL
  54. IntervalTreeNode *IT_insert(IntervalTree *, long low, long high, void *data);
  55. YASM_LIB_DECL
  56. IntervalTreeNode *IT_get_predecessor(const IntervalTree *, IntervalTreeNode *);
  57. YASM_LIB_DECL
  58. IntervalTreeNode *IT_get_successor(const IntervalTree *, IntervalTreeNode *);
  59. YASM_LIB_DECL
  60. void IT_enumerate(IntervalTree *, long low, long high, void *cbd,
  61. void (*callback) (IntervalTreeNode *node, void *cbd));
  62. #endif