Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
RandomStringFactory.cpp 2.27 KiB
/*
 * RandomStringFactory.cpp
 *
 *  Created on: 27. 3. 2014
 *	  Author: Jan Travnicek
 */

#include "RandomStringFactory.h"

#include <algorithm>
#include <random>
#include <vector>

#include <exception/CommonException.h>

namespace string {

namespace generate {

string::LinearString < > RandomStringFactory::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 < DefaultSymbolType > elems;

	for ( size_t i = 0; i < size; i++ )
		elems.push_back ( DefaultSymbolType ( static_cast < int > ( ext::random_devices::semirandom ( ) % alphabetSize ) ) );

	return string::LinearString < > ( elems );
}

string::LinearString < > RandomStringFactory::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 < DefaultSymbolType > symbols;

	for ( int i = 0; i < 26; i++ ) symbols.push_back ( DefaultSymbolType ( ( char ) ( i + 'a' ) ) );

	if ( randomizedAlphabet ) shuffle ( symbols.begin ( ), symbols.end ( ), ext::random_devices::semirandom );

	std::set < DefaultSymbolType > alphabet ( symbols.begin ( ), symbols.begin ( ) + alphabetSize );

	return RandomStringFactory::generateLinearString ( size, alphabet );
}

string::LinearString < > RandomStringFactory::generateLinearString ( size_t size, std::set < DefaultSymbolType > alphabet ) {

	if ( alphabet.size ( ) > 26 )
		throw exception::CommonException ( "Too big alphabet." );

	if ( alphabet.size ( ) <= 0 )
		throw exception::CommonException ( "Alphabet size must be greater than 0." );

	ext::vector < DefaultSymbolType > alphabetList ( alphabet.begin ( ), alphabet.end ( ) );
	ext::vector < DefaultSymbolType > elems;

	for ( size_t i = 0; i < size; i++ )
		elems.push_back ( alphabetList[ext::random_devices::semirandom ( ) % alphabetList.size ( )] );

	return string::LinearString < > ( elems );
}

} /* namespace generate */
} /* namespace string */