21 #ifndef FST_STATE_REACHABLE_H_ 22 #define FST_STATE_REACHABLE_H_ 44 template <
class Arc,
class I =
typename Arc::StateId,
class S = IntervalSet<I>>
47 using Label =
typename Arc::Label;
56 std::vector<Index> *state2index)
59 state2index_(state2index),
60 index_(state2index->empty() ? 1 : -1),
68 while (isets_->size() <= s) isets_->push_back(S());
69 while (state2index_->size() <= s) state2index_->push_back(-1);
70 if (fst_.
Final(s) != Weight::Zero()) {
72 auto *intervals = (*isets_)[s].MutableIntervals();
75 FSTERROR() <<
"IntervalReachVisitor: state2index map must be empty " 80 const auto index = (*state2index_)[s];
82 FSTERROR() <<
"IntervalReachVisitor: state2index map incomplete";
86 intervals->push_back(
Interval(index, index + 1));
88 intervals->push_back(
Interval(index_, index_ + 1));
89 (*state2index_)[s] = index_++;
98 FSTERROR() <<
"IntervalReachVisitor: Cyclic input";
105 (*isets_)[s].Union((*isets_)[arc.nextstate]);
110 if (index_ >= 0 && fst_.
Final(s) != Weight::Zero()) {
111 auto *intervals = (*isets_)[s].MutableIntervals();
112 (*intervals)[0].end = index_;
114 (*isets_)[s].Normalize();
116 (*isets_)[p].Union((*isets_)[s]);
122 bool Error()
const {
return error_; }
126 std::vector<ISet> *isets_;
127 std::vector<Index> *state2index_;
136 template <
class Arc,
class I =
typename Arc::StateId,
class S = IntervalSet<I>>
149 AcyclicStateReachable(fst);
151 CyclicStateReachable(fst);
156 FSTERROR() <<
"Copy constructor for state reachable class " 157 <<
"not implemented.";
166 if (s >= state2index_.size())
return false;
167 const auto i = state2index_[s];
169 FSTERROR() <<
"StateReachable: State non-final: " << s;
173 return isets_[s_].Member(i);
183 bool Error()
const {
return error_; }
190 if (reach_visitor.
Error()) error_ =
true;
193 void CyclicStateReachable(
const Fst<Arc> &fst) {
196 std::vector<StateId> scc;
199 if (reachable.
Error()) {
204 std::vector<size_t> nscc;
205 for (
StateId s = 0; s < scc.size(); ++s) {
206 const auto c = scc[s];
207 while (c >= nscc.size()) nscc.push_back(0);
212 state2index_.resize(scc.size(), -1);
213 isets_.resize(scc.size());
214 for (
StateId s = 0; s < scc.size(); ++s) {
215 const auto c = scc[s];
220 if (cfst.
Final(c) != Weight::Zero() && nscc[c] > 1) {
221 FSTERROR() <<
"StateReachable: Final state contained in a cycle";
229 std::vector<ISet> isets_;
230 std::vector<Index> state2index_;
238 #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
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
Weight Final(StateId s) const override
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 *)