Skip to content

Commit

Permalink
Merge pull request #3 from DEIS-Tools/bugfix-weighted
Browse files Browse the repository at this point in the history
Fixes some bugs in Weighted PDAAAL
  • Loading branch information
petergjoel authored Mar 26, 2020
2 parents 523647a + 0a55572 commit c37e347
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/pdaaal/PDA.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ namespace pdaaal {
template<typename W, typename C>
struct rule_t<W, C, std::enable_if_t<!is_weighted<W>>> {
size_t _to = 0;
op_t _operation = NOOP;
uint32_t _op_label = uint32_t{};
op_t _operation = PUSH;
uint32_t _op_label = 0;
labels_t _labels;
bool operator<(const rule_t<W,C>& other) const {
if (_to != other._to)
Expand All @@ -108,9 +108,9 @@ namespace pdaaal {
template<typename W, typename C>
struct rule_t<W, C, std::enable_if_t<is_weighted<W>>> {
size_t _to = 0;
op_t _operation = NOOP;
W _weight;
uint32_t _op_label = uint32_t{};
op_t _operation = PUSH;
W _weight = zero<W>()();
uint32_t _op_label = 0;
labels_t _labels;
bool operator<(const rule_t<W,C>& other) const {
if (_to != other._to)
Expand Down
12 changes: 6 additions & 6 deletions src/pdaaal/PDAFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ namespace pdaaal {
_des_stack.compile();
};

PDA_Adapter<T,W,C> compile() {
PDA_Adapter<T,W,C> result(_all_labels);
PDAAdapter<T,W,C> compile() {
PDAAdapter<T,W,C> result(_all_labels);
bool cons_empty_accept = false;
bool des_empty_accept = empty_desctruction_accept();

Expand Down Expand Up @@ -110,7 +110,7 @@ namespace pdaaal {
}

protected:
bool initialize_construction(PDA_Adapter<T,W,C>& result, std::unordered_set<const nfastate_t*>& seen, std::vector<const nfastate_t*>& waiting) {
bool initialize_construction(PDAAdapter<T,W,C>& result, std::unordered_set<const nfastate_t*>& seen, std::vector<const nfastate_t*>& waiting) {
bool has_empty_accept = false;
std::vector<T> empty;
for (auto& i : _cons_stack.initial()) {
Expand Down Expand Up @@ -145,7 +145,7 @@ namespace pdaaal {
return has_empty_accept;
}

void build_construction(PDA_Adapter<T,W,C>& result, std::unordered_set<const nfastate_t*>& seen, std::vector<const nfastate_t*>& waiting) {
void build_construction(PDAAdapter<T,W,C>& result, std::unordered_set<const nfastate_t*>& seen, std::vector<const nfastate_t*>& waiting) {
while (!waiting.empty()) {
auto top = waiting.back();
waiting.pop_back();
Expand Down Expand Up @@ -188,7 +188,7 @@ namespace pdaaal {
return false;
}

void build_pda(PDA_Adapter<T,W,C>& result, bool des_empty_accept) {
void build_pda(PDAAdapter<T,W,C>& result, bool des_empty_accept) {
auto pdawaiting = initial();
std::unordered_set<size_t> pdaseen(pdawaiting.begin(), pdawaiting.end());
std::vector<T> empty;
Expand Down Expand Up @@ -247,7 +247,7 @@ namespace pdaaal {
}
}

void build_destruction(PDA_Adapter<T,W,C>& result) {
void build_destruction(PDAAdapter<T,W,C>& result) {
std::vector<nfastate_t*> waiting_next = _des_stack.initial();
std::unordered_set<nfastate_t*> seen_next(waiting_next.begin(), waiting_next.end());
std::vector<T> empty;
Expand Down
2 changes: 1 addition & 1 deletion src/pdaaal/Solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ namespace pdaaal {
if (t._from >= n_pda_states) { continue; }
for (auto pre_state : pda_states[t._from]._pre_states) {
const auto &rules = pda_states[pre_state]._rules;
rule_t<W,C> dummy_rule{t._from, PUSH, 0}; // PUSH and 0 are the smallest w.r.t. PDA::rule_t::operator<
rule_t<W,C> dummy_rule{t._from};
auto lb = std::lower_bound(rules.begin(), rules.end(), dummy_rule);
while (lb != rules.end() && lb->_to == t._from) {
auto &rule = *lb;
Expand Down
6 changes: 4 additions & 2 deletions src/pdaaal/Weight.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ namespace pdaaal {
const std::optional<std::function<W(Args...)>> _function;
public:
static_assert(is_weighted<W>);
using result_type = W;

// A single function
explicit linear_weight_function(std::function<W(Args...)> function) : _function(function) {}
Expand All @@ -165,7 +166,7 @@ namespace pdaaal {
static_assert(has_mult_v<W>, "For a linear combination, he weight type needs to specialize mult<W>.");
}

constexpr W operator()(Args... args) const {
constexpr result_type operator()(Args... args) const {
if (_function) {
return _function.value()(args...);
}
Expand All @@ -184,10 +185,11 @@ namespace pdaaal {
const std::vector<linear_weight_function<W, Args...>> _functions;
public:
static_assert(is_weighted<W>);
using result_type = std::vector<W>;

explicit ordered_weight_function(std::vector<linear_weight_function<W, Args...>> functions) : _functions(functions) {}

constexpr std::vector<W> operator()(Args... args) {
constexpr result_type operator()(Args... args) const {
std::vector<W> result;
std::transform(_functions.begin(), _functions.end(), std::back_inserter(result),
[&args...](const linear_weight_function<W, Args...>& f) -> W { return f(args...); });
Expand Down
13 changes: 7 additions & 6 deletions test/PAutomaton_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@ BOOST_AUTO_TEST_CASE(WeightedPreStar)
// This is pretty much the rules from the example in Figure 3.1 (Schwoon-php02)
// However r_2 requires a swap and a push, which is done through auxiliary state 3.
std::unordered_set<char> labels{'A', 'B', 'C'};
TypedPDA<char, int> pda(labels);
pda.add_rule(0, 1, PUSH, 'B', false, 'A', 1);
pda.add_rule(0, 0, POP , '*', false, 'B', 1);
pda.add_rule(1, 3, SWAP, 'A', false, 'B', 1);
pda.add_rule(2, 0, SWAP, 'B', false, 'C', 1);
pda.add_rule(3, 2, PUSH, 'C', false, 'A', 1);
TypedPDA<char, std::vector<int>> pda(labels);
std::vector<int> w{1};
pda.add_rule(0, 1, PUSH, 'B', false, 'A', w);
pda.add_rule(0, 0, POP , '*', false, 'B', w);
pda.add_rule(1, 3, SWAP, 'A', false, 'B', w);
pda.add_rule(2, 0, SWAP, 'B', false, 'C', w);
pda.add_rule(3, 2, PUSH, 'C', false, 'A', w);

std::vector<char> init_stack{'A', 'A'};
PAutomaton automaton(pda, 0, pda.encode_pre(init_stack));
Expand Down

0 comments on commit c37e347

Please sign in to comment.