From 79fbc8bf495770e4a8b7c66c46acf07f4e47e568 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 30 Mar 2020 18:33:01 +0200 Subject: Speed up compile --- bnf.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 6 deletions(-) (limited to 'bnf.cpp') diff --git a/bnf.cpp b/bnf.cpp index 32e1175..04a6be9 100644 --- a/bnf.cpp +++ b/bnf.cpp @@ -1,8 +1,8 @@ #include "bnf.h" -std::unordered_map> Reverse(BNF bnf) +std::unordered_map> Reverse(const BNF& bnf) { - std::unordered_map> result; + std::unordered_map> result; for (const auto& [from, to] : bnf) { for (const auto& list : to) { @@ -11,7 +11,7 @@ std::unordered_map> Reverse(BNF bnf) if (i != result.end()) // already present i->second.insert(from); else // new element - result.emplace(element, std::set{from}); + result.emplace(element, std::unordered_set{from}); } } } @@ -19,9 +19,9 @@ std::unordered_map> Reverse(BNF bnf) return result; } -std::unordered_map> reverseFirst(BNF bnf) +std::unordered_map> reverseFirst(const BNF& bnf) { - std::unordered_map> result; + std::unordered_map> result; for (const auto& [from, to] : bnf) { for (const auto& list : to) { @@ -31,7 +31,7 @@ std::unordered_map> reverseFirst(BNF bnf) if (i != result.end()) // already present i->second.insert(from); else // new element - result.emplace(element, std::set{from}); + result.emplace(element, std::unordered_set{from}); } } } @@ -73,3 +73,55 @@ bool isTerminal(const BNF& bnf, const std::string& symbol) return bnf.find(symbol) == bnf.end(); } +std::unordered_set getTerminals(const BNF& bnf) +{ + std::unordered_set result; + + for (const auto& [from, to] : bnf) { + for (const auto& list : to) { + for (const auto& element : list) { + if (isTerminal(bnf, element)) + result.insert(element); + } + } + } + + return result; +} + +std::unordered_set, PairHash> getEmptyPositions(const BNF& bnf) +{ + std::unordered_set, PairHash> result; + + for (const auto& [from, to] : bnf) { + for (size_t i = 0; i < to.size(); i++) { + const auto& list{to[i]}; + if (list.size() == 0) + result.insert({from, i}); + } + } + + return result; +} + +std::unordered_map, PairHash>> reversePosFirst(const BNF& bnf) +{ + std::unordered_map, PairHash>> result; + + for (const auto& [from, to] : bnf) { + for (size_t i = 0; i < to.size(); i++) { + const auto& list{to[i]}; + if (list.size() > 0) { + const auto& element{list[0]}; + auto it{result.find(element)}; + if (it != result.end()) // already present + it->second.insert({from, i}); + else // new element + result.emplace(element, std::unordered_set, PairHash>{{from, i}}); + } + } + } + + return result; +} + -- cgit v1.2.3