minor optimization/refactoring in the grammar reader

This commit is contained in:
kr.angelov
2013-09-13 11:02:17 +00:00
parent 239eb22df5
commit 605772fda1
3 changed files with 142 additions and 181 deletions

View File

@@ -35,29 +35,11 @@ pgf_lzr_add_overl_entry(PgfCncOverloadMap* overl_table,
gu_buf_push(entries, void*, entry);
}
typedef struct {
GuMapItor fn;
PgfConcr* concr;
GuPool* pool;
} PgfLzrIndexFn;
static void
pgf_lzr_index_itor(GuMapItor* fn, const void* key, void* value, GuExn* err)
void
pgf_lzr_index(PgfConcr* concr,
PgfCCat* ccat, PgfProduction prod,
GuPool *pool)
{
(void) (key && err);
PgfLzrIndexFn* clo = (PgfLzrIndexFn*) fn;
PgfCCat* ccat = *((PgfCCat**) value);
PgfConcr *concr = clo->concr;
GuPool *pool = clo->pool;
if (gu_seq_is_null(ccat->prods))
return;
size_t n_prods = gu_seq_length(ccat->prods);
for (size_t i = 0; i < n_prods; i++) {
PgfProduction prod = gu_seq_get(ccat->prods, PgfProduction, i);
void* data = gu_variant_data(prod);
switch (gu_variant_tag(prod)) {
case PGF_PRODUCTION_APPLY: {
@@ -82,14 +64,6 @@ pgf_lzr_index_itor(GuMapItor* fn, const void* key, void* value, GuExn* err)
gu_impossible();
}
}
}
void
pgf_lzr_index(PgfConcr* concr, GuPool *pool)
{
PgfLzrIndexFn clo = { { pgf_lzr_index_itor }, concr, pool };
gu_map_iter(concr->ccats, &clo.fn, NULL);
}
typedef struct PgfLzn PgfLzn;

View File

@@ -2561,28 +2561,12 @@ pgf_parser_index_epsilon(PgfConcr* concr,
gu_buf_push(gu_seq_buf(prods), PgfProduction, prod);
}
typedef struct {
GuMapItor fn;
PgfConcr* concr;
GuPool* pool;
} PgfLeftcornerFn;
static void
pgf_parser_index_cats(GuMapItor* fn, const void* key, void* value, GuExn* err)
void
pgf_parser_index(PgfConcr* concr,
PgfCCat* ccat, PgfProduction prod,
GuPool *pool)
{
(void) (key && err);
PgfLeftcornerFn* clo = (PgfLeftcornerFn*) fn;
PgfCCat* ccat = *((PgfCCat**) value);
if (gu_seq_is_null(ccat->prods))
return;
for (size_t lin_idx = 0; lin_idx < ccat->cnccat->n_lins; lin_idx++) {
size_t n_prods = gu_seq_length(ccat->prods);
for (size_t i = 0; i < n_prods; i++) {
PgfProduction prod = gu_seq_get(ccat->prods, PgfProduction, i);
GuVariantInfo i = gu_variant_open(prod);
switch (i.tag) {
case PGF_PRODUCTION_APPLY: {
@@ -2597,23 +2581,23 @@ pgf_parser_index_cats(GuMapItor* fn, const void* key, void* value, GuExn* err)
switch (i.tag) {
case PGF_SYMBOL_KS: {
PgfSymbolKS* sks = i.data;
pgf_parser_index_token(clo->concr,
pgf_parser_index_token(concr,
sks->tokens,
ccat, lin_idx, prod,
clo->pool);
pool);
break;
}
case PGF_SYMBOL_KP: {
PgfSymbolKP* skp = i.data;
pgf_parser_index_token(clo->concr,
pgf_parser_index_token(concr,
skp->default_form,
ccat, lin_idx, prod,
clo->pool);
pool);
for (size_t i = 0; i < skp->n_forms; i++) {
pgf_parser_index_token(clo->concr,
pgf_parser_index_token(concr,
skp->forms[i].form,
ccat, lin_idx, prod,
clo->pool);
pool);
}
break;
}
@@ -2627,9 +2611,9 @@ pgf_parser_index_cats(GuMapItor* fn, const void* key, void* value, GuExn* err)
gu_impossible();
}
} else {
pgf_parser_index_epsilon(clo->concr,
pgf_parser_index_epsilon(concr,
ccat, lin_idx, prod,
clo->pool);
pool);
}
}
break;
@@ -2641,15 +2625,6 @@ pgf_parser_index_cats(GuMapItor* fn, const void* key, void* value, GuExn* err)
}
}
}
}
void
pgf_parser_index(PgfConcr* concr, GuPool *pool)
{
PgfLeftcornerFn clo1 = { { pgf_parser_index_cats },
concr, pool };
gu_map_iter(concr->ccats, &clo1.fn, NULL);
}
prob_t
pgf_ccat_set_viterbi_prob(PgfCCat* ccat) {

View File

@@ -1024,25 +1024,14 @@ pgf_read_cnccats(PgfReader* rdr, PgfAbstr* abstr, PgfConcr* concr)
return cnccats;
}
typedef struct {
GuMapItor fn;
PgfReader* rdr;
} PgfIndexFn;
static PgfCncCat*
pgf_ccat_set_cnccat(PgfCCat* ccat)
static void
pgf_ccat_set_cnccat(PgfCCat* ccat, PgfProduction prod)
{
if (!ccat->cnccat) {
size_t n_prods = gu_seq_length(ccat->prods);
for (size_t i = 0; i < n_prods; i++) {
PgfProduction prod =
gu_seq_get(ccat->prods, PgfProduction, i);
GuVariantInfo i = gu_variant_open(prod);
switch (i.tag) {
case PGF_PRODUCTION_COERCE: {
PgfProductionCoerce* pcoerce = i.data;
PgfCncCat* cnccat =
pgf_ccat_set_cnccat(pcoerce->coerce);
PgfCncCat* cnccat = pcoerce->coerce->cnccat;
if (!ccat->cnccat) {
ccat->cnccat = cnccat;
} else if (ccat->cnccat != cnccat) {
@@ -1060,28 +1049,54 @@ pgf_ccat_set_cnccat(PgfCCat* ccat)
gu_impossible();
}
}
}
return ccat->cnccat;
}
extern float
extern prob_t
pgf_ccat_set_viterbi_prob(PgfCCat* ccat);
typedef struct {
GuMapItor fn;
PgfConcr* concr;
GuPool* pool;
} PgfIndexFn;
extern void
pgf_parser_index(PgfConcr* concr,
PgfCCat* ccat, PgfProduction prod,
GuPool *pool);
void
pgf_lzr_index(PgfConcr* concr,
PgfCCat* ccat, PgfProduction prod,
GuPool *pool);
static void
pgf_read_ccat_cb(GuMapItor* fn, const void* key, void* value, GuExn* err)
{
(void) (key && err);
PgfCCat* ccat = *((PgfCCat**) value);
pgf_ccat_set_cnccat(ccat);
// pgf_ccat_set_viterbi_prob(ccat);
PgfIndexFn* clo = (PgfIndexFn*) fn;
PgfCCat* ccat = *((PgfCCat**) value);
PgfConcr *concr = clo->concr;
GuPool *pool = clo->pool;
if (gu_seq_is_null(ccat->prods))
return;
size_t n_prods = gu_seq_length(ccat->prods);
for (size_t i = 0; i < n_prods; i++) {
PgfProduction prod =
gu_seq_get(ccat->prods, PgfProduction, i);
if (!ccat->cnccat) {
pgf_ccat_set_cnccat(ccat, prod);
}
void
pgf_parser_index(PgfConcr* concr, GuPool *pool);
pgf_parser_index(concr, ccat, prod, pool);
pgf_lzr_index(concr, ccat, prod, pool);
}
void
pgf_lzr_index(PgfConcr* concr, GuPool *pool);
// pgf_ccat_set_viterbi_prob(ccat);
}
static PgfConcr*
pgf_read_concrete(PgfReader* rdr, PgfAbstr* abstr)
@@ -1121,12 +1136,9 @@ pgf_read_concrete(PgfReader* rdr, PgfAbstr* abstr)
concr->callbacks = pgf_new_callbacks_map(concr, rdr->opool);
concr->total_cats = pgf_read_int(rdr);
PgfIndexFn clo1 = { { pgf_read_ccat_cb }, rdr };
PgfIndexFn clo1 = { { pgf_read_ccat_cb }, concr, rdr->opool };
gu_map_iter(concr->ccats, &clo1.fn, NULL);
pgf_parser_index(concr, rdr->opool);
pgf_lzr_index(concr, rdr->opool);
return concr;
}