diff --git a/alib2algo/src/stringology/indexing/CompressedBitParallelIndexConstruction.cpp b/alib2algo/src/stringology/indexing/CompressedBitParallelIndexConstruction.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0219ca14cb131e6bdcd8bca723168f1a2e843d73 --- /dev/null +++ b/alib2algo/src/stringology/indexing/CompressedBitParallelIndexConstruction.cpp @@ -0,0 +1,24 @@ +/* + * CompressedBitParallelIndexConstruction.cpp + * + * Created on: 6. 2. 2017 + * Author: Jan Travnicek + */ + +#include "CompressedBitParallelIndexConstruction.h" + +#include <string/LinearString.h> + +namespace stringology { + +namespace indexing { + +indexes::CompressedBitParallelIndex < DefaultSymbolType > CompressedBitParallelIndexConstruction::construct ( const string::String & string ) { + return dispatch ( string.getData ( ) ); +} + +auto compressedCompressedBitParallelIndexConstructionLinearString = CompressedBitParallelIndexConstruction::RegistratorWrapper < indexes::CompressedBitParallelIndex < DefaultSymbolType >, string::LinearString < > > ( CompressedBitParallelIndexConstruction::construct ); + +} /* namespace indexing */ + +} /* namespace stringology */ diff --git a/alib2algo/src/stringology/indexing/CompressedBitParallelIndexConstruction.h b/alib2algo/src/stringology/indexing/CompressedBitParallelIndexConstruction.h new file mode 100644 index 0000000000000000000000000000000000000000..32b61d51148fb8a21c7d35d96769295078df2d5f --- /dev/null +++ b/alib2algo/src/stringology/indexing/CompressedBitParallelIndexConstruction.h @@ -0,0 +1,56 @@ +/* + * CompressedBitParallelIndex.h + * + * Created on: 6. 2. 2017 + * Author: Jan Travnicek + */ + +#ifndef COMPRESSED_BIT_PARALLEL_INDEX_CONSTRUCTION_H_ +#define COMPRESSED_BIT_PARALLEL_INDEX_CONSTRUCTION_H_ + +#include <indexes/CompressedBitParallelIndex.h> +#include <string/String.h> +#include <string/LinearString.h> +#include <core/multipleDispatch.hpp> +#include <exception/CommonException.h> + +namespace stringology { + +namespace indexing { + +/** + * Constructs a compressed bit parallel index for given string. + * + */ + +class CompressedBitParallelIndexConstruction : public std::SingleDispatch < CompressedBitParallelIndexConstruction, indexes::CompressedBitParallelIndex < DefaultSymbolType >, const string::StringBase & > { +public: + /** + * Creates suffix trie + * @param string string to construct suffix trie for + * @return automaton + */ + static indexes::CompressedBitParallelIndex < DefaultSymbolType > construct ( const string::String & string ); + + template < class SymbolType > + static indexes::CompressedBitParallelIndex < SymbolType > construct ( const string::LinearString < SymbolType > & string ); + +}; + +template < class SymbolType > +indexes::CompressedBitParallelIndex < SymbolType > CompressedBitParallelIndexConstruction::construct ( const string::LinearString < SymbolType > & w ) { + std::map < SymbolType, common::SparseBoolVector > res; + for ( const SymbolType & symbol : w.getAlphabet ( ) ) + res [ symbol ].resize ( w.getContent ( ).size ( ) ); + + for ( unsigned i = 0; i < w.getContent ( ).size ( ); ++i ) + res [ w.getContent ( ) [ i ] ] [ i ] = true; + + return indexes::CompressedBitParallelIndex < SymbolType > ( w.getAlphabet ( ), res ); +} + +} /* namespace indexing */ + +} /* namespace stringology */ + +#endif /* COMPRESSED_BIT_PARALLEL_INDEX_CONSTRUCTION_H_ */ diff --git a/alib2algo/src/stringology/query/CompressedBitParallelismFactors.cpp b/alib2algo/src/stringology/query/CompressedBitParallelismFactors.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be3a416889a3b237ac2508eac22d961f94580de0 --- /dev/null +++ b/alib2algo/src/stringology/query/CompressedBitParallelismFactors.cpp @@ -0,0 +1,24 @@ +/* + * CompressedBitParallelismFactors.cpp + * + * Created on: 2. 1. 2017 + * Author: Jan Travnicek + */ + +#include "CompressedBitParallelismFactors.h" + +#include <string/LinearString.h> + +namespace stringology { + +namespace query { + +std::set < unsigned > CompressedBitParallelismFactors::query ( const indexes::CompressedBitParallelIndex < DefaultSymbolType > & compressedBitParallelIndex, const string::String & string ) { + return dispatch ( compressedBitParallelIndex, string.getData ( ) ); +} + +auto CompressedBitParallelismFactorsLinearString = CompressedBitParallelismFactors::RegistratorWrapper < std::set < unsigned >, string::LinearString < > > ( CompressedBitParallelismFactors::query ); + +} /* namespace query */ + +} /* namespace stringology */ diff --git a/alib2algo/src/stringology/query/CompressedBitParallelismFactors.h b/alib2algo/src/stringology/query/CompressedBitParallelismFactors.h new file mode 100644 index 0000000000000000000000000000000000000000..f6f393e6c9ef165370d5895cb0984195d6e0862a --- /dev/null +++ b/alib2algo/src/stringology/query/CompressedBitParallelismFactors.h @@ -0,0 +1,82 @@ +/* + * CompressedBitParallelismFactors.h + * + * Created on: 2. 1. 2017 + * Author: Jan Travnicek + */ + +#ifndef COMPRESSED_BIT_PARALLELISM_FACTORS_H_ +#define COMPRESSED_BIT_PARALLELISM_FACTORS_H_ + +#include <indexes/CompressedBitParallelIndex.h> +#include <string/String.h> +#include <string/LinearString.h> +#include <core/multipleDispatch.hpp> +#include <global/GlobalData.h> + +namespace stringology { + +namespace query { + +/** + * Query compressedBit parallel index for given string. + * + */ + +class CompressedBitParallelismFactors : public std::SingleDispatchFirstStaticParam < CompressedBitParallelismFactors, std::set < unsigned >, const indexes::CompressedBitParallelIndex < DefaultSymbolType > &, const string::StringBase & > { + +public: + /** + * Query a suffix trie + * @param suffix trie to query + * @param string string to query by + * @return occurences of factors + */ + static std::set < unsigned > query ( const indexes::CompressedBitParallelIndex < DefaultSymbolType > & compressedBitParallelIndex, const string::String & string ); + + template < class SymbolType > + static std::set < unsigned > query ( const indexes::CompressedBitParallelIndex < SymbolType > & compressedBitParallelIndex, const string::LinearString < SymbolType > & string ); + +}; + +template < class SymbolType > +std::set < unsigned > CompressedBitParallelismFactors::query ( const indexes::CompressedBitParallelIndex < SymbolType > & compressedBitParallelIndex, const string::LinearString < SymbolType > & string ) { + if ( string.getContent ( ).size ( ) == 0 ) { + if ( compressedBitParallelIndex.getData ( ).begin ( ) == compressedBitParallelIndex.getData ( ).end ( ) ) + return { }; + + std::set < unsigned > res; + for ( unsigned i = 0; i < compressedBitParallelIndex.getData ( ).begin ( )->second.size ( ); ++ i ) { + res.insert ( i ); + } + } + + auto symbolIter = string.getContent ( ).begin ( ); + typename std::map < SymbolType, common::SparseBoolVector >::const_iterator symbolVectorIter = compressedBitParallelIndex.getData ( ).find ( * symbolIter ); + if ( symbolVectorIter == compressedBitParallelIndex.getData ( ).end ( ) ) + return { }; + + common::SparseBoolVector indexVector = symbolVectorIter->second; + indexVector.resize ( indexVector.size ( ) + 1 ); + indexVector <<= 1; + + for ( ++ symbolIter; symbolIter != string.getContent ( ).end ( ); ++ symbolIter ) { + symbolVectorIter = compressedBitParallelIndex.getData ( ).find ( * symbolIter ); + if ( symbolVectorIter == compressedBitParallelIndex.getData ( ).end ( ) ) + return { }; + + indexVector = ( indexVector & symbolVectorIter->second ) << 1; + } + + std::set < unsigned > res; + for ( unsigned i : indexVector ) + res.insert ( i - string.getContent ( ).size ( ) ); + + return res; +} + +} /* namespace query */ + +} /* namespace stringology */ + +#endif /* COMPRESSED_BIT_PARALLELISM_FACTORS_H_ */ diff --git a/alib2data/src/indexes/CompressedBitParallelIndex.cpp b/alib2data/src/indexes/CompressedBitParallelIndex.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e7ac15a1c9d4fa5e5af1ec79efdadcfde4bdf20 --- /dev/null +++ b/alib2data/src/indexes/CompressedBitParallelIndex.cpp @@ -0,0 +1,14 @@ +/* + * CompressedBitParallelIndex.cpp + * + * Created on: Jan 8, 2017 + * Author: Jan Travnicek + */ + +#include "CompressedBitParallelIndex.h" + +namespace alib { + +auto compressedBitParallelIndexParserRegister = xmlApi < alib::Object >::ParserRegister < indexes::CompressedBitParallelIndex < > > ( ); + +} /* namespace alib */ diff --git a/alib2data/src/indexes/CompressedBitParallelIndex.h b/alib2data/src/indexes/CompressedBitParallelIndex.h new file mode 100644 index 0000000000000000000000000000000000000000..fe0528cfdcbfc72ee986a8a18818bf78cc301e60 --- /dev/null +++ b/alib2data/src/indexes/CompressedBitParallelIndex.h @@ -0,0 +1,225 @@ +/* + * CompressedBitParallelIndex.h + * + * Created on: Jan 8, 2017 + * Author: Jan Travnicek + */ + +#ifndef COMPRESSED_BIT_PARALLEL_INDEX_H_ +#define COMPRESSED_BIT_PARALLEL_INDEX_H_ + +#include <string> +#include <iostream> +#include <sstream> + +#include <common/DefaultSymbolType.h> + +#include <core/components.hpp> +#include <exception/CommonException.h> + +#include <object/UniqueObject.h> +#include <object/ObjectBase.h> + +#include <sax/FromXMLParserHelper.h> +#include <core/xmlApi.hpp> + +#include <container/ObjectsSet.h> +#include <container/ObjectsMap.h> +#include <common/SparseBoolVector.hpp> +#include <primitive/Bool.h> + +namespace indexes { + +class GeneralAlphabet; + +/** + * Represents regular expression parsed from the XML. Regular expression is stored + * as a tree of RegExpElement. + */ +template < class SymbolType = DefaultSymbolType > +class CompressedBitParallelIndex : public alib::ObjectBase, public std::Components < CompressedBitParallelIndex < SymbolType >, SymbolType, std::tuple < GeneralAlphabet >, std::tuple < > > { +protected: + std::map < SymbolType, common::SparseBoolVector > m_vectors; + +public: + /** + * @copydoc SuffixTrieNode::clone() const + */ + virtual ObjectBase * clone ( ) const; + + /** + * @copydoc SuffixTrieNode::plunder() const + */ + virtual ObjectBase * plunder ( ) &&; + + explicit CompressedBitParallelIndex ( std::set < SymbolType > alphabet, std::map < SymbolType, common::SparseBoolVector > vectors ); + + /** + * @return Root node of the trie + */ + const std::map < SymbolType, common::SparseBoolVector > & getData ( ) const; + + const std::vector < SymbolType > & getString ( ) const; + + const std::set < SymbolType > & getAlphabet ( ) const { + return this->template accessComponent < GeneralAlphabet > ( ).get ( ); + } + + /** + * Sets the compressedBit vector for given symbol + * @param tree root node to set + */ + void setCompressedBitVectorForSymbol ( SymbolType symbol, common::SparseBoolVector data ); + + /** + * Removes symbol from the alphabet of symbol available in the regular expression + * @param symbol removed symbol from the alphabet + */ + bool removeSymbolFromAlphabet ( const SymbolType & symbol ) { + return this->template accessComponent < GeneralAlphabet > ( ).remove ( symbol ); + } + + /** + * Prints XML representation of the tree to the output stream. + * @param out output stream to which print the tree + * @param tree tree to print + */ + virtual void operator >>( std::ostream & out ) const; + + virtual int compare ( const ObjectBase & other ) const { + if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); + + return std::type_index ( typeid ( * this ) ) - std::type_index ( typeid ( other ) ); + } + + virtual int compare ( const CompressedBitParallelIndex & other ) const; + + virtual explicit operator std::string ( ) const; + + static const std::string & getXmlTagName() { + static std::string xmlTagName = "CompressedBitParallelIndex"; + + return xmlTagName; + } + + static CompressedBitParallelIndex parse ( std::deque < sax::Token >::iterator & input ); + + void compose ( std::deque < sax::Token > & out ) const; + + virtual alib::ObjectBase * inc ( ) &&; +}; + +} /* namespace indexes */ + +namespace indexes { + +template < class SymbolType > +CompressedBitParallelIndex < SymbolType >::CompressedBitParallelIndex ( std::set < SymbolType > alphabet, std::map < SymbolType, common::SparseBoolVector > vectors ) : std::Components < CompressedBitParallelIndex, SymbolType, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( alphabet ) ), std::tuple < > ( ) ), m_vectors ( std::move ( vectors ) ) { +} + +template < class SymbolType > +alib::ObjectBase * CompressedBitParallelIndex < SymbolType >::clone ( ) const { + return new CompressedBitParallelIndex ( * this ); +} + +template < class SymbolType > +alib::ObjectBase * CompressedBitParallelIndex < SymbolType >::plunder ( ) && { + return new CompressedBitParallelIndex ( std::move ( * this ) ); +} + +template < class SymbolType > +const std::map < SymbolType, common::SparseBoolVector > & CompressedBitParallelIndex < SymbolType >::getData ( ) const { + return m_vectors; +} + +template < class SymbolType > +const std::vector < SymbolType > & CompressedBitParallelIndex < SymbolType >::getString ( ) const { + std::vector < SymbolType > res; + + unsigned index = 0; + do { + for ( const std::pair < const SymbolType, common::SparseBoolVector > & compressedBitVector : m_vectors ) + if ( compressedBitVector.second.size ( ) > index && compressedBitVector.second [ index ] ) { + res.push_back ( compressedBitVector.first ); + continue; + } + + } while ( res.size ( ) == index ++ + 1 ); + + return res; +} + +template < class SymbolType > +void CompressedBitParallelIndex < SymbolType >::setCompressedBitVectorForSymbol ( SymbolType symbol, common::SparseBoolVector data ) { + this->m_vectors [ symbol ] = std::move ( data ); +} + +template < class SymbolType > +void CompressedBitParallelIndex < SymbolType >::operator >>( std::ostream & out ) const { + out << "(CompressedBitParallelIndex " << this->m_vectors << ")"; +} + +template < class SymbolType > +int CompressedBitParallelIndex < SymbolType >::compare ( const CompressedBitParallelIndex & other ) const { + auto first = std::tie ( getData ( ), getAlphabet ( ) ); + auto second = std::tie ( other.getData ( ), other.getAlphabet ( ) ); + + static std::compare < decltype ( first ) > comp; + + return comp ( first, second ); +} + +template < class SymbolType > +CompressedBitParallelIndex < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +template < class SymbolType > +CompressedBitParallelIndex < SymbolType > CompressedBitParallelIndex < SymbolType >::parse ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, CompressedBitParallelIndex::getXmlTagName() ); + std::set < SymbolType > alphabet = alib::xmlApi < std::set < SymbolType > >::parse ( input ); + std::map < SymbolType, common::SparseBoolVector > data = alib::xmlApi < std::map < SymbolType, common::SparseBoolVector > >::parse ( input ); + CompressedBitParallelIndex < SymbolType > res ( std::move ( alphabet ), std::move ( data ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, CompressedBitParallelIndex::getXmlTagName() ); + return res; +} + +template < class SymbolType > +void CompressedBitParallelIndex < SymbolType >::compose ( std::deque < sax::Token > & out ) const { + out.emplace_back ( CompressedBitParallelIndex::getXmlTagName(), sax::Token::TokenType::START_ELEMENT ); + alib::xmlApi < std::set < SymbolType > >::compose ( out, getAlphabet ( ) ); + alib::xmlApi < std::map < SymbolType, common::SparseBoolVector > >::compose ( out, getData ( ) ); + out.emplace_back ( CompressedBitParallelIndex::getXmlTagName(), sax::Token::TokenType::END_ELEMENT ); +} + +template < class SymbolType > +alib::ObjectBase* CompressedBitParallelIndex < SymbolType >::inc() && { + return new alib::UniqueObject(alib::Object(std::move(*this)), primitive::Integer(0)); +} + +} /* namespace indexes */ + +namespace std { + +template < class SymbolType > +class ComponentConstraint < indexes::CompressedBitParallelIndex < SymbolType >, SymbolType, indexes::GeneralAlphabet > { +public: + static bool used ( const indexes::CompressedBitParallelIndex < SymbolType > & index, const SymbolType & symbol ) { + const std::map < SymbolType, common::SparseBoolVector > & content = index.getData ( ); + return content.find( symbol ) != content.end(); + } + + static bool available ( const indexes::CompressedBitParallelIndex < SymbolType > &, const SymbolType & ) { + return true; + } + + static void valid ( const indexes::CompressedBitParallelIndex < SymbolType > &, const SymbolType & ) { + } +}; + +} /* namespace std */ + +#endif /* COMPRESSED_BIT_PARALLEL_INDEX_H_ */ diff --git a/aquery2/src/aquery.cpp b/aquery2/src/aquery.cpp index 9bef2cbad5102eff625638daea4b8366f5f1aaf7..15e00462bb38e1ce81729addc383b788403c0164 100644 --- a/aquery2/src/aquery.cpp +++ b/aquery2/src/aquery.cpp @@ -19,6 +19,7 @@ #include <stringology/query/SuffixArrayFactors.h> #include <stringology/query/PositionHeapFactors.h> #include <stringology/query/BitParallelismFactors.h> +#include <stringology/query/CompressedBitParallelismFactors.h> int main ( int argc, char * argv[] ) { try { @@ -32,6 +33,7 @@ int main ( int argc, char * argv[] ) { allowed.push_back ( "suffixArrayFactors" ); allowed.push_back ( "positionHeapFactors" ); allowed.push_back ( "bitParallelismFactors" ); + allowed.push_back ( "compressedBitParallelismFactors" ); TCLAP::ValuesConstraint < std::string > allowedVals ( allowed ); TCLAP::ValueArg < std::string > query ( "q", "query", "Query index", false, "exactFactorMatch", & allowedVals ); @@ -110,6 +112,19 @@ int main ( int argc, char * argv[] ) { measurements::end ( ); measurements::start ( "Output write", measurements::Type::AUXILIARY ); + alib::XmlDataFactory::toStdout ( res ); + } else if ( query.getValue ( ) == "compressedBitParallelismFactors" ) { + indexes::CompressedBitParallelIndex < > compressedBitParallelIndex = alib::XmlDataFactory::fromTokens < indexes::CompressedBitParallelIndex < > > ( sax::FromXMLParserHelper::parseInput ( indexInput ) ); + string::String pattern = alib::XmlDataFactory::fromTokens < string::String > ( std::move ( sax::FromXMLParserHelper::parseInput(true, patternInput).front ( ) ) ); + + measurements::end ( ); + measurements::start ( "Algorithm", measurements::Type::MAIN ); + + std::set < unsigned > res = stringology::query::CompressedBitParallelismFactors::query ( compressedBitParallelIndex, pattern ); + + measurements::end ( ); + measurements::start ( "Output write", measurements::Type::AUXILIARY ); + alib::XmlDataFactory::toStdout ( res ); } else { throw exception::CommonException ( "Invalid algorithm" ); diff --git a/astringology2/src/astringology.cpp b/astringology2/src/astringology.cpp index 7ef8be2310841cc9b40174e1cd968e310214ac1d..048414bd58079e323b7c06b803b78edf4ac9a8f5 100644 --- a/astringology2/src/astringology.cpp +++ b/astringology2/src/astringology.cpp @@ -38,6 +38,7 @@ #include <stringology/indexing/PositionHeapNaive.h> #include <stringology/indexing/SuffixArrayNaive.h> #include <stringology/indexing/BitParallelIndexConstruction.h> +#include <stringology/indexing/CompressedBitParallelIndexConstruction.h> int main ( int argc, char * argv[] ) { try { @@ -65,6 +66,7 @@ int main ( int argc, char * argv[] ) { allowed.push_back ( "suffixTrie" ); allowed.push_back ( "positionHeap" ); allowed.push_back ( "bitParallelIndex" ); + allowed.push_back ( "compressedBitParallelIndex" ); allowed.push_back ( "suffixArray" ); TCLAP::ValuesConstraint < std::string > allowedVals ( allowed ); @@ -319,6 +321,18 @@ int main ( int argc, char * argv[] ) { measurements::start ( "Output write", measurements::Type::AUXILIARY ); alib::XmlDataFactory::toStdout ( bitParallelIndex ); + } else if ( algorithm.getValue ( ) == "compressedBitParallelIndex" ) { + string::String subject = alib::XmlDataFactory::fromTokens < string::String > ( std::move ( sax::FromXMLParserHelper::parseInput(true, subjectInput).front ( ) ) ); + + measurements::end ( ); + measurements::start ( "Algorithm", measurements::Type::MAIN ); + + indexes::CompressedBitParallelIndex < DefaultSymbolType > compressedBitParallelIndex = stringology::indexing::CompressedBitParallelIndexConstruction::construct ( subject ); + + measurements::end ( ); + measurements::start ( "Output write", measurements::Type::AUXILIARY ); + + alib::XmlDataFactory::toStdout ( compressedBitParallelIndex ); } else if ( algorithm.getValue ( ) == "suffixArray" ) { string::String subject = alib::XmlDataFactory::fromTokens < string::String > ( std::move ( sax::FromXMLParserHelper::parseInput(true, subjectInput).front ( ) ) ); diff --git a/tests.astringology.sh b/tests.astringology.sh index d17db916b5c690ca923e285749bb78912ab82124..90d109ea2b1900c05d5e5f302fc775be86ad298d 100755 --- a/tests.astringology.sh +++ b/tests.astringology.sh @@ -212,6 +212,7 @@ function runTest { clearResults } +runTest "Compressed Bit Parallelism Factors" "./astringology2 -a compressedBitParallelIndex -s \"\$SUBJECT_FILE\" | ./aquery2 -q compressedBitParallelismFactors -p \"\$PATTERN_FILE\" | ./astat2 -p size" runTest "Bit Parallelism Factors" "./astringology2 -a bitParallelIndex -s \"\$SUBJECT_FILE\" | ./aquery2 -q bitParallelismFactors -p \"\$PATTERN_FILE\" | ./astat2 -p size" runTest "Position Heap Factors" "./astringology2 -a positionHeap -s \"\$SUBJECT_FILE\" | ./aquery2 -q positionHeapFactors -p \"\$PATTERN_FILE\" | ./astat2 -p size" runTest "Suffix Array Factors" "./astringology2 -a suffixArray -s \"\$SUBJECT_FILE\" | ./aquery2 -q suffixArrayFactors -p \"\$PATTERN_FILE\" | ./astat2 -p size"