#include #include #include #include "lkb-protocol.h" #include "lkb-chart.h" int edge_compare(const void *A, const void *B) { const lkb_edge *a = (const lkb_edge*)A; const lkb_edge *b = (const lkb_edge*)B; return b->id - a->id; } lkb_chart *build_chart(int id, int size, int nedges, lkb_edge **edges) { lkb_chart *ch; int i; ch = calloc(sizeof(lkb_chart), 1); ch->id = id; ch->size = size; ch->ncells = size*(size+1); ch->cells = malloc(sizeof(lkb_chart_cell) * ch->ncells); for(i=0;iedges+ch->size, ch->nedges-ch->size, sizeof(lkb_edge), edge_compare); return ch; } /* parse_chart(char **INPUT, int *TYPE, void **DATA) { int size_type, edge_type; int size; lkb_edge *edge; char *p = *INPUT; char c; lkb_chart *ch=0; *DATA = 0; //debug("parsing chart at %s\n", *INPUT); c = tokenize(&p); if(c!='[') { fprintf(stderr, "Expected '[' opening chart structure\n"); goto done_chart; } parse_protocol(&p, &size_type, (void**)&size); if(size_type != type_int) { fprintf(stderr, "Size of chart was not a number (type %d)\n", size_type); goto done_chart; } ch = (lkb_chart*)calloc(sizeof(lkb_chart), 1); ch->size = size; ch->nedges = 0; ch->edges = 0; ch->ncells = size*(size+1); ch->cells = malloc(sizeof(lkb_chart_cell) * ch->ncells); do { c = tokenize(&p); if(c==' ')continue; if(c==']')break; p--; parse_protocol(&p, &edge_type, (void**)&edge); if(edge_type != type_edge) { fprintf(stderr, "Edge was not an edge (type %d)\n", edge_type); goto done_chart; } adjoin_edge(ch, edge); } while(*p); //debug("chart finished\n"); qsort(ch->edges+ch->size, ch->nedges-ch->size, sizeof(lkb_edge), edge_compare); done_chart: *TYPE = type_chart; *INPUT=p; if(!ch) { ch = (lkb_chart*)calloc(sizeof(lkb_chart), 1); ch->size = 0; ch->nedges = 0; ch->edges = 0; } *(lkb_chart**)DATA = ch; return 0; }*/ adjoin_edge(lkb_chart *ch, lkb_edge *e) { ch->nedges++; ch->edges = realloc(ch->edges, sizeof(lkb_edge)*ch->nedges); ch->edges[ch->nedges-1] = *e; free(e); } parse_edge(char **INPUT, int *TYPE, void **DATA) { int id_type, from_type, to_type, abbr_type, type_type, daughter_type, key_type, roots_type; char *abbr, *type; int id, from, to, daughter, key; char *p = *INPUT; char c; lkb_edge *e=0; lui_list *root_list; *DATA = 0; //debug("parsing edge at %s\n", *INPUT); c = tokenize(&p); if(c!='[') { fprintf(stderr, "Expected '[' opening edge structure\n"); goto done_edge; } parse_protocol(&p, &key_type, (void**)&key); if(key_type != type_int) { fprintf(stderr, "Key of edge was not a number (type %d)\n", key_type); goto done_edge; } parse_protocol(&p, &id_type, (void**)&id); if(id_type != type_int) { fprintf(stderr, "ID of edge was not a number (type %d)\n", id_type); goto done_edge; } parse_protocol(&p, &from_type, (void**)&from); if(from_type != type_int) { fprintf(stderr, "From of edge was not a number (type %d)\n", from_type); goto done_edge; } parse_protocol(&p, &to_type, (void**)&to); if(to_type != type_int) { fprintf(stderr, "To of edge was not a number (type %d)\n", to_type); goto done_edge; } parse_protocol(&p, &abbr_type, (void**)&abbr); if(abbr_type != type_symbol && abbr_type != type_string) { fprintf(stderr, "Abbreviation of edge was not a symbol or string (type %d)\n", abbr_type); goto done_edge; } parse_protocol(&p, &type_type, (void**)&type); if(type_type != type_symbol && type_type != type_string) { fprintf(stderr, "Full type of edge was not a symbol or string (type %d)\n", type_type); goto done_edge; } parse_protocol(&p, &roots_type, (void**)&root_list); if(roots_type != type_list) { fprintf(stderr, "Root list was not a list (type %d)\n", roots_type); goto done_edge; } e = (lkb_edge*)calloc(sizeof(lkb_edge), 1); e->id = id; e->key = key; e->from = from; e->to = to; e->abbr = abbr; e->type = type; e->ndaughters = 0; e->bold = 0; e->filter = 0; e->label = malloc(strlen(abbr) + 12); //if(e->id >= 0)sprintf(e->label, "%d %s", e->id, e->abbr); if(e->id >= 0)sprintf(e->label, "%s", e->abbr); else sprintf(e->label, "%s", e->abbr); do { c = tokenize(&p); if(c==' ')continue; if(c==']')break; p--; parse_protocol(&p, &daughter_type, (void**)&daughter); if(daughter_type != type_int) { fprintf(stderr, "Daughter id was not a number (type %d)\n", daughter_type); goto done_edge; } adjoin_daughter(e, daughter); } while(*p); if(e->ndaughters && e->daughters[0] <0) if(e->to == e->from+1)e->to = e->from; if(e->ndaughters==0)e->to = e->from; done_edge: *TYPE = type_edge; *INPUT=p; if(!e) { e = (lkb_edge*)calloc(sizeof(lkb_edge), 1); e->id = -1; e->from = e->to = -1; e->abbr = strdup("ERR"); e->type = strdup("error-type"); e->ndaughters = 0; e->bold = e->filter = 0; } *(lkb_edge**)DATA = e; return 0; } adjoin_daughter(lkb_edge *e, int d) { e->ndaughters++; e->daughters = realloc(e->daughters, sizeof(int)*e->ndaughters); e->daughters[e->ndaughters-1] = d; } lkb_edge duplicate_edge(lkb_edge *edge) { lkb_edge ne; ne = *edge; ne.abbr = strdup(edge->abbr); ne.type = strdup(edge->type); ne.label = strdup(edge->label); return ne; } lkb_chart *duplicate_chart(lkb_chart *chart) { lkb_chart *nc; int i; nc = malloc(sizeof(lkb_chart)); nc->size = chart->size; nc->hold_selections = chart->hold_selections; nc->nedges = chart->nedges; nc->edges = malloc(sizeof(lkb_edge) * nc->nedges); for(i=0;inedges;i++) nc->edges[i] = duplicate_edge(chart->edges+i); nc->ncells = chart->ncells; nc->cells = malloc(sizeof(lkb_chart_cell) * nc->ncells); memcpy(nc->cells, chart->cells, sizeof(lkb_chart_cell) * nc->ncells); return nc; }