From f039cc3c8f413f32a8ff8548124319d320edacf6 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 18 May 2014 19:09:47 +0800 Subject: [PATCH] Fix child insertion --- include/r3.h | 2 +- src/edge.c | 21 +++++++++++---------- src/node.c | 6 ++++-- tests/bench_str.csv | 6 ++++++ 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/include/r3.h b/include/r3.h index a761503..dc64d61 100644 --- a/include/r3.h +++ b/include/r3.h @@ -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); diff --git a/src/edge.c b/src/edge.c index c9b621a..b034d78 100644 --- a/src/edge.c +++ b/src/edge.c @@ -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 diff --git a/src/node.c b/src/node.c index 7a36a1a..77978cf 100644 --- a/src/node.c +++ b/src/node.c @@ -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; diff --git a/tests/bench_str.csv b/tests/bench_str.csv index d86820a..98c5182 100644 --- a/tests/bench_str.csv +++ b/tests/bench_str.csv @@ -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