From 03b5a87eeb87fb059c9d51696912bfa9c39c5929 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 31 Mar 2015 20:25:04 +0200 Subject: generator: generate item sets recursively --- src/generator.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/generator.hpp | 12 ++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/generator.cpp b/src/generator.cpp index f8f4f1d..7f5f5e9 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -62,6 +62,45 @@ void generator_t::close_set(std::set *set) { } } +void generator_t::generate_itemsets() { + std::queue> queue; + + { + std::set set0 = get_set(""); + close_set(&set0); + + add_set(set0); + queue.push(set0); + } + + for (; !queue.empty(); queue.pop()) { + const std::set &cur = queue.front(); + + const std::set empty_set; + std::map> new_sets; + + for (const item_t &item : cur) { + if (!item.has_next()) + continue; + + const symbol_t &sym = item.get_next_symbol(); + + item_t shifted = item; + shifted.shift(); + + std::set &set = new_sets.emplace(sym, empty_set).first->second; + set.insert(std::move(shifted)); + } + + for (auto &entry : new_sets) { + close_set(&entry.second); + + if (add_set(entry.second).first) + queue.push(entry.second); + } + } +} + void generator_t::print_item(const item_t &item) { printf("%s |=", item.get_lhs().c_str()); @@ -100,10 +139,17 @@ generator_t::generator_t(const std::set &rules0) { } } - std::set set0 = get_set(""); - close_set(&set0); + generate_itemsets(); - print_set(set0); + const std::set *setlist[itemsets.size()]; + for (const auto &entry : itemsets) + setlist[entry.second] = &entry.first; + + for (unsigned i = 0; i < itemsets.size(); i++) { + printf("Item %u:\n", i); + print_set(*setlist[i]); + printf("\n"); + } } } diff --git a/src/generator.hpp b/src/generator.hpp index d9bcaf6..880323b 100644 --- a/src/generator.hpp +++ b/src/generator.hpp @@ -38,12 +38,20 @@ class generator_t { private: std::multimap rules; std::multimap items; + std::map, unsigned> itemsets; void close_set(std::set *set); std::set get_set(const std::string &nonterm); - void print_item(const item_t &item); - void print_set(const std::set &set); + std::pair add_set(const std::set &set) { + auto ret = itemsets.emplace(set, itemsets.size()); + return std::make_pair(ret.second, ret.first->second); + } + + void generate_itemsets(); + + static void print_item(const item_t &item); + static void print_set(const std::set &set); public: generator_t(const std::set &rules0); -- cgit v1.2.3