tsearch.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "common.h"
  2. #include "tsearch.h"
  3. static void hc_maybe_split_for_insert (hc_node *rootp, hc_node *parentp, hc_node *gparentp, int p_r, int gp_r, int mode)
  4. {
  5. hc_node root = *rootp;
  6. hc_node *rp, *lp;
  7. rp = &(*rootp)->right;
  8. lp = &(*rootp)->left;
  9. /* See if we have to split this node (both successors red). */
  10. if ((mode == 1) || ((*rp) != NULL && (*lp) != NULL && (*rp)->red && (*lp)->red))
  11. {
  12. /* This node becomes red, its successors black. */
  13. root->red = 1;
  14. if (*rp) (*rp)->red = 0;
  15. if (*lp) (*lp)->red = 0;
  16. /* If the parent of this node is also red, we have to do rotations. */
  17. if (parentp != NULL && (*parentp)->red)
  18. {
  19. hc_node gp = *gparentp;
  20. hc_node p = *parentp;
  21. /* There are two main cases:
  22. 1. The edge types (left or right) of the two red edges differ.
  23. 2. Both red edges are of the same type.
  24. There exist two symmetries of each case, so there is a total of 4 cases. */
  25. if ((p_r > 0) != (gp_r > 0))
  26. {
  27. /* Put the child at the top of the tree, with its parent and grandparent as successors. */
  28. p->red = 1;
  29. gp->red = 1;
  30. root->red = 0;
  31. if (p_r < 0)
  32. {
  33. /* Child is left of parent. */
  34. p->left = *rp;
  35. *rp = p;
  36. gp->right = *lp;
  37. *lp = gp;
  38. }
  39. else
  40. {
  41. /* Child is right of parent. */
  42. p->right = *lp;
  43. *lp = p;
  44. gp->left = *rp;
  45. *rp = gp;
  46. }
  47. *gparentp = root;
  48. }
  49. else
  50. {
  51. *gparentp = *parentp;
  52. /* Parent becomes the top of the tree, grandparent and child are its successors. */
  53. p->red = 0;
  54. gp->red = 1;
  55. if (p_r < 0)
  56. {
  57. /* Left edges. */
  58. gp->left = p->right;
  59. p->right = gp;
  60. }
  61. else
  62. {
  63. /* Right edges. */
  64. gp->right = p->left;
  65. p->left = gp;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. /*
  72. * Find or insert datum into search tree.
  73. * KEY is the key to be located, ROOTP is the address of tree root,
  74. * COMPAR the ordering function.
  75. */
  76. void * __hc_tsearch (const void *key, void **vrootp, __hc_compar_fn_t compar)
  77. {
  78. hc_node q;
  79. hc_node *parentp = NULL, *gparentp = NULL;
  80. hc_node *rootp = (hc_node *) vrootp;
  81. hc_node *nextp;
  82. int r = 0, p_r = 0, gp_r = 0; /* No they might not, Mr Compiler. */
  83. if (rootp == NULL) return NULL;
  84. /* This saves some additional tests below. */
  85. if (*rootp != NULL) (*rootp)->red = 0;
  86. nextp = rootp;
  87. while (*nextp != NULL)
  88. {
  89. hc_node root = *rootp;
  90. r = (*compar) (key, root->key);
  91. if (r == 0) return root;
  92. hc_maybe_split_for_insert (rootp, parentp, gparentp, p_r, gp_r, 0);
  93. /*
  94. * If that did any rotations, parentp and gparentp are now garbage.
  95. * That doesn't matter, because the values they contain are never
  96. * used again in that case.
  97. */
  98. nextp = (r < 0) ? &root->left : &root->right;
  99. if (*nextp == NULL) break;
  100. gparentp = parentp;
  101. parentp = rootp;
  102. rootp = nextp;
  103. gp_r = p_r;
  104. p_r = r;
  105. }
  106. q = (struct hc_node_t *) malloc (sizeof (struct hc_node_t));
  107. if (q != NULL)
  108. {
  109. *nextp = q; /* link new node to old */
  110. q->key = key; /* initialize new node */
  111. q->red = 1;
  112. q->left = q->right = NULL;
  113. /*
  114. * There may be two red edges in a row now, which we must avoid by
  115. * rotating the tree.
  116. */
  117. if (nextp != rootp) hc_maybe_split_for_insert (nextp, rootp, parentp, r, p_r, 1);
  118. }
  119. return q;
  120. }
  121. /* Find datum in search tree.
  122. * KEY is the key to be located, ROOTP is the address of tree root,
  123. * COMPAR the ordering function.
  124. */
  125. void * __hc_tfind (const void *key, void *const *vrootp, __hc_compar_fn_t compar)
  126. {
  127. int r;
  128. hc_node *rootp = (hc_node *) vrootp;
  129. if (rootp == NULL) return NULL;
  130. while (*rootp != NULL)
  131. {
  132. hc_node root = *rootp;
  133. r = (*compar) (key, root->key);
  134. if (r == 0) return root;
  135. rootp = (r < 0) ? &root->left : &root->right;
  136. }
  137. return NULL;
  138. }
  139. static void hc_tdestroy_recurse (hc_node root, __hc_free_fn_t freefct)
  140. {
  141. if (root->left != NULL) hc_tdestroy_recurse (root->left, freefct);
  142. if (root->right != NULL) hc_tdestroy_recurse (root->right, freefct);
  143. (*freefct) ((void *) root->key);
  144. /* Free the node itself. */
  145. free (root);
  146. }
  147. void __hc_tdestroy (void *vroot, __hc_free_fn_t freefct)
  148. {
  149. hc_node root = (hc_node) vroot;
  150. if (root != NULL) hc_tdestroy_recurse (root, freefct);
  151. }