tgen
Loading...
Searching...
No Matches

String generator. More...

Inheritance diagram for tgen::str:

Classes

struct  value
 String value. More...

Public Member Functions

 str (int size, char value_left='a', char value_right='z')
 Creates string generator defined by size and range of characters.
 str (int size, std::set< char > chars)
 Creates string generator defined by character set.
template<typename... Args>
 str (const std::string &regex, Args &&...args)
 Creates string generator defined by regex.
strfix (int idx, char character)
 Restricts generator s.t. character at index is fixed.
strequal (std::set< int > indices)
 Restricts generator s.t. all characters in index set are equal.
strequal (int idx_1, int idx_2)
 Restricts generator s.t. characters at two indices are equal.
strequal_range (int left, int right)
 Restricts generator s.t. all characters at index range are equal.
strall_equal ()
 Restricts generator s.t. all values are equal.
strpalindrome (int left, int right)
 Restricts generator s.t. range is a palindrome.
strpalindrome ()
 Restricts generator s.t. string is a palindrome.
strdifferent (std::set< int > indices)
 Restricts generator s.t. all characters in index set are different.
strdifferent (int idx_1, int idx_2)
 Restricts generator s.t. characters at two indices are different.
strdifferent_range (int left, int right)
 Restricts generator s.t. all characters at index range are different.
strall_different ()
 Restricts generator s.t. all characters are different.
value gen () const
 Generates a random value from the set of valid strings.
Public Member Functions inherited from tgen::gen_base< str >
auto gen_list (int size, Args &&...args) const
 Generates a list of several generation calls.
auto gen_until (Pred predicate, int max_tries, Args &&...args) const
 Generates a random value from the valid set until a condition is met.
auto distinct (Args &&...args) const
 Creates distinct generator for current generator.

Detailed Description

String generator.

Defines a set of strings, subject to restrictions.

A uniformly random tgen::str::value from this set of strings (that satisfies the restrictions) can be generated with tgen::str::gen.

Definition at line 3076 of file tgen.h.

Constructor & Destructor Documentation

◆ str() [1/3]

tgen::str::str ( int size,
char value_left = 'a',
char value_right = 'z' )
inline

Creates string generator defined by size and range of characters.

Parameters
sizeSize of the string.
value_leftLeft endpoint of character range.
value_rightRight endpoint of character range.

Defines a generator of strings of length size with characters in [value_left, value_right].

Examples

// Strings of lowercase letters of size 10.
auto str_gen = tgen::str(10, 'a', 'z');
String generator.
Definition tgen.h:3076

Definition at line 3083 of file tgen.h.

◆ str() [2/3]

tgen::str::str ( int size,
std::set< char > chars )
inline

Creates string generator defined by character set.

Parameters
sizeSize of the string.
charsCharacter set.

Defines a generator of strings of length size with characters in chars.

Examples

// Strings size 10 with characters that are either 'a', 'e', or 'i'.
auto seq_gen = tgen::str(10, {'a', 'e', 'i'});

Definition at line 3088 of file tgen.h.

◆ str() [3/3]

template<typename... Args>
tgen::str::str ( const std::string & regex,
Args &&... args )
inline

Creates string generator defined by regex.

Parameters
regexRegex.
argsArguments for the regex.

Defines a generator of strings generated by regex (see Strings), with the given args.

Examples

// Numbers from from 0 up to 10^30.
auto leq1e30 = tgen::str("0 | [1-9][0-9]{0,%d} | 10{%d}", 30-1, 30);

Definition at line 3091 of file tgen.h.

Member Function Documentation

◆ all_different()

str & tgen::str::all_different ( )
inline

Restricts generator s.t. all characters are different.

Equivalent to tgen::str::different({0, 1, ... size-1}).

Note
If you add this restriction, do not add another tgen::str::different restriction! Generation might fail otherwise.
Exceptions
std::runtime_errorif the generator was created from a regex (restrictions cannot be added for regex).

Examples

// Strings consisting of 10 different lowercase letters.
auto str_gen tgen::str(10, 'a', 'z').all_different();
str & all_different()
Restricts generator s.t. all characters are different.
Definition tgen.h:3172

Definition at line 3172 of file tgen.h.

◆ all_equal()

str & tgen::str::all_equal ( )
inline

Restricts generator s.t. all values are equal.

Equivalent to tgen::str::equal({0, 1, ... size-1}).

Examples

// Strings of 5 equal chars from 'a' to 'd'.
auto str_gen = tgen::str(5, 'a', 'd').all_equal();
str & all_equal()
Restricts generator s.t. all values are equal.
Definition tgen.h:3127

Definition at line 3127 of file tgen.h.

◆ different() [1/2]

str & tgen::str::different ( int idx_1,
int idx_2 )
inline

Restricts generator s.t. characters at two indices are different.

Parameters
idx_1First index.
idx_2Second index.
Exceptions
std::runtime_errorif the generator was created from a regex (restrictions cannot be added for regex).

Equivalent to tgen::str::different({idx_1, idx_2}).

Examples

// Strings of 10 lowercase letters with the first and last characters different.
auto str_gen = tgen::str(10, 'a', 'z').different(0, 9);
str & different(std::set< int > indices)
Restricts generator s.t. all characters in index set are different.
Definition tgen.h:3151

Definition at line 3158 of file tgen.h.

◆ different() [2/2]

str & tgen::str::different ( std::set< int > indices)
inline

Restricts generator s.t. all characters in index set are different.

