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