This object caches certain calculation results (f.i. the number of possible combinations for a certain string length), thus greatly speeding up sequentiell Math::String conversations from string to number, and vice versa.

With a simple charset, converting between the number and string is relatively simple and straightforward, albeit slow.

With bigrams, this becomes even more complex. But since all the information on how to convert between number and string in inside the charset definition, Math::String::Charset will produce (and sometimes cache) this information. Thus Math::String is simple a hull around Math::String::Charset and it's subclasses and Math::BigInt.

Starting characters for each string can be 'a','c','b' and 'd' (in that order). Each 'a' can be followed by either 'b', 'c' or 'a' (again in that order), each 'c can be followed by either 'c', 'd' (again in that order), and each 'b' or 'd' can be followed by an 'a' (and nothing else).

This means that each character in a string depends on the previous character. Please note that the probabilities on which characters follows how often which character do not concern us here. We simple enumerate them all. Or put differently: each probability is 1.

The first way is based on the observation that the number of strings in class n+1 only depends on the number of ending chars in class n, and nothing else.

This is, however, not used in the current implemenation, since there is a slightly faster/simpler way based on the count of strings that start with a given character in class n, n-1, n-2 etc. See below for a description.

As you can see, there is one 'a', one 'c', one 'b' and one 'd'. To determine how many strings are in class 2, we must multiply the occurances of each character by the number of how many characters it is followed:

a * 3 + c * 2 + d * 1 + b * 1

which equals

1 * 3 + 1 * 2 + 1 * 1 + 1 * 1

If we summ this all up, we get 3+2+1+1 = 7, which is exactly the number of strings in class 2. But to determine now the number of strings in class 3, we must now how many strings in class 2 end on 'a', how many on 'b' etc.

We can do this in the same loop, by not only keeping a sum, but by counting all the different endings. F.i. exactly one string ended in 'a' in class 1. Since 'a' can be followed by 3 characters, for each character we know that it will occure at least 1 time. So we add the 1 to the character in question.

$new_count->{'b'} += $count->{'a'};

This yields the amounts of strings that end in 'b' in the next class.

We have to do this for every different starting character, and for each of the characters that follows each starting character. In the worst case this means M*M steps, while M is the length of the charset. We must repeat this for each of the classes, so that the complexity becomes O(N*M*M) in the worst case. For strings of higher order this gets worse, adding a *M for each higher order.

For our example, after processing 'a', we will have the following counts for ending chars in class 2:

b => 1
c => 1
a => 1

After processing 'c', it is:

b => 1
c => 2 (+1)
a => 1
d => 1 (+1)

because 'c' is followed by 'd' or 'c'. When we are done with all characters, the following count's are in our $new_count hash:

b => 1
c => 2
a => 3
d => 1

When we sum them up, we get the count of strings in class 2. For class 3, we start with an empty count hash again, and then again for each character process the ones that follow it. Example for a:

b => 0
c => 0
a => 0
d => 0

3 times ending in 'a' followed by 'b','c' or 'd':

b => 3 (+3)
c => 3 (+3)
a => 3 (+3)
d => 0

2 times ending 'c' followed by 'c' or 'd':

b => 3
c => 5 (+2)
a => 3
d => 2 (+2)

After processing 'b' and 'd' in a similiar manner we get:

b => 3
c => 5
a => 5
d => 2

The sum is 15, and we know now that we have 15 different strings in class 3. The process for higher classes is the same again, re-using the counts from the lower class.

The second, and implemented method counts for each class how many strings start with a given character. This gives us two information at once:

A string of length N and a starting char of X, which number it must have at minimum (by summing up the counts of all strings that come before X) and how many strings are there starting with X (although this is not used for X, but only for all strings that come after X).

How many strings are there with a given length, by summing up all the counts for the different starting chars.

