diff --git a/src/pdaaal/PDA.h b/src/pdaaal/PDA.h index e4da189..57ca878 100644 --- a/src/pdaaal/PDA.h +++ b/src/pdaaal/PDA.h @@ -88,8 +88,8 @@ namespace pdaaal { template struct rule_t>> { 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& other) const { if (_to != other._to) @@ -108,9 +108,9 @@ namespace pdaaal { template struct rule_t>> { size_t _to = 0; - op_t _operation = NOOP; - W _weight; - uint32_t _op_label = uint32_t{}; + op_t _operation = PUSH; + W _weight = zero()(); + uint32_t _op_label = 0; labels_t _labels; bool operator<(const rule_t& other) const { if (_to != other._to) diff --git a/src/pdaaal/PDAFactory.h b/src/pdaaal/PDAFactory.h index 6b3ab65..bce6079 100644 --- a/src/pdaaal/PDAFactory.h +++ b/src/pdaaal/PDAFactory.h @@ -75,8 +75,8 @@ namespace pdaaal { _des_stack.compile(); }; - PDA_Adapter compile() { - PDA_Adapter result(_all_labels); + PDAAdapter compile() { + PDAAdapter result(_all_labels); bool cons_empty_accept = false; bool des_empty_accept = empty_desctruction_accept(); @@ -110,7 +110,7 @@ namespace pdaaal { } protected: - bool initialize_construction(PDA_Adapter& result, std::unordered_set& seen, std::vector& waiting) { + bool initialize_construction(PDAAdapter& result, std::unordered_set& seen, std::vector& waiting) { bool has_empty_accept = false; std::vector empty; for (auto& i : _cons_stack.initial()) { @@ -145,7 +145,7 @@ namespace pdaaal { return has_empty_accept; } - void build_construction(PDA_Adapter& result, std::unordered_set& seen, std::vector& waiting) { + void build_construction(PDAAdapter& result, std::unordered_set& seen, std::vector& waiting) { while (!waiting.empty()) { auto top = waiting.back(); waiting.pop_back(); @@ -188,7 +188,7 @@ namespace pdaaal { return false; } - void build_pda(PDA_Adapter& result, bool des_empty_accept) { + void build_pda(PDAAdapter& result, bool des_empty_accept) { auto pdawaiting = initial(); std::unordered_set pdaseen(pdawaiting.begin(), pdawaiting.end()); std::vector empty; @@ -247,7 +247,7 @@ namespace pdaaal { } } - void build_destruction(PDA_Adapter& result) { + void build_destruction(PDAAdapter& result) { std::vector waiting_next = _des_stack.initial(); std::unordered_set seen_next(waiting_next.begin(), waiting_next.end()); std::vector empty; diff --git a/src/pdaaal/Solver.h b/src/pdaaal/Solver.h index 067cbdc..9613095 100644 --- a/src/pdaaal/Solver.h +++ b/src/pdaaal/Solver.h @@ -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 dummy_rule{t._from, PUSH, 0}; // PUSH and 0 are the smallest w.r.t. PDA::rule_t::operator< + rule_t 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; diff --git a/src/pdaaal/Weight.h b/src/pdaaal/Weight.h index 09135bd..36e0a0c 100644 --- a/src/pdaaal/Weight.h +++ b/src/pdaaal/Weight.h @@ -156,6 +156,7 @@ namespace pdaaal { const std::optional> _function; public: static_assert(is_weighted); + using result_type = W; // A single function explicit linear_weight_function(std::function function) : _function(function) {} @@ -165,7 +166,7 @@ namespace pdaaal { static_assert(has_mult_v, "For a linear combination, he weight type needs to specialize mult."); } - constexpr W operator()(Args... args) const { + constexpr result_type operator()(Args... args) const { if (_function) { return _function.value()(args...); } @@ -184,10 +185,11 @@ namespace pdaaal { const std::vector> _functions; public: static_assert(is_weighted); + using result_type = std::vector; explicit ordered_weight_function(std::vector> functions) : _functions(functions) {} - constexpr std::vector operator()(Args... args) { + constexpr result_type operator()(Args... args) const { std::vector result; std::transform(_functions.begin(), _functions.end(), std::back_inserter(result), [&args...](const linear_weight_function& f) -> W { return f(args...); }); diff --git a/test/PAutomaton_test.cpp b/test/PAutomaton_test.cpp index a70b90f..d0364d1 100644 --- a/test/PAutomaton_test.cpp +++ b/test/PAutomaton_test.cpp @@ -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 labels{'A', 'B', 'C'}; - TypedPDA 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> pda(labels); + std::vector 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 init_stack{'A', 'A'}; PAutomaton automaton(pda, 0, pda.encode_pre(init_stack));