56 template <
class A1,
class A2>
60 AddOnPair(std::shared_ptr<A1> a1, std::shared_ptr<A2> a2)
61 : a1_(std::move(a1)), a2_(std::move(a2)) {}
63 const A1 *
First()
const {
return a1_.get(); }
65 const A2 *
Second()
const {
return a2_.get(); }
72 bool have_addon1 =
false;
74 std::unique_ptr<A1> a1;
77 bool have_addon2 =
false;
79 std::unique_ptr<A1> a2;
82 return new AddOnPair(std::move(a1), std::move(a2));
86 bool have_addon1 = a1_ !=
nullptr;
88 if (have_addon1) a1_->Write(ostrm, opts);
89 bool have_addon2 = a2_ !=
nullptr;
91 if (have_addon2) a2_->Write(ostrm, opts);
96 std::shared_ptr<A1> a1_;
97 std::shared_ptr<A2> a2_;
108 template <
class FST,
class T>
112 using Arc =
typename FST::Arc;
126 std::shared_ptr<T> t =
nullptr)
127 : fst_(fst, true), t_(std::move(t)) {
130 SetInputSymbols(fst_.InputSymbols());
131 SetOutputSymbols(fst_.OutputSymbols());
137 std::shared_ptr<T> t =
nullptr)
138 : fst_(fst), t_(std::move(t)) {
141 SetInputSymbols(fst_.InputSymbols());
142 SetOutputSymbols(fst_.OutputSymbols());
150 SetInputSymbols(fst_.InputSymbols());
151 SetOutputSymbols(fst_.OutputSymbols());
163 return fst_.NumOutputEpsilons(s);
177 if (!impl->ReadHeader(strm, nopts, kMinFileVersion, &hdr))
return nullptr;
179 int32_t magic_number = 0;
181 if (magic_number != kAddOnMagicNumber) {
182 LOG(ERROR) <<
"AddOnImpl::Read: Bad add-on header: " << nopts.
source;
187 std::unique_ptr<FST>
fst(FST::Read(strm, fopts));
188 if (!fst)
return nullptr;
189 std::shared_ptr<T> t;
190 bool have_addon =
false;
193 t = std::shared_ptr<T>(T::Read(strm, fopts));
194 if (!t)
return nullptr;
204 WriteHeader(strm, nopts, kFileVersion, &hdr);
208 if (!fst_.Write(strm, fopts))
return false;
209 bool have_addon = !!t_;
212 if (have_addon) t_->Write(strm, opts);
217 fst_.InitStateIterator(data);
221 fst_.InitArcIterator(s, data);
235 explicit AddOnImpl(
const std::string &type) : t_() {
241 static constexpr
int kFileVersion = 1;
243 static constexpr
int kMinFileVersion = 1;
246 std::shared_ptr<T> t_;
254 #endif // FST_ADD_ON_H_ static AddOnPair * Read(std::istream &istrm, const FstReadOptions &opts)
void InitStateIterator(StateIteratorData< Arc > *data) const
bool Write(std::ostream &strm, const FstWriteOptions &opts) const
std::shared_ptr< A2 > SharedSecond() const
bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const
std::unique_ptr< T > WrapUnique(T *ptr)
std::shared_ptr< A1 > SharedFirst() const
void InitArcIterator(StateId s, ArcIteratorData< Arc > *data) const
std::ostream & WriteType(std::ostream &strm, const T t)
size_t NumOutputEpsilons(StateId s) const
AddOnPair(std::shared_ptr< A1 > a1, std::shared_ptr< A2 > a2)
constexpr uint64_t kCopyProperties
size_t NumArcs(StateId s) const
AddOnImpl(const Fst< Arc > &fst, const std::string &type, std::shared_ptr< T > t=nullptr)
std::shared_ptr< T > GetSharedAddOn() const
AddOnImpl(const AddOnImpl &impl)
constexpr int32_t kAddOnMagicNumber
const A2 * Second() const
const std::string & Type() const
constexpr uint64_t kFstProperties
typename Arc::Weight Weight
const T * GetAddOn() const
AddOnImpl(const FST &fst, const std::string &type, std::shared_ptr< T > t=nullptr)
size_t NumInputEpsilons(StateId s) const
typename Arc::Label Label
std::istream & ReadType(std::istream &strm, T *t)
bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const
const FST & GetFst() const
static NullAddOn * Read(std::istream &strm, const FstReadOptions &opts)
static AddOnImpl * Read(std::istream &strm, const FstReadOptions &opts)
constexpr uint64_t kExpanded
Weight Final(StateId s) const
void SetAddOn(std::shared_ptr< T > t)
typename Arc::StateId StateId