37 #include <string_view> 59 template <
class A1,
class A2>
63 AddOnPair(std::shared_ptr<A1> a1, std::shared_ptr<A2> a2)
64 : a1_(std::move(a1)), a2_(std::move(a2)) {}
66 const A1 *
First()
const {
return a1_.get(); }
68 const A2 *
Second()
const {
return a2_.get(); }
75 bool have_addon1 =
false;
77 std::unique_ptr<A1> a1;
80 bool have_addon2 =
false;
82 std::unique_ptr<A1> a2;
85 return new AddOnPair(std::move(a1), std::move(a2));
89 bool have_addon1 = a1_ !=
nullptr;
91 if (have_addon1) a1_->Write(ostrm, opts);
92 bool have_addon2 = a2_ !=
nullptr;
94 if (have_addon2) a2_->Write(ostrm, opts);
99 std::shared_ptr<A1> a1_;
100 std::shared_ptr<A2> a2_;
111 template <
class FST,
class T>
115 using Arc =
typename FST::Arc;
129 std::shared_ptr<T> t =
nullptr)
130 : fst_(fst, true), t_(std::move(t)) {
133 SetInputSymbols(fst_.InputSymbols());
134 SetOutputSymbols(fst_.OutputSymbols());
140 std::shared_ptr<T> t =
nullptr)
141 : fst_(fst), t_(std::move(t)) {
144 SetInputSymbols(fst_.InputSymbols());
145 SetOutputSymbols(fst_.OutputSymbols());
153 SetInputSymbols(fst_.InputSymbols());
154 SetOutputSymbols(fst_.OutputSymbols());
166 return fst_.NumOutputEpsilons(s);
180 if (!impl->ReadHeader(strm, nopts, kMinFileVersion, &hdr))
return nullptr;
182 int32_t magic_number = 0;
184 if (magic_number != kAddOnMagicNumber) {
185 LOG(ERROR) <<
"AddOnImpl::Read: Bad add-on header: " << nopts.
source;
190 std::unique_ptr<FST>
fst(FST::Read(strm, fopts));
191 if (!fst)
return nullptr;
192 std::shared_ptr<T> t;
193 bool have_addon =
false;
196 t = std::shared_ptr<T>(T::Read(strm, fopts));
197 if (!t)
return nullptr;
207 WriteHeader(strm, nopts, kFileVersion, &hdr);
211 if (!fst_.Write(strm, fopts))
return false;
212 bool have_addon = !!t_;
215 if (have_addon) t_->Write(strm, opts);
220 fst_.InitStateIterator(data);
224 fst_.InitArcIterator(s, data);
238 explicit AddOnImpl(std::string_view type) : t_() {
244 static constexpr
int kFileVersion = 1;
246 static constexpr
int kMinFileVersion = 1;
249 std::shared_ptr<T> t_;
257 #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)
AddOnImpl(const FST &fst, std::string_view type, std::shared_ptr< T > t=nullptr)
constexpr uint64_t kCopyProperties
size_t NumArcs(StateId s) const
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
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
AddOnImpl(const Fst< Arc > &fst, std::string_view type, std::shared_ptr< T > t=nullptr)
void SetAddOn(std::shared_ptr< T > t)
typename Arc::StateId StateId