Skip to content

Commit

Permalink
Allow adding new states with levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Adda0 committed Feb 21, 2024
1 parent 5191aab commit 6de05b5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
14 changes: 13 additions & 1 deletion include/mata/nft/nft.hh
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,23 @@ public:
State add_state();

/**
* Add state @p state to @c delta if @p state is not in @c delta yet.
* Add state @p state to @c this with level @p level if @p state is not in @c this yet.
* @return The requested @p state.
*/
State add_state(State state);

/**
* Add a new (fresh) state to the automaton with level @p level.
* @return The newly created state.
*/
State add_state_with_level(Level level);

/**
* Add state @p state to @c this with level @p level if @p state is not in @c this yet.
* @return The requested @p state.
*/
State add_state_with_level(State state, Level level);

/**
* @brief Clear the underlying NFT to a blank NFT.
*
Expand Down
22 changes: 16 additions & 6 deletions src/nft/nft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,26 @@ State Nft::add_state() {
return mata::nfa::Nfa::add_state();
}

State Nft::add_state(State state) {
const size_t levels_size = levels.size();
if (state >= levels_size) {
levels.resize(state + 1);
const size_t begin_idx = (levels_size == 0) ? 0 : levels_size - 1;
std::fill(levels.begin() + static_cast<long int>(begin_idx), levels.end(), 0);
State Nft::add_state(const State state) {
const size_t required_capacity{ state + 1 };
if (levels.size() < required_capacity) {
levels.resize(required_capacity, DEFAULT_LEVEL);
}
return mata::nfa::Nfa::add_state(state);
}

State Nft::add_state_with_level(const Level level) {
const State state{ add_state() };
levels[state] = level;
return state;
}

State Nft::add_state_with_level(const State state, const Level level) {
add_state(state);
levels[state] = level;
return mata::nfa::Nfa::add_state(state);
}

void Nft::clear() {
mata::nfa::Nfa::clear();
levels.clear();
Expand Down
19 changes: 19 additions & 0 deletions tests/nft/nft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3125,3 +3125,22 @@ TEST_CASE("mata::nft::Nft::get_one_level_aut") {
}

}

TEST_CASE("mata::nft::Nft::add_state()") {
Nft nft{};
State state{ nft.add_state() };
CHECK(state == 0);
CHECK(nft.levels[state] == 0);
state = nft.add_state(4);
CHECK(state == 4);
CHECK(nft.levels[state] == 0);
CHECK(nft.num_of_states() == 5);
state = nft.add_state_with_level(3);
CHECK(state == 5);
CHECK(nft.levels[state] == 3);
CHECK(nft.num_of_states() == 6);
state = nft.add_state_with_level(12, 1);
CHECK(state == 12);
CHECK(nft.levels[state] == 1);
CHECK(nft.num_of_states() == 13);
}

0 comments on commit 6de05b5

Please sign in to comment.