diff --git a/alib2algo/src/string/generate/RandomStringFactory.cpp b/alib2algo/src/string/generate/RandomStringFactory.cpp index e8c00e00573e1071296c929027225f8198aa3b7a..bee3413790f762581b315e1e0f1ab90fcfde406b 100644 --- a/alib2algo/src/string/generate/RandomStringFactory.cpp +++ b/alib2algo/src/string/generate/RandomStringFactory.cpp @@ -9,6 +9,9 @@ #include <algorithm> #include <random> +#include <vector> + +#include <exception/AlibException.h> namespace string { diff --git a/alib2algo/src/string/generate/RandomStringFactory.h b/alib2algo/src/string/generate/RandomStringFactory.h index 08694c7e3310174f4fbac3a4177cb10e0e92672b..6cec60b31d69e1b6ce245462635b853450ed5d85 100644 --- a/alib2algo/src/string/generate/RandomStringFactory.h +++ b/alib2algo/src/string/generate/RandomStringFactory.h @@ -1,5 +1,5 @@ /* - * RandomAutomatonFactory.h + * RandomStringFactory.h * * Created on: 27. 3. 2014 * Author: Jan Travnicek @@ -9,9 +9,6 @@ #define RANDOM_STRING_FACTORY_H_ #include <set> -#include <vector> - -#include <exception/AlibException.h> #include <string/LinearString.h> namespace string { diff --git a/alib2algo/src/string/generate/RandomSubstringFactory.cpp b/alib2algo/src/string/generate/RandomSubstringFactory.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68ad587a42828f97ebe9d8adefddb1c573b878b2 --- /dev/null +++ b/alib2algo/src/string/generate/RandomSubstringFactory.cpp @@ -0,0 +1,37 @@ +/* + * RandomSubstringFactory.cpp + * + * Created on: 27. 3. 2014 + * Author: Jan Travnicek + */ + +#include "RandomSubstringFactory.h" + +#include <algorithm> +#include <random> +#include <exception/AlibException.h> + +#include <string/LinearString.h> + +namespace string { + +namespace generate { + +string::LinearString RandomSubstringFactory::generateSubstring( size_t size, const string::LinearString & string) { + if(size > string.getContent().size()) + throw exception::AlibException("String not long enough"); + + srand( time( NULL ) ); + + size_t begin = std::random_devices::semirandom() % (string.getContent().size() - size + 1); + + std::vector<alphabet::Symbol> data(string.getContent().begin() + begin, string.getContent().begin() + begin + size); + + return LinearString {string.getAlphabet(), data}; +} + +auto RandomSubstringFactoryLinearString = RandomSubstringFactory::RegistratorWrapper < string::LinearString, string::LinearString > ( RandomSubstringFactory::getInstance ( ), RandomSubstringFactory::generateSubstring ); + +} /* namespace generate */ + +} /* namespace string */ diff --git a/alib2algo/src/string/generate/RandomSubstringFactory.h b/alib2algo/src/string/generate/RandomSubstringFactory.h new file mode 100644 index 0000000000000000000000000000000000000000..95fa695b2fa918d37475a5de8fed91f47209772a --- /dev/null +++ b/alib2algo/src/string/generate/RandomSubstringFactory.h @@ -0,0 +1,33 @@ +/* + * RandomSubstringFactory.h + * + * Created on: 27. 3. 2014 + * Author: Jan Travnicek + */ + +#ifndef RANDOM_SUBSTRING_FACTORY_H_ +#define RANDOM_SUBSTRING_FACTORY_H_ + +#include <core/multipleDispatch.hpp> +#include <string/String.h> +#include <string/StringFeatures.h> + +namespace string { + +namespace generate { + +class RandomSubstringFactory : public std::SingleDispatchFirstStaticParam < string::LinearString, size_t, string::StringBase > { +public: + static string::LinearString generateSubstring( size_t size, const string::LinearString & ); + + static RandomSubstringFactory& getInstance() { + static RandomSubstringFactory res; + return res; + } +}; + +} /* namespace generate */ + +} /* namespace string */ + +#endif /* RANDOM_SUBSTRING_FACTORY_H_ */