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