tree.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. ** tree.c
  3. ** TecCGraf - PUC-Rio
  4. */
  5. char *rcs_tree="$Id: tree.c,v 1.3 1994/11/10 20:41:37 roberto Exp roberto $";
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "lua.h"
  9. #include "tree.h"
  10. #include "table.h"
  11. #define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
  12. static TreeNode *string_root = NULL;
  13. static TreeNode *constant_root = NULL;
  14. /*
  15. ** Insert a new string/constant/variable at the tree.
  16. */
  17. static TreeNode *tree_create (TreeNode **node, char *str, int *created)
  18. {
  19. if (*node == NULL)
  20. {
  21. *node = (TreeNode *) malloc (sizeof(TreeNode)+strlen(str));
  22. if (*node == NULL)
  23. lua_error("not enough memory");
  24. (*node)->left = (*node)->right = NULL;
  25. strcpy((*node)->str, str);
  26. (*node)->varindex = (*node)->constindex = UNMARKED_STRING;
  27. *created = 1;
  28. return *node;
  29. }
  30. else
  31. {
  32. int c = lua_strcmp(str, (*node)->str);
  33. if (c < 0)
  34. return tree_create(&(*node)->left, str, created);
  35. else if (c > 0)
  36. return tree_create(&(*node)->right, str, created);
  37. else
  38. return *node;
  39. }
  40. }
  41. char *lua_strcreate (char *str)
  42. {
  43. int created=0;
  44. TreeNode *t = tree_create(&string_root, str, &created);
  45. if (created)
  46. {
  47. if (lua_nentity == lua_block) lua_pack ();
  48. lua_nentity++;
  49. }
  50. return t->str;
  51. }
  52. TreeNode *lua_constcreate (char *str)
  53. {
  54. int created;
  55. return tree_create(&constant_root, str, &created);
  56. }
  57. /*
  58. ** Free a node of the tree
  59. */
  60. static TreeNode *lua_strfree (TreeNode *parent)
  61. {
  62. if (parent->left == NULL && parent->right == NULL) /* no child */
  63. {
  64. free (parent);
  65. return NULL;
  66. }
  67. else if (parent->left == NULL) /* only right child */
  68. {
  69. TreeNode *p = parent->right;
  70. free (parent);
  71. return p;
  72. }
  73. else if (parent->right == NULL) /* only left child */
  74. {
  75. TreeNode *p = parent->left;
  76. free (parent);
  77. return p;
  78. }
  79. else /* two children */
  80. {
  81. TreeNode *p = parent, *r = parent->right;
  82. while (r->left != NULL)
  83. {
  84. p = r;
  85. r = r->left;
  86. }
  87. if (p == parent)
  88. {
  89. r->left = parent->left;
  90. parent->left = NULL;
  91. parent->right = r->right;
  92. r->right = lua_strfree(parent);
  93. }
  94. else
  95. {
  96. TreeNode *t = r->right;
  97. r->left = parent->left;
  98. r->right = parent->right;
  99. parent->left = NULL;
  100. parent->right = t;
  101. p->left = lua_strfree(parent);
  102. }
  103. return r;
  104. }
  105. }
  106. /*
  107. ** Traverse tree for garbage collection
  108. */
  109. static TreeNode *lua_travcollector (TreeNode *r)
  110. {
  111. if (r == NULL) return NULL;
  112. r->right = lua_travcollector(r->right);
  113. r->left = lua_travcollector(r->left);
  114. if (r->constindex == UNMARKED_STRING)
  115. {
  116. ++lua_recovered;
  117. return lua_strfree(r);
  118. }
  119. else
  120. {
  121. r->constindex = UNMARKED_STRING;
  122. return r;
  123. }
  124. }
  125. /*
  126. ** Garbage collection function.
  127. ** This function traverse the tree freening unindexed strings
  128. */
  129. void lua_strcollector (void)
  130. {
  131. string_root = lua_travcollector(string_root);
  132. }
  133. /*
  134. ** Return next variable.
  135. */
  136. static TreeNode *tree_next (TreeNode *node, char *str)
  137. {
  138. if (node == NULL) return NULL;
  139. else if (str == NULL) return node;
  140. else
  141. {
  142. int c = lua_strcmp(str, node->str);
  143. if (c == 0)
  144. return node->left != NULL ? node->left : node->right;
  145. else if (c < 0)
  146. {
  147. TreeNode *result = tree_next(node->left, str);
  148. return result != NULL ? result : node->right;
  149. }
  150. else
  151. return tree_next(node->right, str);
  152. }
  153. }
  154. char *lua_varnext (char *n)
  155. {
  156. TreeNode *result = tree_next(constant_root, n);
  157. return result != NULL ? result->str : NULL;
  158. }