21 #ifndef FST_STATE_REACHABLE_H_ 22 #define FST_STATE_REACHABLE_H_ 46 template <
class Arc,
class I =
typename Arc::StateId,
class S = IntervalSet<I>>
49 using Label =
typename Arc::Label;
58 std::vector<Index> *state2index)
61 state2index_(state2index),
62 index_(state2index->empty() ? 1 : -1),
70 while (isets_->size() <= s) isets_->push_back(S());
71 while (state2index_->size() <= s) state2index_->push_back(-1);
72 if (fst_.
Final(s) != Weight::Zero()) {
74 auto *intervals = (*isets_)[s].MutableIntervals();
77 FSTERROR() <<
"IntervalReachVisitor: state2index map must be empty " 82 const auto index = (*state2index_)[s];
84 FSTERROR() <<
"IntervalReachVisitor: state2index map incomplete";
88 intervals->push_back(
Interval(index, index + 1));
90 intervals->push_back(
Interval(index_, index_ + 1));
91 (*state2index_)[s] = index_++;
100 FSTERROR() <<
"IntervalReachVisitor: Cyclic input";
107 (*isets_)[s].Union((*isets_)[arc.nextstate]);
112 if (index_ >= 0 && fst_.
Final(s) != Weight::Zero()) {
113 auto *intervals = (*isets_)[s].MutableIntervals();
114 (*intervals)[0].end = index_;
116 (*isets_)[s].Normalize();
118 (*isets_)[p].Union((*isets_)[s]);
124 bool Error()
const {
return error_; }
128 std::vector<ISet> *isets_;
129 std::vector<Index> *state2index_;
138 template <
class Arc,
class I =
typename Arc::StateId,
class S = IntervalSet<I>>
151 AcyclicStateReachable(fst);
153 CyclicStateReachable(fst);
158 FSTERROR() <<
"Copy constructor for state reachable class " 159 <<
"not implemented.";
168 if (s >= state2index_.size())
return false;
169 const auto i = state2index_[s];
171 FSTERROR() <<
"StateReachable: State non-final: " << s;
175 return isets_[s_].Member(i);
185 bool Error()
const {
return error_; }
192 if (reach_visitor.
Error()) error_ =
true;
195 void CyclicStateReachable(
const Fst<Arc> &fst) {
198 std::vector<StateId> scc;
201 if (reachable.
Error()) {
206 std::vector<size_t> nscc;
207 for (
StateId s = 0; s < scc.size(); ++s) {
208 const auto c = scc[s];
209 while (c >= nscc.size()) nscc.push_back(0);
214 state2index_.resize(scc.size(), -1);
215 isets_.resize(scc.size());
216 for (
StateId s = 0; s < scc.size(); ++s) {
217 const auto c = scc[s];
222 if (cfst.
Final(c) != Weight::Zero() && nscc[c] > 1) {
223 FSTERROR() <<
"StateReachable: Final state contained in a cycle";
231 std::vector<ISet> isets_;
232 std::vector<Index> state2index_;
240 #endif // FST_STATE_REACHABLE_H_ bool InitState(StateId s, StateId r)
IntervalReachVisitor(const Fst< Arc > &fst, std::vector< S > *isets, std::vector< Index > *state2index)
void InitVisit(const Fst< Arc > &)
const std::vector< ISet > & IntervalSets()
virtual uint64_t Properties(uint64_t mask, bool test) const =0
StateReachable(const StateReachable< Arc > &reachable)
bool ForwardOrCrossArc(StateId s, const Arc &arc)
std::vector< Index > & State2Index()
virtual size_t NumArcs(StateId) const =0
bool BackArc(StateId s, const Arc &arc)
typename Arc::StateId StateId
void DfsVisit(const FST &fst, Visitor *visitor, ArcFilter filter, bool access_only=false)
virtual Weight Final(StateId) const =0
typename ISet::Interval Interval
Weight Final(StateId s) const override
typename Arc::Weight Weight
typename Arc::StateId StateId
constexpr bool TreeArc(StateId, const Arc &) const
typename ISet::Interval Interval
constexpr uint64_t kAcyclic
typename Arc::Weight Weight
typename Arc::Label Label
typename Arc::Label Label
void Condense(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, std::vector< typename Arc::StateId > *scc)
StateReachable(const Fst< Arc > &fst)
void FinishState(StateId s, StateId p, const Arc *)