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>
27 #include <fst/memory.h>
28 #include <fst/properties.h>
29 #include <fst/register.h>
30 #include <fst/symbol-table.h>
34 DECLARE_bool(fst_align);
38 bool IsFstHeader(std::istream &, const string &);
43 struct StateIteratorData;
46 struct ArcIteratorData;
51 struct FstReadOptions {
52 // FileReadMode(s) are advisory, there are many conditions than prevent a
53 // file from being mapped, READ mode will be selected in these cases with
54 // a warning indicating why it was chosen.
55 enum FileReadMode { READ, MAP };
57 string source; // Where you're reading from.
58 const FstHeader *header; // Pointer to FST header; if non-zero, use
59 // this info (don't read a stream header).
60 const SymbolTable *isymbols; // Pointer to input symbols; if non-zero, use
61 // this info (read and skip stream isymbols)
62 const SymbolTable *osymbols; // Pointer to output symbols; if non-zero, use
63 // this info (read and skip stream osymbols)
64 FileReadMode mode; // Read or map files (advisory, if possible)
65 bool read_isymbols; // Read isymbols, if any (default: true).
66 bool read_osymbols; // Read osymbols, if any (default: true).
68 explicit FstReadOptions(const string &source = "<unspecified>",
69 const FstHeader *header = nullptr,
70 const SymbolTable *isymbols = nullptr,
71 const SymbolTable *osymbols = nullptr);
73 explicit FstReadOptions(const string &source, const SymbolTable *isymbols,
74 const SymbolTable *osymbols = nullptr);
76 // Helper function to convert strings FileReadModes into their enum value.
77 static FileReadMode ReadMode(const string &mode);
79 // Outputs a debug string for the FstReadOptions object.
80 string DebugString() const;
83 struct FstWriteOptions {
84 string source; // Where you're writing to.
85 bool write_header; // Write the header?
86 bool write_isymbols; // Write input symbols?
87 bool write_osymbols; // Write output symbols?
88 bool align; // Write data aligned (may fail on pipes)?
89 bool stream_write; // Avoid seek operations in writing.
91 explicit FstWriteOptions(const string &source = "<unspecifed>",
92 bool write_header = true, bool write_isymbols = true,
93 bool write_osymbols = true,
94 bool align = FLAGS_fst_align,
95 bool stream_write = false)
97 write_header(write_header),
98 write_isymbols(write_isymbols),
99 write_osymbols(write_osymbols),
101 stream_write(stream_write) {}
106 // This is the recommended file header representation.
111 HAS_ISYMBOLS = 0x1, // Has input symbol table.
112 HAS_OSYMBOLS = 0x2, // Has output symbol table.
113 IS_ALIGNED = 0x4, // Memory-aligned (where appropriate).
116 FstHeader() : version_(0), flags_(0), properties_(0), start_(-1),
117 numstates_(0), numarcs_(0) {}
119 const string &FstType() const { return fsttype_; }
121 const string &ArcType() const { return arctype_; }
123 int32 Version() const { return version_; }
125 int32 GetFlags() const { return flags_; }
127 uint64 Properties() const { return properties_; }
129 int64 Start() const { return start_; }
131 int64 NumStates() const { return numstates_; }
133 int64 NumArcs() const { return numarcs_; }
135 void SetFstType(const string &type) { fsttype_ = type; }
137 void SetArcType(const string &type) { arctype_ = type; }
139 void SetVersion(int32 version) { version_ = version; }
141 void SetFlags(int32 flags) { flags_ = flags; }
143 void SetProperties(uint64 properties) { properties_ = properties; }
145 void SetStart(int64 start) { start_ = start; }
147 void SetNumStates(int64 numstates) { numstates_ = numstates; }
149 void SetNumArcs(int64 numarcs) { numarcs_ = numarcs; }
151 bool Read(std::istream &strm, const string &source,
152 bool rewind = false);
154 bool Write(std::ostream &strm, const string &source) const;
156 // Outputs a debug string for the FstHeader object.
157 string DebugString() const;
160 string fsttype_; // E.g. "vector".
161 string arctype_; // E.g. "standard".
162 int32 version_; // Type version number.
163 int32 flags_; // File format bits.
164 uint64 properties_; // FST property bits.
165 int64 start_; // Start state.
166 int64 numstates_; // # of states.
167 int64 numarcs_; // # of arcs.
170 // Specifies matcher action.
172 MATCH_INPUT = 1, // Match input label.
173 MATCH_OUTPUT = 2, // Match output label.
174 MATCH_BOTH = 3, // Match input or output label.
175 MATCH_NONE = 4, // Match nothing.
177 }; // Otherwise, match type unknown.
179 constexpr int kNoStateId = -1; // Not a valid state ID.
180 constexpr int kNoLabel = -1; // Not a valid label.
182 // A generic FST, templated on the arc definition, with common-demoninator
183 // methods (use StateIterator and ArcIterator to iterate over its states and
189 using StateId = typename Arc::StateId;
190 using Weight = typename Arc::Weight;
195 virtual StateId Start() const = 0;
197 // State's final weight.
198 virtual Weight Final(StateId) const = 0;
200 // State's arc count.
201 virtual size_t NumArcs(StateId) const = 0;
203 // State's input epsilon count.
204 virtual size_t NumInputEpsilons(StateId) const = 0;
206 // State's output epsilon count.
207 virtual size_t NumOutputEpsilons(StateId) const = 0;
209 // Property bits. If test = false, return stored properties bits for mask
210 // (some possibly unknown); if test = true, return property bits for mask
211 // (computing o.w. unknown).
212 virtual uint64 Properties(uint64 mask, bool test) const = 0;
215 virtual const string &Type() const = 0;
217 // Gets a copy of this Fst. The copying behaves as follows:
219 // (1) The copying is constant time if safe = false or if safe = true
220 // and is on an otherwise unaccessed FST.
222 // (2) If safe = true, the copy is thread-safe in that the original
223 // and copy can be safely accessed (but not necessarily mutated) by
224 // separate threads. For some FST types, 'Copy(true)' should only be
225 // called on an FST that has not otherwise been accessed. Behavior is
226 // otherwise undefined.
228 // (3) If a MutableFst is copied and then mutated, then the original is
229 // unmodified and vice versa (often by a copy-on-write on the initial
230 // mutation, which may not be constant time).
231 virtual Fst<Arc> *Copy(bool safe = false) const = 0;
233 // Reads an FST from an input stream; returns nullptr on error.
234 static Fst<Arc> *Read(std::istream &strm, const FstReadOptions &opts) {
235 FstReadOptions ropts(opts);
240 if (!hdr.Read(strm, opts.source)) return nullptr;
243 const auto &fst_type = hdr.FstType();
244 const auto reader = FstRegister<Arc>::GetRegister()->GetReader(fst_type);
246 LOG(ERROR) << "Fst::Read: Unknown FST type " << fst_type
247 << " (arc type = " << Arc::Type() << "): " << ropts.source;
250 return reader(strm, ropts);
253 // Reads an FST from a file; returns nullptr on error. An empty filename
254 // results in reading from standard input.
255 static Fst<Arc> *Read(const string &filename) {
256 if (!filename.empty()) {
257 std::ifstream strm(filename,
258 std::ios_base::in | std::ios_base::binary);
260 LOG(ERROR) << "Fst::Read: Can't open file: " << filename;
263 return Read(strm, FstReadOptions(filename));
265 return Read(std::cin, FstReadOptions("standard input"));
269 // Writes an FST to an output stream; returns false on error.
270 virtual bool Write(std::ostream &strm, const FstWriteOptions &opts) const {
271 LOG(ERROR) << "Fst::Write: No write stream method for " << Type()
276 // Writes an FST to a file; returns false on error; an empty filename
277 // results in writing to standard output.
278 virtual bool Write(const string &filename) const {
279 LOG(ERROR) << "Fst::Write: No write filename method for " << Type()
284 // Returns input label symbol table; return nullptr if not specified.
285 virtual const SymbolTable *InputSymbols() const = 0;
287 // Return output label symbol table; return nullptr if not specified.
288 virtual const SymbolTable *OutputSymbols() const = 0;
290 // For generic state iterator construction (not normally called directly by
292 virtual void InitStateIterator(StateIteratorData<Arc> *data) const = 0;
294 // For generic arc iterator construction (not normally called directly by
296 virtual void InitArcIterator(StateId s, ArcIteratorData<Arc> *data) const = 0;
298 // For generic matcher construction (not normally called directly by users).
299 virtual MatcherBase<Arc> *InitMatcher(MatchType match_type) const;
302 bool WriteFile(const string &filename) const {
303 if (!filename.empty()) {
304 std::ofstream strm(filename,
305 std::ios_base::out | std::ios_base::binary);
307 LOG(ERROR) << "Fst::Write: Can't open file: " << filename;
310 bool val = Write(strm, FstWriteOptions(filename));
311 if (!val) LOG(ERROR) << "Fst::Write failed: " << filename;
314 return Write(std::cout, FstWriteOptions("standard output"));
319 // A useful alias when using StdArc.
320 using StdFst = Fst<StdArc>;
322 // State and arc iterator definitions.
324 // State iterator interface templated on the Arc definition; used for
325 // StateIterator specializations returned by the InitStateIterator FST method.
327 class StateIteratorBase {
329 using StateId = typename Arc::StateId;
331 virtual ~StateIteratorBase() {}
334 virtual bool Done() const = 0;
335 // Returns current state (when !Done()).
336 virtual StateId Value() const = 0;
337 // Advances to next state (when !Done()).
338 virtual void Next() = 0;
339 // Resets to initial condition.
340 virtual void Reset() = 0;
343 // StateIterator initialization data.
346 struct StateIteratorData {
347 using StateId = typename Arc::StateId;
349 // Specialized iterator if non-zero.
350 StateIteratorBase<Arc> *base;
351 // Otherwise, the total number of states.
354 StateIteratorData() : base(nullptr), nstates(0) {}
356 StateIteratorData(const StateIteratorData &) = delete;
357 StateIteratorData &operator=(const StateIteratorData &) = delete;
360 // Generic state iterator, templated on the FST definition (a wrapper
361 // around a pointer to a specific one). Here is a typical use:
363 // for (StateIterator<StdFst> siter(fst);
366 // StateId s = siter.Value();
370 class StateIterator {
372 using Arc = typename FST::Arc;
373 using StateId = typename Arc::StateId;
375 explicit StateIterator(const FST &fst) : s_(0) {
376 fst.InitStateIterator(&data_);
379 ~StateIterator() { delete data_.base; }
382 return data_.base ? data_.base->Done() : s_ >= data_.nstates;
385 StateId Value() const { return data_.base ? data_.base->Value() : s_; }
404 StateIteratorData<Arc> data_;
408 // Flags to control the behavior on an arc iterator.
409 static constexpr uint32 kArcILabelValue =
410 0x0001; // Value() gives valid ilabel.
411 static constexpr uint32 kArcOLabelValue = 0x0002; // " " " olabel.
412 static constexpr uint32 kArcWeightValue = 0x0004; // " " " weight.
413 static constexpr uint32 kArcNextStateValue =
414 0x0008; // " " " nextstate.
415 static constexpr uint32 kArcNoCache = 0x0010; // No need to cache arcs.
417 static constexpr uint32 kArcValueFlags =
418 kArcILabelValue | kArcOLabelValue | kArcWeightValue | kArcNextStateValue;
420 static constexpr uint32 kArcFlags = kArcValueFlags | kArcNoCache;
422 // Arc iterator interface, templated on the arc definition; used for arc
423 // iterator specializations that are returned by the InitArcIterator FST method.
425 class ArcIteratorBase {
427 using StateId = typename Arc::StateId;
429 virtual ~ArcIteratorBase() {}
432 virtual bool Done() const = 0;
433 // Returns current arc (when !Done()).
434 virtual const Arc &Value() const = 0;
435 // Advances to next arc (when !Done()).
436 virtual void Next() = 0;
437 // Returns current position.
438 virtual size_t Position() const = 0;
439 // Returns to initial condition.
440 virtual void Reset() = 0;
441 // Advances to arbitrary arc by position.
442 virtual void Seek(size_t) = 0;
443 // Returns current behavorial flags
444 virtual uint32 Flags() const = 0;
445 // Sets behavorial flags.
446 virtual void SetFlags(uint32, uint32) = 0;
449 // ArcIterator initialization data.
451 struct ArcIteratorData {
453 : base(nullptr), arcs(nullptr), narcs(0), ref_count(nullptr) {}
455 ArcIteratorData(const ArcIteratorData &) = delete;
457 ArcIteratorData &operator=(const ArcIteratorData &) = delete;
459 ArcIteratorBase<Arc> *base; // Specialized iterator if non-zero.
460 const Arc *arcs; // O.w. arcs pointer
461 size_t narcs; // ... and arc count.
462 int *ref_count; // ... and reference count if non-zero.
465 // Generic arc iterator, templated on the FST definition (a wrapper around a
466 // pointer to a specific one). Here is a typical use:
468 // for (ArcIterator<StdFst> aiter(fst, s);
471 // StdArc &arc = aiter.Value();
477 using Arc = typename FST::Arc;
478 using StateId = typename Arc::StateId;
480 ArcIterator(const FST &fst, StateId s) : i_(0) {
481 fst.InitArcIterator(s, &data_);
484 explicit ArcIterator(const ArcIteratorData<Arc> &data) : data_(data), i_(0) {
485 if (data_.ref_count) ++(*data_.ref_count);
491 } else if (data_.ref_count) {
492 --(*data_.ref_count);
497 return data_.base ? data_.base->Done() : i_ >= data_.narcs;
500 const Arc &Value() const {
501 return data_.base ? data_.base->Value() : data_.arcs[i_];
520 void Seek(size_t a) {
528 size_t Position() const { return data_.base ? data_.base->Position() : i_; }
530 uint32 Flags() const {
532 return data_.base->Flags();
534 return kArcValueFlags;
538 void SetFlags(uint32 flags, uint32 mask) {
539 if (data_.base) data_.base->SetFlags(flags, mask);
543 ArcIteratorData<Arc> data_;
549 // ArcIterator placement operator new and destroy function; new needs to be in
550 // the global namespace.
553 void *operator new(size_t size,
554 fst::MemoryPool<fst::ArcIterator<FST>> *pool) {
555 return pool->Allocate();
561 void Destroy(ArcIterator<FST> *aiter, MemoryPool<ArcIterator<FST>> *pool) {
563 aiter->~ArcIterator<FST>();
568 // Matcher definitions.
571 MatcherBase<Arc> *Fst<Arc>::InitMatcher(MatchType match_type) const {
572 return nullptr; // One should just use the default matcher.
575 // FST accessors, useful in high-performance applications.
579 // General case, requires non-abstract, 'final' methods. Use for inlining.
582 inline typename F::Arc::Weight Final(const F &fst, typename F::Arc::StateId s) {
583 return fst.F::Final(s);
587 inline ssize_t NumArcs(const F &fst, typename F::Arc::StateId s) {
588 return fst.F::NumArcs(s);
592 inline ssize_t NumInputEpsilons(const F &fst, typename F::Arc::StateId s) {
593 return fst.F::NumInputEpsilons(s);
597 inline ssize_t NumOutputEpsilons(const F &fst, typename F::Arc::StateId s) {
598 return fst.F::NumOutputEpsilons(s);
601 // Fst<Arc> case, abstract methods.
604 inline typename Arc::Weight Final(const Fst<Arc> &fst,
605 typename Arc::StateId s) {
610 inline size_t NumArcs(const Fst<Arc> &fst, typename Arc::StateId s) {
611 return fst.NumArcs(s);
615 inline size_t NumInputEpsilons(const Fst<Arc> &fst, typename Arc::StateId s) {
616 return fst.NumInputEpsilons(s);
620 inline size_t NumOutputEpsilons(const Fst<Arc> &fst, typename Arc::StateId s) {
621 return fst.NumOutputEpsilons(s);
624 } // namespace internal
626 // FST implementation base.
628 // This is the recommended FST implementation base class. It will handle
629 // reference counts, property bits, type information and symbols.
636 using StateId = typename Arc::StateId;
637 using Weight = typename Arc::Weight;
639 FstImpl() : properties_(0), type_("null") {}
641 FstImpl(const FstImpl<Arc> &impl)
642 : properties_(impl.properties_),
644 isymbols_(impl.isymbols_ ? impl.isymbols_->Copy() : nullptr),
645 osymbols_(impl.osymbols_ ? impl.osymbols_->Copy() : nullptr) {}
647 virtual ~FstImpl() {}
649 const string &Type() const { return type_; }
651 void SetType(const string &type) { type_ = type; }
653 virtual uint64 Properties() const { return properties_; }
655 virtual uint64 Properties(uint64 mask) const { return properties_ & mask; }
657 void SetProperties(uint64 props) {
658 properties_ &= kError; // kError can't be cleared.
659 properties_ |= props;
662 void SetProperties(uint64 props, uint64 mask) {
663 properties_ &= ~mask | kError; // kError can't be cleared.
664 properties_ |= props & mask;
667 // Allows (only) setting error bit on const FST implementations.
668 void SetProperties(uint64 props, uint64 mask) const {
669 if (mask != kError) {
670 FSTERROR() << "FstImpl::SetProperties() const: Can only set kError";
672 properties_ |= kError;
675 const SymbolTable *InputSymbols() const { return isymbols_.get(); }
677 const SymbolTable *OutputSymbols() const { return osymbols_.get(); }
679 SymbolTable *InputSymbols() { return isymbols_.get(); }
681 SymbolTable *OutputSymbols() { return osymbols_.get(); }
683 void SetInputSymbols(const SymbolTable *isyms) {
684 isymbols_.reset(isyms ? isyms->Copy() : nullptr);
687 void SetOutputSymbols(const SymbolTable *osyms) {
688 osymbols_.reset(osyms ? osyms->Copy() : nullptr);
691 // Reads header and symbols from input stream, initializes FST, and returns
692 // the header. If opts.header is non-null, skips reading and uses the option
693 // value instead. If opts.[io]symbols is non-null, reads in (if present), but
694 // uses the option value.
695 bool ReadHeader(std::istream &strm, const FstReadOptions &opts,
696 int min_version, FstHeader *hdr);
698 // Writes header and symbols to output stream. If opts.header is false, skips
699 // writing header. If opts.[io]symbols is false, skips writing those symbols.
700 // This method is needed for implementations that implement Write methods.
701 void WriteHeader(std::ostream &strm, const FstWriteOptions &opts,
702 int version, FstHeader *hdr) const {
703 if (opts.write_header) {
704 hdr->SetFstType(type_);
705 hdr->SetArcType(Arc::Type());
706 hdr->SetVersion(version);
707 hdr->SetProperties(properties_);
708 int32 file_flags = 0;
709 if (isymbols_ && opts.write_isymbols) {
710 file_flags |= FstHeader::HAS_ISYMBOLS;
712 if (osymbols_ && opts.write_osymbols) {
713 file_flags |= FstHeader::HAS_OSYMBOLS;
715 if (opts.align) file_flags |= FstHeader::IS_ALIGNED;
716 hdr->SetFlags(file_flags);
717 hdr->Write(strm, opts.source);
719 if (isymbols_ && opts.write_isymbols) isymbols_->Write(strm);
720 if (osymbols_ && opts.write_osymbols) osymbols_->Write(strm);
723 // Writes out header and symbols to output stream. If opts.header is false,
724 // skips writing header. If opts.[io]symbols is false, skips writing those
725 // symbols. `type` is the FST type being written. This method is used in the
726 // cross-type serialization methods Fst::WriteFst.
727 static void WriteFstHeader(const Fst<Arc> &fst, std::ostream &strm,
728 const FstWriteOptions &opts, int version,
729 const string &type, uint64 properties,
731 if (opts.write_header) {
732 hdr->SetFstType(type);
733 hdr->SetArcType(Arc::Type());
734 hdr->SetVersion(version);
735 hdr->SetProperties(properties);
736 int32 file_flags = 0;
737 if (fst.InputSymbols() && opts.write_isymbols) {
738 file_flags |= FstHeader::HAS_ISYMBOLS;
740 if (fst.OutputSymbols() && opts.write_osymbols) {
741 file_flags |= FstHeader::HAS_OSYMBOLS;
743 if (opts.align) file_flags |= FstHeader::IS_ALIGNED;
744 hdr->SetFlags(file_flags);
745 hdr->Write(strm, opts.source);
747 if (fst.InputSymbols() && opts.write_isymbols) {
748 fst.InputSymbols()->Write(strm);
750 if (fst.OutputSymbols() && opts.write_osymbols) {
751 fst.OutputSymbols()->Write(strm);
755 // In serialization routines where the header cannot be written until after
756 // the machine has been serialized, this routine can be called to seek to the
757 // beginning of the file an rewrite the header with updated fields. It
758 // repositions the file pointer back at the end of the file. Returns true on
759 // success, false on failure.
760 static bool UpdateFstHeader(const Fst<Arc> &fst, std::ostream &strm,
761 const FstWriteOptions &opts, int version,
762 const string &type, uint64 properties,
763 FstHeader *hdr, size_t header_offset) {
764 strm.seekp(header_offset);
766 LOG(ERROR) << "Fst::UpdateFstHeader: Write failed: " << opts.source;
769 WriteFstHeader(fst, strm, opts, version, type, properties, hdr);
771 LOG(ERROR) << "Fst::UpdateFstHeader: Write failed: " << opts.source;
774 strm.seekp(0, std::ios_base::end);
776 LOG(ERROR) << "Fst::UpdateFstHeader: Write failed: " << opts.source;
783 mutable uint64 properties_; // Property bits.
786 string type_; // Unique name of FST class.
787 std::unique_ptr<SymbolTable> isymbols_;
788 std::unique_ptr<SymbolTable> osymbols_;
792 bool FstImpl<Arc>::ReadHeader(std::istream &strm, const FstReadOptions &opts,
793 int min_version, FstHeader *hdr) {
796 } else if (!hdr->Read(strm, opts.source)) {
800 LOG(INFO) << "FstImpl::ReadHeader: source: " << opts.source
801 << ", fst_type: " << hdr->FstType()
802 << ", arc_type: " << Arc::Type()
803 << ", version: " << hdr->Version()
804 << ", flags: " << hdr->GetFlags();
806 if (hdr->FstType() != type_) {
807 LOG(ERROR) << "FstImpl::ReadHeader: FST not of type " << type_
808 << ": " << opts.source;
811 if (hdr->ArcType() != Arc::Type()) {
812 LOG(ERROR) << "FstImpl::ReadHeader: Arc not of type " << Arc::Type()
813 << ": " << opts.source;
816 if (hdr->Version() < min_version) {
817 LOG(ERROR) << "FstImpl::ReadHeader: Obsolete " << type_
818 << " FST version: " << opts.source;
821 properties_ = hdr->Properties();
822 if (hdr->GetFlags() & FstHeader::HAS_ISYMBOLS) {
823 isymbols_.reset(SymbolTable::Read(strm, opts.source));
825 // Deletes input symbol table.
826 if (!opts.read_isymbols) SetInputSymbols(nullptr);
827 if (hdr->GetFlags() & FstHeader::HAS_OSYMBOLS) {
828 osymbols_.reset(SymbolTable::Read(strm, opts.source));
830 // Deletes output symbol table.
831 if (!opts.read_osymbols) SetOutputSymbols(nullptr);
833 isymbols_.reset(opts.isymbols->Copy());
836 osymbols_.reset(opts.osymbols->Copy());
841 } // namespace internal
844 uint64 TestProperties(const Fst<Arc> &fst, uint64 mask, uint64 *known);
846 // This is a helper class template useful for attaching an FST interface to
847 // its implementation, handling reference counting.
848 template <class Impl, class FST = Fst<typename Impl::Arc>>
849 class ImplToFst : public FST {
851 using Arc = typename Impl::Arc;
852 using StateId = typename Arc::StateId;
853 using Weight = typename Arc::Weight;
854 using FST::operator=;
856 StateId Start() const override { return impl_->Start(); }
858 Weight Final(StateId s) const override { return impl_->Final(s); }
860 size_t NumArcs(StateId s) const override { return impl_->NumArcs(s); }
862 size_t NumInputEpsilons(StateId s) const override {
863 return impl_->NumInputEpsilons(s);
866 size_t NumOutputEpsilons(StateId s) const override {
867 return impl_->NumOutputEpsilons(s);
870 uint64 Properties(uint64 mask, bool test) const override {
872 uint64 knownprops, testprops = TestProperties(*this, mask, &knownprops);
873 impl_->SetProperties(testprops, knownprops);
874 return testprops & mask;
876 return impl_->Properties(mask);
880 const string &Type() const override { return impl_->Type(); }
882 const SymbolTable *InputSymbols() const override {
883 return impl_->InputSymbols();
886 const SymbolTable *OutputSymbols() const override {
887 return impl_->OutputSymbols();
891 explicit ImplToFst(std::shared_ptr<Impl> impl) : impl_(std::move(impl)) {}
893 // This constructor presumes there is a copy constructor for the
895 ImplToFst(const ImplToFst<Impl, FST> &fst, bool safe) {
897 impl_ = std::make_shared<Impl>(*(fst.impl_));
903 // Returns raw pointers to the shared object.
904 const Impl *GetImpl() const { return impl_.get(); }
906 Impl *GetMutableImpl() const { return impl_.get(); }
908 // Returns a ref-counted smart poiner to the implementation.
909 std::shared_ptr<Impl> GetSharedImpl() const { return impl_; }
911 bool Unique() const { return impl_.unique(); }
913 void SetImpl(std::shared_ptr<Impl> impl) { impl_ = impl; }
916 template <class IFST, class OFST>
917 friend void Cast(const IFST &ifst, OFST *ofst);
919 std::shared_ptr<Impl> impl_;
922 // Converts FSTs by casting their implementations, where this makes sense
923 // (which excludes implementations with weight-dependent virtual methods).
924 // Must be a friend of the FST classes involved (currently the concrete FSTs:
925 // ConstFst, CompactFst, and VectorFst). This can only be safely used for arc
926 // types that have identical storage characteristics. As with an FST
927 // copy constructor and Copy() method, this is a constant time operation
928 // (but subject to copy-on-write if it is a MutableFst and modified).
929 template <class IFST, class OFST>
930 void Cast(const IFST &ifst, OFST *ofst) {
931 using OImpl = typename OFST::Impl;
932 ofst->impl_ = std::shared_ptr<OImpl>(ifst.impl_,
933 reinterpret_cast<OImpl *>(ifst.impl_.get()));
936 // FST serialization.
939 void FstToString(const Fst<Arc> &fst, string *result) {
940 std::ostringstream ostrm;
941 fst.Write(ostrm, FstWriteOptions("FstToString"));
942 *result = ostrm.str();
946 void FstToString(const Fst<Arc> &fst, string *result,
947 const FstWriteOptions &options) {
948 std::ostringstream ostrm;
949 fst.Write(ostrm, options);
950 *result = ostrm.str();
954 Fst<Arc> *StringToFst(const string &s) {
955 std::istringstream istrm(s);
956 return Fst<Arc>::Read(istrm, FstReadOptions("StringToFst"));
961 #endif // FST_LIB_FST_H_