Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue with bad allocation in post #176

Merged
merged 5 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions include/mata/nfa.hh
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,17 @@ struct Post : private Util::OrdVector<Move> {
*/
struct Delta {
private:
mutable std::vector<Post> post;
std::vector<Post> post;

/// Number of actual states occuring in the transition relation.
///
/// These states are used in the transition relation, either on the left side or on the right side.
/// The value is always consistent with the actual number of states in the transition relation.
mutable size_t m_num_of_states;
size_t m_num_of_states;

public:
inline static const Post empty_post; //when post[q] is not allocated, then delta[q] returns this.

Delta() : post(), m_num_of_states(0) {}
explicit Delta(size_t n) : post(), m_num_of_states(n) {}

Expand All @@ -246,8 +248,17 @@ public:
*/
size_t size() const;


Post & operator[] (State q)
// Get a non const reference to post of a state, which allows modifying the post.
//
// BEWARE, IT HAS A SIDE EFFECT.
//
// Namely, it allocates the post of the state if it was not allocated yet. This in turn may cause that
// the entire post data structure is re-allocated, iterators to it get invalidated ...
// Use the constant [] operator below if possible.
// Or, to prevent the side effect form happening, one might want to make sure that posts of all states in the automaton
// are allocated, e.g., write an NFA method that allocate delta for all states of the NFA.
// But it feels fragile, before doing something like that, better think and talk to people.
Post & get_mutable_post(State q)
{
if (q >= post.size()) {
const size_t new_size{ q + 1 };
Expand All @@ -260,17 +271,13 @@ public:
return post[q];
};

// Get a constant reference to the post of a state. No side effects.
const Post & operator[] (State q) const
{
if (q >= post.size()) {
const size_t new_size{ q + 1 };
post.resize(new_size);
if (new_size > m_num_of_states) {
m_num_of_states = new_size;
}
}

return post[q];
if (q >= post.size())
return empty_post;
else
return post[q];
};

void emplace_back() {
Expand Down Expand Up @@ -461,7 +468,7 @@ public:
void clear_transitions() {
const size_t delta_size = delta.post_size();
for (size_t i = 0; i < delta_size; ++i) {
delta[i] = Post();
delta.get_mutable_post(i) = Post();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/nfa/nfa-intersection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void add_product_transition(Nfa& product, std::unordered_map<std::pair<State,Sta
Move& intersection_transition) {
if (intersection_transition.empty()) { return; }

auto& intersect_state_transitions{ product.delta[product_map[pair_to_process]] };
auto& intersect_state_transitions{ product.delta.get_mutable_post(product_map[pair_to_process]) };
auto symbol_transitions_iter{ intersect_state_transitions.find(intersection_transition) };
if (symbol_transitions_iter == intersect_state_transitions.end()) {
intersect_state_transitions.insert(intersection_transition);
Expand Down Expand Up @@ -86,7 +86,7 @@ namespace Nfa {

Nfa intersection(const Nfa& lhs, const Nfa& rhs, bool preserve_epsilon,
std::unordered_map<std::pair<State,State>, State> *prod_map) {

const std::set<Symbol> epsilons({EPSILON});
return Algorithms::intersection_eps(lhs, rhs, preserve_epsilon, epsilons, prod_map);
}
Expand Down
10 changes: 5 additions & 5 deletions src/nfa/nfa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ namespace {
}

// add the transition 'q_class_state-q_trans.symbol->representatives_class_states' at the end of transition list of transitions starting from q_class_state
// as the q_trans.symbol should be largest symbol we saw (as we iterate trough getTransitionsFromState(q) which is ordered)
result.delta[q_class_state].insert(Move(q_trans.symbol, representatives_class_states));
// as the q_trans.symbol should be the largest symbol we saw (as we iterate trough getTransitionsFromState(q) which is ordered)
result.delta.get_mutable_post(q_class_state).insert(Move(q_trans.symbol, representatives_class_states));
}

if (aut.final[q]) { // if q is final, then all states in its class are final => we make q_class_state final
Expand Down Expand Up @@ -224,7 +224,7 @@ namespace {
}
}
if (!new_state_trans_with_symbol.empty()) {
trimmed_aut.delta[original_state_mapping.second].insert(new_state_trans_with_symbol);
trimmed_aut.delta.get_mutable_post(original_state_mapping.second).insert(new_state_trans_with_symbol);
}
}
}
Expand Down Expand Up @@ -1133,7 +1133,7 @@ Nfa Mata::Nfa::uni(const Nfa &lhs, const Nfa &rhs) {
transitionFromUnionState.insert(thisStateToUnionState[stateTo]);
}

unionAutomaton.delta[unionState].insert(transitionFromUnionState);
unionAutomaton.delta.get_mutable_post(unionState).insert(transitionFromUnionState);
}
}

Expand Down Expand Up @@ -1274,7 +1274,7 @@ Nfa Mata::Nfa::determinize(
}
worklist.emplace_back(std::make_pair(Tid, T));
}
result.delta[Sid].insert(Move(currentSymbol, Tid));
result.delta.get_mutable_post(Sid).insert(Move(currentSymbol, Tid));
}
}

Expand Down
33 changes: 30 additions & 3 deletions src/nfa/tests-nfa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,26 @@ TEST_CASE("Mata::Nfa::determinize()")
REQUIRE(result.final[subset_map[{2}]]);
REQUIRE(result.delta.contains(subset_map[{1}], 'a', subset_map[{2}]));
}

SECTION("This broke Delta when delta[q] could cause re-allocation of post")
{
Nfa x{};
x.initial.add(0);
x.final.add(4);
x.delta.add(0, 1, 3);
x.delta.add(3, 1, 3);
x.delta.add(3, 2, 3);
x.delta.add(3, 0, 1);
x.delta.add(1, 1, 1);
x.delta.add(1, 2, 1);
x.delta.add(1, 0, 2);
x.delta.add(2, 0, 2);
x.delta.add(2, 1, 2);
x.delta.add(2, 2, 2);
x.delta.add(2, 0, 4);
OnTheFlyAlphabet alphabet{};
auto complement_result{determinize(x)};
}
} // }}}

TEST_CASE("Mata::Nfa::minimize() for profiling", "[.profiling],[minimize]") {
Expand Down Expand Up @@ -2732,18 +2752,25 @@ TEST_CASE("Mata::Nfa::delta.operator[]")
FILL_WITH_AUT_A(aut);
REQUIRE(aut.get_num_of_trans() == 15);
aut.delta[25];
REQUIRE(aut.size() == 20);

aut.delta.get_mutable_post(25);
REQUIRE(aut.size() == 26);
REQUIRE(aut.delta[25].empty());

aut.delta[50];
aut.delta.get_mutable_post(50);
REQUIRE(aut.size() == 51);
REQUIRE(aut.delta[50].empty());

const Nfa aut1 = aut;
aut1.delta[60];
Nfa aut1 = aut;
aut1.delta.get_mutable_post(60);
REQUIRE(aut1.size() == 61);
REQUIRE(aut1.delta[60].empty());

const Nfa aut2 = aut;
aut2.delta[60];
REQUIRE(aut2.size() == 51);
REQUIRE(aut2.delta[60].empty());
}

TEST_CASE("Mata::Nfa::Nfa::unify_(initial/final)()") {
Expand Down