| 55 | // static sTree_tuple * sedgewickized_splay (int i, sTree_tuple * t); |
| 56 | |
| 57 | sTree_tuple *splay_t(splay_key_type_t i, sTree_tuple *t) { |
| 58 | /* Simple top down splay, not requiring i to be in the sTree t. */ |
| 59 | /* What it does is described above. */ |
| 60 | sTree_tuple N, *l, *r, *y; |
| 61 | if (t == NULL) return t; |
| 62 | N.left = N.right = NULL; |
| 63 | l = r = &N; |
| 64 | long l_size = 0, r_size = 0; |
| 65 | |
| 66 | for (;;) { |
| 67 | if (key_cmp_t(i, t->key) < 0) { |
| 68 | if (t->left == NULL) break; |
| 69 | if (key_cmp_t(i, t->left->key) < 0) { |
| 70 | y = t->left; /* rotate right */ |
| 71 | t->left = y->right; |
| 72 | y->right = t; |
| 73 | t->value = node_value_t(t->left) + node_value_t(t->right) + 1; |
| 74 | t = y; |
| 75 | if (t->left == NULL) break; |
| 76 | } |
| 77 | r->left = t; /* link right */ |
| 78 | r = t; |
| 79 | t = t->left; |
| 80 | r_size += 1 + node_value_t(r->right); |
| 81 | } else if (key_cmp_t(i, t->key) > 0) { |
| 82 | if (t->right == NULL) break; |
| 83 | if (key_cmp_t(i, t->right->key) > 0) { |
| 84 | y = t->right; /* rotate left */ |
| 85 | t->right = y->left; |
| 86 | y->left = t; |
| 87 | t->value = node_value_t(t->left) + node_value_t(t->right) + 1; |
| 88 | t = y; |
| 89 | if (t->right == NULL) break; |
| 90 | } |
| 91 | l->right = t; /* link left */ |
| 92 | l = t; |
| 93 | t = t->right; |
| 94 | l_size += 1 + node_value_t(l->left); |
| 95 | } else { |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // TODO: there should be a better way to do this!!!!!!!!! |
| 101 | |
| 102 | l_size += node_value_t(t->left); /* Now l_size and r_size are the sizes of */ |
| 103 | r_size += node_value_t(t->right); /* the left and right sTrees we just built.*/ |
| 104 | t->value = l_size + r_size + 1; |
| 105 | |
| 106 | l->right = r->left = NULL; |
| 107 | |
| 108 | /* The following two loops correct the size fields of the right path */ |
| 109 | /* from the left child of the root and the right path from the left */ |
| 110 | /* child of the root. */ |
| 111 | for (y = N.right; y != NULL; y = y->right) { |
| 112 | y->value = l_size; |
| 113 | l_size -= 1 + node_value_t(y->left); |
| 114 | } |
no outgoing calls
no test coverage detected