Skip to content

Commit

Permalink
switch to the new and nicer vector API
Browse files Browse the repository at this point in the history
  • Loading branch information
krangelov committed Aug 28, 2024
1 parent cda3e99 commit c519d4b
Show file tree
Hide file tree
Showing 21 changed files with 513 additions and 497 deletions.
52 changes: 25 additions & 27 deletions src/runtime/c/pgf/data.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ void PgfConcr::release(ref<PgfConcr> concr)

void PgfConcrLincat::release(ref<PgfConcrLincat> lincat)
{
for (size_t i = 0; i < lincat->fields->len; i++) {
text_db_release(*vector_elem(lincat->fields, i));
for (ref<PgfText> field : lincat->fields) {
text_db_release(field);
}
Vector<ref<PgfText>>::release(lincat->fields);
vector<ref<PgfText>>::release(lincat->fields);

for (size_t i = 0; i < lincat->args->len; i++) {
PgfLParam::release(vector_elem(lincat->args, i)->param);
for (size_t i = 0; i < lincat->args.size(); i++) {
PgfLParam::release(lincat->args[i].param);
}
Vector<PgfPArg>::release(lincat->args);
vector<PgfPArg>::release(lincat->args);

for (size_t i = 0; i < lincat->res->len; i++) {
PgfPResult::release(*vector_elem(lincat->res, i));
for (ref<PgfPResult> res : lincat->res) {
PgfPResult::release(res);
}
Vector<ref<PgfPResult>>::release(lincat->res);
vector<ref<PgfPResult>>::release(lincat->res);

Vector<ref<PgfSequence>>::release(lincat->seqs);
vector<ref<PgfSequence>>::release(lincat->seqs);

PgfDB::free(lincat, lincat->name.size+1);
}
Expand All @@ -75,15 +75,13 @@ void PgfLParam::release(ref<PgfLParam> param)
void PgfPResult::release(ref<PgfPResult> res)
{
if (res->vars != 0)
Vector<PgfVariableRange>::release(res->vars);
vector<PgfVariableRange>::release(res->vars);
PgfDB::free(res, res->param.n_terms*sizeof(res->param.terms[0]));
}

void PgfSequence::release(ref<PgfSequence> seq)
{
for (size_t i = 0; i < seq->syms.len; i++) {
PgfSymbol sym = *vector_elem(&seq->syms, i);

for (PgfSymbol sym : seq->syms) {
switch (ref<PgfSymbol>::get_tag(sym)) {
case PgfSymbolCat::tag: {
auto sym_cat = ref<PgfSymbolCat>::untagged(sym);
Expand All @@ -106,13 +104,13 @@ void PgfSequence::release(ref<PgfSequence> seq)
case PgfSymbolKP::tag: {
auto sym_kp = ref<PgfSymbolKP>::untagged(sym);
PgfSequence::release(sym_kp->default_form);
for (size_t i = 0; i < sym_kp->alts.len; i++) {
PgfSequence::release(sym_kp->alts.data[i].form);
for (size_t j = 0; j < sym_kp->alts.data[i].prefixes->len; j++) {
text_db_release(*vector_elem(sym_kp->alts.data[i].prefixes, j));
for (size_t i = 0; i < sym_kp->alts.size(); i++) {
PgfSequence::release(sym_kp->alts[i].form);
for (size_t j = 0; j < sym_kp->alts[i].prefixes.size(); j++) {
text_db_release(sym_kp->alts[i].prefixes[j]);
}
}
PgfDB::free(sym_kp, sym_kp->alts.len*sizeof(PgfAlternative));
inline_vector<PgfAlternative>::release(&PgfSymbolKP::alts, sym_kp);
break;
}
case PgfSymbolBIND::tag:
Expand All @@ -126,22 +124,22 @@ void PgfSequence::release(ref<PgfSequence> seq)
throw pgf_error("Unknown symbol tag");
}
}
PgfDB::free(seq,seq->syms.len*sizeof(PgfSymbol));
inline_vector<PgfSymbol>::release(&PgfSequence::syms, seq);
}

void PgfConcrLin::release(ref<PgfConcrLin> lin)
{
for (size_t i = 0; i < lin->args->len; i++) {
PgfLParam::release(vector_elem(lin->args, i)->param);
for (size_t i = 0; i < lin->args.size(); i++) {
PgfLParam::release(lin->args[i].param);
}
Vector<PgfPArg>::release(lin->args);
vector<PgfPArg>::release(lin->args);

for (size_t i = 0; i < lin->res->len; i++) {
PgfPResult::release(*vector_elem(lin->res, i));
for (ref<PgfPResult> res : lin->res) {
PgfPResult::release(res);
}
Vector<ref<PgfPResult>>::release(lin->res);
vector<ref<PgfPResult>>::release(lin->res);

Vector<ref<PgfSequence>>::release(lin->seqs);
vector<ref<PgfSequence>>::release(lin->seqs);

PgfDB::free(lin, lin->name.size+1);
}
Expand Down
38 changes: 18 additions & 20 deletions src/runtime/c/pgf/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct PGF_INTERNAL_DECL PgfAbsFun {
};

struct PGF_INTERNAL_DECL PgfAbsCat {
ref<Vector<PgfHypo>> context;
vector<PgfHypo> context;
prob_t prob;
PgfText name;

Expand Down Expand Up @@ -145,7 +145,7 @@ struct PGF_INTERNAL_DECL PgfPArg {
};

struct PGF_INTERNAL_DECL PgfPResult {
ref<Vector<PgfVariableRange>> vars;
vector<PgfVariableRange> vars;
PgfLParam param;

static void release(ref<PgfPResult> res);
Expand All @@ -154,7 +154,7 @@ struct PGF_INTERNAL_DECL PgfPResult {
typedef object PgfSymbol;

struct PGF_INTERNAL_DECL PgfSequence {
Vector<PgfSymbol> syms;
inline_vector<PgfSymbol> syms;

static void release(ref<PgfSequence> seq);
};
Expand Down Expand Up @@ -190,15 +190,15 @@ struct PGF_INTERNAL_DECL PgfAlternative {
ref<PgfSequence> form;
/**< The form of this variant as a list of tokens. */

ref<Vector<ref<PgfText>>> prefixes;
vector<ref<PgfText>> prefixes;
/**< The prefixes of the following symbol that trigger this
* form. */
};

struct PGF_INTERNAL_DECL PgfSymbolKP {
static const uint8_t tag = 4;
ref<PgfSequence> default_form;
Vector<PgfAlternative> alts;
inline_vector<PgfAlternative> alts;
};

struct PGF_INTERNAL_DECL PgfSymbolBIND {
Expand Down Expand Up @@ -231,10 +231,10 @@ struct PGF_INTERNAL_DECL PgfConcrLincat {
ref<PgfAbsCat> abscat;

size_t n_lindefs;
ref<Vector<PgfPArg>> args;
ref<Vector<ref<PgfPResult>>> res;
ref<Vector<ref<PgfSequence>>> seqs;
ref<Vector<ref<PgfText>>> fields;
vector<PgfPArg> args;
vector<ref<PgfPResult>> res;
vector<ref<PgfSequence>> seqs;
vector<ref<PgfText>> fields;

PgfText name;

Expand All @@ -247,9 +247,9 @@ struct PGF_INTERNAL_DECL PgfConcrLin {
ref<PgfAbsFun> absfun;
ref<PgfConcrLincat> lincat;

ref<Vector<PgfPArg>> args;
ref<Vector<ref<PgfPResult>>> res;
ref<Vector<ref<PgfSequence>>> seqs;
vector<PgfPArg> args;
vector<ref<PgfPResult>> res;
vector<ref<PgfSequence>> seqs;

PgfText name;

Expand Down Expand Up @@ -280,7 +280,7 @@ struct PgfLRReduceArg;
struct PGF_INTERNAL_DECL PgfLRProduction {
ref<PgfConcrLin> lin;
size_t index;
ref<Vector<ref<PgfLRReduceArg>>> args;
vector<ref<PgfLRReduceArg>> args;
};

struct PGF_INTERNAL_DECL PgfLRReduceArg {
Expand All @@ -301,18 +301,16 @@ struct PGF_INTERNAL_DECL PgfLRReduce {
size_t stk_idx;
};

ref<Vector<Arg>> args;
vector<Arg> args;
};

struct PGF_INTERNAL_DECL PgfLRState {
ref<Vector<PgfLRShift>> shifts;
ref<Vector<PgfLRShiftKS>> tokens;
vector<PgfLRShift> shifts;
vector<PgfLRShiftKS> tokens;
size_t next_bind_state;
ref<Vector<PgfLRReduce>> reductions;
vector<PgfLRReduce> reductions;
};

typedef Vector<PgfLRState> PgfLRTable;

struct PGF_INTERNAL_DECL PgfConcr {
static const uint8_t tag = 1;

Expand All @@ -322,7 +320,7 @@ struct PGF_INTERNAL_DECL PgfConcr {
PgfPhrasetable phrasetable;
Namespace<PgfConcrPrintname> printnames;

ref<PgfLRTable> lrtable;
vector<PgfLRState> lrtable;

PgfText name;

Expand Down
66 changes: 34 additions & 32 deletions src/runtime/c/pgf/expr.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,28 @@ PgfType PgfDBMarshaller::match_type(PgfUnmarshaller *u, PgfType ty)
ref<PgfDTyp> tp = ty;

PgfTypeHypo *hypos = (PgfTypeHypo *)
alloca(tp->hypos->len * sizeof(PgfTypeHypo));
for (size_t i = 0; i < tp->hypos->len; i++) {
hypos[i].bind_type = tp->hypos->data[i].bind_type;
hypos[i].cid = &(*tp->hypos->data[i].cid);
hypos[i].type = match_type(u, tp->hypos->data[i].type.as_object());
alloca(tp->hypos.size() * sizeof(PgfTypeHypo));
for (size_t i = 0; i < tp->hypos.size(); i++) {
hypos[i].bind_type = tp->hypos[i].bind_type;
hypos[i].cid = &(*tp->hypos[i].cid);
hypos[i].type = match_type(u, tp->hypos[i].type.as_object());
}

PgfExpr *exprs = (PgfExpr *)
alloca(tp->exprs->len * sizeof(PgfExpr));
for (size_t i = 0; i < tp->exprs->len; i++) {
exprs[i] = match_expr(u, tp->exprs->data[i]);
alloca(tp->exprs.size() * sizeof(PgfExpr));
for (size_t i = 0; i < tp->exprs.size(); i++) {
exprs[i] = match_expr(u, tp->exprs[i]);
}

PgfType res = u->dtyp(tp->hypos->len, hypos,
PgfType res = u->dtyp(tp->hypos.size(), hypos,
&tp->name,
tp->exprs->len, exprs);
tp->exprs.size(), exprs);

for (size_t i = 0; i < tp->hypos->len; i++) {
for (size_t i = 0; i < tp->hypos.size(); i++) {
u->free_ref(hypos[i].type);
}

for (size_t i = 0; i < tp->exprs->len; i++) {
for (size_t i = 0; i < tp->exprs.size(); i++) {
u->free_ref(exprs[i]);
}

Expand Down Expand Up @@ -202,16 +202,18 @@ PgfType PgfDBUnmarshaller::dtyp(size_t n_hypos, PgfTypeHypo *hypos,
ref<PgfDTyp> ty =
PgfDB::malloc<PgfDTyp>(cat->size+1);
memcpy(&ty->name, cat, sizeof(PgfText)+cat->size+1);
ty->hypos = vector_new<PgfHypo>(n_hypos);
ty->hypos = vector<PgfHypo>::alloc(n_hypos);
for (size_t i = 0; i < n_hypos; i++) {
ref<PgfHypo> hypo = vector_elem(ty->hypos,i);
ref<PgfHypo> hypo = ty->hypos.elem(i);
hypo->bind_type = hypos[i].bind_type;
hypo->cid = textdup_db(hypos[i].cid);
hypo->type = m->match_type(this, hypos[i].type);
PgfType type = m->match_type(this, hypos[i].type);
hypo->type = type;
}
ty->exprs = vector_new<PgfExpr>(n_exprs);
ty->exprs = vector<PgfExpr>::alloc(n_exprs);
for (size_t i = 0; i < n_exprs; i++) {
*vector_elem(ty->exprs,i) = m->match_expr(this, exprs[i]);
PgfExpr expr = m->match_expr(this, exprs[i]);
ty->exprs[i] = expr;
}

return ty.as_object();
Expand Down Expand Up @@ -282,16 +284,16 @@ PgfType PgfInternalMarshaller::match_type(PgfUnmarshaller *u, PgfType ty)
ref<PgfDTyp> tp = ty;

PgfTypeHypo *hypos = (PgfTypeHypo *)
alloca(tp->hypos->len * sizeof(PgfTypeHypo));
for (size_t i = 0; i < tp->hypos->len; i++) {
hypos[i].bind_type = tp->hypos->data[i].bind_type;
hypos[i].cid = &(*tp->hypos->data[i].cid);
hypos[i].type = tp->hypos->data[i].type.as_object();
alloca(tp->hypos.size() * sizeof(PgfTypeHypo));
for (size_t i = 0; i < tp->hypos.size(); i++) {
hypos[i].bind_type = tp->hypos[i].bind_type;
hypos[i].cid = &(*tp->hypos[i].cid);
hypos[i].type = tp->hypos[i].type.as_object();
}

return u->dtyp(tp->hypos->len, hypos,
return u->dtyp(tp->hypos.size(), hypos,
&tp->name,
tp->exprs->len, tp->exprs->data);
tp->exprs.size(), tp->exprs.get_data());
}

PgfExprParser::PgfExprParser(PgfText *input, PgfUnmarshaller *unmarshaller)
Expand Down Expand Up @@ -1329,25 +1331,25 @@ void pgf_expr_release(PgfExpr expr)
}

PGF_INTERNAL
void pgf_context_release(ref<Vector<PgfHypo>> hypos)
void pgf_context_release(vector<PgfHypo> hypos)
{
for (size_t i = 0; i < hypos->len; i++) {
text_db_release(vector_elem(hypos, i)->cid);
pgf_type_release(vector_elem(hypos, i)->type);
for (size_t i = 0; i < hypos.size(); i++) {
text_db_release(hypos[i].cid);
pgf_type_release(hypos[i].type);
}

Vector<PgfHypo>::release(hypos);
vector<PgfHypo>::release(hypos);
}

PGF_INTERNAL
void pgf_type_release(ref<PgfDTyp> dtyp)
{
pgf_context_release(dtyp->hypos);

for (size_t i = 0; i < dtyp->exprs->len; i++) {
pgf_expr_release(*vector_elem(dtyp->exprs, i));
for (PgfExpr expr : dtyp->exprs) {
pgf_expr_release(expr);
}
Vector<PgfExpr>::release(dtyp->exprs);
vector<PgfExpr>::release(dtyp->exprs);

PgfDB::free(dtyp, dtyp->name.size+1);
}
6 changes: 3 additions & 3 deletions src/runtime/c/pgf/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ struct PGF_INTERNAL_DECL PgfHypo {
};

struct PGF_INTERNAL_DECL PgfDTyp {
ref<Vector<PgfHypo>> hypos;
ref<Vector<PgfExpr>> exprs;
vector<PgfHypo> hypos;
vector<PgfExpr> exprs;
PgfText name;
};

Expand Down Expand Up @@ -240,7 +240,7 @@ PGF_INTERNAL_DECL
void pgf_expr_release(PgfExpr expr);

PGF_INTERNAL_DECL
void pgf_context_release(ref<Vector<PgfHypo>> hypos);
void pgf_context_release(vector<PgfHypo> hypos);

PGF_INTERNAL_DECL
void pgf_type_release(ref<PgfDTyp> dtyp);
Expand Down
Loading

0 comments on commit c519d4b

Please sign in to comment.