1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
4 // FST abstract base class definition, state and arc iterator interface, and
5 // suggested base implementation.
10 #include <sys/types.h>
21 #include <fst/compat.h>
22 #include <fst/types.h>
23 #include <fst/flags.h>
28 #include <fst/memory.h>
29 #include <fst/properties.h>
30 #include <fst/register.h>
31 #include <fst/symbol-table.h>
35 DECLARE_bool(fst_align);
39 bool IsFstHeader(std::istream &, const string &);
44 struct StateIteratorData;
47 struct ArcIteratorData;
52 struct FstReadOptions {
53 // FileReadMode(s) are advisory, there are many conditions than prevent a
54 // file from being mapped, READ mode will be selected in these cases with
55 // a warning indicating why it was chosen.
56 enum FileReadMode { READ, MAP };
58 string source; // Where you're reading from.
59 const FstHeader *header; // Pointer to FST header; if non-zero, use
60 // this info (don't read a stream header).
61 const SymbolTable *isymbols; // Pointer to input symbols; if non-zero, use
62 // this info (read and skip stream isymbols)
63 const SymbolTable *osymbols; // Pointer to output symbols; if non-zero, use
64 // this info (read and skip stream osymbols)
65 FileReadMode mode; // Read or map files (advisory, if possible)
66 bool read_isymbols; // Read isymbols, if any (default: true).
67 bool read_osymbols; // Read osymbols, if any (default: true).
69 explicit FstReadOptions(const string &source = "<unspecified>",
70 const FstHeader *header = nullptr,
71 const SymbolTable *isymbols = nullptr,
72 const SymbolTable *osymbols = nullptr);
74 explicit FstReadOptions(const string &source, const SymbolTable *isymbols,
75 const SymbolTable *osymbols = nullptr);
77 // Helper function to convert strings FileReadModes into their enum value.
78 static FileReadMode ReadMode(const string &mode);
80 // Outputs a debug string for the FstReadOptions object.
81 string DebugString() const;
84 struct FstWriteOptions {
85 string source; // Where you're writing to.
86 bool write_header; // Write the header?
87 bool write_isymbols; // Write input symbols?
88 bool write_osymbols; // Write output symbols?
89 bool align; // Write data aligned (may fail on pipes)?
90 bool stream_write; // Avoid seek operations in writing.
92 explicit FstWriteOptions(const string &source = "<unspecifed>",
93 bool write_header = true, bool write_isymbols = true,
94 bool write_osymbols = true,
95 bool align = FLAGS_fst_align,
96 bool stream_write = false)
98 write_header(write_header),
99 write_isymbols(write_isymbols),
100 write_osymbols(write_osymbols),
102 stream_write(stream_write) {}
107 // This is the recommended file header representation.
112 HAS_ISYMBOLS = 0x1, // Has input symbol table.
113 HAS_OSYMBOLS = 0x2, // Has output symbol table.
114 IS_ALIGNED = 0x4, // Memory-aligned (where appropriate).
117 FstHeader() : version_(0), flags_(0), properties_(0), start_(-1),
118 numstates_(0), numarcs_(0) {}
120 const string &FstType() const { return fsttype_; }
122 const string &ArcType() const { return arctype_; }
124 int32 Version() const { return version_; }
126 int32 GetFlags() const { return flags_; }
128 uint64 Properties() const { return properties_; }
130 int64 Start() const { return start_; }
132 int64 NumStates() const { return numstates_; }
134 int64 NumArcs() const { return numarcs_; }
136 void SetFstType(const string &type) { fsttype_ = type; }
138 void SetArcType(const string &type) { arctype_ = type; }
140 void SetVersion(int32 version) { version_ = version; }
142 void SetFlags(int32 flags) { flags_ = flags; }
144 void SetProperties(uint64 properties) { properties_ = properties; }
146 void SetStart(int64 start) { start_ = start; }
148 void SetNumStates(int64 numstates) { numstates_ = numstates; }
150 void SetNumArcs(int64 numarcs) { numarcs_ = numarcs; }
152 bool Read(std::istream &strm, const string &source,
153 bool rewind = false);
155 bool Write(std::ostream &strm, const string &source) const;
157 // Outputs a debug string for the FstHeader object.
158 string DebugString() const;
161 string fsttype_; // E.g. "vector".
162 string arctype_; // E.g. "standard".
163 int32 version_; // Type version number.
164 int32 flags_; // File format bits.
165 uint64 properties_; // FST property bits.
166 int64 start_; // Start state.
167 int64 numstates_; // # of states.
168 int64 numarcs_; // # of arcs.
171 // Specifies matcher action.
173 MATCH_INPUT = 1, // Match input label.
174 MATCH_OUTPUT = 2, // Match output label.
175 MATCH_BOTH = 3, // Match input or output label.
176 MATCH_NONE = 4, // Match nothing.
178 }; // Otherwise, match type unknown.
180 constexpr int kNoStateId = -1; // Not a valid state ID.
181 constexpr int kNoLabel = -1; // Not a valid label.
183 // A generic FST, templated on the arc definition, with common-demoninator
184 // methods (use StateIterator and ArcIterator to iterate over its states and
190 using StateId = typename Arc::StateId;
191 using Weight = typename Arc::Weight;
196 virtual StateId Start() const = 0;
198 // State's final weight.
199 virtual Weight Final(StateId) const = 0;
201 // State's arc count.
202 virtual size_t NumArcs(StateId) const = 0;
204 // State's input epsilon count.
205 virtual size_t NumInputEpsilons(StateId) const = 0;
207 // State's output epsilon count.
208 virtual size_t NumOutputEpsilons(StateId) const = 0;
210 // Property bits. If test = false, return stored properties bits for mask
211 // (some possibly unknown); if test = true, return property bits for mask
212 // (computing o.w. unknown).
213 virtual uint64 Properties(uint64 mask, bool test) const = 0;
216 virtual const string &Type() const = 0;
218 // Gets a copy of this Fst. The copying behaves as follows:
220 // (1) The copying is constant time if safe = false or if safe = true
221 // and is on an otherwise unaccessed FST.
223 // (2) If safe = true, the copy is thread-safe in that the original
224 // and copy can be safely accessed (but not necessarily mutated) by
225 // separate threads. For some FST types, 'Copy(true)' should only be
226 // called on an FST that has not otherwise been accessed. Behavior is
227 // otherwise undefined.
229 // (3) If a MutableFst is copied and then mutated, then the original is
230 // unmodified and vice versa (often by a copy-on-write on the initial
231 // mutation, which may not be constant time).
232 virtual Fst<Arc> *Copy(bool safe = false) const = 0;
234 // Reads an FST from an input stream; returns nullptr on error.
235 static Fst<Arc> *Read(std::istream &strm, const FstReadOptions &opts) {
236 FstReadOptions ropts(opts);
241 if (!hdr.Read(strm, opts.source)) return nullptr;
244 const auto &fst_type = hdr.FstType();
245 const auto reader = FstRegister<Arc>::GetRegister()->GetReader(fst_type);
247 LOG(ERROR) << "Fst::Read: Unknown FST type " << fst_type
248 << " (arc type = " << Arc::Type() << "): " << ropts.source;
251 return reader(strm, ropts);
254 // Reads an FST from a file; returns nullptr on error. An empty filename
255 // results in reading from standard input.
256 static Fst<Arc> *Read(const string &filename) {
257 if (!filename.empty()) {
258 std::ifstream strm(filename,
259 std::ios_base::in | std::ios_base::binary);
261 LOG(ERROR) << "Fst::Read: Can't open file: " << filename;
264 return Read(strm, FstReadOptions(filename));
266 return Read(std::cin, FstReadOptions("standard input"));
270 // Writes an FST to an output stream; returns false on error.
271 virtual bool Write(std::ostream &strm, const FstWriteOptions &opts) const {
272 LOG(ERROR) << "Fst::Write: No write stream method for " << Type()
277 // Writes an FST to a file; returns false on error; an empty filename
278 // results in writing to standard output.
279 virtual bool Write(const string &filename) const {
280 LOG(ERROR) << "Fst::Write: No write filename method for " << Type()
285 // Returns input label symbol table; return nullptr if not specified.
286 virtual const SymbolTable *InputSymbols() const = 0;
288 // Return output label symbol table; return nullptr if not specified.
289 virtual const SymbolTable *OutputSymbols() const = 0;
291 // For generic state iterator construction (not normally called directly by
293 virtual void InitStateIterator(StateIteratorData<Arc> *data) const = 0;
295 // For generic arc iterator construction (not normally called directly by
297 virtual void InitArcIterator(StateId s, ArcIteratorData<Arc> *data) const = 0;
299 // For generic matcher construction (not normally called directly by users).
300 virtual MatcherBase<Arc> *InitMatcher(MatchType match_type) const;
303 bool WriteFile(const string &filename) const {
304 if (!filename.empty()) {
305 std::ofstream strm(filename,
306 std::ios_base::out | std::ios_base::binary);
308 LOG(ERROR) << "Fst::Write: Can't open file: " << filename;
311 bool val = Write(strm, FstWriteOptions(filename));
312 if (!val) LOG(ERROR) << "Fst::Write failed: " << filename;
315 return Write(std::cout, FstWriteOptions("standard output"));
320 // A useful alias when using StdArc.
321 using StdFst = Fst<StdArc>;
323 // State and arc iterator definitions.
325 // State iterator interface templated on the Arc definition; used for
326 // StateIterator specializations returned by the InitStateIterator FST method.
328 class StateIteratorBase {
330 using StateId = typename Arc::StateId;
332 virtual ~StateIteratorBase() {}
335 virtual bool Done() const = 0;
336 // Returns current state (when !Done()).
337 virtual StateId Value() const = 0;
338 // Advances to next state (when !Done()).
339 virtual void Next() = 0;
340 // Resets to initial condition.
341 virtual void Reset() = 0;
344 // StateIterator initialization data.
347 struct StateIteratorData {
348 using StateId = typename Arc::StateId;
350 // Specialized iterator if non-zero.
351 StateIteratorBase<Arc> *base;
352 // Otherwise, the total number of states.
355 StateIteratorData() : base(nullptr), nstates(0) {}
357 StateIteratorData(const StateIteratorData &) = delete;
358 StateIteratorData &operator=(const StateIteratorData &) = delete;
361 // Generic state iterator, templated on the FST definition (a wrapper
362 // around a pointer to a specific one). Here is a typical use:
364 // for (StateIterator<StdFst> siter(fst);
367 // StateId s = siter.Value();
371 class StateIterator {
373 using Arc = typename FST::Arc;
374 using StateId = typename Arc::StateId;
376 explicit StateIterator(const FST &fst) : s_(0) {
377 fst.InitStateIterator(&data_);
380 ~StateIterator() { delete data_.base; }
383 return data_.base ? data_.base->Done() : s_ >= data_.nstates;
386 StateId Value() const { return data_.base ? data_.base->Value() : s_; }
405 StateIteratorData<Arc> data_;
409 // Flags to control the behavior on an arc iterator.
410 static constexpr uint32 kArcILabelValue =
411 0x0001; // Value() gives valid ilabel.
412 static constexpr uint32 kArcOLabelValue = 0x0002; // " " " olabel.
413 static constexpr uint32 kArcWeightValue = 0x0004; // " " " weight.
414 static constexpr uint32 kArcNextStateValue =
415 0x0008; // " " " nextstate.
416 static constexpr uint32 kArcNoCache = 0x0010; // No need to cache arcs.
418 static constexpr uint32 kArcValueFlags =
419 kArcILabelValue | kArcOLabelValue | kArcWeightValue | kArcNextStateValue;
421 static constexpr uint32 kArcFlags = kArcValueFlags | kArcNoCache;
423 // Arc iterator interface, templated on the arc definition; used for arc
424 // iterator specializations that are returned by the InitArcIterator FST method.
426 class ArcIteratorBase {
428 using StateId = typename Arc::StateId;
430 virtual ~ArcIteratorBase() {}
433 virtual bool Done() const = 0;
434 // Returns current arc (when !Done()).
435 virtual const Arc &Value() const = 0;
436 // Advances to next arc (when !Done()).
437 virtual void Next() = 0;
438 // Returns current position.
439 virtual size_t Position() const = 0;
440 // Returns to initial condition.
441 virtual void Reset() = 0;
442 // Advances to arbitrary arc by position.
443 virtual void Seek(size_t) = 0;
444 // Returns current behavorial flags
445 virtual uint32 Flags() const = 0;
446 // Sets behavorial flags.
447 virtual void SetFlags(uint32, uint32) = 0;
450 // ArcIterator initialization data.
452 struct ArcIteratorData {
454 : base(nullptr), arcs(nullptr), narcs(0), ref_count(nullptr) {}
456 ArcIteratorData(const ArcIteratorData &) = delete;
458 ArcIteratorData &operator=(const ArcIteratorData &) = delete;
460 ArcIteratorBase<Arc> *base; // Specialized iterator if non-zero.
461 const Arc *arcs; // O.w. arcs pointer
462 size_t narcs; // ... and arc count.
463 int *ref_count; // ... and reference count if non-zero.
466 // Generic arc iterator, templated on the FST definition (a wrapper around a
467 // pointer to a specific one). Here is a typical use:
469 // for (ArcIterator<StdFst> aiter(fst, s);
472 // StdArc &arc = aiter.Value();
478 using Arc = typename FST::Arc;
479 using StateId = typename Arc::StateId;
481 ArcIterator(const FST &fst, StateId s) : i_(0) {
482 fst.InitArcIterator(s, &data_);
485 explicit ArcIterator(const ArcIteratorData<Arc> &data) : data_(data), i_(0) {
486 if (data_.ref_count) ++(*data_.ref_count);
492 } else if (data_.ref_count) {
493 --(*data_.ref_count);
498 return data_.base ? data_.base->Done() : i_ >= data_.narcs;
501 const Arc &Value() const {
502 return data_.base ? data_.base->Value() : data_.arcs[i_];
521 void Seek(size_t a) {
529 size_t Position() const { return data_.base ? data_.base->Position() : i_; }
531 uint32 Flags() const {
533 return data_.base->Flags();
535 return kArcValueFlags;
539 void SetFlags(uint32 flags, uint32 mask) {
540 if (data_.base) data_.base->SetFlags(flags, mask);
544 ArcIteratorData<Arc> data_;
550 // ArcIterator placement operator new and destroy function; new needs to be in
551 // the global namespace.
554 void *operator new(size_t size,
555 fst::MemoryPool<fst::ArcIterator<FST>> *pool) {
556 return pool->Allocate();
562 void Destroy(ArcIterator<FST> *aiter, MemoryPool<ArcIterator<FST>> *pool) {
564 aiter->~ArcIterator<FST>();
569 // Matcher definitions.
572 MatcherBase<Arc> *Fst<Arc>::InitMatcher(MatchType match_type) const {
573 return nullptr; // One should just use the default matcher.
576 // FST accessors, useful in high-performance applications.
580 // General case, requires non-abstract, 'final' methods. Use for inlining.
583 inline typename F::Arc::Weight Final(const F &fst, typename F::Arc::StateId s) {
584 return fst.F::Final(s);
588 inline ssize_t NumArcs(const F &fst, typename F::Arc::StateId s) {
589 return fst.F::NumArcs(s);
593 inline ssize_t NumInputEpsilons(const F &fst, typename F::Arc::StateId s) {
594 return fst.F::NumInputEpsilons(s);
598 inline ssize_t NumOutputEpsilons(const F &fst, typename F::Arc::StateId s) {
599 return fst.F::NumOutputEpsilons(s);
602 // Fst<Arc> case, abstract methods.
605 inline typename Arc::Weight Final(const Fst<Arc> &fst,
606 typename Arc::StateId s) {
611 inline size_t NumArcs(const Fst<Arc> &fst, typename Arc::StateId s) {
612 return fst.NumArcs(s);
616 inline size_t NumInputEpsilons(const Fst<Arc> &fst, typename Arc::StateId s) {
617 return fst.NumInputEpsilons(s);
621 inline size_t NumOutputEpsilons(const Fst<Arc> &fst, typename Arc::StateId s) {
622 return fst.NumOutputEpsilons(s);
625 // FST implementation base.
627 // This is the recommended FST implementation base class. It will handle
628 // reference counts, property bits, type information and symbols.
630 // Users are discouraged, but not prohibited, from subclassing this outside the
635 using StateId = typename Arc::StateId;
636 using Weight = typename Arc::Weight;
638 FstImpl() : properties_(0), type_("null") {}
640 FstImpl(const FstImpl<Arc> &impl)
641 : properties_(impl.properties_),
643 isymbols_(impl.isymbols_ ? impl.isymbols_->Copy() : nullptr),
644 osymbols_(impl.osymbols_ ? impl.osymbols_->Copy() : nullptr) {}
646 virtual ~FstImpl() {}
648 const string &Type() const { return type_; }
650 void SetType(const string &type) { type_ = type; }
652 virtual uint64 Properties() const { return properties_; }
654 virtual uint64 Properties(uint64 mask) const { return properties_ & mask; }
656 void SetProperties(uint64 props) {
657 properties_ &= kError; // kError can't be cleared.
658 properties_ |= props;
661 void SetProperties(uint64 props, uint64 mask) {
662 properties_ &= ~mask | kError; // kError can't be cleared.
663 properties_ |= props & mask;
666 // Allows (only) setting error bit on const FST implementations.
667 void SetProperties(uint64 props, uint64 mask) const {
668 if (mask != kError) {
669 FSTERROR() << "FstImpl::SetProperties() const: Can only set kError";
671 properties_ |= kError;
674 const SymbolTable *InputSymbols() const { return isymbols_.get(); }
676 const SymbolTable *OutputSymbols() const { return osymbols_.get(); }
678 SymbolTable *InputSymbols() { return isymbols_.get(); }
680 SymbolTable *OutputSymbols() { return osymbols_.get(); }
682 void SetInputSymbols(const SymbolTable *isyms) {
683 isymbols_.reset(isyms ? isyms->Copy() : nullptr);
686 void SetOutputSymbols(const SymbolTable *osyms) {
687 osymbols_.reset(osyms ? osyms->Copy() : nullptr);
690 // Reads header and symbols from input stream, initializes FST, and returns
691 // the header. If opts.header is non-null, skips reading and uses the option
692 // value instead. If opts.[io]symbols is non-null, reads in (if present), but
693 // uses the option value.
694 bool ReadHeader(std::istream &strm, const FstReadOptions &opts,
695 int min_version, FstHeader *hdr);
697 // Writes header and symbols to output stream. If opts.header is false, skips
698 // writing header. If opts.[io]symbols is false, skips writing those symbols.
699 // This method is needed for implementations that implement Write methods.
700 void WriteHeader(std::ostream &strm, const FstWriteOptions &opts,
701 int version, FstHeader *hdr) const {
702 if (opts.write_header) {
703 hdr->SetFstType(type_);
704 hdr->SetArcType(Arc::Type());
705 hdr->SetVersion(version);
706 hdr->SetProperties(properties_);
707 int32 file_flags = 0;
708 if (isymbols_ && opts.write_isymbols) {
709 file_flags |= FstHeader::HAS_ISYMBOLS;
711 if (osymbols_ && opts.write_osymbols) {
712 file_flags |= FstHeader::HAS_OSYMBOLS;
714 if (opts.align) file_flags |= FstHeader::IS_ALIGNED;
715 hdr->SetFlags(file_flags);
716 hdr->Write(strm, opts.source);
718 if (isymbols_ && opts.write_isymbols) isymbols_->Write(strm);
719 if (osymbols_ && opts.write_osymbols) osymbols_->Write(strm);
722 // Writes out header and symbols to output stream. If opts.header is false,
723 // skips writing header. If opts.[io]symbols is false, skips writing those
724 // symbols. `type` is the FST type being written. This method is used in the
725 // cross-type serialization methods Fst::WriteFst.
726 static void WriteFstHeader(const Fst<Arc> &fst, std::ostream &strm,
727 const FstWriteOptions &opts, int version,
728 const string &type, uint64 properties,
730 if (opts.write_header) {
731 hdr->SetFstType(type);
732 hdr->SetArcType(Arc::Type());
733 hdr->SetVersion(version);
734 hdr->SetProperties(properties);
735 int32 file_flags = 0;
736 if (fst.InputSymbols() && opts.write_isymbols) {
737 file_flags |= FstHeader::HAS_ISYMBOLS;
739 if (fst.OutputSymbols() && opts.write_osymbols) {
740 file_flags |= FstHeader::HAS_OSYMBOLS;
742 if (opts.align) file_flags |= FstHeader::IS_ALIGNED;
743 hdr->SetFlags(file_flags);
744 hdr->Write(strm, opts.source);
746 if (fst.InputSymbols() && opts.write_isymbols) {
747 fst.InputSymbols()->Write(strm);
749 if (fst.OutputSymbols() && opts.write_osymbols) {
750 fst.OutputSymbols()->Write(strm);
754 // In serialization routines where the header cannot be written until after
755 // the machine has been serialized, this routine can be called to seek to the
756 // beginning of the file an rewrite the header with updated fields. It
757 // repositions the file pointer back at the end of the file. Returns true on
758 // success, false on failure.
759 static bool UpdateFstHeader(const Fst<Arc> &fst, std::ostream &strm,
760 const FstWriteOptions &opts, int version,
761 const string &type, uint64 properties,
762 FstHeader *hdr, size_t header_offset) {
763 strm.seekp(header_offset);
765 LOG(ERROR) << "Fst::UpdateFstHeader: Write failed: " << opts.source;
768 WriteFstHeader(fst, strm, opts, version, type, properties, hdr);
770 LOG(ERROR) << "Fst::UpdateFstHeader: Write failed: " << opts.source;
773 strm.seekp(0, std::ios_base::end);
775 LOG(ERROR) << "Fst::UpdateFstHeader: Write failed: " << opts.source;
782 mutable uint64 properties_; // Property bits.
785 string type_; // Unique name of FST class.
786 std::unique_ptr<SymbolTable> isymbols_;
787 std::unique_ptr<SymbolTable> osymbols_;
791 bool FstImpl<Arc>::ReadHeader(std::istream &strm, const FstReadOptions &opts,
792 int min_version, FstHeader *hdr) {
795 } else if (!hdr->Read(strm, opts.source)) {
799 LOG(INFO) << "FstImpl::ReadHeader: source: " << opts.source
800 << ", fst_type: " << hdr->FstType()
801 << ", arc_type: " << Arc::Type()
802 << ", version: " << hdr->Version()
803 << ", flags: " << hdr->GetFlags();
805 if (hdr->FstType() != type_) {
806 LOG(ERROR) << "FstImpl::ReadHeader: FST not of type " << type_
807 << ": " << opts.source;
810 if (hdr->ArcType() != Arc::Type()) {
811 LOG(ERROR) << "FstImpl::ReadHeader: Arc not of type " << Arc::Type()
812 << ": " << opts.source;
815 if (hdr->Version() < min_version) {
816 LOG(ERROR) << "FstImpl::ReadHeader: Obsolete " << type_
817 << " FST version: " << opts.source;
820 properties_ = hdr->Properties();
821 if (hdr->GetFlags() & FstHeader::HAS_ISYMBOLS) {
822 isymbols_.reset(SymbolTable::Read(strm, opts.source));
824 // Deletes input symbol table.
825 if (!opts.read_isymbols) SetInputSymbols(nullptr);
826 if (hdr->GetFlags() & FstHeader::HAS_OSYMBOLS) {
827 osymbols_.reset(SymbolTable::Read(strm, opts.source));
829 // Deletes output symbol table.
830 if (!opts.read_osymbols) SetOutputSymbols(nullptr);
832 isymbols_.reset(opts.isymbols->Copy());
835 osymbols_.reset(opts.osymbols->Copy());
840 } // namespace internal
843 uint64 TestProperties(const Fst<Arc> &fst, uint64 mask, uint64 *known);
845 // This is a helper class template useful for attaching an FST interface to
846 // its implementation, handling reference counting.
847 template <class Impl, class FST = Fst<typename Impl::Arc>>
848 class ImplToFst : public FST {
850 using Arc = typename Impl::Arc;
851 using StateId = typename Arc::StateId;
852 using Weight = typename Arc::Weight;
853 using FST::operator=;
855 StateId Start() const override { return impl_->Start(); }
857 Weight Final(StateId s) const override { return impl_->Final(s); }
859 size_t NumArcs(StateId s) const override { return impl_->NumArcs(s); }
861 size_t NumInputEpsilons(StateId s) const override {
862 return impl_->NumInputEpsilons(s);
865 size_t NumOutputEpsilons(StateId s) const override {
866 return impl_->NumOutputEpsilons(s);
869 uint64 Properties(uint64 mask, bool test) const override {
871 uint64 knownprops, testprops = TestProperties(*this, mask, &knownprops);
872 impl_->SetProperties(testprops, knownprops);
873 return testprops & mask;
875 return impl_->Properties(mask);
879 const string &Type() const override { return impl_->Type(); }
881 const SymbolTable *InputSymbols() const override {
882 return impl_->InputSymbols();
885 const SymbolTable *OutputSymbols() const override {
886 return impl_->OutputSymbols();
890 explicit ImplToFst(std::shared_ptr<Impl> impl) : impl_(std::move(impl)) {}
892 // This constructor presumes there is a copy constructor for the
894 ImplToFst(const ImplToFst<Impl, FST> &fst, bool safe) {
896 impl_ = std::make_shared<Impl>(*(fst.impl_));
902 // Returns raw pointers to the shared object.
903 const Impl *GetImpl() const { return impl_.get(); }
905 Impl *GetMutableImpl() const { return impl_.get(); }
907 // Returns a ref-counted smart poiner to the implementation.
908 std::shared_ptr<Impl> GetSharedImpl() const { return impl_; }
910 bool Unique() const { return impl_.unique(); }
912 void SetImpl(std::shared_ptr<Impl> impl) { impl_ = impl; }
915 template <class IFST, class OFST>
916 friend void Cast(const IFST &ifst, OFST *ofst);
918 std::shared_ptr<Impl> impl_;
921 // Converts FSTs by casting their implementations, where this makes sense
922 // (which excludes implementations with weight-dependent virtual methods).
923 // Must be a friend of the FST classes involved (currently the concrete FSTs:
924 // ConstFst, CompactFst, and VectorFst). This can only be safely used for arc
925 // types that have identical storage characteristics. As with an FST
926 // copy constructor and Copy() method, this is a constant time operation
927 // (but subject to copy-on-write if it is a MutableFst and modified).
928 template <class IFST, class OFST>
929 void Cast(const IFST &ifst, OFST *ofst) {
930 using OImpl = typename OFST::Impl;
931 ofst->impl_ = std::shared_ptr<OImpl>(ifst.impl_,
932 reinterpret_cast<OImpl *>(ifst.impl_.get()));
935 // FST serialization.
938 void FstToString(const Fst<Arc> &fst, string *result) {
939 std::ostringstream ostrm;
940 fst.Write(ostrm, FstWriteOptions("FstToString"));
941 *result = ostrm.str();
945 void FstToString(const Fst<Arc> &fst, string *result,
946 const FstWriteOptions &options) {
947 std::ostringstream ostrm;
948 fst.Write(ostrm, options);
949 *result = ostrm.str();
953 Fst<Arc> *StringToFst(const string &s) {
954 std::istringstream istrm(s);
955 return Fst<Arc>::Read(istrm, FstReadOptions("StringToFst"));