From c5c8a5fe6e956dd173825580d77c3a5824df88ac Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 20 Feb 2017 14:17:50 +0100 Subject: [PATCH] bit parallel index data structure and construction --- .../indexing/BitParallelIndexConstruction.cpp | 24 ++ .../indexing/BitParallelIndexConstruction.h | 56 +++++ alib2data/src/indexes/BitParallelIndex.cpp | 14 ++ alib2data/src/indexes/BitParallelIndex.h | 225 ++++++++++++++++++ astringology2/src/astringology.cpp | 14 ++ 5 files changed, 333 insertions(+) create mode 100644 alib2algo/src/stringology/indexing/BitParallelIndexConstruction.cpp create mode 100644 alib2algo/src/stringology/indexing/BitParallelIndexConstruction.h create mode 100644 alib2data/src/indexes/BitParallelIndex.cpp create mode 100644 alib2data/src/indexes/BitParallelIndex.h diff --git a/alib2algo/src/stringology/indexing/BitParallelIndexConstruction.cpp b/alib2algo/src/stringology/indexing/BitParallelIndexConstruction.cpp new file mode 100644 index 0000000000..e535b8313a --- /dev/null +++ b/alib2algo/src/stringology/indexing/BitParallelIndexConstruction.cpp @@ -0,0 +1,24 @@ +/* + * BitParallelIndexConstruction.cpp + * + * Created on: 6. 2. 2017 + * Author: Jan Travnicek + */ + +#include "BitParallelIndexConstruction.h" + +#include <string/LinearString.h> + +namespace stringology { + +namespace indexing { + +indexes::BitParallelIndex < DefaultSymbolType > BitParallelIndexConstruction::construct ( const string::String & string ) { + return dispatch ( string.getData ( ) ); +} + +auto bitParallelIndexConstructionLinearString = BitParallelIndexConstruction::RegistratorWrapper < indexes::BitParallelIndex < DefaultSymbolType >, string::LinearString < > > ( BitParallelIndexConstruction::construct ); + +} /* namespace indexing */ + +} /* namespace stringology */ diff --git a/alib2algo/src/stringology/indexing/BitParallelIndexConstruction.h b/alib2algo/src/stringology/indexing/BitParallelIndexConstruction.h new file mode 100644 index 0000000000..de02c92a13 --- /dev/null +++ b/alib2algo/src/stringology/indexing/BitParallelIndexConstruction.h @@ -0,0 +1,56 @@ +/* + * BitParallelIndex.h + * + * Created on: 6. 2. 2017 + * Author: Jan Travnicek + */ + +#ifndef BIT_PARALLEL_INDEX_CONSTRUCTION_H_ +#define BIT_PARALLEL_INDEX_CONSTRUCTION_H_ + +#include <indexes/BitParallelIndex.h> +#include <string/String.h> +#include <string/LinearString.h> +#include <core/multipleDispatch.hpp> +#include <exception/CommonException.h> + +namespace stringology { + +namespace indexing { + +/** + * Constructs a bit parallel index for given string. + * + */ + +class BitParallelIndexConstruction : public std::SingleDispatch < BitParallelIndexConstruction, indexes::BitParallelIndex < DefaultSymbolType >, const string::StringBase & > { +public: + /** + * Creates suffix trie + * @param string string to construct suffix trie for + * @return automaton + */ + static indexes::BitParallelIndex < DefaultSymbolType > construct ( const string::String & string ); + + template < class SymbolType > + static indexes::BitParallelIndex < SymbolType > construct ( const string::LinearString < SymbolType > & string ); + +}; + +template < class SymbolType > +indexes::BitParallelIndex < SymbolType > BitParallelIndexConstruction::construct ( const string::LinearString < SymbolType > & w ) { + std::map < SymbolType, std::vector < bool > > 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::BitParallelIndex < SymbolType > ( w.getAlphabet ( ), res ); +} + +} /* namespace indexing */ + +} /* namespace stringology */ + +#endif /* BIT_PARALLEL_INDEX_CONSTRUCTION_H_ */ diff --git a/alib2data/src/indexes/BitParallelIndex.cpp b/alib2data/src/indexes/BitParallelIndex.cpp new file mode 100644 index 0000000000..af8bc32b68 --- /dev/null +++ b/alib2data/src/indexes/BitParallelIndex.cpp @@ -0,0 +1,14 @@ +/* + * BitParallelIndex.cpp + * + * Created on: Jan 8, 2017 + * Author: Jan Travnicek + */ + +#include "BitParallelIndex.h" + +namespace alib { + +auto bitParallelIndexParserRegister = xmlApi < alib::Object >::ParserRegister < indexes::BitParallelIndex < > > ( ); + +} /* namespace alib */ diff --git a/alib2data/src/indexes/BitParallelIndex.h b/alib2data/src/indexes/BitParallelIndex.h new file mode 100644 index 0000000000..79ab57d583 --- /dev/null +++ b/alib2data/src/indexes/BitParallelIndex.h @@ -0,0 +1,225 @@ +/* + * BitParallelIndex.h + * + * Created on: Jan 8, 2017 + * Author: Jan Travnicek + */ + +#ifndef BIT_PARALLEL_INDEX_H_ +#define 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 <container/ObjectsVector.h> +#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 BitParallelIndex : public alib::ObjectBase, public std::Components < BitParallelIndex < SymbolType >, SymbolType, std::tuple < GeneralAlphabet >, std::tuple < > > { +protected: + std::map < SymbolType, std::vector < bool > > m_vectors; + +public: + /** + * @copydoc SuffixTrieNode::clone() const + */ + virtual ObjectBase * clone ( ) const; + + /** + * @copydoc SuffixTrieNode::plunder() const + */ + virtual ObjectBase * plunder ( ) &&; + + explicit BitParallelIndex ( std::set < SymbolType > alphabet, std::map < SymbolType, std::vector < bool > > vectors ); + + /** + * @return Root node of the trie + */ + const std::map < SymbolType, std::vector < bool > > & getData ( ) const; + + const std::vector < SymbolType > & getString ( ) const; + + const std::set < SymbolType > & getAlphabet ( ) const { + return this->template accessComponent < GeneralAlphabet > ( ).get ( ); + } + + /** + * Sets the bit vector for given symbol + * @param tree root node to set + */ + void setBitVectorForSymbol ( SymbolType symbol, std::vector < bool > 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 BitParallelIndex & other ) const; + + virtual explicit operator std::string ( ) const; + + static const std::string & getXmlTagName() { + static std::string xmlTagName = "BitParallelIndex"; + + return xmlTagName; + } + + static BitParallelIndex 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 > +BitParallelIndex < SymbolType >::BitParallelIndex ( std::set < SymbolType > alphabet, std::map < SymbolType, std::vector < bool > > vectors ) : std::Components < BitParallelIndex, SymbolType, std::tuple < GeneralAlphabet >, std::tuple < > > ( std::make_tuple ( std::move ( alphabet ) ), std::tuple < > ( ) ), m_vectors ( std::move ( vectors ) ) { +} + +template < class SymbolType > +alib::ObjectBase * BitParallelIndex < SymbolType >::clone ( ) const { + return new BitParallelIndex ( * this ); +} + +template < class SymbolType > +alib::ObjectBase * BitParallelIndex < SymbolType >::plunder ( ) && { + return new BitParallelIndex ( std::move ( * this ) ); +} + +template < class SymbolType > +const std::map < SymbolType, std::vector < bool > > & BitParallelIndex < SymbolType >::getData ( ) const { + return m_vectors; +} + +template < class SymbolType > +const std::vector < SymbolType > & BitParallelIndex < SymbolType >::getString ( ) const { + std::vector < SymbolType > res; + + unsigned index = 0; + do { + for ( const std::pair < const SymbolType, std::vector < bool > > & bitVector : m_vectors ) + if ( bitVector.second.size ( ) > index && bitVector.second [ index ] ) { + res.push_back ( bitVector.first ); + continue; + } + + } while ( res.size ( ) == index ++ + 1 ); + + return res; +} + +template < class SymbolType > +void BitParallelIndex < SymbolType >::setBitVectorForSymbol ( SymbolType symbol, std::vector < bool > data ) { + this->m_vectors [ symbol ] = std::move ( data ); +} + +template < class SymbolType > +void BitParallelIndex < SymbolType >::operator >>( std::ostream & out ) const { + out << "(BitParallelIndex " << this->m_vectors << ")"; +} + +template < class SymbolType > +int BitParallelIndex < SymbolType >::compare ( const BitParallelIndex & 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 > +BitParallelIndex < SymbolType >::operator std::string ( ) const { + std::stringstream ss; + ss << * this; + return ss.str ( ); +} + +template < class SymbolType > +BitParallelIndex < SymbolType > BitParallelIndex < SymbolType >::parse ( std::deque < sax::Token >::iterator & input ) { + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, BitParallelIndex::getXmlTagName() ); + std::set < SymbolType > alphabet = alib::xmlApi < std::set < SymbolType > >::parse ( input ); + std::map < SymbolType, std::vector < bool > > data = alib::xmlApi < std::map < SymbolType, std::vector < bool > > >::parse ( input ); + BitParallelIndex < SymbolType > res ( std::move ( alphabet ), std::move ( data ) ); + + sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::END_ELEMENT, BitParallelIndex::getXmlTagName() ); + return res; +} + +template < class SymbolType > +void BitParallelIndex < SymbolType >::compose ( std::deque < sax::Token > & out ) const { + out.emplace_back ( BitParallelIndex::getXmlTagName(), sax::Token::TokenType::START_ELEMENT ); + alib::xmlApi < std::set < SymbolType > >::compose ( out, getAlphabet ( ) ); + alib::xmlApi < std::map < SymbolType, std::vector < bool > > >::compose ( out, getData ( ) ); + out.emplace_back ( BitParallelIndex::getXmlTagName(), sax::Token::TokenType::END_ELEMENT ); +} + +template < class SymbolType > +alib::ObjectBase* BitParallelIndex < SymbolType >::inc() && { + return new alib::UniqueObject(alib::Object(std::move(*this)), primitive::Integer(0)); +} + +} /* namespace indexes */ + +namespace std { + +template < class SymbolType > +class ComponentConstraint < indexes::BitParallelIndex < SymbolType >, SymbolType, indexes::GeneralAlphabet > { +public: + static bool used ( const indexes::BitParallelIndex < SymbolType > & index, const SymbolType & symbol ) { + const std::map < SymbolType, std::vector < bool > > & content = index.getData ( ); + return content.find( symbol ) != content.end(); + } + + static bool available ( const indexes::BitParallelIndex < SymbolType > &, const SymbolType & ) { + return true; + } + + static void valid ( const indexes::BitParallelIndex < SymbolType > &, const SymbolType & ) { + } +}; + +} /* namespace std */ + +#endif /* BIT_PARALLEL_INDEX_H_ */ diff --git a/astringology2/src/astringology.cpp b/astringology2/src/astringology.cpp index 777f125f79..7ef8be2310 100644 --- a/astringology2/src/astringology.cpp +++ b/astringology2/src/astringology.cpp @@ -37,6 +37,7 @@ #include <stringology/indexing/SuffixTrieNaive.h> #include <stringology/indexing/PositionHeapNaive.h> #include <stringology/indexing/SuffixArrayNaive.h> +#include <stringology/indexing/BitParallelIndexConstruction.h> int main ( int argc, char * argv[] ) { try { @@ -63,6 +64,7 @@ int main ( int argc, char * argv[] ) { allowed.push_back ( "borderArray" ); allowed.push_back ( "suffixTrie" ); allowed.push_back ( "positionHeap" ); + allowed.push_back ( "bitParallelIndex" ); allowed.push_back ( "suffixArray" ); TCLAP::ValuesConstraint < std::string > allowedVals ( allowed ); @@ -305,6 +307,18 @@ int main ( int argc, char * argv[] ) { measurements::start ( "Output write", measurements::Type::AUXILIARY ); alib::XmlDataFactory::toStdout ( positionHeap ); + } else if ( algorithm.getValue ( ) == "bitParallelIndex" ) { + 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::BitParallelIndex < DefaultSymbolType > bitParallelIndex = stringology::indexing::BitParallelIndexConstruction::construct ( subject ); + + measurements::end ( ); + measurements::start ( "Output write", measurements::Type::AUXILIARY ); + + alib::XmlDataFactory::toStdout ( bitParallelIndex ); } else if ( algorithm.getValue ( ) == "suffixArray" ) { string::String subject = alib::XmlDataFactory::fromTokens < string::String > ( std::move ( sax::FromXMLParserHelper::parseInput(true, subjectInput).front ( ) ) ); -- GitLab