Delete unnecessary shared pointer
This commit is contained in:
parent
7023d46c56
commit
1aed271643
|
@ -51,7 +51,8 @@ class DFAGraph {
|
|||
void RemoveFinalVertex(size_t number);
|
||||
void RemoveStartVertex(size_t number);
|
||||
|
||||
std::shared_ptr<Vertex> GetVertex(size_t number);
|
||||
Vertex& GetVertex(size_t number);
|
||||
bool NotExistVertex(size_t number);
|
||||
|
||||
size_t GetCountVertexes() const;
|
||||
const std::vector<size_t>& GetFinalVertexes() const;
|
||||
|
@ -62,7 +63,7 @@ class DFAGraph {
|
|||
void CreateDotFile(const std::string& filename) const;
|
||||
private:
|
||||
size_t count_vertexes_ = 0;
|
||||
std::map<size_t, std::shared_ptr<Vertex>> vertexes_;
|
||||
std::map<size_t, Vertex> vertexes_;
|
||||
std::vector<size_t> final_vertexes_;
|
||||
std::vector<size_t> start_vertexes_;
|
||||
};
|
||||
|
|
|
@ -55,7 +55,9 @@ class NFAGraph {
|
|||
std::vector<size_t> start_vertexes,
|
||||
std::vector<size_t> final_vertexes);
|
||||
|
||||
std::shared_ptr<Vertex> GetVertex(size_t number);
|
||||
Vertex& GetVertex(size_t number);
|
||||
bool NotExistVertex(size_t number);
|
||||
|
||||
size_t GetCountVertexes() const;
|
||||
const std::vector<size_t>& GetFinalVertexes() const;
|
||||
const std::vector<size_t>& GetStartVertexes() const;
|
||||
|
@ -64,7 +66,7 @@ class NFAGraph {
|
|||
void CreateDotFile(const std::string& filename) const;
|
||||
private:
|
||||
size_t count_vertexes_ = 0;
|
||||
std::map<size_t, std::shared_ptr<Vertex>> vertexes_;
|
||||
std::map<size_t, Vertex> vertexes_;
|
||||
std::vector<size_t> final_vertexes_;
|
||||
std::vector<size_t> start_vertexes_;
|
||||
};
|
||||
|
|
|
@ -11,9 +11,9 @@ DFAGraph::DFAGraph(DFAGraph&& another) {
|
|||
std::swap(start_vertexes_, another.start_vertexes_);
|
||||
|
||||
for (auto& i: vertexes_)
|
||||
i.second->owner_ = this;
|
||||
i.second.owner_ = this;
|
||||
for (auto& i: another.vertexes_)
|
||||
i.second->owner_ = &another;
|
||||
i.second.owner_ = &another;
|
||||
}
|
||||
|
||||
DFAGraph& DFAGraph::operator=(DFAGraph&& another) {
|
||||
|
@ -23,27 +23,27 @@ DFAGraph& DFAGraph::operator=(DFAGraph&& another) {
|
|||
std::swap(start_vertexes_, another.start_vertexes_);
|
||||
|
||||
for (auto& i: vertexes_)
|
||||
i.second->owner_ = this;
|
||||
i.second.owner_ = this;
|
||||
for (auto& i: another.vertexes_)
|
||||
i.second->owner_ = &another;
|
||||
i.second.owner_ = &another;
|
||||
return *this;
|
||||
}
|
||||
size_t DFAGraph::AddNewVertex() {
|
||||
vertexes_[count_vertexes_] = std::make_shared<Vertex>(this);
|
||||
vertexes_[count_vertexes_]->number_ = count_vertexes_;
|
||||
vertexes_.emplace(count_vertexes_, this);
|
||||
GetVertex(count_vertexes_).number_ = count_vertexes_;
|
||||
return count_vertexes_++;
|
||||
}
|
||||
|
||||
void DFAGraph::AddFinalVertex(size_t number) {
|
||||
if (!vertexes_[number]->is_final_) {
|
||||
vertexes_[number]->is_final_ = true;
|
||||
if (!GetVertex(number).is_final_) {
|
||||
GetVertex(number).is_final_ = true;
|
||||
final_vertexes_.push_back(number);
|
||||
}
|
||||
}
|
||||
|
||||
void DFAGraph::AddStartVertex(size_t number) {
|
||||
if (!vertexes_[number]->is_start_) {
|
||||
vertexes_[number]->is_start_ = true;
|
||||
if (!GetVertex(number).is_start_) {
|
||||
GetVertex(number).is_start_ = true;
|
||||
start_vertexes_.push_back(number);
|
||||
}
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ void DFAGraph::RemoveVertex(size_t number) {
|
|||
void DFAGraph::RemoveFinalVertex(size_t number) {
|
||||
for (size_t i = 0; i < final_vertexes_.size(); ++i) {
|
||||
if (final_vertexes_[i] == number) {
|
||||
vertexes_[number]->is_final_ = false;
|
||||
GetVertex(number).is_final_ = false;
|
||||
std::swap(final_vertexes_[i], final_vertexes_.back());
|
||||
final_vertexes_.pop_back();
|
||||
break;
|
||||
|
@ -69,7 +69,7 @@ void DFAGraph::RemoveFinalVertex(size_t number) {
|
|||
void DFAGraph::RemoveStartVertex(size_t number) {
|
||||
for (size_t i = 0; i < start_vertexes_.size(); ++i) {
|
||||
if (start_vertexes_[i] == number) {
|
||||
vertexes_[number]->is_start_ = false;
|
||||
GetVertex(number).is_start_ = false;
|
||||
std::swap(start_vertexes_[i], start_vertexes_.back());
|
||||
start_vertexes_.pop_back();
|
||||
break;
|
||||
|
@ -77,10 +77,14 @@ void DFAGraph::RemoveStartVertex(size_t number) {
|
|||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<DFAGraph::Vertex> DFAGraph::GetVertex(size_t number) {
|
||||
if (vertexes_.count(number))
|
||||
return vertexes_[number];
|
||||
return nullptr;
|
||||
DFAGraph::Vertex& DFAGraph::GetVertex(size_t number) {
|
||||
if (!NotExistVertex(number))
|
||||
return vertexes_.at(number);
|
||||
throw std::out_of_range("This vertex don't exist");
|
||||
}
|
||||
|
||||
bool DFAGraph::NotExistVertex(size_t number) {
|
||||
return !vertexes_.count(number);
|
||||
}
|
||||
|
||||
size_t DFAGraph::GetCountVertexes() const {
|
||||
|
@ -97,12 +101,12 @@ const std::vector<size_t>& DFAGraph::GetStartVertexes() const {
|
|||
|
||||
void DFAGraph::Print() const {
|
||||
for (auto i: vertexes_) {
|
||||
std::cout << i.second->number_ << " " << "f-" << i.second->is_final_ << " s-" <<
|
||||
i.second->is_start_ << std::endl;
|
||||
std::cout << i.second.number_ << " " << "f-" << i.second.is_final_ << " s-" <<
|
||||
i.second.is_start_ << std::endl;
|
||||
}
|
||||
for (auto& i: vertexes_) {
|
||||
for (auto& j: i.second->transitions_) {
|
||||
std::cout << i.second->number_ << "->" << j.second << " <" << j.first << ">" <<
|
||||
for (auto& j: i.second.transitions_) {
|
||||
std::cout << i.second.number_ << "->" << j.second << " <" << j.first << ">" <<
|
||||
std::endl;
|
||||
}
|
||||
}
|
||||
|
@ -110,35 +114,35 @@ void DFAGraph::Print() const {
|
|||
}
|
||||
|
||||
bool DFAGraph::Accepted(const std::string& str) const {
|
||||
std::shared_ptr<const Vertex> current = vertexes_.at(start_vertexes_[0]);
|
||||
size_t current = vertexes_.at(start_vertexes_[0]).GetNumber();
|
||||
for (auto i: str) {
|
||||
if (current->GetTransitions().count(i)) {
|
||||
current = vertexes_.at(current->GetTransitions().at(i));
|
||||
if (vertexes_.at(current).GetTransitions().count(i)) {
|
||||
current = vertexes_.at(vertexes_.at(current).GetTransitions().at(i)).GetNumber();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return current->IsFinal();
|
||||
return vertexes_.at(current).IsFinal();
|
||||
}
|
||||
|
||||
void DFAGraph::CreateDotFile(const std::string& filename) const {
|
||||
std::ofstream out(filename);
|
||||
out << "digraph G {\n";
|
||||
|
||||
for (auto& i: vertexes_) {
|
||||
for (auto& j: i.second->transitions_) {
|
||||
out << i.first << "->" << j.second << "[label=" << j.first << "]\n";
|
||||
for (auto& j: i.second.transitions_) {
|
||||
out << i.first << "." << j.second << "[label=" << j.first << "]\n";
|
||||
}
|
||||
if (i.second->IsStart() && i.second->IsFinal()) {
|
||||
if (i.second.IsStart() && i.second.IsFinal()) {
|
||||
out << " " << i.first << " [shape=star];\n";
|
||||
} else if (i.second->IsStart()) {
|
||||
} else if (i.second.IsStart()) {
|
||||
out << " " << i.first << " [shape=rarrow];\n";
|
||||
} else if (i.second->IsFinal()) {
|
||||
} else if (i.second.IsFinal()) {
|
||||
out << " " << i.first << " [shape=Msquare];\n";
|
||||
}
|
||||
}
|
||||
|
||||
out << "}\n";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,11 +26,11 @@ const std::map<char, size_t>& Vertex::GetBackTransitions() const {
|
|||
|
||||
void Vertex::AddEdge(char symbol, size_t number) {
|
||||
transitions_[symbol] = number;
|
||||
owner_->GetVertex(number)->back_transitions_[symbol] = number_;
|
||||
owner_->GetVertex(number).back_transitions_[symbol] = number_;
|
||||
}
|
||||
|
||||
void Vertex::RemoveEdge(char symbol) {
|
||||
owner_->GetVertex(transitions_[symbol])->back_transitions_.erase(symbol);
|
||||
owner_->GetVertex(transitions_[symbol]).back_transitions_.erase(symbol);
|
||||
transitions_.erase(symbol);
|
||||
}
|
||||
|
||||
|
|
|
@ -11,9 +11,9 @@ NFAGraph::NFAGraph(NFAGraph&& another) {
|
|||
std::swap(start_vertexes_, another.start_vertexes_);
|
||||
|
||||
for (auto& i: vertexes_)
|
||||
i.second->owner_ = this;
|
||||
i.second.owner_ = this;
|
||||
for (auto& i: another.vertexes_)
|
||||
i.second->owner_ = &another;
|
||||
i.second.owner_ = &another;
|
||||
}
|
||||
|
||||
NFAGraph& NFAGraph::operator=(NFAGraph&& another) {
|
||||
|
@ -23,28 +23,28 @@ NFAGraph& NFAGraph::operator=(NFAGraph&& another) {
|
|||
std::swap(start_vertexes_, another.start_vertexes_);
|
||||
|
||||
for (auto& i: vertexes_)
|
||||
i.second->owner_ = this;
|
||||
i.second.owner_ = this;
|
||||
for (auto& i: another.vertexes_)
|
||||
i.second->owner_ = &another;
|
||||
i.second.owner_ = &another;
|
||||
return *this;
|
||||
}
|
||||
|
||||
size_t NFAGraph::AddNewVertex() {
|
||||
vertexes_[count_vertexes_] = std::make_shared<Vertex>(this);
|
||||
vertexes_[count_vertexes_]->number_ = count_vertexes_;
|
||||
vertexes_.emplace(count_vertexes_, this);
|
||||
GetVertex(count_vertexes_).number_ = count_vertexes_;
|
||||
return count_vertexes_++;
|
||||
}
|
||||
|
||||
void NFAGraph::AddFinalVertex(size_t number) {
|
||||
if (!vertexes_[number]->is_final_) {
|
||||
vertexes_[number]->is_final_ = true;
|
||||
if (!GetVertex(number).is_final_) {
|
||||
GetVertex(number).is_final_ = true;
|
||||
final_vertexes_.push_back(number);
|
||||
}
|
||||
}
|
||||
|
||||
void NFAGraph::AddStartVertex(size_t number) {
|
||||
if (!vertexes_[number]->is_start_) {
|
||||
vertexes_[number]->is_start_ = true;
|
||||
if (!GetVertex(number).is_start_) {
|
||||
GetVertex(number).is_start_ = true;
|
||||
start_vertexes_.push_back(number);
|
||||
}
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ void NFAGraph::RemoveVertex(size_t number) {
|
|||
void NFAGraph::RemoveFinalVertex(size_t number) {
|
||||
for (size_t i = 0; i < final_vertexes_.size(); ++i) {
|
||||
if (final_vertexes_[i] == number) {
|
||||
vertexes_[number]->is_final_ = false;
|
||||
GetVertex(number).is_final_ = false;
|
||||
std::swap(final_vertexes_[i], final_vertexes_.back());
|
||||
final_vertexes_.pop_back();
|
||||
break;
|
||||
|
@ -69,7 +69,7 @@ void NFAGraph::RemoveFinalVertex(size_t number) {
|
|||
void NFAGraph::RemoveStartVertex(size_t number) {
|
||||
for (size_t i = 0; i < start_vertexes_.size(); ++i) {
|
||||
if (start_vertexes_[i] == number) {
|
||||
vertexes_[number]->is_start_ = false;
|
||||
GetVertex(number).is_start_ = false;
|
||||
std::swap(start_vertexes_[i], start_vertexes_.back());
|
||||
start_vertexes_.pop_back();
|
||||
break;
|
||||
|
@ -87,24 +87,24 @@ void NFAGraph::Composition(NFAGraph&& nfa_graph,
|
|||
|
||||
for (auto& i: nfa_graph.vertexes_) {
|
||||
new_count_vertexes = std::max(new_count_vertexes, i.first + count_vertexes_ + 1);
|
||||
i.second->number_ += count_vertexes_;
|
||||
i.second.number_ += count_vertexes_;
|
||||
std::map<char, std::set<size_t>> new_transitions;
|
||||
for (auto& j: i.second->transitions_) {
|
||||
for (auto& j: i.second.transitions_) {
|
||||
for (auto& k: j.second) {
|
||||
new_transitions[j.first].insert(k + count_vertexes_);
|
||||
}
|
||||
}
|
||||
std::map<char, std::set<size_t>> new_back_transitions;
|
||||
for (auto& j: i.second->back_transitions_) {
|
||||
for (auto& j: i.second.back_transitions_) {
|
||||
for (auto& k: j.second) {
|
||||
new_back_transitions[j.first].insert(k + count_vertexes_);
|
||||
}
|
||||
}
|
||||
i.second->transitions_ = std::move(new_transitions);
|
||||
i.second->back_transitions_ = std::move(new_back_transitions);
|
||||
i.second.transitions_ = std::move(new_transitions);
|
||||
i.second.back_transitions_ = std::move(new_back_transitions);
|
||||
|
||||
i.second->SetOwner(this);
|
||||
vertexes_[i.second->number_] = std::move(i.second);
|
||||
i.second.SetOwner(this);
|
||||
vertexes_.emplace(i.second.number_, std::move(i.second));
|
||||
}
|
||||
|
||||
for (auto& i: add_start_vertexes) {
|
||||
|
@ -118,28 +118,32 @@ void NFAGraph::Composition(NFAGraph&& nfa_graph,
|
|||
|
||||
for (auto& i: start_vertexes) {
|
||||
for (auto& j: add_start_vertexes) {
|
||||
vertexes_[i]->transitions_[' '].insert(j);
|
||||
vertexes_[j]->back_transitions_[' '].insert(i);
|
||||
GetVertex(i).transitions_[' '].insert(j);
|
||||
GetVertex(j).back_transitions_[' '].insert(i);
|
||||
}
|
||||
}
|
||||
for (auto& i: add_final_vertexes) {
|
||||
for (auto& j: final_vertexes) {
|
||||
vertexes_[i]->transitions_[' '].insert(j);
|
||||
vertexes_[j]->back_transitions_[' '].insert(i);
|
||||
GetVertex(i).transitions_[' '].insert(j);
|
||||
GetVertex(j).back_transitions_[' '].insert(i);
|
||||
}
|
||||
}
|
||||
for (auto& i: add_final_vertexes) {
|
||||
vertexes_[i]->is_final_ = false;
|
||||
GetVertex(i).is_final_ = false;
|
||||
}
|
||||
for (auto& i: add_start_vertexes) {
|
||||
vertexes_[i]->is_start_ = false;
|
||||
GetVertex(i).is_start_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<NFAGraph::Vertex> NFAGraph::GetVertex(size_t number) {
|
||||
if (vertexes_.count(number))
|
||||
return vertexes_[number];
|
||||
return nullptr;
|
||||
NFAGraph::Vertex& NFAGraph::GetVertex(size_t number) {
|
||||
if (!NotExistVertex(number))
|
||||
return vertexes_.at(number);
|
||||
throw std::out_of_range("This vertex don't exist");
|
||||
}
|
||||
|
||||
bool NFAGraph::NotExistVertex(size_t number) {
|
||||
return !vertexes_.count(number);
|
||||
}
|
||||
|
||||
size_t NFAGraph::GetCountVertexes() const {
|
||||
|
@ -156,13 +160,13 @@ const std::vector<size_t>& NFAGraph::GetStartVertexes() const {
|
|||
|
||||
void NFAGraph::Print() const {
|
||||
for (auto i: vertexes_) {
|
||||
std::cout << i.second->number_ << " " << "f-" << i.second->is_final_ << " s-" <<
|
||||
i.second->is_start_ << std::endl;
|
||||
std::cout << i.second.number_ << " " << "f-" << i.second.is_final_ << " s-" <<
|
||||
i.second.is_start_ << std::endl;
|
||||
}
|
||||
for (auto& i: vertexes_) {
|
||||
for (auto& j: i.second->transitions_) {
|
||||
for (auto& j: i.second.transitions_) {
|
||||
for (auto k: j.second) {
|
||||
std::cout << i.second->number_ << "->" << k << " <" << j.first << ">" <<
|
||||
std::cout << i.second.number_ << "." << k << " <" << j.first << ">" <<
|
||||
std::endl;
|
||||
}
|
||||
}
|
||||
|
@ -175,16 +179,16 @@ void NFAGraph::CreateDotFile(const std::string& filename) const {
|
|||
out << "digraph G {\n";
|
||||
|
||||
for (auto& i: vertexes_) {
|
||||
for (auto& j: i.second->transitions_) {
|
||||
for (auto& j: i.second.transitions_) {
|
||||
for (auto& k: j.second) {
|
||||
out << i.first << "->" << k << "[label=" << j.first << "]\n";
|
||||
out << i.first << "." << k << "[label=" << j.first << "]\n";
|
||||
}
|
||||
}
|
||||
if (i.second->IsStart() && i.second->IsFinal()) {
|
||||
if (i.second.IsStart() && i.second.IsFinal()) {
|
||||
out << " " << i.first << " [shape=Mstar];\n";
|
||||
} else if (i.second->IsStart()) {
|
||||
} else if (i.second.IsStart()) {
|
||||
out << " " << i.first << " [shape=Mdiamond];\n";
|
||||
} else if (i.second->IsFinal()) {
|
||||
} else if (i.second.IsFinal()) {
|
||||
out << " " << i.first << " [shape=Msquare];\n";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,17 +26,17 @@ const std::map<char, std::set<size_t>>& Vertex::GetBackTransitions() const {
|
|||
|
||||
void Vertex::AddEdge(char symbol, size_t number) {
|
||||
transitions_[symbol].insert(number);
|
||||
owner_->GetVertex(number)->back_transitions_[symbol];
|
||||
owner_->GetVertex(number)->back_transitions_[symbol].insert(number_);
|
||||
owner_->GetVertex(number).back_transitions_[symbol];
|
||||
owner_->GetVertex(number).back_transitions_[symbol].insert(number_);
|
||||
}
|
||||
|
||||
void Vertex::RemoveEdge(char symbol, size_t number) {
|
||||
transitions_[symbol].erase(number);
|
||||
if (transitions_[symbol].size() == 0)
|
||||
transitions_.erase(symbol);
|
||||
owner_->GetVertex(number)->back_transitions_[symbol].erase(number_);
|
||||
if (owner_->GetVertex(number)->back_transitions_[symbol].size() == 0)
|
||||
owner_->GetVertex(number)->back_transitions_.erase(symbol);
|
||||
owner_->GetVertex(number).back_transitions_[symbol].erase(number_);
|
||||
if (owner_->GetVertex(number).back_transitions_[symbol].size() == 0)
|
||||
owner_->GetVertex(number).back_transitions_.erase(symbol);
|
||||
}
|
||||
|
||||
void Vertex::SetOwner(NFAGraph* owner) {
|
||||
|
|
|
@ -6,38 +6,38 @@ DFAGraph DFAGraphToFDFAGraph(DFAGraph&& graph, const std::vector<char>& alphabet
|
|||
const int n = graph.GetCountVertexes();
|
||||
std::map<size_t, size_t> number_vertex_in_DFA;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!graph.GetVertex(i)) continue;
|
||||
if (graph.NotExistVertex(i)) continue;
|
||||
number_vertex_in_DFA[i] = result.AddNewVertex();
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!graph.GetVertex(i)) continue;
|
||||
if (graph.GetVertex(i)->IsFinal())
|
||||
result.GetVertex(number_vertex_in_DFA[i])->SetFinal(true);
|
||||
if (graph.GetVertex(i)->IsStart())
|
||||
result.GetVertex(number_vertex_in_DFA[i])->SetStart(true);
|
||||
if (graph.NotExistVertex(i)) continue;
|
||||
if (graph.GetVertex(i).IsFinal())
|
||||
result.GetVertex(number_vertex_in_DFA[i]).SetFinal(true);
|
||||
if (graph.GetVertex(i).IsStart())
|
||||
result.GetVertex(number_vertex_in_DFA[i]).SetStart(true);
|
||||
|
||||
const auto& transitions = graph.GetVertex(i)->GetTransitions();
|
||||
const auto& transitions = graph.GetVertex(i).GetTransitions();
|
||||
for (const auto& t: transitions) {
|
||||
result.GetVertex(number_vertex_in_DFA[i])->AddEdge(t.first, t.second);
|
||||
result.GetVertex(number_vertex_in_DFA[i]).AddEdge(t.first, t.second);
|
||||
}
|
||||
}
|
||||
|
||||
size_t drain = result.AddNewVertex();
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
const auto& transitions = graph.GetVertex(i)->GetTransitions();
|
||||
const auto& transitions = graph.GetVertex(i).GetTransitions();
|
||||
for (char j: alphabet) {
|
||||
if (!transitions.count(j)) {
|
||||
result.GetVertex(i)->AddEdge(j, drain);
|
||||
result.GetVertex(i).AddEdge(j, drain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result.GetVertex(drain)->GetBackTransitions().size() == 0) {
|
||||
if (result.GetVertex(drain).GetBackTransitions().size() == 0) {
|
||||
result.RemoveVertex(drain);
|
||||
} else {
|
||||
for (char j: alphabet) {
|
||||
result.GetVertex(drain)->AddEdge(j, drain);
|
||||
result.GetVertex(drain).AddEdge(j, drain);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,8 +11,8 @@ DFAGraph DFAGraphToMinDFAGraph(DFAGraph&& graph) {
|
|||
{
|
||||
std::set<char> set_alphabet;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!graph.GetVertex(i)) continue;
|
||||
for (auto i: graph.GetVertex(i)->GetTransitions()) {
|
||||
if (graph.NotExistVertex(i)) continue;
|
||||
for (auto i: graph.GetVertex(i).GetTransitions()) {
|
||||
set_alphabet.insert(i.first);
|
||||
}
|
||||
}
|
||||
|
@ -25,11 +25,11 @@ DFAGraph DFAGraphToMinDFAGraph(DFAGraph&& graph) {
|
|||
{
|
||||
std::vector<std::vector<size_t>>& layer(table[0]);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
layer[i][0] = graph.GetVertex(i)->IsFinal();
|
||||
layer[i][0] = graph.GetVertex(i).IsFinal();
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < alphabet.size(); ++j) {
|
||||
const auto& transitions = graph.GetVertex(i)->GetTransitions();
|
||||
const auto& transitions = graph.GetVertex(i).GetTransitions();
|
||||
if (transitions.count(alphabet[j]))
|
||||
layer[i][j + 1] = layer[transitions.at(alphabet[j])][0];
|
||||
else
|
||||
|
@ -57,7 +57,7 @@ DFAGraph DFAGraphToMinDFAGraph(DFAGraph&& graph) {
|
|||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < alphabet.size(); ++j) {
|
||||
const auto& transitions = graph.GetVertex(i)->GetTransitions();
|
||||
const auto& transitions = graph.GetVertex(i).GetTransitions();
|
||||
if (transitions.count(alphabet[j]))
|
||||
layer[i][j + 1] = layer[transitions.at(alphabet[j])][0];
|
||||
else
|
||||
|
@ -92,16 +92,16 @@ DFAGraph DFAGraphToMinDFAGraph(DFAGraph&& graph) {
|
|||
for (int i = 0; i < n; ++i) {
|
||||
size_t v = number_vertex_in_DFA[last_layer[i][0]];
|
||||
|
||||
const auto& transitions = graph.GetVertex(i)->GetTransitions();
|
||||
const auto& transitions = graph.GetVertex(i).GetTransitions();
|
||||
for (const auto& t: transitions) {
|
||||
if (last_layer[t.second][0] != -1LL) {
|
||||
size_t u = number_vertex_in_DFA[last_layer[t.second][0]];
|
||||
result.GetVertex(v)->AddEdge(t.first, u);
|
||||
result.GetVertex(v).AddEdge(t.first, u);
|
||||
}
|
||||
}
|
||||
|
||||
if (graph.GetVertex(i)->IsStart()) result.GetVertex(v)->SetStart(true);
|
||||
if (graph.GetVertex(i)->IsFinal()) result.GetVertex(v)->SetFinal(true);
|
||||
if (graph.GetVertex(i).IsStart()) result.GetVertex(v).SetStart(true);
|
||||
if (graph.GetVertex(i).IsFinal()) result.GetVertex(v).SetFinal(true);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -12,8 +12,8 @@ std::string DFAGraphToRegular(DFAGraph&& graph) {
|
|||
size_t start = -1;
|
||||
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
if (!graph.GetVertex(i)) continue;
|
||||
if (graph.GetVertex(i)->IsStart()) start = i;
|
||||
if (graph.NotExistVertex(i)) continue;
|
||||
if (graph.GetVertex(i).IsStart()) start = i;
|
||||
}
|
||||
|
||||
rename[start] = 1;
|
||||
|
@ -26,13 +26,13 @@ std::string DFAGraphToRegular(DFAGraph&& graph) {
|
|||
|
||||
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
if (!graph.GetVertex(i)) continue;
|
||||
const auto& vertex_transitions = graph.GetVertex(i)->GetTransitions();
|
||||
if (graph.NotExistVertex(i)) continue;
|
||||
const auto& vertex_transitions = graph.GetVertex(i).GetTransitions();
|
||||
|
||||
for (auto j: vertex_transitions) {
|
||||
transitions[rename[i]][rename[j.second]].insert(std::string(1, j.first));
|
||||
}
|
||||
if (graph.GetVertex(i)->IsFinal()) {
|
||||
if (graph.GetVertex(i).IsFinal()) {
|
||||
transitions[rename[i]][0].insert("( )");
|
||||
}
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ std::string DFAGraphToRegular(DFAGraph&& graph) {
|
|||
std::string begin_to_end;
|
||||
|
||||
if (transitions[1][1].size() != 0) {
|
||||
for (const std::string s: transitions[1][1]) {
|
||||
for (const std::string& s: transitions[1][1]) {
|
||||
loop += "(";
|
||||
loop += s;
|
||||
loop += ")";
|
||||
|
@ -109,7 +109,7 @@ std::string DFAGraphToRegular(DFAGraph&& graph) {
|
|||
loop.pop_back();
|
||||
loop = "(" + loop + ")*";
|
||||
}
|
||||
for (const std::string s: transitions[1][0]) {
|
||||
for (const std::string& s: transitions[1][0]) {
|
||||
begin_to_end += "(";
|
||||
begin_to_end += s;
|
||||
begin_to_end += ")";
|
||||
|
|
|
@ -4,8 +4,8 @@ namespace converters {
|
|||
DFAGraph InvertFDFAGraph(DFAGraph&& fdfa_graph) {
|
||||
const size_t n = fdfa_graph.GetCountVertexes();
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!fdfa_graph.GetVertex(i)) continue;
|
||||
fdfa_graph.GetVertex(i)->SetFinal(!fdfa_graph.GetVertex(i)->IsFinal());
|
||||
if (fdfa_graph.NotExistVertex(i)) continue;
|
||||
fdfa_graph.GetVertex(i).SetFinal(fdfa_graph.GetVertex(i).IsFinal());
|
||||
}
|
||||
return std::move(fdfa_graph);
|
||||
}
|
||||
|
|
|
@ -10,13 +10,13 @@ NFAGraph AddAllEpsilonTransitions(NFAGraph&& nfa_graph) {
|
|||
|
||||
std::function<void(int, int)> dfs = [&nfa_graph, &used, &dfs](int u, int v) -> void {
|
||||
used[v] = true;
|
||||
const auto& transitions = nfa_graph.GetVertex(v)->GetTransitions();
|
||||
const auto& transitions = nfa_graph.GetVertex(v).GetTransitions();
|
||||
if (transitions.count(' ')) {
|
||||
const auto& s = transitions.at(' ');
|
||||
for (auto i: s) {
|
||||
if (!used[i]) {
|
||||
if (u != i)
|
||||
nfa_graph.GetVertex(u)->AddEdge(' ', i);
|
||||
nfa_graph.GetVertex(u).AddEdge(' ', i);
|
||||
dfs(u, i);
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ NFAGraph AddAllEpsilonTransitions(NFAGraph&& nfa_graph) {
|
|||
};
|
||||
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!nfa_graph.GetVertex(i)) continue;
|
||||
if (nfa_graph.NotExistVertex(i)) continue;
|
||||
|
||||
used.assign(n, false);
|
||||
dfs(i, i);
|
||||
|
@ -36,15 +36,15 @@ NFAGraph AddAllPossibleStartFinal(NFAGraph&& nfa_graph) {
|
|||
if (nfa_graph.GetStartVertexes().size() != 1) {
|
||||
size_t start_vertex = nfa_graph.AddNewVertex();
|
||||
for (auto v: nfa_graph.GetStartVertexes()) {
|
||||
nfa_graph.GetVertex(start_vertex)->AddEdge(' ', v);
|
||||
nfa_graph.GetVertex(start_vertex).AddEdge(' ', v);
|
||||
}
|
||||
}
|
||||
for (const auto& v: nfa_graph.GetFinalVertexes()) {
|
||||
const auto& transitions = nfa_graph.GetVertex(v)->GetBackTransitions();
|
||||
const auto& transitions = nfa_graph.GetVertex(v).GetBackTransitions();
|
||||
if (transitions.count(' ')) {
|
||||
const auto& s = transitions.at(' ');
|
||||
for (auto i: s) {
|
||||
nfa_graph.GetVertex(i)->SetFinal(true);
|
||||
nfa_graph.GetVertex(i).SetFinal(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -55,21 +55,21 @@ NFAGraph DeleteEpsilonTransitions(NFAGraph&& nfa_graph) {
|
|||
const int n = nfa_graph.GetCountVertexes();
|
||||
|
||||
for (int v = 0; v < n; ++v) {
|
||||
if (!nfa_graph.GetVertex(v)) continue;
|
||||
if (nfa_graph.NotExistVertex(v)) continue;
|
||||
|
||||
const auto& transitions = nfa_graph.GetVertex(v)->GetTransitions();
|
||||
const auto& transitions = nfa_graph.GetVertex(v).GetTransitions();
|
||||
if (transitions.count(' ')) {
|
||||
auto s = transitions.at(' ');
|
||||
for (auto u: s) {
|
||||
for (auto& i: nfa_graph.GetVertex(u)->GetTransitions()) {
|
||||
for (auto& i: nfa_graph.GetVertex(u).GetTransitions()) {
|
||||
if (i.first == ' ') continue;
|
||||
for (auto t: i.second) {
|
||||
nfa_graph.GetVertex(v)->AddEdge(i.first, t);
|
||||
nfa_graph.GetVertex(v).AddEdge(i.first, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto u: s) {
|
||||
nfa_graph.GetVertex(v)->RemoveEdge(' ', u);
|
||||
nfa_graph.GetVertex(v).RemoveEdge(' ', u);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,8 +87,8 @@ NFAGraph DeleteTransitionsByOneLetter(NFAGraph&& nfa_graph) {
|
|||
|
||||
std::set<char> alphabet;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!nfa_graph.GetVertex(i)) continue;
|
||||
for (const auto& j: nfa_graph.GetVertex(i)->GetTransitions()) {
|
||||
if (nfa_graph.NotExistVertex(i)) continue;
|
||||
for (const auto& j: nfa_graph.GetVertex(i).GetTransitions()) {
|
||||
if (j.second.size() > 0)
|
||||
alphabet.insert(j.first);
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ NFAGraph DeleteTransitionsByOneLetter(NFAGraph&& nfa_graph) {
|
|||
for (auto symbol: alphabet) {
|
||||
std::set<size_t> result;
|
||||
for (auto v: current) {
|
||||
const auto& transitions = nfa_graph.GetVertex(v)->GetTransitions();
|
||||
const auto& transitions = nfa_graph.GetVertex(v).GetTransitions();
|
||||
if (transitions.count(symbol)) {
|
||||
const auto& s = transitions.at(symbol);
|
||||
result.insert(s.begin(), s.end());
|
||||
|
@ -127,7 +127,7 @@ NFAGraph DeleteTransitionsByOneLetter(NFAGraph&& nfa_graph) {
|
|||
int v = number_vertex_in_result_tree[i.first.first];
|
||||
int u = number_vertex_in_result_tree[i.second];
|
||||
char symbol = i.first.second;
|
||||
result_tree.GetVertex(v)->AddEdge(symbol, u);
|
||||
result_tree.GetVertex(v).AddEdge(symbol, u);
|
||||
}
|
||||
|
||||
for (auto i: number_vertex_in_result_tree) {
|
||||
|
@ -135,10 +135,10 @@ NFAGraph DeleteTransitionsByOneLetter(NFAGraph&& nfa_graph) {
|
|||
auto v = i.second;
|
||||
|
||||
for (auto i: s) {
|
||||
if (nfa_graph.GetVertex(i)->IsFinal())
|
||||
result_tree.GetVertex(v)->SetFinal(true);
|
||||
if (nfa_graph.GetVertex(i)->IsStart())
|
||||
result_tree.GetVertex(v)->SetStart(true);
|
||||
if (nfa_graph.GetVertex(i).IsFinal())
|
||||
result_tree.GetVertex(v).SetFinal(true);
|
||||
if (nfa_graph.GetVertex(i).IsStart())
|
||||
result_tree.GetVertex(v).SetStart(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -146,6 +146,7 @@ NFAGraph DeleteTransitionsByOneLetter(NFAGraph&& nfa_graph) {
|
|||
}
|
||||
|
||||
DFAGraph NFAGraphToDFAGraph(NFAGraph&& nfa_graph) {
|
||||
nfa_graph = AddAllEpsilonTransitions(std::move(nfa_graph));
|
||||
nfa_graph = DeleteTransitionsByOneLetter(DeleteEpsilonTransitions(AddAllPossibleStartFinal(
|
||||
AddAllEpsilonTransitions(std::move(nfa_graph)))));
|
||||
|
||||
|
@ -153,20 +154,20 @@ DFAGraph NFAGraphToDFAGraph(NFAGraph&& nfa_graph) {
|
|||
DFAGraph result;
|
||||
std::map<int, int> number_vertex_in_DFA;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!nfa_graph.GetVertex(i)) continue;
|
||||
if (nfa_graph.NotExistVertex(i)) continue;
|
||||
number_vertex_in_DFA[i] = result.AddNewVertex();
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!nfa_graph.GetVertex(i)) continue;
|
||||
if (nfa_graph.GetVertex(i)->IsFinal())
|
||||
result.GetVertex(number_vertex_in_DFA[i])->SetFinal(true);
|
||||
if (nfa_graph.GetVertex(i)->IsStart())
|
||||
result.GetVertex(number_vertex_in_DFA[i])->SetStart(true);
|
||||
if (nfa_graph.NotExistVertex(i)) continue;
|
||||
if (nfa_graph.GetVertex(i).IsFinal())
|
||||
result.GetVertex(number_vertex_in_DFA[i]).SetFinal(true);
|
||||
if (nfa_graph.GetVertex(i).IsStart())
|
||||
result.GetVertex(number_vertex_in_DFA[i]).SetStart(true);
|
||||
|
||||
const auto& transitions = nfa_graph.GetVertex(i)->GetTransitions();
|
||||
const auto& transitions = nfa_graph.GetVertex(i).GetTransitions();
|
||||
for (const auto& t: transitions) {
|
||||
if (t.second.size() != 1) throw std::logic_error("");
|
||||
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());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
|
|
@ -20,14 +20,14 @@ NFAGraph RegularToNFA(std::unique_ptr<Node, deleter> node) {
|
|||
result.AddStartVertex(end);
|
||||
for (char i: node->word) {
|
||||
auto tmp = result.AddNewVertex();
|
||||
result.GetVertex(end)->AddEdge(i, tmp);
|
||||
result.GetVertex(end).AddEdge(i, tmp);
|
||||
end = tmp;
|
||||
}
|
||||
if (node->modifier == Node::Modifier::Plus) {
|
||||
result.GetVertex(end)->AddEdge(' ', start);
|
||||
result.GetVertex(end).AddEdge(' ', start);
|
||||
} else if (node->modifier == Node::Modifier::Star) {
|
||||
result.GetVertex(end)->AddEdge(' ', start);
|
||||
result.GetVertex(start)->AddEdge(' ', end);
|
||||
result.GetVertex(end).AddEdge(' ', start);
|
||||
result.GetVertex(start).AddEdge(' ', end);
|
||||
}
|
||||
result.AddFinalVertex(end);
|
||||
} else if (node->type == Node::Type::Concatenation) {
|
||||
|
@ -47,14 +47,14 @@ NFAGraph RegularToNFA(std::unique_ptr<Node, deleter> node) {
|
|||
if (node->modifier == Node::Modifier::Plus) {
|
||||
for (auto start: start_vertexes) {
|
||||
for (auto end: end_vertexes) {
|
||||
result.GetVertex(end)->AddEdge(' ', start);
|
||||
result.GetVertex(end).AddEdge(' ', start);
|
||||
}
|
||||
}
|
||||
} else if (node->modifier == Node::Modifier::Star) {
|
||||
for (auto start: start_vertexes) {
|
||||
for (auto end: end_vertexes) {
|
||||
result.GetVertex(end)->AddEdge(' ', start);
|
||||
result.GetVertex(start)->AddEdge(' ', end);
|
||||
result.GetVertex(end).AddEdge(' ', start);
|
||||
result.GetVertex(start).AddEdge(' ', end);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -69,10 +69,10 @@ NFAGraph RegularToNFA(std::unique_ptr<Node, deleter> node) {
|
|||
result.Composition(std::move(tmp), {start}, {end});
|
||||
}
|
||||
if (node->modifier == Node::Modifier::Plus) {
|
||||
result.GetVertex(end)->AddEdge(' ', start);
|
||||
result.GetVertex(end).AddEdge(' ', start);
|
||||
} else if (node->modifier == Node::Modifier::Star) {
|
||||
result.GetVertex(end)->AddEdge(' ', start);
|
||||
result.GetVertex(start)->AddEdge(' ', end);
|
||||
result.GetVertex(end).AddEdge(' ', start);
|
||||
result.GetVertex(start).AddEdge(' ', end);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
|
|
@ -36,7 +36,7 @@ void example2() {
|
|||
a[i] = nfa_graph.AddNewVertex();
|
||||
|
||||
auto AddEdge = [&nfa_graph, &a](char symbol, int u, int v) {
|
||||
nfa_graph.GetVertex(a[u])->AddEdge(symbol, a[v]);
|
||||
nfa_graph.GetVertex(a[u]).AddEdge(symbol, a[v]);
|
||||
};
|
||||
|
||||
AddEdge('a', 0, 1);
|
||||
|
|
Loading…
Reference in a new issue