MCPcopy Create free account
hub / github.com/1a1a11a/libCacheSim / splay

Function splay

libCacheSim/dataStructure/splay.c:61–129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 6

insertFunction · 0.85
splay_deleteFunction · 0.85
insertMethod · 0.85
find_Method · 0.85
removeMethod · 0.85
get_stack_dist_add_reqFunction · 0.85

Calls 1

node_valueFunction · 0.85

Tested by

no test coverage detected