1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Finite State Transducer (FST) implementations for weighted automata.
//!
//! This module provides a comprehensive suite of FST implementations optimized for
//! different use cases in speech recognition, natural language processing, and
//! computational linguistics. The implementations are based on the theoretical
//! framework of weighted finite-state transducers as described in the seminal
//! work by Mohri and colleagues.
//!
//! # Theoretical Background
//!
//! A weighted finite-state transducer (WFST) is a finite automaton where each
//! transition carries an input label, an output label, and a weight from a
//! semiring. Formally, a WFST $`T`$ over a semiring $`(K, \oplus, \otimes, \bar{0}, \bar{1})`$
//! is defined as an 8-tuple $`T = (\Sigma, \Delta, Q, I, F, E, \lambda, \rho)`$ where:
//!
//! - $`\Sigma`$ is the finite input alphabet
//! - $`\Delta`$ is the finite output alphabet
//! - $`Q`$ is a finite set of states
//! - $`I \subseteq Q`$ is the set of initial states
//! - $`F \subseteq Q`$ is the set of final states
//! - $`E \subseteq Q \times (\Sigma \cup \{\epsilon\}) \times (\Delta \cup \{\epsilon\}) \times K \times Q`$
//! is a finite set of transitions
//! - $`\lambda: I \to K`$ is the initial weight function
//! - $`\rho: F \to K`$ is the final weight function
//!
//! # FST Types
//!
//! ## [`VectorFst`] - Mutable Vector-Based FST
//!
//! The primary mutable FST implementation using dynamic vectors for state and arc
//! storage. Provides $`O(1)`$ amortized insertion and $`O(1)`$ random access.
//!
//! - **Use case:** General-purpose FST construction and modification
//! - **Performance:** Fast random access, $`O(1)`$ state/arc insertion (amortized)
//! - **Memory:** Moderate overhead with dynamic growth
//! - **Best for:** Building FSTs incrementally, algorithm development
//!
//! ## [`ConstFst`] - Immutable Optimized FST
//!
//! Read-only FST with contiguous memory layout for optimal cache performance.
//! Derived from `VectorFst` after construction is complete.
//!
//! - **Use case:** Production deployment of finalized FSTs
//! - **Performance:** Excellent traversal speed with $`O(1)`$ state access
//! - **Memory:** 15-25% less than `VectorFst` with better cache locality
//! - **Best for:** Large read-only FSTs, production systems
//!
//! ## [`CompactFst`] - Memory-Efficient Compressed FST
//!
//! Compression-oriented FST using pluggable compaction strategies for minimal
//! memory footprint. Trades computation for memory savings.
//!
//! - **Use case:** Memory-constrained environments, very large FSTs
//! - **Performance:** Slower access due to decompression overhead
//! - **Memory:** 40-70% reduction compared to `VectorFst`
//! - **Best for:** Mobile deployment, embedded systems, storage optimization
//!
//! ## [`CacheFst`] - Caching Wrapper
//!
//! Thread-safe caching wrapper for any FST implementation. Caches computed
//! arcs and weights for accelerated repeated access patterns.
//!
//! - **Use case:** Expensive computations with locality of reference
//! - **Performance:** $`O(1)`$ for cached accesses, base FST cost for misses
//! - **Memory:** Base FST size plus cache overhead
//! - **Best for:** Wrapping lazy FSTs, composition results
//!
//! ## [`LazyFstImpl`] - On-Demand Computation
//!
//! Lazy FST implementation that computes states dynamically using a user-provided
//! function. Enables handling of potentially infinite state spaces.
//!
//! - **Use case:** Dynamic FST generation, search space exploration
//! - **Performance:** Varies by computation function complexity
//! - **Memory:** $`O(\text{accessed states})`$, not full FST size
//! - **Best for:** Large composition chains, grammar intersection
//!
//! ## [`LazyComposeFst`] - On-the-fly Composition
//!
//! Specialized lazy FST for computing composition results on demand without
//! materializing the full composed automaton.
//!
//! - **Use case:** Composition where only a subset of paths is needed
//! - **Performance:** 10-100x faster than full materialization for sparse access
//! - **Memory:** Only stores accessed state pairs
//! - **Best for:** Shortest path through composed FSTs, beam search
//!
//! ## [`ConcurrentFst`] - Thread-Safe Mutable FST
//!
//! Lock-free concurrent FST implementation for multi-threaded construction
//! and traversal. Uses epoch-based memory reclamation.
//!
//! - **Use case:** Parallel FST construction and concurrent access
//! - **Performance:** Near-linear scaling for read operations
//! - **Memory:** Higher overhead due to synchronization primitives
//! - **Best for:** Server applications, parallel algorithms
//!
//! ## [`CsrFst`] - Cache-Optimized CSR Format
//!
//! Compressed Sparse Row format with Structure-of-Arrays layout for
//! SIMD-friendly traversal and optimal cache utilization.
//!
//! - **Use case:** High-performance batch processing
//! - **Performance:** Excellent for sequential traversal, SIMD-accelerated
//! - **Memory:** Minimal overhead with cache-line alignment
//! - **Best for:** Large-scale decoding, parallel shortest path
//!
//! ## [`FailureFst`] - Failure Transition Wrapper
//!
//! Wrapper that adds Aho-Corasick style failure transitions to any FST
//! for compact representation of large automata.
//!
//! - **Use case:** Pattern matching, dictionary lookup
//! - **Performance:** Efficient failure arc traversal
//! - **Memory:** Base FST plus failure transition map
//! - **Best for:** Multiple pattern matching, large lexicons
//!
//! # Choosing an FST Type
//!
//! ```
//! use arcweight::prelude::*;
//! use arcweight::fst::CacheFst;
//!
//! // For building and modifying FSTs
//! let mut mutable_fst = VectorFst::<TropicalWeight>::new();
//! mutable_fst.add_state();
//!
//! // For read-only, high-performance access
//! let const_fst = ConstFst::from_fst(&mutable_fst)?;
//!
//! // For caching expensive operations
//! let cached_fst = CacheFst::new(const_fst);
//! # Ok::<(), arcweight::Error>(())
//! ```
//!
//! # Core Traits
//!
//! All FST types implement the core [`Fst`] trait for read operations,
//! while mutable types also implement [`MutableFst`] for modifications.
//! Some types implement [`ExpandedFst`] for direct slice access to arcs.
//!
//! # References
//!
//! - Mohri, M. (1997). Finite-State Transducers in Language and Speech Processing.
//! *Computational Linguistics*, 23(2), 269-311.
//!
//! - Mohri, M., Pereira, F., & Riley, M. (2002). Weighted Finite-State Transducers
//! in Speech Recognition. *Computer Speech & Language*, 16(1), 69-88.
//!
//! - Allauzen, C., Riley, M., Schalkwyk, J., Skut, W., & Mohri, M. (2007).
//! OpenFst: A General and Efficient Weighted Finite-State Transducer Library.
//! In *Proc. CIAA 2007*, LNCS 4783, pp. 11-23. Springer.
pub use CacheFst;
pub use ;
pub use ;
pub use ConstFst;
pub use ;
pub use CsrFst;
pub use FailureFst;
pub use ;
pub use ;
pub use *;
pub use VectorFst;