diff --git a/alib2algo/src/string/generate/RandomStringFactory.cpp b/alib2algo/src/string/generate/RandomStringFactory.cpp index 34559af78ae65cf3e957f902c813fa0dafbae120..d49dc8175e5e179961e6bcbaeb27fc995251ec0d 100644 --- a/alib2algo/src/string/generate/RandomStringFactory.cpp +++ b/alib2algo/src/string/generate/RandomStringFactory.cpp @@ -21,22 +21,19 @@ string::LinearString < std::string > string::generate::RandomStringFactory::gene if ( alphabetSize <= 0 ) throw exception::CommonException ( "Alphabet size must be greater than 0." ); - ext::vector < std::string > elems; + ext::vector < std::string > symbols; - for ( size_t i = 0; i < size; i++ ) - elems.push_back ( ext::to_string ( static_cast < int > ( ext::random_devices::semirandom ( ) % alphabetSize ) ) ); + int i = 0; + while ( symbols.size ( ) < alphabetSize ) + if ( randomizedAlphabet ) + symbols.push_back ( ext::to_string ( static_cast < int > ( ext::random_devices::semirandom ( ) % alphabetSize ) ) ); + else + symbols.push_back ( ext::to_string ( i ++ ) ); - return string::LinearString < std::string > ( elems ); -} + if ( randomizedAlphabet ) shuffle ( symbols.begin ( ), symbols.end ( ), ext::random_devices::semirandom ); -auto GenerateLinearString1 = registration::AbstractRegister < string::generate::RandomStringFactory, string::LinearString < std::string >, size_t, size_t, bool, bool > ( string::generate::RandomStringFactory::generateLinearString, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, "size", "alphabetSize", "randomizedAlphabet", "integerSymbols" ).setDocumentation ( -"Generates a random string of given size.\n\ -\n\ -@param size the length of the generated string\n\ -@param alphabetSize size of the alphabet (1-26 for characters and 0-INT_MAX for integers)\n\ -@param randomizedAlphabet selects random symbols from a-z range if true\n\ -@param integerSymbols use integers as symbols in the generated string is true, randomize alphabet is not used if integer alphabet is requested\n\ -@return random string" ); + return string::generate::RandomStringFactory::generateLinearString ( size, symbols ); +} string::LinearString < std::string > string::generate::RandomStringFactory::generateLinearString ( size_t size, size_t alphabetSize, bool randomizedAlphabet ) { if ( alphabetSize > 26 ) @@ -52,9 +49,9 @@ string::LinearString < std::string > string::generate::RandomStringFactory::gene if ( randomizedAlphabet ) shuffle ( symbols.begin ( ), symbols.end ( ), ext::random_devices::semirandom ); - ext::set < std::string > alphabet ( symbols.begin ( ), symbols.begin ( ) + alphabetSize ); + symbols.resize ( alphabetSize ); - return string::generate::RandomStringFactory::generateLinearString ( size, alphabet ); + return string::generate::RandomStringFactory::generateLinearString ( size, symbols ); } } /* namespace generate */ @@ -63,6 +60,15 @@ string::LinearString < std::string > string::generate::RandomStringFactory::gene namespace { +auto GenerateLinearString1 = registration::AbstractRegister < string::generate::RandomStringFactory, string::LinearString < std::string >, size_t, size_t, bool, bool > ( string::generate::RandomStringFactory::generateLinearString, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, "size", "alphabetSize", "randomizedAlphabet", "integerSymbols" ).setDocumentation ( +"Generates a random string of given size.\n\ +\n\ +@param size the length of the generated string\n\ +@param alphabetSize size of the alphabet (1-26 for characters and 0-INT_MAX for integers)\n\ +@param randomizedAlphabet selects random symbols from a-z range if true\n\ +@param integerSymbols use integers as symbols in the generated string is true, randomize alphabet is not used if integer alphabet is requested\n\ +@return random string" ); + auto GenerateLinearString2 = registration::AbstractRegister < string::generate::RandomStringFactory, string::LinearString < std::string >, size_t, size_t, bool > ( string::generate::RandomStringFactory::generateLinearString, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, "size", "alphabetSize", "randomizedAlphabet" ).setDocumentation ( "Generates a random string of given size.\n\ \n\ @@ -71,7 +77,7 @@ auto GenerateLinearString2 = registration::AbstractRegister < string::generate:: @param randomizedAlphabet selects random symbols from a-z range if true\n\ @return random string" ); -auto GenerateLinearString3 = registration::AbstractRegister < string::generate::RandomStringFactory, string::LinearString < >, size_t, ext::set < DefaultSymbolType > > ( string::generate::RandomStringFactory::generateLinearString, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, "size", "alphabet" ).setDocumentation ( +auto GenerateLinearString3 = registration::AbstractRegister < string::generate::RandomStringFactory, string::LinearString < >, size_t, const ext::set < DefaultSymbolType > & > ( string::generate::RandomStringFactory::generateLinearString, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, "size", "alphabet" ).setDocumentation ( "Generates a random string of given size\n\ \n\ @param size the length of the generated string\n\ diff --git a/alib2algo/src/string/generate/RandomStringFactory.h b/alib2algo/src/string/generate/RandomStringFactory.h index e046d0b85a80c1e58950e39a533c56f8569cf574..99b01e16f9d61f784a45c4ae54aebfe4f4e8324a 100644 --- a/alib2algo/src/string/generate/RandomStringFactory.h +++ b/alib2algo/src/string/generate/RandomStringFactory.h @@ -25,6 +25,9 @@ namespace generate { * */ class RandomStringFactory { + template < class SymbolType > + static string::LinearString < SymbolType > generateLinearString ( size_t size, const ext::vector < SymbolType > & alphabet ); + public: /** * Generates a random string of given size. @@ -59,20 +62,28 @@ public: * \return random string */ template < class SymbolType > - static string::LinearString < SymbolType > generateLinearString ( size_t size, ext::set < SymbolType > alphabet ); + static string::LinearString < SymbolType > generateLinearString ( size_t size, const ext::set < SymbolType > & alphabet ); }; template < class SymbolType > -string::LinearString < SymbolType > RandomStringFactory::generateLinearString ( size_t size, ext::set < SymbolType > alphabet ) { +string::LinearString < SymbolType > RandomStringFactory::generateLinearString ( size_t size, const ext::set < SymbolType > & alphabet ) { + + if ( alphabet.empty ( ) ) + throw exception::CommonException ( "Alphabet size must be greater than 0." ); + + return generateLinearString ( size, ext::vector < SymbolType > ( alphabet.begin ( ), alphabet.end ( ) ) ); +} + +template < class SymbolType > +string::LinearString < SymbolType > RandomStringFactory::generateLinearString ( size_t size, const ext::vector < SymbolType > & alphabet ) { if ( alphabet.empty ( ) ) throw exception::CommonException ( "Alphabet size must be greater than 0." ); - ext::vector < SymbolType > alphabetList ( alphabet.begin ( ), alphabet.end ( ) ); ext::vector < SymbolType > elems; for ( size_t i = 0; i < size; i++ ) - elems.push_back ( alphabetList[ext::random_devices::semirandom ( ) % alphabetList.size ( )] ); + elems.push_back ( alphabet [ext::random_devices::semirandom ( ) % alphabet.size ( )] ); return string::LinearString < SymbolType > ( elems ); } diff --git a/alib2algo/src/string/generate/RandomStringSequence.cpp b/alib2algo/src/string/generate/RandomStringSequence.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d394c1527379e8bb144a467c78049ca09cbbb255 --- /dev/null +++ b/alib2algo/src/string/generate/RandomStringSequence.cpp @@ -0,0 +1,87 @@ +/* + * RandomStringSequence.cpp + * + * Created on: 27. 3. 2014 + * Author: Jan Travnicek + */ + +#include "RandomStringSequence.h" + +#include <alib/algorithm> + +#include <registration/AlgoRegistration.hpp> + +namespace string { + +namespace generate { + +string::LinearString < std::string > string::generate::RandomStringSequence::generateLinearString ( size_t size, size_t alphabetSize, bool randomizedAlphabet, bool integerSymbols ) { + if ( !integerSymbols ) return generateLinearString ( size, alphabetSize, randomizedAlphabet ); + + if ( alphabetSize <= 0 ) + throw exception::CommonException ( "Alphabet size must be greater than 0." ); + + ext::vector < std::string > symbols; + + int i = 0; + while ( symbols.size ( ) < alphabetSize ) + if ( randomizedAlphabet ) + symbols.push_back ( ext::to_string ( static_cast < int > ( ext::random_devices::semirandom ( ) % alphabetSize ) ) ); + else + symbols.push_back ( ext::to_string ( i ++ ) ); + + if ( randomizedAlphabet ) shuffle ( symbols.begin ( ), symbols.end ( ), ext::random_devices::semirandom ); + + return string::generate::RandomStringSequence::generateLinearString ( size, symbols ); +} + +string::LinearString < std::string > string::generate::RandomStringSequence::generateLinearString ( size_t size, size_t alphabetSize, bool randomizedAlphabet ) { + if ( alphabetSize > 26 ) + throw exception::CommonException ( "Too big alphabet." ); + + if ( alphabetSize <= 0 ) + throw exception::CommonException ( "Alphabet size must be greater than 0." ); + + ext::vector < std::string > symbols; + + for(char i = 'a'; i <= 'z'; i++) + symbols.push_back ( std::string ( 1, i ) ); + + if ( randomizedAlphabet ) shuffle ( symbols.begin ( ), symbols.end ( ), ext::random_devices::semirandom ); + + symbols.resize ( alphabetSize ); + + return string::generate::RandomStringSequence::generateLinearString ( size, symbols ); +} + +} /* namespace generate */ + +} /* namespace string */ + +namespace { + +auto SequenceLinearString1 = registration::AbstractRegister < string::generate::RandomStringSequence, string::LinearString < std::string >, size_t, size_t, bool, bool > ( string::generate::RandomStringSequence::generateLinearString, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, "size", "alphabetSize", "randomizedAlphabet", "integerSymbols" ).setDocumentation ( +"Generates a random string of given size.\n\ +\n\ +@param size the length of the generated string\n\ +@param alphabetSize size of the alphabet (1-26 for characters and 0-INT_MAX for integers)\n\ +@param randomizedAlphabet selects random symbols from a-z range if true\n\ +@param integerSymbols use integers as symbols in the generated string is true, randomize alphabet is not used if integer alphabet is requested\n\ +@return random string" ); + +auto SequenceLinearString2 = registration::AbstractRegister < string::generate::RandomStringSequence, string::LinearString < std::string >, size_t, size_t, bool > ( string::generate::RandomStringSequence::generateLinearString, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, "size", "alphabetSize", "randomizedAlphabet" ).setDocumentation ( +"Generates a random string of given size.\n\ +\n\ +@param size the length of the generated string\n\ +@param alphabetSize size of the alphabet (1-26 for characters)\n\ +@param randomizedAlphabet selects random symbols from a-z range if true\n\ +@return random string" ); + +auto SequenceLinearString3 = registration::AbstractRegister < string::generate::RandomStringSequence, string::LinearString < >, size_t, const ext::set < DefaultSymbolType > & > ( string::generate::RandomStringSequence::generateLinearString, abstraction::AlgorithmCategories::AlgorithmCategory::DEFAULT, "size", "alphabet" ).setDocumentation ( +"Generates a random string of given size\n\ +\n\ +@param size the length of the generated string\n\ +@param alphabet alphabet of the generated string\n\ +@return random string" ); + +} /* namespace */ diff --git a/alib2algo/src/string/generate/RandomStringSequence.h b/alib2algo/src/string/generate/RandomStringSequence.h new file mode 100644 index 0000000000000000000000000000000000000000..b0af94ab5c905eefa246b62df1495a47250ed964 --- /dev/null +++ b/alib2algo/src/string/generate/RandomStringSequence.h @@ -0,0 +1,95 @@ +/* + * RandomStringSequence.h + * + * Created on: 27. 3. 2014 + * Author: Jan Travnicek + */ + +#ifndef RANDOM_STRING_SEQUENCE_H_ +#define RANDOM_STRING_SEQUENCE_H_ + +#include <alib/set> +#include <alib/random> +#include <alib/vector> + +#include <exception/CommonException.h> + +#include <string/LinearString.h> + +namespace string { + +namespace generate { + +/** + * Generator of random strings. + * + */ +class RandomStringSequence { + template < class SymbolType > + static string::LinearString < SymbolType > generateLinearString ( size_t size, const ext::vector < SymbolType > & alphabet ); + +public: + /** + * Generates a random string of given size. + * + * \param size the length of the generated string + * \param alphabetSize size of the alphabet (1-26 for characters and 0-INT_MAX for integers) + * \param randomizedAlphabet selects random symbols from a-z range if true + * \param integerSymbols use integers as symbols in the generated string is true, randomize alphabet is not used if integer alphabet is requested + * + * \return random string + */ + static string::LinearString < std::string > generateLinearString ( size_t size, size_t alphabetSize, bool randomizedAlphabet, bool integerSymbols ); + + /** + * Generates a random string of given size. + * + * \param size the length of the generated string + * \param alphabetSize size of the alphabet (1-26 for characters) + * \param randomizedAlphabet selects random symbols from a-z range if true + * + * \return random string + */ + static string::LinearString < std::string > generateLinearString ( size_t size, size_t alphabetSize, bool randomizedAlphabet ); + + /** + * Generates a random string of given size + * \tparam SymbolType the type of symbols of the random string + * + * \param size the length of the generated string + * \param alphabet alphabet of the generated string + * + * \return random string + */ + template < class SymbolType > + static string::LinearString < SymbolType > generateLinearString ( size_t size, const ext::set < SymbolType > & alphabet ); +}; + +template < class SymbolType > +string::LinearString < SymbolType > RandomStringSequence::generateLinearString ( size_t size, const ext::set < SymbolType > & alphabet ) { + + if ( alphabet.empty ( ) ) + throw exception::CommonException ( "Alphabet size must be greater than 0." ); + + return generateLinearString ( size, ext::vector < SymbolType > ( alphabet.begin ( ), alphabet.end ( ) ) ); +} + +template < class SymbolType > +string::LinearString < SymbolType > RandomStringSequence::generateLinearString ( size_t size, const ext::vector < SymbolType > & alphabet ) { + + if ( alphabet.empty ( ) ) + throw exception::CommonException ( "Alphabet size must be greater than 0." ); + + ext::vector < SymbolType > elems; + + for ( size_t i = 0; i < size; i++ ) + elems.push_back ( alphabet [ i % alphabet.size ( ) ] ); + + return string::LinearString < SymbolType > ( elems ); +} + +} /* namespace generate */ + +} /* namespace string */ + +#endif /* RANDOM_STRING_SEQUENCE_H_ */ diff --git a/examples2/aql/quantumLeapBorderline.aql b/examples2/aql/quantumLeapBorderline.aql new file mode 100644 index 0000000000000000000000000000000000000000..a8a43a96de5e399f02b388e7a21db2dbbcaa0976 --- /dev/null +++ b/examples2/aql/quantumLeapBorderline.aql @@ -0,0 +1,20 @@ +begin + declare auto $i = 0; + declare auto $limit = 20; + while ( $i < $limit ) do begin + calc $i ++; + execute string::generate::RandomStringSequence $limit $i false false > $subject; + execute string::generate::RandomStringSequence 3 $i false false > $pattern; + execute string::GeneralAlphabet::add $pattern <( string::GeneralAlphabet::get $subject ); + declare auto $z = 1; + while ( $z < 7 ) do begin + print $subject; + print $pattern; + print $z; + print stringology::exact::ExactFactorMatch $subject $pattern; + if ( ! compare::SetCompare ( stringology::exact::ExactFactorMatch ( $subject, $pattern ), stringology::exact::QuantumLeap ( $subject, $pattern, $z ) ) ) then + exit 1; + calc $z = $z + 1; + end + end +end