FST  openfst-1.8.1
OpenFst Library
fst-class.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 
18 #ifndef FST_SCRIPT_FST_CLASS_H_
19 #define FST_SCRIPT_FST_CLASS_H_
20 
21 #include <algorithm>
22 #include <istream>
23 #include <limits>
24 #include <memory>
25 #include <string>
26 #include <type_traits>
27 
28 #include <fst/expanded-fst.h>
29 #include <fst/fst.h>
30 #include <fst/generic-register.h>
31 #include <fst/mutable-fst.h>
32 #include <fst/vector-fst.h>
33 #include <fst/script/arc-class.h>
35 
36 // Classes to support "boxing" all existing types of FST arcs in a single
37 // FstClass which hides the arc types. This allows clients to load
38 // and work with FSTs without knowing the arc type. These classes are only
39 // recommended for use in high-level scripting applications. Most users should
40 // use the lower-level templated versions corresponding to these classes.
41 
42 namespace fst {
43 namespace script {
44 
45 // Abstract base class defining the set of functionalities implemented in all
46 // impls and passed through by all bases. Below FstClassBase the class
47 // hierarchy bifurcates; FstClassImplBase serves as the base class for all
48 // implementations (of which FstClassImpl is currently the only one) and
49 // FstClass serves as the base class for all interfaces.
50 
51 class FstClassBase {
52  public:
53  virtual const std::string &ArcType() const = 0;
54  virtual WeightClass Final(int64) const = 0;
55  virtual const std::string &FstType() const = 0;
56  virtual const SymbolTable *InputSymbols() const = 0;
57  virtual size_t NumArcs(int64) const = 0;
58  virtual size_t NumInputEpsilons(int64) const = 0;
59  virtual size_t NumOutputEpsilons(int64) const = 0;
60  virtual const SymbolTable *OutputSymbols() const = 0;
61  virtual uint64 Properties(uint64, bool) const = 0;
62  virtual int64 Start() const = 0;
63  virtual const std::string &WeightType() const = 0;
64  virtual bool ValidStateId(int64) const = 0;
65  virtual bool Write(const std::string &) const = 0;
66  virtual bool Write(std::ostream &, const std::string &) const = 0;
67  virtual ~FstClassBase() {}
68 };
69 
70 // Adds all the MutableFst methods.
72  public:
73  virtual bool AddArc(int64, const ArcClass &) = 0;
74  virtual int64 AddState() = 0;
75  virtual void AddStates(size_t) = 0;
76  virtual FstClassImplBase *Copy() = 0;
77  virtual bool DeleteArcs(int64, size_t) = 0;
78  virtual bool DeleteArcs(int64) = 0;
79  virtual bool DeleteStates(const std::vector<int64> &) = 0;
80  virtual void DeleteStates() = 0;
81  virtual SymbolTable *MutableInputSymbols() = 0;
82  virtual SymbolTable *MutableOutputSymbols() = 0;
83  virtual int64 NumStates() const = 0;
84  virtual bool ReserveArcs(int64, size_t) = 0;
85  virtual void ReserveStates(int64) = 0;
86  virtual void SetInputSymbols(const SymbolTable *) = 0;
87  virtual bool SetFinal(int64, const WeightClass &) = 0;
88  virtual void SetOutputSymbols(const SymbolTable *) = 0;
89  virtual void SetProperties(uint64, uint64) = 0;
90  virtual bool SetStart(int64) = 0;
91  ~FstClassImplBase() override {}
92 };
93 
94 // Containiner class wrapping an Fst<Arc>, hiding its arc type. Whether this
95 // Fst<Arc> pointer refers to a special kind of FST (e.g. a MutableFst) is
96 // known by the type of interface class that owns the pointer to this
97 // container.
98 
99 template <class Arc>
101  public:
102  explicit FstClassImpl(std::unique_ptr<Fst<Arc>> impl)
103  : impl_(std::move(impl)) {}
104 
105  explicit FstClassImpl(const Fst<Arc> &impl) : impl_(impl.Copy()) {}
106 
107  // Warning: calling this method casts the FST to a mutable FST.
108  bool AddArc(int64 s, const ArcClass &ac) final {
109  if (!ValidStateId(s)) return false;
110  // Note that we do not check that the destination state is valid, so users
111  // can add arcs before they add the corresponding states. Verify can be
112  // used to determine whether any arc has a nonexisting destination.
113  Arc arc(ac.ilabel, ac.olabel, *ac.weight.GetWeight<typename Arc::Weight>(),
114  ac.nextstate);
115  fst::down_cast<MutableFst<Arc> *>(impl_.get())->AddArc(s, arc);
116  return true;
117  }
118 
119  // Warning: calling this method casts the FST to a mutable FST.
120  int64 AddState() final {
121  return fst::down_cast<MutableFst<Arc> *>(impl_.get())->AddState();
122  }
123 
124  // Warning: calling this method casts the FST to a mutable FST.
125  void AddStates(size_t n) final {
126  return fst::down_cast<MutableFst<Arc> *>(impl_.get())->AddStates(n);
127  }
128 
129  const std::string &ArcType() const final { return Arc::Type(); }
130 
131  FstClassImpl *Copy() final { return new FstClassImpl<Arc>(*impl_); }
132 
133  // Warning: calling this method casts the FST to a mutable FST.
134  bool DeleteArcs(int64 s, size_t n) final {
135  if (!ValidStateId(s)) return false;
136  fst::down_cast<MutableFst<Arc> *>(impl_.get())->DeleteArcs(s, n);
137  return true;
138  }
139 
140  // Warning: calling this method casts the FST to a mutable FST.
141  bool DeleteArcs(int64 s) final {
142  if (!ValidStateId(s)) return false;
143  fst::down_cast<MutableFst<Arc> *>(impl_.get())->DeleteArcs(s);
144  return true;
145  }
146 
147  // Warning: calling this method casts the FST to a mutable FST.
148  bool DeleteStates(const std::vector<int64> &dstates) final {
149  for (const auto &state : dstates)
150  if (!ValidStateId(state)) return false;
151  // Warning: calling this method with any integers beyond the precision of
152  // the underlying FST will result in truncation.
153  std::vector<typename Arc::StateId> typed_dstates(dstates.size());
154  std::copy(dstates.begin(), dstates.end(), typed_dstates.begin());
155  fst::down_cast<MutableFst<Arc> *>(impl_.get())->DeleteStates(typed_dstates);
156  return true;
157  }
158 
159  // Warning: calling this method casts the FST to a mutable FST.
160  void DeleteStates() final {
161  fst::down_cast<MutableFst<Arc> *>(impl_.get())->DeleteStates();
162  }
163 
164  WeightClass Final(int64 s) const final {
165  if (!ValidStateId(s)) return WeightClass::NoWeight(WeightType());
166  WeightClass w(impl_->Final(s));
167  return w;
168  }
169 
170  const std::string &FstType() const final { return impl_->Type(); }
171 
172  const SymbolTable *InputSymbols() const final {
173  return impl_->InputSymbols();
174  }
175 
176  // Warning: calling this method casts the FST to a mutable FST.
178  return fst::down_cast<MutableFst<Arc> *>(impl_.get())->MutableInputSymbols();
179  }
180 
181  // Warning: calling this method casts the FST to a mutable FST.
183  return fst::down_cast<MutableFst<Arc> *>(impl_.get())->MutableOutputSymbols();
184  }
185 
186  // Signals failure by returning size_t max.
187  size_t NumArcs(int64 s) const final {
188  return ValidStateId(s) ? impl_->NumArcs(s)
189  : std::numeric_limits<size_t>::max();
190  }
191 
192  // Signals failure by returning size_t max.
193  size_t NumInputEpsilons(int64 s) const final {
194  return ValidStateId(s) ? impl_->NumInputEpsilons(s)
195  : std::numeric_limits<size_t>::max();
196  }
197 
198  // Signals failure by returning size_t max.
199  size_t NumOutputEpsilons(int64 s) const final {
200  return ValidStateId(s) ? impl_->NumOutputEpsilons(s)
201  : std::numeric_limits<size_t>::max();
202  }
203 
204  // Warning: calling this method casts the FST to a mutable FST.
205  int64 NumStates() const final {
206  return fst::down_cast<MutableFst<Arc> *>(impl_.get())->NumStates();
207  }
208 
209  uint64 Properties(uint64 mask, bool test) const final {
210  return impl_->Properties(mask, test);
211  }
212 
213  // Warning: calling this method casts the FST to a mutable FST.
214  bool ReserveArcs(int64 s, size_t n) final {
215  if (!ValidStateId(s)) return false;
216  fst::down_cast<MutableFst<Arc> *>(impl_.get())->ReserveArcs(s, n);
217  return true;
218  }
219 
220  // Warning: calling this method casts the FST to a mutable FST.
221  void ReserveStates(int64 n) final {
222  fst::down_cast<MutableFst<Arc> *>(impl_.get())->ReserveStates(n);
223  }
224 
225  const SymbolTable *OutputSymbols() const final {
226  return impl_->OutputSymbols();
227  }
228 
229  // Warning: calling this method casts the FST to a mutable FST.
230  void SetInputSymbols(const SymbolTable *isyms) final {
231  fst::down_cast<MutableFst<Arc> *>(impl_.get())->SetInputSymbols(isyms);
232  }
233 
234  // Warning: calling this method casts the FST to a mutable FST.
235  bool SetFinal(int64 s, const WeightClass &weight) final {
236  if (!ValidStateId(s)) return false;
237  fst::down_cast<MutableFst<Arc> *>(impl_.get())
238  ->SetFinal(s, *weight.GetWeight<typename Arc::Weight>());
239  return true;
240  }
241 
242  // Warning: calling this method casts the FST to a mutable FST.
243  void SetOutputSymbols(const SymbolTable *osyms) final {
244  fst::down_cast<MutableFst<Arc> *>(impl_.get())->SetOutputSymbols(osyms);
245  }
246 
247  // Warning: calling this method casts the FST to a mutable FST.
248  void SetProperties(uint64 props, uint64 mask) final {
249  fst::down_cast<MutableFst<Arc> *>(impl_.get())->SetProperties(props, mask);
250  }
251 
252  // Warning: calling this method casts the FST to a mutable FST.
253  bool SetStart(int64 s) final {
254  if (!ValidStateId(s)) return false;
255  fst::down_cast<MutableFst<Arc> *>(impl_.get())->SetStart(s);
256  return true;
257  }
258 
259  int64 Start() const final { return impl_->Start(); }
260 
261  bool ValidStateId(int64 s) const final {
262  // This cowardly refuses to count states if the FST is not yet expanded.
263  if (!Properties(kExpanded, true)) {
264  FSTERROR() << "Cannot get number of states for unexpanded FST";
265  return false;
266  }
267  // If the FST is already expanded, CountStates calls NumStates.
268  if (s < 0 || s >= CountStates(*impl_)) {
269  FSTERROR() << "State ID " << s << " not valid";
270  return false;
271  }
272  return true;
273  }
274 
275  const std::string &WeightType() const final { return Arc::Weight::Type(); }
276 
277  bool Write(const std::string &source) const final {
278  return impl_->Write(source);
279  }
280 
281  bool Write(std::ostream &ostr, const std::string &source) const final {
282  const FstWriteOptions opts(source);
283  return impl_->Write(ostr, opts);
284  }
285 
286  ~FstClassImpl() override {}
287 
288  Fst<Arc> *GetImpl() const { return impl_.get(); }
289 
290  private:
291  std::unique_ptr<Fst<Arc>> impl_;
292 };
293 
294 // BASE CLASS DEFINITIONS
295 
296 class MutableFstClass;
297 
298 class FstClass : public FstClassBase {
299  public:
300  FstClass() : impl_(nullptr) {}
301 
302  template <class Arc>
303  explicit FstClass(std::unique_ptr<Fst<Arc>> fst)
304  : impl_(std::make_unique<FstClassImpl<Arc>>(std::move(fst))) {}
305 
306  template <class Arc>
307  explicit FstClass(const Fst<Arc> &fst)
308  : impl_(std::make_unique<FstClassImpl<Arc>>(fst)) {}
309 
310  FstClass(const FstClass &other)
311  : impl_(other.impl_ == nullptr ? nullptr : other.impl_->Copy()) {}
312 
313  FstClass &operator=(const FstClass &other) {
314  impl_.reset(other.impl_ == nullptr ? nullptr : other.impl_->Copy());
315  return *this;
316  }
317 
318  WeightClass Final(int64 s) const final { return impl_->Final(s); }
319 
320  const std::string &ArcType() const final { return impl_->ArcType(); }
321 
322  const std::string &FstType() const final { return impl_->FstType(); }
323 
324  const SymbolTable *InputSymbols() const final {
325  return impl_->InputSymbols();
326  }
327 
328  size_t NumArcs(int64 s) const final { return impl_->NumArcs(s); }
329 
330  size_t NumInputEpsilons(int64 s) const final {
331  return impl_->NumInputEpsilons(s);
332  }
333 
334  size_t NumOutputEpsilons(int64 s) const final {
335  return impl_->NumOutputEpsilons(s);
336  }
337 
338  const SymbolTable *OutputSymbols() const final {
339  return impl_->OutputSymbols();
340  }
341 
342  uint64 Properties(uint64 mask, bool test) const final {
343  // Special handling for FSTs with a null impl.
344  if (!impl_) return kError & mask;
345  return impl_->Properties(mask, test);
346  }
347 
348  static FstClass *Read(const std::string &source);
349 
350  static FstClass *Read(std::istream &istrm, const std::string &source);
351 
352  int64 Start() const final { return impl_->Start(); }
353 
354  bool ValidStateId(int64 s) const final { return impl_->ValidStateId(s); }
355 
356  const std::string &WeightType() const final { return impl_->WeightType(); }
357 
358  // Helper that logs an ERROR if the weight type of an FST and a WeightClass
359  // don't match.
360 
361  bool WeightTypesMatch(const WeightClass &weight,
362  const std::string &op_name) const;
363 
364  bool Write(const std::string &source) const final {
365  return impl_->Write(source);
366  }
367 
368  bool Write(std::ostream &ostr, const std::string &source) const final {
369  return impl_->Write(ostr, source);
370  }
371 
372  ~FstClass() override {}
373 
374  // These methods are required by IO registration.
375 
376  template <class Arc>
377  static FstClassImplBase *Convert(const FstClass &other) {
378  FSTERROR() << "Doesn't make sense to convert any class to type FstClass";
379  return nullptr;
380  }
381 
382  template <class Arc>
384  FSTERROR() << "Doesn't make sense to create an FstClass with a "
385  << "particular arc type";
386  return nullptr;
387  }
388 
389  template <class Arc>
390  const Fst<Arc> *GetFst() const {
391  if (Arc::Type() != ArcType()) {
392  return nullptr;
393  } else {
394  FstClassImpl<Arc> *typed_impl =
395  fst::down_cast<FstClassImpl<Arc> *>(impl_.get());
396  return typed_impl->GetImpl();
397  }
398  }
399 
400  template <class Arc>
401  static FstClass *Read(std::istream &stream, const FstReadOptions &opts) {
402  if (!opts.header) {
403  LOG(ERROR) << "FstClass::Read: Options header not specified";
404  return nullptr;
405  }
406  const FstHeader &hdr = *opts.header;
407  if (hdr.Properties() & kMutable) {
408  return ReadTypedFst<MutableFstClass, MutableFst<Arc>>(stream, opts);
409  } else {
410  return ReadTypedFst<FstClass, Fst<Arc>>(stream, opts);
411  }
412  }
413 
414  protected:
415  explicit FstClass(std::unique_ptr<FstClassImplBase> impl)
416  : impl_(std::move(impl)) {}
417 
418  const FstClassImplBase *GetImpl() const { return impl_.get(); }
419 
420  FstClassImplBase *GetImpl() { return impl_.get(); }
421 
422  // Generic template method for reading an arc-templated FST of type
423  // UnderlyingT, and returning it wrapped as FstClassT, with appropriate
424  // error checking. Called from arc-templated Read() static methods.
425  template <class FstClassT, class UnderlyingT>
426  static FstClassT *ReadTypedFst(std::istream &stream,
427  const FstReadOptions &opts) {
428  std::unique_ptr<UnderlyingT> u(UnderlyingT::Read(stream, opts));
429  return u ? new FstClassT(std::move(u)) : nullptr;
430  }
431 
432  private:
433  std::unique_ptr<FstClassImplBase> impl_;
434 };
435 
436 // Specific types of FstClass with special properties
437 
438 class MutableFstClass : public FstClass {
439  public:
440  bool AddArc(int64 s, const ArcClass &ac) {
441  if (!WeightTypesMatch(ac.weight, "AddArc")) return false;
442  return GetImpl()->AddArc(s, ac);
443  }
444 
445  int64 AddState() { return GetImpl()->AddState(); }
446 
447  void AddStates(size_t n) { return GetImpl()->AddStates(n); }
448 
449  bool DeleteArcs(int64 s, size_t n) { return GetImpl()->DeleteArcs(s, n); }
450 
451  bool DeleteArcs(int64 s) { return GetImpl()->DeleteArcs(s); }
452 
453  bool DeleteStates(const std::vector<int64> &dstates) {
454  return GetImpl()->DeleteStates(dstates);
455  }
456 
457  void DeleteStates() { GetImpl()->DeleteStates(); }
458 
460  return GetImpl()->MutableInputSymbols();
461  }
462 
464  return GetImpl()->MutableOutputSymbols();
465  }
466 
467  int64 NumStates() const { return GetImpl()->NumStates(); }
468 
469  bool ReserveArcs(int64 s, size_t n) { return GetImpl()->ReserveArcs(s, n); }
470 
471  void ReserveStates(int64 n) { GetImpl()->ReserveStates(n); }
472 
473  static MutableFstClass *Read(const std::string &source, bool convert = false);
474 
475  void SetInputSymbols(const SymbolTable *isyms) {
476  GetImpl()->SetInputSymbols(isyms);
477  }
478 
479  bool SetFinal(int64 s, const WeightClass &weight) {
480  if (!WeightTypesMatch(weight, "SetFinal")) return false;
481  return GetImpl()->SetFinal(s, weight);
482  }
483 
484  void SetOutputSymbols(const SymbolTable *osyms) {
485  GetImpl()->SetOutputSymbols(osyms);
486  }
487 
488  void SetProperties(uint64 props, uint64 mask) {
489  GetImpl()->SetProperties(props, mask);
490  }
491 
492  bool SetStart(int64 s) { return GetImpl()->SetStart(s); }
493 
494  template <class Arc>
495  explicit MutableFstClass(std::unique_ptr<MutableFst<Arc>> fst)
496  // NB: The natural cast-less way to do this doesn't compile for some
497  // arcane reason.
498  : FstClass(
499  fst::implicit_cast<std::unique_ptr<Fst<Arc>>>(std::move(fst))) {}
500 
501  template <class Arc>
502  explicit MutableFstClass(const MutableFst<Arc> &fst) : FstClass(fst) {}
503 
504  // These methods are required by IO registration.
505 
506  template <class Arc>
507  static FstClassImplBase *Convert(const FstClass &other) {
508  FSTERROR() << "Doesn't make sense to convert any class to type "
509  << "MutableFstClass";
510  return nullptr;
511  }
512 
513  template <class Arc>
515  FSTERROR() << "Doesn't make sense to create a MutableFstClass with a "
516  << "particular arc type";
517  return nullptr;
518  }
519 
520  template <class Arc>
522  Fst<Arc> *fst = const_cast<Fst<Arc> *>(this->GetFst<Arc>());
524  return mfst;
525  }
526 
527  template <class Arc>
528  static MutableFstClass *Read(std::istream &stream,
529  const FstReadOptions &opts) {
530  std::unique_ptr<MutableFst<Arc>> mfst(MutableFst<Arc>::Read(stream, opts));
531  return mfst ? new MutableFstClass(*mfst) : nullptr;
532  }
533 
534  protected:
535  explicit MutableFstClass(std::unique_ptr<FstClassImplBase> impl)
536  : FstClass(std::move(impl)) {}
537 };
538 
540  public:
541  explicit VectorFstClass(std::unique_ptr<FstClassImplBase> impl)
542  : MutableFstClass(std::move(impl)) {}
543 
544  explicit VectorFstClass(const FstClass &other);
545 
546  explicit VectorFstClass(const std::string &arc_type);
547 
548  static VectorFstClass *Read(const std::string &source);
549 
550  template <class Arc>
551  static VectorFstClass *Read(std::istream &stream,
552  const FstReadOptions &opts) {
553  std::unique_ptr<VectorFst<Arc>> mfst(VectorFst<Arc>::Read(stream, opts));
554  return mfst ? new VectorFstClass(*mfst) : nullptr;
555  }
556 
557  template <class Arc>
558  explicit VectorFstClass(std::unique_ptr<VectorFst<Arc>> fst)
559  // NB: The natural cast-less way to do this doesn't compile for some
560  // arcane reason.
561  : MutableFstClass(fst::implicit_cast<std::unique_ptr<MutableFst<Arc>>>(
562  std::move(fst))) {}
563 
564  template <class Arc>
566 
567  template <class Arc>
568  static FstClassImplBase *Convert(const FstClass &other) {
569  return new FstClassImpl<Arc>(
570  std::make_unique<VectorFst<Arc>>(*other.GetFst<Arc>()));
571  }
572 
573  template <class Arc>
575  return new FstClassImpl<Arc>(std::make_unique<VectorFst<Arc>>());
576  }
577 };
578 
579 // Registration stuff.
580 
581 // This class definition is to avoid a nested class definition inside the
582 // FstClassIORegistration struct.
583 template <class Reader, class Creator, class Converter>
585  Reader reader;
586  Creator creator;
587  Converter converter;
588 
589  FstClassRegEntry(Reader r, Creator cr, Converter co)
590  : reader(r), creator(cr), converter(co) {}
591 
592  FstClassRegEntry() : reader(nullptr), creator(nullptr), converter(nullptr) {}
593 };
594 
595 // Actual FST IO method register.
596 template <class Reader, class Creator, class Converter>
598  : public GenericRegister<std::string,
599  FstClassRegEntry<Reader, Creator, Converter>,
600  FstClassIORegister<Reader, Creator, Converter>> {
601  public:
602  Reader GetReader(const std::string &arc_type) const {
603  return this->GetEntry(arc_type).reader;
604  }
605 
606  Creator GetCreator(const std::string &arc_type) const {
607  return this->GetEntry(arc_type).creator;
608  }
609 
610  Converter GetConverter(const std::string &arc_type) const {
611  return this->GetEntry(arc_type).converter;
612  }
613 
614  protected:
615  std::string ConvertKeyToSoFilename(const std::string &key) const final {
616  std::string legal_type(key);
617  ConvertToLegalCSymbol(&legal_type);
618  return legal_type + "-arc.so";
619  }
620 };
621 
622 // Struct containing everything needed to register a particular type
623 // of FST class (e.g., a plain FstClass, or a MutableFstClass, etc.).
624 template <class FstClassType>
626  using Reader = FstClassType *(*)(std::istream &stream,
627  const FstReadOptions &opts);
628 
629  using Creator = FstClassImplBase *(*)();
630 
631  using Converter = FstClassImplBase *(*)(const FstClass &other);
632 
634 
635  // FST class Register.
637 
638  // FST class Register-er.
639  using Registerer =
641 };
642 
643 // Macros for registering other arc types.
644 
645 #define REGISTER_FST_CLASS(Class, Arc) \
646  static FstClassIORegistration<Class>::Registerer Class##_##Arc##_registerer( \
647  Arc::Type(), \
648  FstClassIORegistration<Class>::Entry( \
649  Class::Read<Arc>, Class::Create<Arc>, Class::Convert<Arc>))
650 
651 #define REGISTER_FST_CLASSES(Arc) \
652  REGISTER_FST_CLASS(FstClass, Arc); \
653  REGISTER_FST_CLASS(MutableFstClass, Arc); \
654  REGISTER_FST_CLASS(VectorFstClass, Arc);
655 
656 } // namespace script
657 } // namespace fst
658 
659 #endif // FST_SCRIPT_FST_CLASS_H_
virtual const std::string & WeightType() const =0
bool ReserveArcs(int64 s, size_t n) final
Definition: fst-class.h:214
bool AddArc(int64 s, const ArcClass &ac) final
Definition: fst-class.h:108
FstClassImplBase *(*)(const FstClass &other) Converter
Definition: fst-class.h:631
FstClass & operator=(const FstClass &other)
Definition: fst-class.h:313
int64 NumStates() const
Definition: fst-class.h:467
const Fst< Arc > * GetFst() const
Definition: fst-class.h:390
Creator GetCreator(const std::string &arc_type) const
Definition: fst-class.h:606
void ConvertToLegalCSymbol(std::string *s)
Definition: util.cc:81
FstClassImpl(std::unique_ptr< Fst< Arc >> impl)
Definition: fst-class.h:102
const std::string & ArcType() const final
Definition: fst-class.h:129
VectorFstClass(const VectorFst< Arc > &fst)
Definition: fst-class.h:565
const FstHeader * header
Definition: fst.h:75
MutableFst< Arc > * GetMutableFst()
Definition: fst-class.h:521
~FstClass() override
Definition: fst-class.h:372
uint64_t uint64
Definition: types.h:32
Reader GetReader(const std::string &arc_type) const
Definition: fst-class.h:602
bool SetFinal(int64 s, const WeightClass &weight) final
Definition: fst-class.h:235
bool AddArc(int64 s, const ArcClass &ac)
Definition: fst-class.h:440
virtual size_t NumInputEpsilons(int64) const =0
FstClass(const FstClass &other)
Definition: fst-class.h:310
virtual bool Write(const std::string &) const =0
FstClass(std::unique_ptr< FstClassImplBase > impl)
Definition: fst-class.h:415
const FstClassImplBase * GetImpl() const
Definition: fst-class.h:418
WeightClass Final(int64 s) const final
Definition: fst-class.h:164
size_t NumInputEpsilons(int64 s) const final
Definition: fst-class.h:193
void SetInputSymbols(const SymbolTable *isyms) final
Definition: fst-class.h:230
const SymbolTable * InputSymbols() const final
Definition: fst-class.h:324
bool Write(std::ostream &ostr, const std::string &source) const final
Definition: fst-class.h:281
void AddStates(size_t n)
Definition: fst-class.h:447
const std::string & ArcType() const final
Definition: fst-class.h:320
#define LOG(type)
Definition: log.h:46
WeightClass Final(int64 s) const final
Definition: fst-class.h:318
const std::string & FstType() const final
Definition: fst-class.h:170
void ReserveStates(int64 n) final
Definition: fst-class.h:221
void ReserveStates(int64 n)
Definition: fst-class.h:471
static WeightClass NoWeight(const std::string &weight_type)
Definition: weight-class.cc:53
virtual const std::string & ArcType() const =0
To down_cast(From *f)
Definition: compat.h:51
uint64 Properties(uint64 mask, bool test) const final
Definition: fst-class.h:209
bool DeleteStates(const std::vector< int64 > &dstates)
Definition: fst-class.h:453
FstClassImpl * Copy() final
Definition: fst-class.h:131
bool ValidStateId(int64 s) const final
Definition: fst-class.h:261
VectorFstClass(std::unique_ptr< FstClassImplBase > impl)
Definition: fst-class.h:541
FstClassType *(*)(std::istream &stream, const FstReadOptions &opts) Reader
Definition: fst-class.h:627
MutableFstClass(const MutableFst< Arc > &fst)
Definition: fst-class.h:502
const std::string & WeightType() const final
Definition: fst-class.h:356
void DeleteStates() final
Definition: fst-class.h:160
constexpr uint64 kExpanded
Definition: properties.h:44
size_t NumOutputEpsilons(int64 s) const final
Definition: fst-class.h:334
int64_t int64
Definition: types.h:27
#define FSTERROR()
Definition: util.h:53
uint64 Properties(uint64 mask, bool test) const final
Definition: fst-class.h:342
const std::string & FstType() const final
Definition: fst-class.h:322
bool Write(const std::string &source) const final
Definition: fst-class.h:364
virtual const SymbolTable * InputSymbols() const =0
MutableFstClass(std::unique_ptr< FstClassImplBase > impl)
Definition: fst-class.h:535
constexpr To implicit_cast(typename internal::identity_t< To > to)
Definition: compat.h:83
FstClassImpl(const Fst< Arc > &impl)
Definition: fst-class.h:105
FstClass(std::unique_ptr< Fst< Arc >> fst)
Definition: fst-class.h:303
static FstClassImplBase * Convert(const FstClass &other)
Definition: fst-class.h:568
Fst< Arc > * GetImpl() const
Definition: fst-class.h:288
size_t NumArcs(int64 s) const final
Definition: fst-class.h:328
bool DeleteArcs(int64 s) final
Definition: fst-class.h:141
bool SetFinal(int64 s, const WeightClass &weight)
Definition: fst-class.h:479
static FstClass * Read(std::istream &stream, const FstReadOptions &opts)
Definition: fst-class.h:401
bool Write(std::ostream &ostr, const std::string &source) const final
Definition: fst-class.h:368
bool ReserveArcs(int64 s, size_t n)
Definition: fst-class.h:469
int64 Start() const final
Definition: fst-class.h:352
virtual size_t NumOutputEpsilons(int64) const =0
FstClassRegEntry(Reader r, Creator cr, Converter co)
Definition: fst-class.h:589
const std::string & WeightType() const final
Definition: fst-class.h:275
bool ValidStateId(int64 s) const final
Definition: fst-class.h:354
static FstClassImplBase * Create()
Definition: fst-class.h:383
void AddStates(size_t n) final
Definition: fst-class.h:125
MutableFstClass(std::unique_ptr< MutableFst< Arc >> fst)
Definition: fst-class.h:495
static MutableFstClass * Read(std::istream &stream, const FstReadOptions &opts)
Definition: fst-class.h:528
bool Write(const std::string &source) const final
Definition: fst-class.h:277
virtual WeightClass Final(int64) const =0
int64 Start() const final
Definition: fst-class.h:259
void SetOutputSymbols(const SymbolTable *osyms)
Definition: fst-class.h:484
Arc::StateId CountStates(const Fst< Arc > &fst)
Definition: expanded-fst.h:169
void SetProperties(uint64 props, uint64 mask) final
Definition: fst-class.h:248
virtual size_t NumArcs(int64) const =0
int64 AddState() final
Definition: fst-class.h:120
virtual bool ValidStateId(int64) const =0
bool SetStart(int64 s) final
Definition: fst-class.h:253
FstClassImplBase * GetImpl()
Definition: fst-class.h:420
constexpr uint64 kError
Definition: properties.h:50
void SetOutputSymbols(const SymbolTable *osyms) final
Definition: fst-class.h:243
VectorFstClass(std::unique_ptr< VectorFst< Arc >> fst)
Definition: fst-class.h:558
bool DeleteArcs(int64 s, size_t n)
Definition: fst-class.h:449
const SymbolTable * OutputSymbols() const final
Definition: fst-class.h:225
static FstClassT * ReadTypedFst(std::istream &stream, const FstReadOptions &opts)
Definition: fst-class.h:426
static FstClassImplBase * Create()
Definition: fst-class.h:574
virtual const std::string & FstType() const =0
static VectorFstClass * Read(std::istream &stream, const FstReadOptions &opts)
Definition: fst-class.h:551
void SetInputSymbols(const SymbolTable *isyms)
Definition: fst-class.h:475
static FstClassImplBase * Create()
Definition: fst-class.h:514
WeightClass weight
Definition: arc-class.h:49
FstClass(const Fst< Arc > &fst)
Definition: fst-class.h:307
SymbolTable * MutableInputSymbols() final
Definition: fst-class.h:177
virtual const SymbolTable * OutputSymbols() const =0
int64 NumStates() const final
Definition: fst-class.h:205
void SetProperties(uint64 props, uint64 mask)
Definition: fst-class.h:488
virtual int64 Start() const =0
SymbolTable * MutableOutputSymbols()
Definition: fst-class.h:463
std::string ConvertKeyToSoFilename(const std::string &key) const final
Definition: fst-class.h:615
Converter GetConverter(const std::string &arc_type) const
Definition: fst-class.h:610
constexpr uint64 kMutable
Definition: properties.h:47
static FstClassImplBase * Convert(const FstClass &other)
Definition: fst-class.h:377
bool DeleteStates(const std::vector< int64 > &dstates) final
Definition: fst-class.h:148
virtual uint64 Properties(uint64, bool) const =0
bool DeleteArcs(int64 s, size_t n) final
Definition: fst-class.h:134
const SymbolTable * InputSymbols() const final
Definition: fst-class.h:172
size_t NumOutputEpsilons(int64 s) const final
Definition: fst-class.h:199
static FstClassImplBase * Convert(const FstClass &other)
Definition: fst-class.h:507
uint64 Properties() const
Definition: fst.h:150
size_t NumArcs(int64 s) const final
Definition: fst-class.h:187
SymbolTable * MutableOutputSymbols() final
Definition: fst-class.h:182
const SymbolTable * OutputSymbols() const final
Definition: fst-class.h:338
SymbolTable * MutableInputSymbols()
Definition: fst-class.h:459
size_t NumInputEpsilons(int64 s) const final
Definition: fst-class.h:330