13 breaking releases

0.78.0 Oct 29, 2021
0.76.0 Aug 2, 2021
0.75.0 Jun 9, 2021
0.72.0 Mar 16, 2021
0.2.0 Jun 11, 2020

#1149 in Algorithms


Used in 3 crates (2 directly)

Apache-2.0 WITH LLVM-exception

59KB
980 lines

Finite-state transducer automata.

A transducer is a type of automata that has not only an input that it accepts or rejects, but also an output. While regular automata check whether an input string is in the set that the automata accepts, a transducer maps the input strings to values. A regular automata is sort of a compressed, immutable set, and a transducer is sort of a compressed, immutable key-value dictionary. A trie compresses a set of strings or map from a string to a value by sharing prefixes of the input string. Automata and transducers can compress even better: they can share both prefixes and suffixes. Index 1,600,000,000 Keys with Automata and Rust by Andrew Gallant (aka burntsushi) is a top-notch introduction.

If you're looking for a general-purpose transducers crate in Rust you're probably looking for the fst crate. While this implementation is fully generic and has no dependencies, its feature set is specific to peepmatic's needs:

  • We need to associate extra data with each state: the match operation to evaluate next.

  • We can't provide the full input string up front, so this crate must support incremental lookups. This is because the peephole optimizer is computing the input string incrementally and dynamically: it looks at the current state's match operation, evaluates it, and then uses the result as the next character of the input string.

  • We also support incremental insertion and output when building the transducer. This is necessary because we don't want to emit output values that bind a match on an optimization's left-hand side's pattern (for example) until after we've succeeded in matching it, which might not happen until we've reached the n^th state.

  • We need to support generic output values. The fst crate only supports u64 outputs, while we need to build up an optimization's right-hand side instructions.

This implementation is based on Direct Construction of Minimal Acyclic Subsequential Transducers by Mihov and Maurel. That means that keys must be inserted in lexicographic order during construction.

Dependencies

~165KB