This method also has the advantage that it doesn't need to re-calculate the count for each level. If we have cached the information for class 7, we can calculate class 8 right-away. The old method would either need to start at class 1, working up to 8 again, or cache additional information of the order N (where N is the number of different characters in the charset).

As you can see, for length one, there is exactly one string for each starting character.

For the next class, we can find out how many strings start with a given char, by adding together all the counts of strings in the previous class.

F.i. in class 3, there are 6 strings starting with 'a'. We find this out by adding together 1 (there is 1 string starting with 'b' in class 2), 2 (there are two strings starting with 'c' in class 2) and 3 (three strings starting with 'a' in class 2).

As a special case we must throw away all strings in class 2 that have invalid ending characters. By doing this, we automatically have restricted all strings to only valid ending characters. Therefore, class 1 and 2 are setup upon creating the charset object, the others are calculated on-demand and then cached.

Since we are calculating the strings in the order of the starting characters, we can sum up all strings up to this character.

From the section above we know that we can find out which number a string of a certain class has at minimum and at maximum. But what number has the string in that range, actually?

Well, given the information it is easy. First, find out which minimum number a string has with the given starting character in the class. Add this to it's base number. Then reduce the class by one, look at the next character and repeat this. In pseudo code:

As you can see, there is a (independend) tree for each of the starting characters, which in turn contains independed sub-trees for each string in the next class etc. It is interesting to note that each string deeper in the tree starts with the same common starting string, aka 'd', 'da', 'dab' etc.

With a simple charset, all these trees contain the same number of nodes. With higher order charsets, this is no longer true.

start contains an array reference to all valid ending characters, e.g. no valid string can end with a character not listed here. Note that strings of length 1 start and end with their only character, so the character must be listed in end and start to produce a string with one character. Also all characters that are not followed by any other character are added silently to the end set.

Optional minimum string length. Any string shorter than this will be invalid. Must be shorter than maxlen. If not given is set to -inf.

Note that the minlen might be adjusted to a greater number, if it is set to 1 or greater, but there are not valid strings with 2,3 etc. In this case the minlen will be set to the first non-empty class of the charset.

Returns the count of all possible strings described by the charset as a positive BigInt. Returns 'inf' if no maxlen is defined, because there should be no upper bound on how many strings are possible. (This might change if we can calculate an upper bound - not sure if this is possible with bigrams).

If maxlen is defined, forces a calculation of all possible class() values and may therefore be very slow on the first call, it also caches possible lot's of values.

In list context, returns a list of all characters in the start set, for simple charsets (e.g. no bi, tri-grams etc) simple returns the charset. In scalar context returns the lenght of the start set.

Note that the returned end set can be differen from what you specified upon constructing the charset, because characters that are not followed by any other character will be excluded from the start set (they can't possible start a string longer than one character).

Think of the start set as the set of all characters that can start a string with more than one character. The set for one character strings is called ones and you can access if via ones().

In list context, returns a list of all characters in the end set, aka all characters a string can end with. For simple charsets (e.g. no bi, tri-grams etc) simple returns the charset. In scalar context returns the lenght of the end set.

Note that the returned end set can be differen from what you specified upon constructing the charset, because characters that are not followed by any other character will be included in the end set, too.

In list context, returns a list of all strings consisting of one character, for simple charsets (e.g. no bi, tri-grams etc) simple returns the charset. In scalar context returns the lenght of the ones set.

This list is the cross of start and end that is calculated after adding characters with no followers to end, but before removing the characters with no followers from start.

Think of a string of only one character as if it starts with and ends in this character at the same time. For instance, if you have the following definition:

Give the charset and a string, calculates the previous string in the sequence. This is faster than decrementing the number of the string and converting the new number to a string. This routine is mainly used internally by Math::String and updates the cache of the given Math::String.

Give the charset and a string, calculates the next string in the sequence. This is faster than incrementing the number of the string and converting the new number to a string. This routine is mainly used internally by Math::String and updates the cache of the given Math::String.