An alphabet is a set of symbols. In theory, any object represented in a binary form can be seen as in a set of symbols, with all possible valid bit patterns in the set. The math::alphabet class represents a bounded set of symbols explicitly. This makes it possible to
There is an additional twist to math::alphabet. The symbols can be of two types:
Alphabet of symbols.
This deals with two types of symbols: normal symbols, and special symbols. Normal symbols are all of one type, but have different values. For example, they can be words in a word list. Special symbols are compile-time symbols: they have different types, but no meaningful value. Objects of these types must always compare equal to each other. For example, these could be the “empty” symbol, which might be program-internal.
Symbols are assigned a dense (integer) type, too. The first normal symbol is assigned a value of 0, the second of 1, et cetera. Special symbols are assigned negative values from -1 down. This makes debugging easier.
The integer type is chosen automatically as the smallest integer that can contain the set. By default, space is reserved in a 32-bit integer with 128 special symbols.
Normal symbols are added to an alphabet at run time. To make sure that they cannot be accidentally re-used, symbols cannot be removed from an alphabet.
Special symbols are added at compile time, as it were, with the add_special_symbol function, which returns the alphabet with the extra special symbol type. This alphabet shares the set of normal symbols with its parent. If a normal symbol is added to its parent, the symbol is at the same tim added to it.
The type for normal symbols.
An arbitrary type that can be used to distinguish alphabets at compile time. The default value is void.
The number of normal symbols to reserve room for. The default value uses all of 32 bits, except for 129 symbols.
The special symbol types contained in the alphabet, as a meta range. The default value is meta::vector<>.
The maximum number of special symbols. The default value is 128.
Public Type
Signed integer type that is used for dense symbols.
The normal symbol type. Equal to NormalSymbol.
The dense symbol type. This is an object type, with inside the smallest integer type that can fit all symbols in the set.
Public Functions
Construct an alphabet that is empty apart from any special symbols.
Add a new normal symbol to the alphabet.
if there is no room in the alphabet.
The representation. This can be of the normal symbol type, or of any of the special symbol types.
Check whether an dense symbol represents a specific type. If SuspectedSymbol is a compile-time symbol and dense is a compile-time representation, the result is a compile-time constant.
The type that it is suspected the symbol might represent.
The symbol to be checked. This can be a run-time or a compile-time symbol.
Retrieve the symbol represented by the dense symbol passed in.
SuspectedSymbol is the actual type of the symbol. If this is not the case, then an assertion may be triggered.
The dense symbol exists, at run time, in the alphabet. If this is not the case, symbol_not_found_of will be thrown.
The return type, which should be the actual type of the symbol.
Dispatch a function with two parameters: the symbol type corresponding to symbol wrapped as an symbol_type object, and the original symbol.
Represent a symbol from an alphabet with an integer. There is usually no reason to explicitly use this class; alphabet will normally produce objects of this class.
This class has all comparison operators. The ordering is not meaningful: all it does is provide a strict weak ordering (which corresponds to the strict weak ordering on the underlying id’s). There is no relation to the ordering of the normal symbols.
This class also supports boost::hash.
The integer type that the symbol is mapped to. This type can also be a compile-time constant (normally rime::constant), if the value is known at compile time, which is often the case for special symbols.
A user-defined tag to distinguish alphabets at compile time. Must be the same as the Tag template parameter of the matching alphabet.
Public Functions
Copy constructor that takes a compile-time symbol if this is a run-time symbol.
Return the dense id of the symbol. This always returns an object, not an reference: the object may be constructed on the fly.
Return a new alphabet with a new special symbol added to it. If the alphabet already contains the symbol, then return the alphabet itself.
Exception that is thrown on an attempt to add another element to an alphabet when it is full.
Exception that can be thrown when a symbol is not contained in an alphabet. An object should normally be of derived type symbol_not_found_of.
Exception that can be thrown when a symbol is not contained in an alphabet.
The type of the symbol that was not found.
Public Functions