diff options
| author | kr.angelov <kr.angelov@gmail.com> | 2013-09-13 11:02:17 +0000 |
|---|---|---|
| committer | kr.angelov <kr.angelov@gmail.com> | 2013-09-13 11:02:17 +0000 |
| commit | c721c5548e2f85b01ec932bdeb6dac00cef7ef42 (patch) | |
| tree | b8b12020c7a97d36e90323f88fafb654b3194b39 /src/runtime/c/pgf/reader.c | |
| parent | df91908e636f10caabce39504cfd5d9f962d6711 (diff) | |
minor optimization/refactoring in the grammar reader
Diffstat (limited to 'src/runtime/c/pgf/reader.c')
| -rw-r--r-- | src/runtime/c/pgf/reader.c | 108 |
1 files changed, 60 insertions, 48 deletions
diff --git a/src/runtime/c/pgf/reader.c b/src/runtime/c/pgf/reader.c index 9b2844e34..abfbfe025 100644 --- a/src/runtime/c/pgf/reader.c +++ b/src/runtime/c/pgf/reader.c @@ -1024,65 +1024,80 @@ 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); - if (!ccat->cnccat) { - ccat->cnccat = cnccat; - } else if (ccat->cnccat != cnccat) { - // XXX: real error - gu_impossible(); - } - break; - } - case PGF_PRODUCTION_APPLY: - // Shouldn't happen with current PGF. - // XXX: real error - gu_impossible(); - break; - default: - gu_impossible(); - } + GuVariantInfo i = gu_variant_open(prod); + switch (i.tag) { + case PGF_PRODUCTION_COERCE: { + PgfProductionCoerce* pcoerce = i.data; + PgfCncCat* cnccat = pcoerce->coerce->cnccat; + if (!ccat->cnccat) { + ccat->cnccat = cnccat; + } else if (ccat->cnccat != cnccat) { + // XXX: real error + gu_impossible(); } + break; + } + case PGF_PRODUCTION_APPLY: + // Shouldn't happen with current PGF. + // XXX: real error + gu_impossible(); + break; + default: + 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); + + 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); + } + + pgf_parser_index(concr, ccat, prod, pool); + pgf_lzr_index(concr, ccat, prod, pool); + } - pgf_ccat_set_cnccat(ccat); // pgf_ccat_set_viterbi_prob(ccat); } -void -pgf_parser_index(PgfConcr* concr, GuPool *pool); - -void -pgf_lzr_index(PgfConcr* concr, GuPool *pool); - 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; } |
