FST  openfst-1.7.1
OpenFst Library
arc.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Commonly used FST arc types.
5 
6 #ifndef FST_ARC_H_
7 #define FST_ARC_H_
8 
9 #include <climits>
10 #include <string>
11 #include <type_traits>
12 #include <utility>
13 
14 
15 #include <fst/expectation-weight.h>
16 #include <fst/float-weight.h>
18 #include <fst/power-weight.h>
19 #include <fst/product-weight.h>
20 #include <fst/signed-log-weight.h>
22 #include <fst/string-weight.h>
23 
24 
25 namespace fst {
26 
27 template <class W>
28 struct ArcTpl {
29  public:
30  using Weight = W;
31  using Label = int;
32  using StateId = int;
33 
38 
39  ArcTpl() noexcept(std::is_nothrow_default_constructible<Weight>::value) {}
40 
41  template <class T>
42  ArcTpl(Label ilabel, Label olabel, T &&weight, StateId nextstate)
43  : ilabel(ilabel),
44  olabel(olabel),
45  weight(std::forward<T>(weight)),
46  nextstate(nextstate) {}
47 
48  static const string &Type() {
49  static const auto *const type =
50  new string(Weight::Type() == "tropical" ? "standard" : Weight::Type());
51  return *type;
52  }
53 };
54 
61 
62 // Arc with integer labels and state IDs and string weights.
63 template <StringType S = STRING_LEFT>
64 struct StringArc {
65  public:
66  using Label = int;
68  using StateId = int;
69 
74 
75  StringArc() = default;
76 
77  template <class W>
78  StringArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
79  : ilabel(ilabel),
80  olabel(olabel),
81  weight(std::forward<W>(weight)),
82  nextstate(nextstate) {}
83 
84  static const string &Type() {
85  static const auto *const type = new string(
86  S == STRING_LEFT ? "left_standard_string"
87  : (S == STRING_RIGHT ? "right_standard_string"
88  : "restricted_standard_string"));
89  return *type;
90  }
91 };
92 
93 // Arc with label and state Id type the same as template arg and with
94 // weights over the Gallic semiring w.r.t the output labels and weights of A.
95 template <class A, GallicType G = GALLIC_LEFT>
96 struct GallicArc {
97  using Arc = A;
98  using Label = typename Arc::Label;
99  using StateId = typename Arc::StateId;
101 
106 
107  GallicArc() = default;
108 
109  template <class W>
110  GallicArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
111  : ilabel(ilabel),
112  olabel(olabel),
113  weight(std::forward<W>(weight)),
114  nextstate(nextstate) {}
115 
116  explicit GallicArc(const Arc &arc)
117  : ilabel(arc.ilabel), olabel(arc.ilabel), weight(arc.olabel, arc.weight),
118  nextstate(arc.nextstate) {}
119 
120  static const string &Type() {
121  static const auto *const type = new string(
122  (G == GALLIC_LEFT
123  ? "left_gallic_"
124  : (G == GALLIC_RIGHT
125  ? "right_gallic_"
126  : (G == GALLIC_RESTRICT
127  ? "restricted_gallic_"
128  : (G == GALLIC_MIN ? "min_gallic_" : "gallic_")))) +
129  Arc::Type());
130  return *type;
131  }
132 };
133 
134 // Arc with the reverse of the weight found in its template arg.
135 template <class A>
136 struct ReverseArc {
137  using Arc = A;
138  using Label = typename Arc::Label;
139  using StateId = typename Arc::StateId;
140  using AWeight = typename Arc::Weight;
141  using Weight = typename AWeight::ReverseWeight;
142 
147 
148  ReverseArc() = default;
149 
150  template <class W>
151  ReverseArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
152  : ilabel(ilabel),
153  olabel(olabel),
154  weight(std::forward<W>(weight)),
155  nextstate(nextstate) {}
156 
157  static const string &Type() {
158  static const auto *const type = new string("reverse_" + Arc::Type());
159  return *type;
160  }
161 };
162 
163 // Arc with integer labels and state IDs and lexicographic weights.
164 template <class Weight1, class Weight2>
166  using Label = int;
167  using StateId = int;
169 
174 
175  LexicographicArc() = default;
176 
177  template <class W>
178  LexicographicArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
179  : ilabel(ilabel),
180  olabel(olabel),
181  weight(std::forward<W>(weight)),
182  nextstate(nextstate) {}
183 
184  static const string &Type() {
185  static const string *const type = new string(Weight::Type());
186  return *type;
187  }
188 };
189 
190 // Arc with integer labels and state IDs and product weights.
191 template <class Weight1, class Weight2>
192 struct ProductArc {
193  using Label = int;
194  using StateId = int;
196 
201 
202  ProductArc() = default;
203 
204  template <class W>
205  ProductArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
206  : ilabel(ilabel),
207  olabel(olabel),
208  weight(std::forward<W>(weight)),
209  nextstate(nextstate) {}
210 
211  static const string &Type() {
212  static const auto *const type = new string(Weight::Type());
213  return *type;
214  }
215 };
216 
217 // Arc with label and state ID type the same as first template argument and with
218 // weights over the n-th Cartesian power of the weight type of the template
219 // argument.
220 template <class A, size_t n>
221 struct PowerArc {
222  using Arc = A;
223  using Label = typename Arc::Label;
224  using StateId = typename Arc::StateId;
226 
231 
232  PowerArc() = default;
233 
234  template <class W>
235  PowerArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
236  : ilabel(ilabel),
237  olabel(olabel),
238  weight(std::forward<W>(weight)),
239  nextstate(nextstate) {}
240 
241  static const string &Type() {
242  static const auto *const type =
243  new string(Arc::Type() + "_^" + std::to_string(n));
244  return *type;
245  }
246 };
247 
248 // Arc with label and state ID type the same as first template argument and with
249 // weights over the arbitrary Cartesian power of the weight type.
250 template <class A, class K = int>
252  using Arc = A;
253  using Label = typename Arc::Label;
254  using StateId = typename Arc::Label;
256 
261 
262  SparsePowerArc() = default;
263 
264  template <class W>
265  SparsePowerArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
266  : ilabel(ilabel),
267  olabel(olabel),
268  weight(std::forward<W>(weight)),
269  nextstate(nextstate) {}
270 
271  static const string &Type() {
272  static const string *const type = [] {
273  string type = Arc::Type() + "_^n";
274  if (sizeof(K) != sizeof(uint32)) {
275  type += "_" + std::to_string(CHAR_BIT * sizeof(K));
276  }
277  return new string(type);
278  }();
279  return *type;
280  }
281 };
282 
283 // Arc with label and state ID type the same as first template argument and with
284 // expectation weight over the first template argument's weight type and the
285 // second template argument.
286 template <class A, class X2>
288  using Arc = A;
289  using Label = typename Arc::Label;
290  using StateId = typename Arc::StateId;
291  using X1 = typename Arc::Weight;
293 
298 
299  ExpectationArc() = default;
300 
301  template <class W>
302  ExpectationArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
303  : ilabel(ilabel),
304  olabel(olabel),
305  weight(std::forward<W>(weight)),
306  nextstate(nextstate) {}
307 
308  static const string &Type() {
309  static const auto *const type =
310  new string("expectation_" + Arc::Type() + "_" + X2::Type());
311  return *type;
312  }
313 };
314 
315 } // namespace fst
316 
317 #endif // FST_ARC_H_
Label olabel
Definition: arc.h:144
typename Arc::Weight X1
Definition: arc.h:291
typename Arc::StateId StateId
Definition: arc.h:99
int Label
Definition: arc.h:193
Label ilabel
Definition: arc.h:70
static const string & Type()
Definition: arc.h:308
W Weight
Definition: arc.h:30
Label ilabel
Definition: arc.h:257
Label ilabel
Definition: arc.h:102
int Label
Definition: arc.h:31
ReverseArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
Definition: arc.h:151
static const string & Type()
Definition: arc.h:184
Label ilabel
Definition: arc.h:227
typename Arc::StateId StateId
Definition: arc.h:224
int StateId
Definition: arc.h:68
SparsePowerArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
Definition: arc.h:265
typename Arc::Label Label
Definition: arc.h:138
Label olabel
Definition: arc.h:198
Label ilabel
Definition: arc.h:197
Label olabel
Definition: arc.h:103
StateId nextstate
Definition: arc.h:173
typename Arc::Label Label
Definition: arc.h:98
Weight weight
Definition: arc.h:72
StateId nextstate
Definition: arc.h:297
Weight weight
Definition: arc.h:259
typename Arc::Label Label
Definition: arc.h:289
Label olabel
Definition: arc.h:35
StringArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
Definition: arc.h:78
static const string & Type()
Definition: arc.h:120
ArcTpl() noexcept(std::is_nothrow_default_constructible< Weight >::value)
Definition: arc.h:39
typename Arc::StateId StateId
Definition: arc.h:139
typename Arc::Label Label
Definition: arc.h:253
Label olabel
Definition: arc.h:228
typename AWeight::ReverseWeight Weight
Definition: arc.h:141
ExpectationArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
Definition: arc.h:302
int StateId
Definition: arc.h:32
int StateId
Definition: arc.h:194
Weight weight
Definition: arc.h:229
static const string & Type()
Definition: arc.h:157
int Label
Definition: arc.h:66
static const string & Type()
Definition: arc.h:211
Label olabel
Definition: arc.h:295
StateId nextstate
Definition: arc.h:73
typename Arc::StateId StateId
Definition: arc.h:290
Weight weight
Definition: arc.h:199
Label ilabel
Definition: arc.h:294
StateId nextstate
Definition: arc.h:260
ArcTpl(Label ilabel, Label olabel, T &&weight, StateId nextstate)
Definition: arc.h:42
Weight weight
Definition: arc.h:145
PowerArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
Definition: arc.h:235
Label olabel
Definition: arc.h:71
typename Arc::Label Label
Definition: arc.h:223
Label ilabel
Definition: arc.h:143
static const string & Type()
Definition: arc.h:84
Label ilabel
Definition: arc.h:34
ProductArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
Definition: arc.h:205
uint32_t uint32
Definition: types.h:31
GallicArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
Definition: arc.h:110
StateId nextstate
Definition: arc.h:37
Weight weight
Definition: arc.h:36
static const string & Type()
Definition: arc.h:48
Weight weight
Definition: arc.h:296
LexicographicArc(Label ilabel, Label olabel, W &&weight, StateId nextstate)
Definition: arc.h:178
GallicArc(const Arc &arc)
Definition: arc.h:116
StateId nextstate
Definition: arc.h:105
Label olabel
Definition: arc.h:258
Weight weight
Definition: arc.h:104
StateId nextstate
Definition: arc.h:146
typename Arc::Label StateId
Definition: arc.h:254
StateId nextstate
Definition: arc.h:230
static const string & Type()
Definition: arc.h:271
typename Arc::Weight AWeight
Definition: arc.h:140
StateId nextstate
Definition: arc.h:200
static const string & Type()
Definition: arc.h:241