From 6fcfe0a5cf8f53658e50e346076768eec229e695 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 1 Mar 2020 22:34:35 +0100 Subject: Vector invalidation fix --- bnf.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'bnf.cpp') diff --git a/bnf.cpp b/bnf.cpp index f240f29..7290962 100644 --- a/bnf.cpp +++ b/bnf.cpp @@ -19,6 +19,26 @@ std::map> Reverse(BNF bnf) return result; } +std::map> reverseFirst(BNF bnf) +{ + std::map> result; + + for (const auto& [from, to] : bnf) { + for (const auto& list : to) { + if (list.size() > 0) { + const auto& element{list[0]}; + auto i{result.find(element)}; + if (i != result.end()) // already present + i->second.insert(from); + else // new element + result.emplace(element, std::set{from}); + } + } + } + + return result; +} + BNF SubBNF(const BNF& bnf, const std::string& top) { BNF result; -- cgit v1.2.3