Skip to content

Commit bf2ed0a

Browse files
committed
Remove code for supporting OpenFst < 1.5.3.
This commit removes all code that has to do with supporting old OpenFst releases. It also makes some updates to TableMatcher, ContextFst and TrivialFactorWeight.
1 parent b23f720 commit bf2ed0a

25 files changed

+104
-674
lines changed

src/bin/phones-to-prons.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,7 @@ int main(int argc, char *argv[]) {
170170
<< "not reach end-state, or mismatched lexicon.)";
171171
if (g_kaldi_verbose_level >= 2) {
172172
KALDI_LOG << "phn2word FST is below:";
173-
#if OPENFST_VER >= 10400
174173
fst::FstPrinter<StdArc> fstprinter(phn2word, NULL, NULL, NULL, false, true, "\t");
175-
#else
176-
fst::FstPrinter<StdArc> fstprinter(phn2word, NULL, NULL, NULL, false, true);
177-
#endif
178174
fstprinter.Print(&std::cerr, "standard error");
179175
KALDI_LOG << "phone sequence is: ";
180176
for (size_t i = 0; i < phones.size(); i++)
@@ -219,5 +215,3 @@ int main(int argc, char *argv[]) {
219215
return -1;
220216
}
221217
}
222-
223-

src/fstext/context-fst-inl.h

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ typename ContextFstImpl<Arc, LabelT>::StateId
4141
VectorToStateIter iter = state_map_.find(seq);
4242
if (iter == state_map_.end()) { // Not already in map.
4343
StateId this_state_id = (StateId)state_seqs_.size();
44-
//This check is not needed with OpenFst >= 1.4
45-
#if OPENFST_VER >= 10400
46-
#else
47-
StateId this_state_id_check = CacheImpl<Arc>::AddState();
48-
// goes back to VectorFstBaseImpl<Arc>, inherited via CacheFst<Arc>
49-
assert(this_state_id == this_state_id_check);
50-
#endif
5144
state_seqs_.push_back(seq);
5245
state_map_[seq] = this_state_id;
5346
return this_state_id;
@@ -326,60 +319,34 @@ void ContextFstImpl<Arc, LabelT>::Expand(StateId s) { // expands arcs only [not
326319
// We just try adding all possible symbols on the output side.
327320
Arc arc;
328321
if (this->CreateArc(s, subsequential_symbol_, &arc)) {
329-
#if OPENFST_VER >= 10400
330322
this->PushArc(s, arc);
331-
#else
332-
this->AddArc(s, arc);
333-
#endif
334323
}
335324
for (typename kaldi::ConstIntegerSet<Label>::iterator iter = phone_syms_.begin();
336325
iter != phone_syms_.end(); ++iter) {
337326
Label phone = *iter;
338327
if (this->CreateArc(s, phone, &arc)) {
339-
#if OPENFST_VER >= 10400
340328
this->PushArc(s, arc);
341-
#else
342-
this->AddArc(s, arc);
343-
#endif
344329
}
345330
}
346331
for (typename kaldi::ConstIntegerSet<Label>::iterator iter = disambig_syms_.begin();
347332
iter != disambig_syms_.end(); ++iter) {
348333
Label disambig_sym = *iter;
349334
if (this->CreateArc(s, disambig_sym, &arc)) {
350-
#if OPENFST_VER >= 10400
351335
this->PushArc(s, arc);
352-
#else
353-
this->AddArc(s, arc);
354-
#endif
355336
}
356337
}
357338
this->SetArcs(s); // mark the arcs as "done". [so HasArcs returns true].
358339
}
359340

360341

361-
template<class Arc, class LabelT>
362-
ContextFst<Arc, LabelT>::ContextFst(const ContextFst<Arc, LabelT> &fst, bool reset) {
363-
#if OPENFST_VER >= 10500
364-
if (reset) {
365-
impl_ = std::make_shared<ContextFstImpl<Arc, LabelT> >(*(fst.impl_));
366-
} else {
367-
impl_ = fst.impl_;
368-
}
369-
#else
370-
if (reset) {
371-
impl_ = new ContextFstImpl<Arc, LabelT>(*(fst.impl_));
372-
// Copy constructor of ContextFstImpl.
373-
// Main use of calling with reset = true is to free up memory
374-
// (e.g. then you could delete original one). Might be useful in transcription
375-
// expansion during training.
376-
} else {
377-
impl_ = fst.impl_;
378-
impl_->IncrRefCount();
379-
}
380-
#endif
381-
}
382-
342+
// template<class Arc, class LabelT>
343+
// ContextFst<Arc, LabelT>::ContextFst(const ContextFst<Arc, LabelT> &fst, bool reset) {
344+
// if (reset) {
345+
// impl_ = std::make_shared<ContextFstImpl<Arc, LabelT> >(*(fst.impl_));
346+
// } else {
347+
// impl_ = fst.impl_;
348+
// }
349+
// }
383350

384351

385352
template<class Arc, class LabelT>

src/fstext/context-fst-test.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,7 @@ template<class Arc> static void TestContextFst(bool verbose, bool use_matcher) {
192192
}
193193

194194
if (verbose) { // Try to print the fst.
195-
#if OPENFST_VER >= 10400
196195
FstPrinter<Arc> fstprinter(cfst, cfst.InputSymbols(), cfst.OutputSymbols(), NULL, false, true, "\t");
197-
#else
198-
FstPrinter<Arc> fstprinter(cfst, cfst.InputSymbols(), cfst.OutputSymbols(), NULL, false, true);
199-
#endif
200196
fstprinter.Print(&std::cout, "standard output");
201197
}
202198

@@ -211,11 +207,7 @@ template<class Arc> static void TestContextFst(bool verbose, bool use_matcher) {
211207
if (verbose) {
212208
std::cout << "Sequence FST is:\n";
213209
{ // Try to print the fst.
214-
#if OPENFST_VER >= 10400
215210
FstPrinter<Arc> fstprinter(*f, f->InputSymbols(), f->OutputSymbols(), NULL, false, true, "\t");
216-
#else
217-
FstPrinter<Arc> fstprinter(*f, f->InputSymbols(), f->OutputSymbols(), NULL, false, true);
218-
#endif
219211
fstprinter.Print(&std::cout, "standard output");
220212
}
221213
}
@@ -257,13 +249,8 @@ template<class Arc> static void TestContextFst(bool verbose, bool use_matcher) {
257249
if (verbose) {
258250
std::cout << "Composed FST is:\n";
259251
{ // Try to print the fst.
260-
#if OPENFST_VER >= 10400
261252
FstPrinter<Arc> fstprinter(fst_composed, fst_composed.InputSymbols(),
262253
fst_composed.OutputSymbols(), NULL, false, true, "\t");
263-
#else
264-
FstPrinter<Arc> fstprinter(fst_composed, fst_composed.InputSymbols(),
265-
fst_composed.OutputSymbols(), NULL, false, true);
266-
#endif
267254
fstprinter.Print(&std::cout, "standard output");
268255
}
269256
}

src/fstext/context-fst.h

Lines changed: 34 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,8 @@
4848
efficient to compose with.
4949
*/
5050

51-
#ifdef _MSC_VER
5251
#include <unordered_map>
5352
using 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.
8375
class 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

212202
template <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".
332264
template<class I>
333265
void 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

381310
template <class A, class I> inline

0 commit comments

Comments
 (0)