4848 efficient to compose with.
4949*/
5050
51- #ifdef _MSC_VER
5251#include < unordered_map>
5352using std::unordered_map;
54- #elif __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
55- #include < unordered_map>
56- using std::unordered_map;
57- #else
58- #include < tr1/unordered_map>
59- using std::tr1::unordered_map;
60- #endif
6153
6254#include < algorithm>
6355#include < string>
@@ -77,9 +69,9 @@ namespace fst {
7769*/
7870
7971
80- template < class Arc ,
81- class LabelT = int32> // make the vector<Label> things actually vector<int32> for
82- // easier compatibility with Kaldi code.
72+ template <class Arc ,
73+ class LabelT = int32> // make the vector<Label> things actually vector<int32> for
74+ // easier compatibility with Kaldi code.
8375class ContextFstImpl : public CacheImpl <Arc> {
8476 public:
8577
@@ -94,10 +86,8 @@ class ContextFstImpl : public CacheImpl<Arc> {
9486 typedef typename Arc::Weight Weight;
9587 typedef typename Arc::StateId StateId;
9688 typedef typename Arc::Label Label;
97- #if OPENFST_VER >= 10400
9889 typedef DefaultCacheStore<Arc> Store;
9990 typedef typename Store::State State;
100- #endif
10191 typedef unordered_map<vector<LabelT>,
10292 StateId, kaldi::VectorHasher<LabelT> > VectorToStateType;
10393 typedef unordered_map<vector<LabelT>,
@@ -119,7 +109,7 @@ class ContextFstImpl : public CacheImpl<Arc> {
119109 // See \ref tree_ilabel
120110 // "http://kaldi-asr.org/doc/tree_externals.html#tree_ilabel" for more
121111 // information about the ilabel_info.
122- const vector<vector<LabelT> > &ILabelInfo () { return ilabel_info_; }
112+ const vector<vector<LabelT> > &ILabelInfo () const { return ilabel_info_; }
123113
124114 StateId Start ();
125115
@@ -211,124 +201,66 @@ class ContextFstImpl : public CacheImpl<Arc> {
211201
212202template <class Arc ,
213203 class LabelT = int32> // make the vector<LabelT> things actually vector<int32> for
214- // easier compatibility with Kaldi code.
215- class ContextFst : public Fst < Arc> {
204+ // easier compatibility with Kaldi code.
205+ class ContextFst : public ImplToFst <ContextFstImpl< Arc, LabelT> > {
216206 public:
217- friend class ArcIterator < ContextFst<Arc> >;
218- friend class StateIterator < ContextFst<Arc> >;
219- #if OPENFST_VER >= 10400
220- #else
221- // We have to supply the default template argument below to work around a
222- // Visual Studio bug.
223- friend class CacheArcIterator < ContextFst<Arc>,
224- DefaultCacheStateAllocator<CacheState<Arc> > >;
225- #endif
207+ friend class ArcIterator <ContextFst<Arc>>;
208+ friend class StateIterator <ContextFst<Arc>>;
226209
227210 typedef typename Arc::Weight Weight;
228211 typedef typename Arc::Label Label;
229212 typedef typename Arc::StateId StateId;
230- #if OPENFST_VER >= 10400
231213 typedef DefaultCacheStore<Arc> Store;
232214 typedef typename Store::State State;
233- #else
234- typedef CacheState<Arc> State;
235- #endif
215+ typedef ContextFstImpl<Arc, LabelT> Impl;
236216
237217 // / See \ref graph_context for more details.
238218 ContextFst (Label subsequential_symbol, // epsilon not allowed.
239219 const vector<LabelT>& phones, // symbols on output side of fst.
240220 const vector<LabelT>& disambig_syms, // symbols on output side of fst.
241221 int32 N, // Size of context window
242- int32 P): // Pos of "central" phone in ctx window, from 0..N-1.
243- impl_ ( new ContextFstImpl<Arc, LabelT>(subsequential_symbol, phones, disambig_syms, N, P))
244- { assert (std::numeric_limits<LabelT>::is_signed); }
245-
246- ContextFst ( const ContextFst<Arc, LabelT> &fst, bool reset = false );
222+ int32 P) // Pos of "central" phone in ctx window, from 0..N-1.
223+ : ImplToFst<Impl>(std::make_shared<Impl>(
224+ subsequential_symbol, phones, disambig_syms, N, P)) {
225+ assert (std::numeric_limits<LabelT>::is_signed);
226+ }
247227
248- #if OPENFST_VER >= 10500
249- #else
250- virtual ~ContextFst () { if (!impl_->DecrRefCount ()) delete impl_; }
251- #endif
228+ ContextFst (const ContextFst<Arc, LabelT> &fst, bool safe = false )
229+ : ImplToFst<Impl>(fst, safe) {}
252230
253- virtual StateId Start () const { return impl_->Start (); }
231+ ContextFst<Arc, LabelT> *Copy (bool safe = false ) const override {
232+ return new ContextFst<Arc, LabelT>(*this , safe);
233+ }
254234
255- virtual Weight Final (StateId s ) const { return impl_-> Final (s); }
235+ inline void InitStateIterator (StateIteratorData<Arc> *data ) const override ;
256236
257- StateId NumStates () const { return impl_->NumStates (); }
237+ void InitArcIterator (StateId s, ArcIteratorData<Arc> *data) const override {
238+ GetMutableImpl ()->InitArcIterator (s, data);
239+ }
258240
259241 // This function is used in ContextMatcher.
260242 // Semantically this is not really const, as it causes states to be
261243 // added to the state table in impl_, and the input vocabulary to be
262244 // expanded, but C++ lets us make this const, and compose.h
263- // requires it (because it provides const fst's to the Matcher
264- // object.
245+ // requires it (because it provides const fst's to the Matcher object.
265246 bool CreateArc (StateId s, Label olabel, Arc *oarc) const {
266- return impl_->CreateArc (s, olabel, oarc);
267- }
268-
269- size_t NumArcs (StateId s) const { return impl_->NumArcs (s); }
270-
271- size_t NumInputEpsilons (StateId s) const {
272- return impl_->NumInputEpsilons (s);
273- }
274-
275- size_t NumOutputEpsilons (StateId s) const {
276- return impl_->NumOutputEpsilons (s);
277- }
278-
279- virtual uint64 Properties (uint64 mask, bool test) const {
280- if (test) {
281- uint64 knownprops, testprops = TestProperties (*this , mask, &knownprops);
282- impl_->SetProperties (knownprops, testprops);
283- return testprops & mask;
284- } else {
285- return impl_->Properties (mask);
286- }
247+ return GetMutableImpl ()->CreateArc (s, olabel, oarc);
287248 }
288249
289250 // Careful: the output of ILabelInfo depends on what has been visited.
290- const vector<vector<LabelT> > &ILabelInfo () { return impl_->ILabelInfo (); }
291-
292- virtual const string& Type () const { return impl_->Type (); }
293-
294- virtual ContextFst<Arc> *Copy (bool reset = false ) const {
295- return new ContextFst<Arc>(*this , reset);
251+ const vector<vector<LabelT> > &ILabelInfo () const {
252+ return GetImpl ()->ILabelInfo ();
296253 }
297254
298- virtual const SymbolTable* InputSymbols () const {
299- return impl_->InputSymbols ();
300- }
301-
302- virtual const SymbolTable* OutputSymbols () const {
303- return impl_->OutputSymbols ();
304- }
305-
306- virtual inline void InitStateIterator (StateIteratorData<Arc> *data) const ;
307-
308- virtual void InitArcIterator (StateId s, ArcIteratorData<Arc> *data) const {
309- impl_->InitArcIterator (s, data);
310- }
311-
312- friend class CacheStateIterator <ContextFst<Arc> >; // so it can see impl_.
313255 private:
314- #if OPENFST_VER >= 10500
315- std::shared_ptr<ContextFstImpl<Arc, LabelT> > impl_; // protected so CacheStateIterator
316- ContextFstImpl<Arc, LabelT> *GetImpl () const { return impl_.get (); }
317- #else
318- ContextFstImpl<Arc, LabelT> *impl_; // protected so CacheStateIterator
319- // Makes visible to friends.
320- ContextFstImpl<Arc, LabelT> *GetImpl () const { return impl_; }
321- // would be: ImplToFst<ContextFstImpl<Arc, LabelT> >::GetImpl();
322- // but need to convert to using the ImplToFst stuff.
323-
324- void operator = (const ContextFstImpl<Arc> &fst); // disallow
325- #endif
256+ using ImplToFst<Impl>::GetImpl;
257+ using ImplToFst<Impl>::GetMutableImpl;
326258
259+ ContextFst &operator =(const ContextFst &fst) = delete ;
327260};
328261
329262// / Useful utility function for writing these vectors to disk.
330- // / writes as int32 for binary compatibility since I will typically
331- // / be "int".
263+ // / writes as int32 for binary compatibility since it will typically be "int".
332264template <class I >
333265void WriteILabelInfo (std::ostream &os, bool binary,
334266 const vector<vector<I> > &info);
@@ -356,7 +288,7 @@ class StateIterator< ContextFst<A> >
356288 : public CacheStateIterator< ContextFst<A> > {
357289 public:
358290 explicit StateIterator (const ContextFst<A> &fst)
359- : CacheStateIterator< ContextFst<A> >(fst, fst.GetImpl ()) {}
291+ : CacheStateIterator< ContextFst<A> >(fst, fst.GetMutableImpl ()) {}
360292};
361293
362294
@@ -369,13 +301,10 @@ class ArcIterator< ContextFst<A> >
369301 typedef typename A::StateId StateId;
370302
371303 ArcIterator (const ContextFst<A> &fst, StateId s)
372- : CacheArcIterator< ContextFst<A> >(fst.GetImpl (), s) {
304+ : CacheArcIterator< ContextFst<A> >(fst.GetMutableImpl (), s) {
373305 if (!fst.GetImpl ()->HasArcs (s)) // arcs not already computed.
374- fst.GetImpl ()->Expand (s);
306+ fst.GetMutableImpl ()->Expand (s);
375307 }
376-
377- private:
378- DISALLOW_COPY_AND_ASSIGN (ArcIterator);
379308};
380309
381310template <class A , class I > inline
0 commit comments