Fix child insertion

This commit is contained in:
c9s 2014-05-18 19:09:47 +08:00
parent 7d6113d17d
commit f039cc3c8f
4 changed files with 22 additions and 13 deletions

View file

@ -132,7 +132,7 @@ bool r3_node_has_slug_edges(node *n);
edge * r3_edge_create(char * pattern, int pattern_len, node * child);
void r3_edge_branch(edge *e, int dl);
node * r3_edge_branch(edge *e, int dl);
void r3_edge_free(edge * edge);

View file

@ -38,13 +38,14 @@ edge * r3_edge_create(char * pattern, int pattern_len, node * child) {
* branch the edge pattern at "dl" offset,
* insert a dummy child between the edges.
*
* parent -> [edge1] -> childA
* parent -> [edge1] -> childB -> edge2 ->child A
*
* A -> [prefix..suffix] -> B
* A -> [prefix] -> C -> [suffix] -> B
*
*/
void r3_edge_branch(edge *e, int dl) {
node *c1; // child 1, child 2
edge *e1; // edge 1, edge 2
node * r3_edge_branch(edge *e, int dl) {
node *new_child;
edge *e1;
char * s1 = e->pattern + dl;
int s1_len = 0;
@ -52,14 +53,14 @@ void r3_edge_branch(edge *e, int dl) {
int tmp_edge_len = e->child->edge_len;
// the suffix edge of the leaf
c1 = r3_tree_create(3);
new_child = r3_tree_create(3);
s1_len = e->pattern_len - dl;
e1 = r3_edge_create(strndup(s1, s1_len), s1_len, c1);
e1 = r3_edge_create(strndup(s1, s1_len), s1_len, new_child);
// printf("edge left: %s\n", e1->pattern);
// Migrate the child edges to the new edge we just created.
for ( int i = 0 ; i < tmp_edge_len ; i++ ) {
r3_node_append_edge(c1, tmp_edges[i]);
r3_node_append_edge(new_child, tmp_edges[i]);
e->child->edges[i] = NULL;
}
e->child->edge_len = 0;
@ -68,8 +69,8 @@ void r3_edge_branch(edge *e, int dl) {
// info("branched pattern: %s\n", e1->pattern);
r3_node_append_edge(e->child, e1);
c1->endpoint++;
c1->data = e->child->data; // copy data pointer
new_child->endpoint++;
new_child->data = e->child->data; // copy data pointer
e->child->data = NULL;
// truncate the original edge pattern

View file

@ -432,7 +432,7 @@ node * r3_tree_insert_pathl(node *tree, char *path, int path_len, route * route,
r3_node_add_child(n, strndup(path, (int)(p - path)), child);
// and insert the rest part to the child
return r3_tree_insert_pathl(tree, p, path_len - (int)(p - path), route, data);
return r3_tree_insert_pathl(child, p, path_len - (int)(p - path), route, data);
} else {
node * child = r3_tree_create(3);
r3_node_add_child(n, strndup(path, path_len) , child);
@ -478,6 +478,7 @@ node * r3_tree_insert_pathl(node *tree, char *path, int path_len, route * route,
int s2_len = 0;
r3_edge_branch(e, prefix_len);
// return r3_tree_insert_pathl(e->child, s2 , s2_len, route , data);
// here is the new edge from.
c2 = r3_tree_create(3);
@ -489,12 +490,13 @@ node * r3_tree_insert_pathl(node *tree, char *path, int path_len, route * route,
// move n->edges to c1
c2->endpoint++;
c2->data = data;
if (route) {
route->data = data;
r3_node_append_route(c2, route);
}
return c2;
/*
*/
} else {
printf("unexpected route.");
return NULL;

View file

@ -246,3 +246,9 @@
1400410399,11007341.02
1400410441,10929677.98
1400410704,10685427.91
1400411026,11125022.32
1400411267,10710017.05
1400411298,10809190.81
1400411322,10574819.37
1400411340,10536563.80
1400411381,10703727.13

1 1400242718 5649455.80
246 1400410399 11007341.02
247 1400410441 10929677.98
248 1400410704 10685427.91
249 1400411026 11125022.32
250 1400411267 10710017.05
251 1400411298 10809190.81
252 1400411322 10574819.37
253 1400411340 10536563.80
254 1400411381 10703727.13