Parameters
indicesIndex set.
Note
Generation might fail if restrictions are considered to be too complex! The restrictions are considered too complex if the following condition does not hold. Consider the graph defined with vertices as the tgen::str::different sets added, and there is an edge between two vertices if they share an index. Note that you need to consider indices (directed or indirectly) connected by equality restrictions as being the same. This resulting graph must be a acyclic. Moreover, for every tree in this graph, all of its indices with a tgen::str::set operation must be covered by a single "different" restriction set.
Exceptions
std::runtime_errorif the generator was created from regex (restrictions cannot be added for regex).

Examples

// Strings of 10 lowercase letters with first 3 characters distinct.
auto str_gen = tgen::str(10, 'a', 'z').different({0, 1, 2});

Definition at line 3151 of file tgen.h.

◆ different_range()

str & tgen::str::different_range ( int left,
int right )
inline

Restricts generator s.t. all characters at index range are different.

Parameters
leftLeft endpoint of index range (inclusive).
rightRight endpoint of index range (inclusive).

Equivalent to tgen::str::different({left, left+1, ... , right}).

Examples

// Strings of 10 characters with the first half all different.
auto str_gen = tgen::str(10, 'a', 'z').different_range(0, 4);
str & different_range(int left, int right)
Restricts generator s.t. all characters at index range are different.
Definition tgen.h:3165

Definition at line 3165 of file tgen.h.

◆ equal() [1/2]

str & tgen::str::equal ( int idx_1,
int idx_2 )
inline

Restricts generator s.t. characters at two indices are equal.

Parameters
idx_1First index.
idx_2Second index.

Equivalent to tgen::str::equal({idx_1, idx_2}).

Exceptions
std::runtime_errorif the generator was created from a regex (restrictions cannot be added for regex).

Examples

// Strings of 7 lowercase letters that start and end with the same character.
auto str_gen = tgen::str(7, '0', '9').equal(0, 6);
str & equal(std::set< int > indices)
Restricts generator s.t. all characters in index set are equal.
Definition tgen.h:3106

Definition at line 3113 of file tgen.h.

◆ equal() [2/2]

str & tgen::str::equal ( std::set< int > indices)
inline

Restricts generator s.t. all characters in index set are equal.

Parameters
indicesIndex set.
Exceptions
std::runtime_errorif the generator was created from a regex (restrictions cannot be added for regex).

Examples

// Strings of 10 chars with the first half equal.
auto str_gen = tgen::str(10, 'a', 'z').equal({0, 1, 2, 3, 4});

Definition at line 3106 of file tgen.h.

◆ equal_range()

str & tgen::str::equal_range ( int left,
int right )
inline

Restricts generator s.t. all characters at index range are equal.

Parameters
leftLeft endpoint of index range (inclusive).
rightRight endpoint of index range (inclusive).

Equivalent to tgen::str::equal({left, left+1, ... , right}).

Exceptions
std::runtime_errorif the generator was created from a regex (restrictions cannot be added for regex).

Examples

// Strings of 10 chars with the first half equal.
auto str_gen = tgen::str(10, 'a', 'z').equal_range(0, 4);
str & equal_range(int left, int right)
Restricts generator s.t. all characters at index range are equal.
Definition tgen.h:3120

Definition at line 3120 of file tgen.h.

◆ fix()

str & tgen::str::fix ( int idx,
char character )
inline

Restricts generator s.t. character at index is fixed.

Parameters
idxIndex.
characterCharacter.
Exceptions
std::runtime_errorif the generator was created from a regex (restrictions cannot be added for regex).

Examples

// Strings of 5 lowercase letters starting with 'x'.
auto str_gen = tgen::str(5, 'a', 'z').fix(0, 'x');
str & fix(int idx, char character)
Restricts generator s.t. character at index is fixed.
Definition tgen.h:3099

Definition at line 3099 of file tgen.h.

◆ gen()

value tgen::str::gen ( ) const
inline

Generates a random value from the set of valid strings.

Returns
A uniformly random tgen::str::value satisfying the restrictions, or (for regex generators) a random match according to the regex semantics described in Strings.
Note
For regex, the generation is only guaranteed to be uniform if there is a unique way to generate each string generated by the regex (see Strings).
Exceptions
std::runtime_errorif there is no valid string satisfying all restrictions.

Complexity

If created from restrictions: O(n log n).

If created from regex: expected linear.

Examples

// Prints a random number from 0 to 10^3.
std::cout << tgen::str("0 | [1-9][0-9]{0,%d} | 10{%d}", 3-1, 3).gen() << std::endl;
value gen() const
Generates a random value from the set of valid strings.
Definition tgen.h:3252

Definition at line 3252 of file tgen.h.

◆ palindrome() [1/2]

str & tgen::str::palindrome ( )
inline

Restricts generator s.t. string is a palindrome.

Equivalent to tgen::str::palindrome(0, size - 1).

Exceptions
std::runtime_errorif the generator was created from a regex (restrictions cannot be added for regex).

Examples

// Palindromes of 10 lowercase letters.
auto str_gen = tgen::str(10, 'a', 'z').palindrome();
str & palindrome(int left, int right)
Restricts generator s.t. range is a palindrome.
Definition tgen.h:3134

Definition at line 3144 of file tgen.h.

◆ palindrome() [2/2]

str & tgen::str::palindrome ( int left,
int right )
inline

Restricts generator s.t. range is a palindrome.

Parameters
leftLeft endpoint of the index range (inclusive).
rightRight endpoint of the index range (inclusive).
Exceptions
std::runtime_errorif the generator was created from a regex (restrictions cannot be added for regex).

Examples

// Strings of 10 lowercase letters that are the concatenation of two length 5 palindromes.
auto str_gen = tgen::str(10, 'a', 'z').palindrome(0, 4).palindrome(5, 9);

Definition at line 3134 of file tgen.h.


The documentation for this struct was generated from the following files:
  • /home/runner/work/tgen/tgen/single_include/tgen.h
  • str.dox