Some refactor
This commit is contained in:
parent
40b634b722
commit
346f0016a5
|
@ -45,7 +45,7 @@ class DFAGraph {
|
||||||
|
|
||||||
size_t AddNewVertex();
|
size_t AddNewVertex();
|
||||||
void AddFinalVertex(size_t number);
|
void AddFinalVertex(size_t number);
|
||||||
void AddStartVertex(size_t number);
|
void SetStartVertex(size_t number);
|
||||||
|
|
||||||
void RemoveVertex(size_t number);
|
void RemoveVertex(size_t number);
|
||||||
void RemoveFinalVertex(size_t number);
|
void RemoveFinalVertex(size_t number);
|
||||||
|
@ -56,7 +56,7 @@ class DFAGraph {
|
||||||
|
|
||||||
size_t GetCountVertexes() const;
|
size_t GetCountVertexes() const;
|
||||||
const std::vector<size_t>& GetFinalVertexes() const;
|
const std::vector<size_t>& GetFinalVertexes() const;
|
||||||
const std::vector<size_t>& GetStartVertexes() const;
|
size_t GetStartVertex() const;
|
||||||
|
|
||||||
void Print() const;
|
void Print() const;
|
||||||
bool Accepted(const std::string&) const;
|
bool Accepted(const std::string&) const;
|
||||||
|
@ -65,6 +65,6 @@ class DFAGraph {
|
||||||
size_t count_vertexes_ = 0;
|
size_t count_vertexes_ = 0;
|
||||||
std::map<size_t, Vertex> vertexes_;
|
std::map<size_t, Vertex> vertexes_;
|
||||||
std::vector<size_t> final_vertexes_;
|
std::vector<size_t> final_vertexes_;
|
||||||
std::vector<size_t> start_vertexes_;
|
size_t start_vertex_ = -1;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace converters {
|
||||||
using namespace NFA;
|
using namespace NFA;
|
||||||
using namespace DFA;
|
using namespace DFA;
|
||||||
NFAGraph AddAllEpsilonTransitions(NFAGraph&&);
|
NFAGraph AddAllEpsilonTransitions(NFAGraph&&);
|
||||||
NFAGraph AddAllPossibleStartFinal(NFAGraph&&);
|
NFAGraph AddAllPossibleFinalVertexes(NFAGraph&&);
|
||||||
NFAGraph DeleteEpsilonTransitions(NFAGraph&&);
|
NFAGraph DeleteEpsilonTransitions(NFAGraph&&);
|
||||||
NFAGraph DeleteTransitionsByOneLetter(NFAGraph&&);
|
NFAGraph DeleteTransitionsByOneLetter(NFAGraph&&);
|
||||||
DFAGraph NFAGraphToDFAGraph(NFAGraph&&);
|
DFAGraph NFAGraphToDFAGraph(NFAGraph&&);
|
||||||
|
|
|
@ -8,7 +8,8 @@ DFAGraph::DFAGraph(DFAGraph&& another) {
|
||||||
std::swap(count_vertexes_, another.count_vertexes_);
|
std::swap(count_vertexes_, another.count_vertexes_);
|
||||||
std::swap(vertexes_, another.vertexes_);
|
std::swap(vertexes_, another.vertexes_);
|
||||||
std::swap(final_vertexes_, another.final_vertexes_);
|
std::swap(final_vertexes_, another.final_vertexes_);
|
||||||
std::swap(start_vertexes_, another.start_vertexes_);
|
// TODO
|
||||||
|
// std::swap(start_vertexes_, another.start_vertexes_);
|
||||||
|
|
||||||
for (auto& i: vertexes_)
|
for (auto& i: vertexes_)
|
||||||
i.second.owner_ = this;
|
i.second.owner_ = this;
|
||||||
|
@ -20,7 +21,8 @@ DFAGraph& DFAGraph::operator=(DFAGraph&& another) {
|
||||||
std::swap(count_vertexes_, another.count_vertexes_);
|
std::swap(count_vertexes_, another.count_vertexes_);
|
||||||
std::swap(vertexes_, another.vertexes_);
|
std::swap(vertexes_, another.vertexes_);
|
||||||
std::swap(final_vertexes_, another.final_vertexes_);
|
std::swap(final_vertexes_, another.final_vertexes_);
|
||||||
std::swap(start_vertexes_, another.start_vertexes_);
|
// TODO
|
||||||
|
// std::swap(start_vertexes_, another.start_vertexes_);
|
||||||
|
|
||||||
for (auto& i: vertexes_)
|
for (auto& i: vertexes_)
|
||||||
i.second.owner_ = this;
|
i.second.owner_ = this;
|
||||||
|
@ -41,10 +43,10 @@ void DFAGraph::AddFinalVertex(size_t number) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DFAGraph::AddStartVertex(size_t number) {
|
void DFAGraph::SetStartVertex(size_t number) {
|
||||||
if (!GetVertex(number).is_start_) {
|
if (!GetVertex(number).is_start_) {
|
||||||
GetVertex(number).is_start_ = true;
|
GetVertex(number).is_start_ = true;
|
||||||
start_vertexes_.push_back(number);
|
start_vertex_ = number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,13 +69,8 @@ void DFAGraph::RemoveFinalVertex(size_t number) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DFAGraph::RemoveStartVertex(size_t number) {
|
void DFAGraph::RemoveStartVertex(size_t number) {
|
||||||
for (size_t i = 0; i < start_vertexes_.size(); ++i) {
|
if (start_vertex_ == number) {
|
||||||
if (start_vertexes_[i] == number) {
|
start_vertex_ = -1;
|
||||||
GetVertex(number).is_start_ = false;
|
|
||||||
std::swap(start_vertexes_[i], start_vertexes_.back());
|
|
||||||
start_vertexes_.pop_back();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,8 +92,8 @@ const std::vector<size_t>& DFAGraph::GetFinalVertexes() const {
|
||||||
return final_vertexes_;
|
return final_vertexes_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::vector<size_t>& DFAGraph::GetStartVertexes() const {
|
size_t DFAGraph::GetStartVertex() const {
|
||||||
return start_vertexes_;
|
return start_vertex_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DFAGraph::Print() const {
|
void DFAGraph::Print() const {
|
||||||
|
@ -114,7 +111,7 @@ void DFAGraph::Print() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DFAGraph::Accepted(const std::string& str) const {
|
bool DFAGraph::Accepted(const std::string& str) const {
|
||||||
size_t current = vertexes_.at(start_vertexes_[0]).GetNumber();
|
size_t current = start_vertex_;
|
||||||
for (auto i: str) {
|
for (auto i: str) {
|
||||||
if (vertexes_.at(current).GetTransitions().count(i)) {
|
if (vertexes_.at(current).GetTransitions().count(i)) {
|
||||||
current = vertexes_.at(vertexes_.at(current).GetTransitions().at(i)).GetNumber();
|
current = vertexes_.at(vertexes_.at(current).GetTransitions().at(i)).GetNumber();
|
||||||
|
|
|
@ -50,7 +50,7 @@ void Vertex::SetFinal(bool status) {
|
||||||
void Vertex::SetStart(bool status) {
|
void Vertex::SetStart(bool status) {
|
||||||
if (status != is_start_) {
|
if (status != is_start_) {
|
||||||
if (status)
|
if (status)
|
||||||
owner_->AddStartVertex(number_);
|
owner_->SetStartVertex(number_);
|
||||||
else
|
else
|
||||||
owner_->RemoveStartVertex(number_);
|
owner_->RemoveStartVertex(number_);
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ NFAGraph AddAllEpsilonTransitions(NFAGraph&& nfa_graph) {
|
||||||
return std::move(nfa_graph);
|
return std::move(nfa_graph);
|
||||||
}
|
}
|
||||||
|
|
||||||
NFAGraph AddAllPossibleStartFinal(NFAGraph&& nfa_graph) {
|
NFAGraph AddAllPossibleFinalVertexes(NFAGraph&& nfa_graph) {
|
||||||
if (nfa_graph.GetStartVertexes().size() != 1) {
|
if (nfa_graph.GetStartVertexes().size() != 1) {
|
||||||
size_t start_vertex = nfa_graph.AddNewVertex();
|
size_t start_vertex = nfa_graph.AddNewVertex();
|
||||||
for (auto v: nfa_graph.GetStartVertexes()) {
|
for (auto v: nfa_graph.GetStartVertexes()) {
|
||||||
|
@ -146,8 +146,7 @@ NFAGraph DeleteTransitionsByOneLetter(NFAGraph&& nfa_graph) {
|
||||||
}
|
}
|
||||||
|
|
||||||
DFAGraph NFAGraphToDFAGraph(NFAGraph&& nfa_graph) {
|
DFAGraph NFAGraphToDFAGraph(NFAGraph&& nfa_graph) {
|
||||||
nfa_graph = AddAllEpsilonTransitions(std::move(nfa_graph));
|
nfa_graph = DeleteTransitionsByOneLetter(DeleteEpsilonTransitions(AddAllPossibleFinalVertexes(
|
||||||
nfa_graph = DeleteTransitionsByOneLetter(DeleteEpsilonTransitions(AddAllPossibleStartFinal(
|
|
||||||
AddAllEpsilonTransitions(std::move(nfa_graph)))));
|
AddAllEpsilonTransitions(std::move(nfa_graph)))));
|
||||||
|
|
||||||
const int n = nfa_graph.GetCountVertexes();
|
const int n = nfa_graph.GetCountVertexes();
|
||||||
|
@ -157,12 +156,18 @@ DFAGraph NFAGraphToDFAGraph(NFAGraph&& nfa_graph) {
|
||||||
if (nfa_graph.NotExistVertex(i)) continue;
|
if (nfa_graph.NotExistVertex(i)) continue;
|
||||||
number_vertex_in_DFA[i] = result.AddNewVertex();
|
number_vertex_in_DFA[i] = result.AddNewVertex();
|
||||||
}
|
}
|
||||||
|
bool exists_start_vertex = false;
|
||||||
for (int i = 0; i < n; ++i) {
|
for (int i = 0; i < n; ++i) {
|
||||||
if (nfa_graph.NotExistVertex(i)) continue;
|
if (nfa_graph.NotExistVertex(i)) continue;
|
||||||
if (nfa_graph.GetVertex(i).IsFinal())
|
if (nfa_graph.GetVertex(i).IsFinal())
|
||||||
result.GetVertex(number_vertex_in_DFA[i]).SetFinal(true);
|
result.GetVertex(number_vertex_in_DFA[i]).SetFinal(true);
|
||||||
if (nfa_graph.GetVertex(i).IsStart())
|
if (nfa_graph.GetVertex(i).IsStart()) {
|
||||||
|
if (exists_start_vertex) {
|
||||||
|
throw std::runtime_error("I can't delete starts vertex");
|
||||||
|
}
|
||||||
result.GetVertex(number_vertex_in_DFA[i]).SetStart(true);
|
result.GetVertex(number_vertex_in_DFA[i]).SetStart(true);
|
||||||
|
exists_start_vertex = true;
|
||||||
|
}
|
||||||
|
|
||||||
const auto& transitions = nfa_graph.GetVertex(i).GetTransitions();
|
const auto& transitions = nfa_graph.GetVertex(i).GetTransitions();
|
||||||
for (const auto& t: transitions) {
|
for (const auto& t: transitions) {
|
||||||
|
@ -170,6 +175,9 @@ DFAGraph NFAGraphToDFAGraph(NFAGraph&& nfa_graph) {
|
||||||
result.GetVertex(number_vertex_in_DFA[i]).AddEdge(t.first, *t.second.begin());
|
result.GetVertex(number_vertex_in_DFA[i]).AddEdge(t.first, *t.second.begin());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!exists_start_vertex) {
|
||||||
|
throw std::runtime_error("I can't find start vertex");
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue