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

Function insert

libCacheSim/dataStructure/splay.c:158–193  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

156}
157
158sTree * insert(key_type i, sTree * t) {
159 /* Insert i into the sTree t, unless it's already there. */
160 /* Return a pointer to the resulting sTree. */
161 sTree * new;
162
163 new = (sTree *) malloc (sizeof (sTree));
164 if (new == NULL) {
165 printf("Ran out of space\n");
166 exit(1);
167 }
168 assign_key(new, i);
169 new->value = 1;
170 if (t == NULL) {
171 new->left = new->right = NULL;
172 return new;
173 }
174 t = splay(i,t);
175 if (key_cmp(i, t->key) < 0) {
176 new->left = t->left;
177 new->right = t;
178 t->left = NULL;
179 t->value = 1 + node_value(t->right);
180 } else if (key_cmp(i, t->key) > 0) {
181 new->right = t->right;
182 new->left = t;
183 t->right = NULL;
184 t->value = 1 + node_value(t->left);
185 } else { /* We get here if it's already in the sTree */
186 /* Don't add it again */
187 free_node(new);
188 assert (t->value == 1 + node_value(t->left) + node_value(t->right));
189 return t;
190 }
191 new->value = 1 + node_value(new->left) + node_value(new->right);
192 return new;
193}
194
195sTree * splay_delete(key_type i, sTree * t) {
196 /* Deletes i from the sTree if it's there. */

Callers 6

CandidateMapClass · 0.85
TableMethod · 0.85
insertMethod · 0.85
insertMethod · 0.85
insertMethod · 0.85
get_stack_dist_add_reqFunction · 0.85

Calls 5

assign_keyFunction · 0.85
splayFunction · 0.85
node_valueFunction · 0.85
free_nodeFunction · 0.85
assertFunction · 0.85

Tested by

no test coverage detected