diff --git a/lib/trie.cpp b/lib/trie.cpp index 7c2b07c..a1642d5 100644 --- a/lib/trie.cpp +++ b/lib/trie.cpp @@ -1,9 +1,17 @@ #include "trie.hpp" -std::vector> flatten(const trie& t) { +std::vector> flatten(const trie & t) { std::vector> ret; - - t.for_each([&ret](auto&& w) { ret.push_back(w); }); - + t.for_each([&ret](auto && w) { ret.push_back(w); }); return ret; } + +std::pair total_size(const trie & t) { + size_t count = 0; + size_t total_count = 0; + t.for_each([&count, &total_count](auto && w) { + ++count; + total_count += w.size(); + }); + return {count, total_count}; +} diff --git a/lib/trie.hpp b/lib/trie.hpp index bdf0f03..aaba44f 100644 --- a/lib/trie.hpp +++ b/lib/trie.hpp @@ -37,7 +37,6 @@ struct trie { b = trie(); b->insert(begin, end); - count++; return true; } @@ -53,32 +52,35 @@ struct trie { /// \brief Empties the complete set void clear() { - count = 0; branches.clear(); } private: template void for_each_impl(Fun && function, std::vector & word) const { - if (count == 0) { - const auto & cword = word; - function(cword); // we don't want function to modify word - return; - } - + size_t count = 0; for (size_t i = 0; i < branches.size(); ++i) { auto const & b = branches[i]; if (b) { + ++count; word.push_back(i); b->for_each_impl(function, word); word.resize(word.size() - 1); } } + + if (count == 0) { + const auto & cword = word; + function(cword); // we don't want function to modify word + return; + } } - size_t count = 0; std::vector> branches; }; /// \brief Flattens a trie \p t /// \returns an array of words (without the prefixes) std::vector> flatten(trie const & t); + +/// \brief Returns size and total sum of symbols +std::pair total_size(trie const & t); diff --git a/src/methods.cpp b/src/methods.cpp new file mode 100644 index 0000000..0f8a5ad --- /dev/null +++ b/src/methods.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +using namespace std; + +int main(int argc, char * argv[]) { + if (argc != 2) return 1; + + const string filename = argv[1]; + const size_t k_max = 1; + + const auto machine = read_mealy_from_dot(filename).first; + + auto sequence_fut = async([&] { + const auto tree = create_splitting_tree(machine, randomized_lee_yannakakis_style); + return create_adaptive_distinguishing_sequence(tree); + }); + + auto pairs_fut = async([&] { + const auto tree = create_splitting_tree(machine, randomized_hopcroft_style); + return create_all_pair_seperating_sequences(tree.root); + }); + + auto prefixes_fut = async([&] { + return create_transfer_sequences(machine, 0); + }); + + auto middles_fut = async([&] { + vector all_sequences(1); + for (size_t k = 0; k < k_max; ++k) { + const auto new_sequences = all_seqs(0, machine.input_size, all_sequences); + all_sequences.reserve(all_sequences.size() + new_sequences.size()); + copy(begin(new_sequences), end(new_sequences), back_inserter(all_sequences)); + } + return all_sequences; + }); + + clog << "getting sequence and pairs" << endl; + auto suffixes_fut = async([&] { + return create_seperating_family(sequence_fut.get(), pairs_fut.get()); + }); + + clog << "getting prefixes, middles and suffixes" << endl; + const auto prefixes = prefixes_fut.get(); + const auto middles = middles_fut.get(); + const auto suffixes = suffixes_fut.get(); + trie test_suite; + + clog << "start testing" << endl; + const state start = 0; + const word empty = {}; + for (auto && p : prefixes) { + const state s1 = apply(machine, start, begin(p), end(p)).to; + const word w1 = concat(empty, p); + for (auto && m : middles) { + const state s2 = apply(machine, s1, begin(m), end(m)).to; + const word w2 = concat(w1, m); + const auto & suffixes_for_state = (m.size() == k_max) ? suffixes[s2].local_suffixes + : suffixes[s2].global_suffixes; + for (auto && s : suffixes_for_state) { + word test = concat(w2, s); + test_suite.insert(test); + } + } + } + + const auto p = total_size(test_suite); + cout << p.first << endl; + cout << p.second << endl; +}