rotatingtree.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "rotatingtree.h"
  2. #define KEY_LOWER_THAN(key1, key2) ((char*)(key1) < (char*)(key2))
  3. /* The randombits() function below is a fast-and-dirty generator that
  4. * is probably irregular enough for our purposes. Note that it's biased:
  5. * I think that ones are slightly more probable than zeroes. It's not
  6. * important here, though.
  7. */
  8. static unsigned int random_value = 1;
  9. static unsigned int random_stream = 0;
  10. static int
  11. randombits(int bits)
  12. {
  13. int result;
  14. if (random_stream < (1U << bits)) {
  15. random_value *= 1082527;
  16. random_stream = random_value;
  17. }
  18. result = random_stream & ((1<<bits)-1);
  19. random_stream >>= bits;
  20. return result;
  21. }
  22. /* Insert a new node into the tree.
  23. (*root) is modified to point to the new root. */
  24. void
  25. RotatingTree_Add(rotating_node_t **root, rotating_node_t *node)
  26. {
  27. while (*root != NULL) {
  28. if (KEY_LOWER_THAN(node->key, (*root)->key))
  29. root = &((*root)->left);
  30. else
  31. root = &((*root)->right);
  32. }
  33. node->left = NULL;
  34. node->right = NULL;
  35. *root = node;
  36. }
  37. /* Locate the node with the given key. This is the most complicated
  38. function because it occasionally rebalances the tree to move the
  39. resulting node closer to the root. */
  40. rotating_node_t *
  41. RotatingTree_Get(rotating_node_t **root, void *key)
  42. {
  43. if (randombits(3) != 4) {
  44. /* Fast path, no rebalancing */
  45. rotating_node_t *node = *root;
  46. while (node != NULL) {
  47. if (node->key == key)
  48. return node;
  49. if (KEY_LOWER_THAN(key, node->key))
  50. node = node->left;
  51. else
  52. node = node->right;
  53. }
  54. return NULL;
  55. }
  56. else {
  57. rotating_node_t **pnode = root;
  58. rotating_node_t *node = *pnode;
  59. rotating_node_t *next;
  60. int rotate;
  61. if (node == NULL)
  62. return NULL;
  63. while (1) {
  64. if (node->key == key)
  65. return node;
  66. rotate = !randombits(1);
  67. if (KEY_LOWER_THAN(key, node->key)) {
  68. next = node->left;
  69. if (next == NULL)
  70. return NULL;
  71. if (rotate) {
  72. node->left = next->right;
  73. next->right = node;
  74. *pnode = next;
  75. }
  76. else
  77. pnode = &(node->left);
  78. }
  79. else {
  80. next = node->right;
  81. if (next == NULL)
  82. return NULL;
  83. if (rotate) {
  84. node->right = next->left;
  85. next->left = node;
  86. *pnode = next;
  87. }
  88. else
  89. pnode = &(node->right);
  90. }
  91. node = next;
  92. }
  93. }
  94. }
  95. /* Enumerate all nodes in the tree. The callback enumfn() should return
  96. zero to continue the enumeration, or non-zero to interrupt it.
  97. A non-zero value is directly returned by RotatingTree_Enum(). */
  98. int
  99. RotatingTree_Enum(rotating_node_t *root, rotating_tree_enum_fn enumfn,
  100. void *arg)
  101. {
  102. int result;
  103. rotating_node_t *node;
  104. while (root != NULL) {
  105. result = RotatingTree_Enum(root->left, enumfn, arg);
  106. if (result != 0) return result;
  107. node = root->right;
  108. result = enumfn(root, arg);
  109. if (result != 0) return result;
  110. root = node;
  111. }
  112. return 0;
  113. }