_Rb_tree doesn't exist on Windows and macOS. Go back to using map

This commit is contained in:
Krasimir Angelov
2023-03-14 09:40:38 +01:00
parent 92c2840b2b
commit 9a0a730820
3 changed files with 43 additions and 85 deletions

View File

@@ -7,7 +7,7 @@
#include <stdexcept> #include <stdexcept>
#include <functional> #include <functional>
#include <queue> #include <queue>
#include <bits/stl_tree.h> #include <map>
#include "pgf.h" #include "pgf.h"

View File

@@ -294,9 +294,10 @@ PgfExhaustiveGenerator::PgfExhaustiveGenerator(ref<PgfPGF> pgf,
PgfLiteral lint = u->lint(1,&value); PgfLiteral lint = u->lint(1,&value);
PgfExpr expr = u->elit(lint); PgfExpr expr = u->elit(lint);
u->free_ref(lint); u->free_ref(lint);
Result *res = new Result(ref<PgfText>::from_ptr(&cat_Int->name)); Goal g(ref<PgfText>::from_ptr(&cat_Int->name),NULL);
Result *res = new Result();
res->exprs.push_back(std::pair<PgfExpr,prob_t>(expr,0)); res->exprs.push_back(std::pair<PgfExpr,prob_t>(expr,0));
results._M_insert_unique(res); results[g] = res;
} }
PgfText *text_Float = string2text("Float"); PgfText *text_Float = string2text("Float");
@@ -307,9 +308,10 @@ PgfExhaustiveGenerator::PgfExhaustiveGenerator(ref<PgfPGF> pgf,
PgfLiteral lflt = u->lflt(3.14); PgfLiteral lflt = u->lflt(3.14);
PgfExpr expr = u->elit(lflt); PgfExpr expr = u->elit(lflt);
u->free_ref(lflt); u->free_ref(lflt);
Result *res = new Result(ref<PgfText>::from_ptr(&cat_Float->name)); Goal g(ref<PgfText>::from_ptr(&cat_Float->name),NULL);
Result *res = new Result();
res->exprs.push_back(std::pair<PgfExpr,prob_t>(expr,0)); res->exprs.push_back(std::pair<PgfExpr,prob_t>(expr,0));
results._M_insert_unique(res); results[g] = res;
} }
PgfText *text_String = string2text("String"); PgfText *text_String = string2text("String");
@@ -324,9 +326,10 @@ PgfExhaustiveGenerator::PgfExhaustiveGenerator(ref<PgfPGF> pgf,
PgfLiteral lstr = u->lstr(value); PgfLiteral lstr = u->lstr(value);
PgfExpr expr = u->elit(lstr); PgfExpr expr = u->elit(lstr);
u->free_ref(lstr); u->free_ref(lstr);
Result *res = new Result(ref<PgfText>::from_ptr(&cat_String->name)); Goal g(ref<PgfText>::from_ptr(&cat_String->name),NULL);
Result *res = new Result();
res->exprs.push_back(std::pair<PgfExpr,prob_t>(expr,0)); res->exprs.push_back(std::pair<PgfExpr,prob_t>(expr,0));
results._M_insert_unique(res); results[g] = res;
} }
} }
@@ -420,15 +423,12 @@ PgfType PgfExhaustiveGenerator::dtyp(size_t n_hypos, PgfTypeHypo *hypos,
if (abscat == 0) if (abscat == 0)
return 0; return 0;
Goal g(ref<PgfText>::from_ptr(&abscat->name)); Goal g(ref<PgfText>::from_ptr(&abscat->name), NULL);
Result *&res = results[g];
auto i = results.lower_bound(g); if (res == NULL) {
if (i == results.end() || results.key_comp()(g, **i)) { res = new Result();
top_res = new Result(g);
results._M_emplace_hint_unique(i, top_res);
} else {
top_res = *i;
} }
top_res = res;
push_left_states(pgf->abstract.funs_by_cat, cat, top_res, 0); push_left_states(pgf->abstract.funs_by_cat, cat, top_res, 0);
return 0; return 0;
@@ -490,14 +490,15 @@ bool PgfExhaustiveGenerator::State1::process(PgfExhaustiveGenerator *gen, PgfUnm
return true; return true;
} }
Scope *scope = res->scope;
size_t scope_len = res->scope_len;
ref<PgfDTyp> arg_type = vector_elem(type->hypos, n_args)->type; ref<PgfDTyp> arg_type = vector_elem(type->hypos, n_args)->type;
Goal g(ref<PgfText>::from_ptr(&arg_type->name), *res);
for (size_t i = 0; i < arg_type->hypos->len; i++) { for (size_t i = 0; i < arg_type->hypos->len; i++) {
ref<PgfHypo> hypo = vector_elem(arg_type->hypos, i); ref<PgfHypo> hypo = vector_elem(arg_type->hypos, i);
size_t buf_size = 16; size_t buf_size = 16;
Scope *new_scope = (Scope *) malloc(sizeof(Scope)+buf_size); Scope *new_scope = (Scope *) malloc(sizeof(Scope)+buf_size);
new_scope->next = g.scope; new_scope->next = scope;
new_scope->type = hypo->type.as_object(); new_scope->type = hypo->type.as_object();
new_scope->m = &gen->i_m; new_scope->m = &gen->i_m;
new_scope->bind_type = hypo->bind_type; new_scope->bind_type = hypo->bind_type;
@@ -505,7 +506,7 @@ bool PgfExhaustiveGenerator::State1::process(PgfExhaustiveGenerator *gen, PgfUnm
size_t out; size_t out;
again: { again: {
new_scope->var.size = new_scope->var.size =
snprintf(new_scope->var.text, buf_size, "v%zu", g.scope_len+1); snprintf(new_scope->var.text, buf_size, "v%zu", res->scope_len+1);
if (new_scope->var.size >= buf_size) { if (new_scope->var.size >= buf_size) {
buf_size = new_scope->var.size+1; buf_size = new_scope->var.size+1;
new_scope = (Scope*) new_scope = (Scope*)
@@ -515,31 +516,26 @@ again: {
} }
gen->scopes.push_back(new_scope); gen->scopes.push_back(new_scope);
g.scope = new_scope; scope = new_scope;
g.scope_len++; scope_len++;
} }
Result *arg_res; Goal g(ref<PgfText>::from_ptr(&arg_type->name), scope);
Result *tmp = NULL; Result *&arg_res = gen->results[g];
auto i = gen->results.lower_bound(g); Result *tmp = arg_res;
if (i == gen->results.end() || gen->results.key_comp()(g, **i)) { if (arg_res == NULL) {
arg_res = new Result(g); arg_res = new Result(scope, scope_len);
gen->results._M_emplace_hint_unique(i, res);
} else {
arg_res = *i;
tmp = res;
} }
arg_res->states.push_back(this); arg_res->states.push_back(this);
if (tmp == NULL) { if (tmp == NULL) {
// predict local variables // predict local variables
size_t index = 0; size_t index = 0;
Scope *s = g.scope; Scope *s = scope;
prob_t outside_prob = this->prob; prob_t outside_prob = this->prob;
while (s != NULL) { while (s != NULL) {
ref<PgfDTyp> type = s->type; ref<PgfDTyp> type = s->type;
if (textcmp(&type->name, g.cat) == 0) { if (textcmp(&type->name, g.first) == 0) {
State1 *var_state = new State1(); State1 *var_state = new State1();
var_state->res = arg_res; var_state->res = arg_res;
var_state->prob = outside_prob - log(Scope::VAR_PROB); var_state->prob = outside_prob - log(Scope::VAR_PROB);
@@ -556,7 +552,7 @@ again: {
} }
// predict global functions // predict global functions
gen->push_left_states(gen->pgf->abstract.funs_by_cat, g.cat, arg_res, outside_prob); gen->push_left_states(gen->pgf->abstract.funs_by_cat, g.first, arg_res, outside_prob);
} else { } else {
for (std::pair<PgfExpr,prob_t> p : arg_res->exprs) { for (std::pair<PgfExpr,prob_t> p : arg_res->exprs) {
this->combine(gen,arg_res->scope,p.first,p.second,u); this->combine(gen,arg_res->scope,p.first,p.second,u);
@@ -664,7 +660,7 @@ void PgfExhaustiveGenerator::free_refs(PgfUnmarshaller *u)
} }
for (auto i : results) { for (auto i : results) {
for (auto j : i->exprs) { for (auto j : i.second->exprs) {
free_ref(j.first); free_ref(j.first);
} }
} }

View File

@@ -91,45 +91,25 @@ class PGF_INTERNAL_DECL PgfExhaustiveGenerator : public PgfUnmarshaller, public
void complete(PgfExhaustiveGenerator *gen, PgfUnmarshaller *u); void complete(PgfExhaustiveGenerator *gen, PgfUnmarshaller *u);
}; };
struct Goal { typedef std::pair<ref<PgfText>,Scope*> Goal;
ref<PgfText> cat;
struct Result {
size_t ref_count;
Scope *scope; Scope *scope;
size_t scope_len; size_t scope_len;
std::vector<State1*> states;
std::vector<std::pair<PgfExpr,prob_t>> exprs;
Goal(ref<PgfText> cat) { Result() {
this->cat = cat; this->ref_count = 0;
this->scope = NULL; this->scope = NULL;
this->scope_len = 0; this->scope_len = 0;
} }
Goal(ref<PgfText> cat, Goal &g) { Result(Scope *scope, size_t scope_len) {
this->cat = cat;
this->scope = g.scope;
this->scope_len = g.scope_len;
}
Goal(Goal &g) {
this->cat = g.cat;
this->scope = g.scope;
this->scope_len = g.scope_len;
}
};
struct Result : Goal {
size_t ref_count;
std::vector<State1*> states;
std::vector<std::pair<PgfExpr,prob_t>> exprs;
Result(ref<PgfText> cat) : Goal(cat) {
this->ref_count = 0;
}
Result(ref<PgfText> cat, Goal &g) : Goal(cat,g) {
this->ref_count = 0;
}
Result(Goal &g) : Goal(g) {
this->ref_count = 0; this->ref_count = 0;
this->scope = scope;
this->scope_len = scope_len;
} }
}; };
@@ -140,25 +120,7 @@ class PGF_INTERNAL_DECL PgfExhaustiveGenerator : public PgfUnmarshaller, public
} }
}; };
struct CompareGoal : public std::less<Goal> { std::map<Goal, Result*> results;
bool operator() (const Goal &g1, const Goal &g2) const {
int cmp = textcmp(g1.cat, g2.cat);
if (cmp < 0)
return true;
else if (cmp > 0)
return false;
else
return (g1.scope < g2.scope);
}
};
struct Result2Goal {
Goal &operator()(Result *res) {
return *res;
}
};
std::_Rb_tree<Goal&, Result*, Result2Goal, CompareGoal> results;
std::priority_queue<State*, std::vector<State*>, CompareState> queue; std::priority_queue<State*, std::vector<State*>, CompareState> queue;
std::vector<Scope*> scopes; std::vector<Scope*> scopes;