diff --git a/src/edge.c b/src/edge.c new file mode 100644 index 0000000..bb6b60b --- /dev/null +++ b/src/edge.c @@ -0,0 +1,78 @@ +/* + * edge.c + * Copyright (C) 2014 c9s + * + * Distributed under terms of the MIT license. + */ +#include +#include +#include +#include + +// Jemalloc memory management +#include + +// PCRE +#include + +// Judy array +#include + +#include "r3_define.h" +#include "r3_str.h" +#include "r3.h" +#include "str_array.h" + + + +/** + * branch the edge pattern at "dl" offset + * + */ +void r3_edge_branch(edge *e, int dl) { + node *c1; // child 1, child 2 + edge *e1; // edge 1, edge 2 + char * s1 = e->pattern + dl; + int s1_len = 0; + + edge **tmp_edges = e->child->edges; + int tmp_r3_edge_len = e->child->r3_edge_len; + + // the suffix edge of the leaf + c1 = r3_tree_create(3); + s1_len = e->pattern_len - dl; + e1 = r3_edge_create(strndup(s1, s1_len), s1_len, c1); + // printf("edge left: %s\n", e1->pattern); + + // Migrate the child edges to the new edge we just created. + for ( int i = 0 ; i < tmp_r3_edge_len ; i++ ) { + r3_tree_append_edge(c1, tmp_edges[i]); + e->child->edges[i] = NULL; + } + e->child->r3_edge_len = 0; + e->child->endpoint--; + + info("branched pattern: %s\n", e1->pattern); + + r3_tree_append_edge(e->child, e1); + c1->endpoint++; +} + + +edge * r3_edge_create(char * pattern, int pattern_len, node * child) { + edge * e = (edge*) malloc( sizeof(edge) ); + e->pattern = pattern; + e->pattern_len = pattern_len; + e->child = child; + return e; +} + +void r3_edge_free(edge * e) { + if (e->pattern) { + free(e->pattern); + } + if ( e->child ) { + r3_tree_free(e->child); + } +} +