diff --git a/aarbology2/src/aarbology.cpp b/aarbology2/src/aarbology.cpp index e307d08558f7be884deb34a9e1155dc128428418..a47a9992539d86dbde7e94093cf5849029fc7e4e 100644 --- a/aarbology2/src/aarbology.cpp +++ b/aarbology2/src/aarbology.cpp @@ -27,6 +27,7 @@ #include <arbology/exact/ExactPatternMatchingAutomaton.h> #include <arbology/exact/ExactSubtreeAutomaton.h> #include <arbology/exact/ExactTreePatternAutomaton.h> +#include <arbology/exact/ExactSubtreeRepeatsNaive.h> #include <arbology/transform/BeginToEndIndex.h> int main ( int argc, char * argv[] ) { @@ -44,6 +45,7 @@ int main ( int argc, char * argv[] ) { allowed.push_back ( "exactPatternMatchingAutomaton" ); allowed.push_back ( "exactSubtreeAutomaton" ); allowed.push_back ( "exactTreePatternAutomaton" ); + allowed.push_back ( "exactSubtreeRepeatsNaive" ); TCLAP::ValuesConstraint < std::string > allowedVals ( allowed ); TCLAP::ValueArg < std::string > algorithm ( "a", "algorithm", "Execute algorithm", false, "exactSubtreeMatch", & allowedVals ); @@ -211,6 +213,18 @@ int main ( int argc, char * argv[] ) { measurements::end ( ); measurements::start ( "Output write", measurements::Type::AUXILIARY ); + alib::XmlDataFactory::toStdout ( res ); + } else if ( algorithm.getValue ( ) == "exactSubtreeRepeatsNaive" ) { + tree::Tree subject = alib::XmlDataFactory::fromTokens < tree::Tree > ( std::move ( sax::FromXMLParserHelper::parseInput(true, subjectInput).front ( ) ) ); + + measurements::end ( ); + measurements::start ( "Algorithm", measurements::Type::MAIN ); + + tree::Tree res = arbology::exact::ExactSubtreeRepeatsNaive::repeats ( subject ); + + measurements::end ( ); + measurements::start ( "Output write", measurements::Type::AUXILIARY ); + alib::XmlDataFactory::toStdout ( res ); } else { throw exception::AlibException ( "Invalid algorithm" ); diff --git a/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.cpp b/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f20ba3a504d1cbfcdcbd17343fb9a8e825f394a2 --- /dev/null +++ b/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.cpp @@ -0,0 +1,55 @@ +/* + * ExactSubtreeRepeatsNaive.cpp + * + * Created on: 1. 4. 2016 + * Author: Jan Travnicek + */ + +#include "ExactSubtreeRepeatsNaive.h" +#include "SubtreeJumpTable.h" + +#include <exception/AlibException.h> +#include <tree/ranked/RankedTree.h> +#include <tree/Tree.h> +#include <global/GlobalData.h> + +namespace arbology { + +namespace exact { + +tree::Tree ExactSubtreeRepeatsNaive::repeats ( const tree::Tree & tree ) { + return getInstance ( ).dispatch ( tree.getData ( ) ); +} + +tree::RankedNode * ExactSubtreeRepeatsNaive::repeats ( const tree::RankedNode & node, std::map < std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > >, int > & data, int & minId ) { + std::vector < tree::RankedNode * > children; + std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > > childRepeatsKey ( node.getSymbol ( ), std::vector < alphabet::RankedSymbol > ( ) ); + + for ( const tree::RankedNode * child : node.getChildren ( ) ) { + children.push_back ( repeats ( * child, data, minId ) ); + childRepeatsKey.second.push_back ( children.back ( )->getSymbol ( ) ); + } + + int & uniqueRepeatId = data[childRepeatsKey]; + + if ( uniqueRepeatId == 0 ) uniqueRepeatId = minId++; + + return new tree::RankedNode ( alphabet::RankedSymbol ( alphabet::symbolFrom ( uniqueRepeatId ), children.size ( ) ), std::move ( children ) ); +} + +tree::RankedTree ExactSubtreeRepeatsNaive::repeats ( const tree::RankedTree & tree ) { + int minId = 1; + std::map < std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > >, int > data; + + tree::RankedNode * resNode = repeats ( tree.getRoot ( ), data, minId ); + tree::RankedTree res ( * resNode ); + + delete resNode; + return res; +} + +auto ExactRepeatsSimpleRankedTree = ExactSubtreeRepeatsNaive::RegistratorWrapper < tree::RankedTree, tree::RankedTree > ( ExactSubtreeRepeatsNaive::getInstance ( ), ExactSubtreeRepeatsNaive::repeats ); + +} /* namespace exact */ + +} /* namespace arbology */ diff --git a/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.h b/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.h new file mode 100644 index 0000000000000000000000000000000000000000..6b7764c6ba319d3001f176b0326abb5124890e6d --- /dev/null +++ b/alib2algo/src/arbology/exact/ExactSubtreeRepeatsNaive.h @@ -0,0 +1,53 @@ +/* + * ExactSubtreeRepeatsNaive.h + * + * Created on: 5. 11. 2014 + * Author: Jan Travnicek + */ + +#ifndef _ARBOLOGY_SUBTREE_REPEATS_NAIVE_H_ +#define _ARBOLOGY_SUBTREE_REPEATS_NAIVE_H_ + +#include <tree/TreeFeatures.h> +#include <alphabet/SymbolFeatures.h> +#include <core/multipleDispatch.hpp> + +#include <map> +#include <vector> + +namespace arbology { + +namespace exact { + +/** + * Simple computation of subtree repeats + */ +class ExactSubtreeRepeatsNaive : public std::SingleDispatch < tree::Tree, tree::TreeBase > { + static tree::RankedNode * repeats ( const tree::RankedNode & node, std::map < std::pair < alphabet::RankedSymbol, std::vector < alphabet::RankedSymbol > >, int > & data, int & minId ); + +public: + /** + * Compute a same shaped tree with nodes containing unique subtree ids. + * @return Tree of repeats + */ + static tree::Tree repeats ( const tree::Tree & pattern ); + + /** + * Compute a same shaped tree with nodes containing unique subtree ids. + * @return Tree of repeats + */ + static tree::RankedTree repeats ( const tree::RankedTree & tree ); + + static ExactSubtreeRepeatsNaive & getInstance ( ) { + static ExactSubtreeRepeatsNaive res; + + return res; + } + +}; + +} /* namespace exact */ + +} /* namespace arbology */ + +#endif /* _ARBOLOGY_SUBTREE_REPEATS_NAIVE_H_ */