FST  openfst-1.8.4
OpenFst Library
state-map.h
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Class to map over/transform states e.g., sort transitions.
19 //
20 // Consider using when operation does not change the number of states.
21 
22 #ifndef FST_STATE_MAP_H_
23 #define FST_STATE_MAP_H_
24 
25 #include <sys/types.h>
26 
27 #include <algorithm>
28 #include <cstddef>
29 #include <cstdint>
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 #include <fst/log.h>
36 #include <fst/arc-map.h>
37 #include <fst/arc.h>
38 #include <fst/cache.h>
39 #include <fst/expanded-fst.h>
40 #include <fst/float-weight.h>
41 #include <fst/fst.h>
42 #include <fst/impl-to-fst.h>
43 #include <fst/mutable-fst.h>
44 #include <fst/properties.h>
45 
46 namespace fst {
47 
48 // StateMapper Interface. The class determines how states are mapped; useful for
49 // implementing operations that do not change the number of states.
50 //
51 // class StateMapper {
52 // public:
53 // using FromArc = ...;
54 // using ToArc = ...;
55 //
56 // // Typical constructor.
57 // StateMapper(const Fst<FromArc> &fst);
58 //
59 // // Required copy constructor that allows updating FST argument;
60 // // pass only if relevant and changed.
61 // StateMapper(const StateMapper &mapper, const Fst<FromArc> *fst = 0);
62 //
63 // // Specifies initial state of result.
64 // ToArc::StateId Start() const;
65 // // Specifies state's final weight in result.
66 // ToArc::Weight Final(ToArc::StateId state) const;
67 //
68 // // These methods iterate through a state's arcs in result.
69 //
70 // // Specifies state to iterate over.
71 // void SetState(ToArc::StateId state);
72 //
73 // // End of arcs?
74 // bool Done() const;
75 //
76 // // Current arc.
77 // const ToArc &Value() const;
78 //
79 // // Advances to next arc (when !Done)
80 // void Next();
81 //
82 // // Specifies input symbol table action the mapper requires (see above).
83 // MapSymbolsAction InputSymbolsAction() const;
84 //
85 // // Specifies output symbol table action the mapper requires (see above).
86 // MapSymbolsAction OutputSymbolsAction() const;
87 //
88 // // This specifies the known properties of an FST mapped by this
89 // // mapper. It takes as argument the input FST's known properties.
90 // uint64_t Properties(uint64_t props) const;
91 // };
92 //
93 // We include a various state map versions below. One dimension of variation is
94 // whether the mapping mutates its input, writes to a new result FST, or is an
95 // on-the-fly Fst. Another dimension is how we pass the mapper. We allow passing
96 // the mapper by pointer for cases that we need to change the state of the
97 // user's mapper. We also include map versions that pass the mapper by value or
98 // const reference when this suffices.
99 
100 // Maps an arc type A using a mapper function object C, passed by pointer. This
101 // version modifies the input FST.
102 template <class A, class C>
103 void StateMap(MutableFst<A> *fst, C *mapper) {
104  if (mapper->InputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
105  fst->SetInputSymbols(nullptr);
106  }
107  if (mapper->OutputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
108  fst->SetOutputSymbols(nullptr);
109  }
110  if (fst->Start() == kNoStateId) return;
111  const auto props = fst->Properties(kFstProperties, false);
112  fst->SetStart(mapper->Start());
113  for (StateIterator<Fst<A>> siter(*fst); !siter.Done(); siter.Next()) {
114  const auto state = siter.Value();
115  mapper->SetState(state);
116  fst->DeleteArcs(state);
117  for (; !mapper->Done(); mapper->Next()) {
118  fst->AddArc(state, mapper->Value());
119  }
120  fst->SetFinal(state, mapper->Final(state));
121  }
122  fst->SetProperties(mapper->Properties(props), kFstProperties);
123 }
124 
125 // Maps an arc type A using a mapper function object C, passed by value.
126 // This version modifies the input FST.
127 template <class A, class C>
128 void StateMap(MutableFst<A> *fst, C mapper) {
129  StateMap(fst, &mapper);
130 }
131 
132 // Maps an arc type A to an arc type B using mapper functor C, passed by
133 // pointer. This version writes to an output FST.
134 template <class A, class B, class C>
135 void StateMap(const Fst<A> &ifst, MutableFst<B> *ofst, C *mapper) {
136  ofst->DeleteStates();
137  if (mapper->InputSymbolsAction() == MAP_COPY_SYMBOLS) {
138  ofst->SetInputSymbols(ifst.InputSymbols());
139  } else if (mapper->InputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
140  ofst->SetInputSymbols(nullptr);
141  }
142  if (mapper->OutputSymbolsAction() == MAP_COPY_SYMBOLS) {
143  ofst->SetOutputSymbols(ifst.OutputSymbols());
144  } else if (mapper->OutputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
145  ofst->SetOutputSymbols(nullptr);
146  }
147  const auto iprops = ifst.Properties(kCopyProperties, false);
148  if (ifst.Start() == kNoStateId) {
149  if (iprops & kError) ofst->SetProperties(kError, kError);
150  return;
151  }
152  // Adds all states.
153  if (std::optional<typename A::StateId> num_states = ifst.NumStatesIfKnown()) {
154  ofst->ReserveStates(*num_states);
155  }
156  for (StateIterator<Fst<A>> siter(ifst); !siter.Done(); siter.Next()) {
157  ofst->AddState();
158  }
159  ofst->SetStart(mapper->Start());
160  for (StateIterator<Fst<A>> siter(ifst); !siter.Done(); siter.Next()) {
161  const auto state = siter.Value();
162  mapper->SetState(state);
163  for (; !mapper->Done(); mapper->Next()) {
164  ofst->AddArc(state, mapper->Value());
165  }
166  ofst->SetFinal(state, mapper->Final(state));
167  }
168  const auto oprops = ofst->Properties(kFstProperties, false);
169  ofst->SetProperties(mapper->Properties(iprops) | oprops, kFstProperties);
170 }
171 
172 // Maps an arc type A to an arc type B using mapper functor object C, passed by
173 // value. This version writes to an output FST.
174 template <class A, class B, class C>
175 void StateMap(const Fst<A> &ifst, MutableFst<B> *ofst, C mapper) {
176  StateMap(ifst, ofst, &mapper);
177 }
178 
180 
181 template <class A, class B, class C>
183 
184 // Facade around StateIteratorBase<A> inheriting from StateIteratorBase<B>.
185 template <class A, class B>
187  public:
188  using Arc = B;
189  using StateId = typename Arc::StateId;
190 
191  explicit StateMapStateIteratorBase(std::unique_ptr<StateIteratorBase<A>> base)
192  : base_(std::move(base)) {}
193 
194  bool Done() const final { return base_->Done(); }
195 
196  StateId Value() const final { return base_->Value(); }
197 
198  void Next() final { base_->Next(); }
199 
200  void Reset() final { base_->Reset(); }
201 
202  private:
203  std::unique_ptr<StateIteratorBase<A>> base_;
204 
205  StateMapStateIteratorBase() = delete;
206 };
207 
208 namespace internal {
209 
210 // Implementation of delayed StateMapFst.
211 template <class A, class B, class C>
212 class StateMapFstImpl : public CacheImpl<B> {
213  public:
214  using Arc = B;
215  using StateId = typename Arc::StateId;
216  using Weight = typename Arc::Weight;
217 
218  using FstImpl<B>::SetType;
222 
223  using CacheImpl<B>::PushArc;
224  using CacheImpl<B>::HasArcs;
227  using CacheImpl<B>::SetArcs;
230 
231  friend class StateIterator<StateMapFst<A, B, C>>;
232 
233  StateMapFstImpl(const Fst<A> &fst, const C &mapper,
234  const StateMapFstOptions &opts)
235  : CacheImpl<B>(opts),
236  fst_(fst.Copy()),
237  mapper_(new C(mapper, fst_.get())),
238  own_mapper_(true) {
239  Init();
240  }
241 
242  StateMapFstImpl(const Fst<A> &fst, C *mapper, const StateMapFstOptions &opts)
243  : CacheImpl<B>(opts),
244  fst_(fst.Copy()),
245  mapper_(mapper),
246  own_mapper_(false) {
247  Init();
248  }
249 
251  : CacheImpl<B>(impl),
252  fst_(impl.fst_->Copy(true)),
253  mapper_(new C(*impl.mapper_, fst_.get())),
254  own_mapper_(true) {
255  Init();
256  }
257 
258  ~StateMapFstImpl() override {
259  if (own_mapper_) delete mapper_;
260  }
261 
263  if (!HasStart()) SetStart(mapper_->Start());
264  return CacheImpl<B>::Start();
265  }
266 
268  if (!HasFinal(state)) SetFinal(state, mapper_->Final(state));
269  return CacheImpl<B>::Final(state);
270  }
271 
272  size_t NumArcs(StateId state) {
273  if (!HasArcs(state)) Expand(state);
274  return CacheImpl<B>::NumArcs(state);
275  }
276 
277  size_t NumInputEpsilons(StateId state) {
278  if (!HasArcs(state)) Expand(state);
279  return CacheImpl<B>::NumInputEpsilons(state);
280  }
281 
282  size_t NumOutputEpsilons(StateId state) {
283  if (!HasArcs(state)) Expand(state);
284  return CacheImpl<B>::NumOutputEpsilons(state);
285  }
286 
289  fst_->InitStateIterator(&data);
290  datb->base = data.base ? std::make_unique<StateMapStateIteratorBase<A, B>>(
291  std::move(data.base))
292  : nullptr;
293  datb->nstates = data.nstates;
294  }
295 
297  if (!HasArcs(state)) Expand(state);
298  CacheImpl<B>::InitArcIterator(state, data);
299  }
300 
301  uint64_t Properties() const override { return Properties(kFstProperties); }
302 
303  uint64_t Properties(uint64_t mask) const override {
304  if ((mask & kError) && (fst_->Properties(kError, false) ||
305  (mapper_->Properties(0) & kError))) {
306  SetProperties(kError, kError);
307  }
308  return FstImpl<Arc>::Properties(mask);
309  }
310 
311  void Expand(StateId state) {
312  // Adds exiting arcs.
313  for (mapper_->SetState(state); !mapper_->Done(); mapper_->Next()) {
314  PushArc(state, mapper_->Value());
315  }
316  SetArcs(state);
317  }
318 
319  const Fst<A> *GetFst() const { return fst_.get(); }
320 
321  private:
322  void Init() {
323  SetType("statemap");
324  if (mapper_->InputSymbolsAction() == MAP_COPY_SYMBOLS) {
325  SetInputSymbols(fst_->InputSymbols());
326  } else if (mapper_->InputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
327  SetInputSymbols(nullptr);
328  }
329  if (mapper_->OutputSymbolsAction() == MAP_COPY_SYMBOLS) {
330  SetOutputSymbols(fst_->OutputSymbols());
331  } else if (mapper_->OutputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
332  SetOutputSymbols(nullptr);
333  }
334  const auto props = fst_->Properties(kCopyProperties, false);
335  SetProperties(mapper_->Properties(props));
336  }
337 
338  std::unique_ptr<const Fst<A>> fst_;
339  C *mapper_;
340  bool own_mapper_;
341 };
342 
343 } // namespace internal
344 
345 // Maps an arc type A to an arc type B using Mapper function object
346 // C. This version is a delayed FST.
347 template <class A, class B, class C>
348 class StateMapFst : public ImplToFst<internal::StateMapFstImpl<A, B, C>> {
350 
351  public:
352  friend class ArcIterator<StateMapFst<A, B, C>>;
353 
354  using Arc = B;
355  using StateId = typename Arc::StateId;
356  using Weight = typename Arc::Weight;
358  using State = typename Store::State;
359  using typename Base::Impl;
360 
361  StateMapFst(const Fst<A> &fst, const C &mapper,
362  const StateMapFstOptions &opts)
363  : Base(std::make_shared<Impl>(fst, mapper, opts)) {}
364 
365  StateMapFst(const Fst<A> &fst, C *mapper, const StateMapFstOptions &opts)
366  : Base(std::make_shared<Impl>(fst, mapper, opts)) {}
367 
368  StateMapFst(const Fst<A> &fst, const C &mapper)
369  : Base(std::make_shared<Impl>(fst, mapper, StateMapFstOptions())) {}
370 
371  StateMapFst(const Fst<A> &fst, C *mapper)
372  : Base(std::make_shared<Impl>(fst, mapper, StateMapFstOptions())) {}
373 
374  // See Fst<>::Copy() for doc.
375  StateMapFst(const StateMapFst &fst, bool safe = false) : Base(fst, safe) {}
376 
377  // Get a copy of this StateMapFst. See Fst<>::Copy() for further doc.
378  StateMapFst *Copy(bool safe = false) const override {
379  return new StateMapFst(*this, safe);
380  }
381 
382  void InitStateIterator(StateIteratorData<B> *data) const override {
383  GetImpl()->InitStateIterator(data);
384  }
385 
386  void InitArcIterator(StateId state, ArcIteratorData<B> *data) const override {
387  GetMutableImpl()->InitArcIterator(state, data);
388  }
389 
390  protected:
391  using Base::GetImpl;
392  using Base::GetMutableImpl;
393 
394  private:
395  StateMapFst &operator=(const StateMapFst &) = delete;
396 };
397 
398 // Specialization for StateMapFst.
399 template <class A, class B, class C>
400 class ArcIterator<StateMapFst<A, B, C>>
401  : public CacheArcIterator<StateMapFst<A, B, C>> {
402  public:
403  using StateId = typename A::StateId;
404 
406  : CacheArcIterator<StateMapFst<A, B, C>>(fst.GetMutableImpl(), state) {
407  if (!fst.GetImpl()->HasArcs(state)) fst.GetMutableImpl()->Expand(state);
408  }
409 };
410 
411 // Utility mappers.
412 
413 // Mapper that returns its input.
414 template <class Arc>
416  public:
417  using FromArc = Arc;
418  using ToArc = Arc;
419 
420  using StateId = typename Arc::StateId;
421  using Weight = typename Arc::Weight;
422 
423  explicit IdentityStateMapper(const Fst<Arc> &fst) : fst_(fst) {}
424 
425  // Allows updating FST argument; pass only if changed.
427  const Fst<Arc> *fst = nullptr)
428  : fst_(fst ? *fst : mapper.fst_) {}
429 
430  StateId Start() const { return fst_.Start(); }
431 
432  Weight Final(StateId state) const { return fst_.Final(state); }
433 
434  void SetState(StateId state) {
435  aiter_ = std::make_unique<ArcIterator<Fst<Arc>>>(fst_, state);
436  }
437 
438  bool Done() const { return aiter_->Done(); }
439 
440  const Arc &Value() const { return aiter_->Value(); }
441 
442  void Next() { aiter_->Next(); }
443 
445  return MAP_COPY_SYMBOLS;
446  }
447 
449  return MAP_COPY_SYMBOLS;
450  }
451 
452  uint64_t Properties(uint64_t props) const { return props; }
453 
454  private:
455  const Fst<Arc> &fst_;
456  std::unique_ptr<ArcIterator<Fst<Arc>>> aiter_;
457 };
458 
459 template <class Arc>
461  public:
462  using FromArc = Arc;
463  using ToArc = Arc;
464 
465  using StateId = typename Arc::StateId;
466  using Weight = typename Arc::Weight;
467 
468  explicit ArcSumMapper(const Fst<Arc> &fst) : fst_(fst), i_(0) {}
469 
470  // Allows updating FST argument; pass only if changed.
471  ArcSumMapper(const ArcSumMapper<Arc> &mapper, const Fst<Arc> *fst = nullptr)
472  : fst_(fst ? *fst : mapper.fst_), i_(0) {}
473 
474  StateId Start() const { return fst_.Start(); }
475 
476  Weight Final(StateId state) const { return fst_.Final(state); }
477 
478  void SetState(StateId state) {
479  i_ = 0;
480  arcs_.clear();
481  arcs_.reserve(fst_.NumArcs(state));
482  for (ArcIterator<Fst<Arc>> aiter(fst_, state); !aiter.Done();
483  aiter.Next()) {
484  arcs_.push_back(aiter.Value());
485  }
486  // First sorts the exiting arcs by input label, output label and destination
487  // state and then sums weights of arcs with the same input label, output
488  // label, and destination state.
489  std::sort(arcs_.begin(), arcs_.end(), comp_);
490  size_t narcs = 0;
491  for (const auto &arc : arcs_) {
492  if (narcs > 0 && equal_(arc, arcs_[narcs - 1])) {
493  arcs_[narcs - 1].weight = Plus(arcs_[narcs - 1].weight, arc.weight);
494  } else {
495  arcs_[narcs] = arc;
496  ++narcs;
497  }
498  }
499  arcs_.resize(narcs);
500  }
501 
502  bool Done() const { return i_ >= arcs_.size(); }
503 
504  const Arc &Value() const { return arcs_[i_]; }
505 
506  void Next() { ++i_; }
507 
509  return MAP_COPY_SYMBOLS;
510  }
511 
513  return MAP_COPY_SYMBOLS;
514  }
515 
516  uint64_t Properties(uint64_t props) const {
517  return props & kArcSortProperties & kDeleteArcsProperties &
519  }
520 
521  private:
522  struct Compare {
523  bool operator()(const Arc &x, const Arc &y) const {
524  if (x.ilabel < y.ilabel) return true;
525  if (x.ilabel > y.ilabel) return false;
526  if (x.olabel < y.olabel) return true;
527  if (x.olabel > y.olabel) return false;
528  if (x.nextstate < y.nextstate) return true;
529  if (x.nextstate > y.nextstate) return false;
530  return false;
531  }
532  };
533 
534  struct Equal {
535  bool operator()(const Arc &x, const Arc &y) const {
536  return (x.ilabel == y.ilabel && x.olabel == y.olabel &&
537  x.nextstate == y.nextstate);
538  }
539  };
540 
541  const Fst<Arc> &fst_;
542  Compare comp_;
543  Equal equal_;
544  std::vector<Arc> arcs_;
545  ssize_t i_; // Current arc position.
546 
547  ArcSumMapper &operator=(const ArcSumMapper &) = delete;
548 };
549 
550 template <class Arc>
552  public:
553  using FromArc = Arc;
554  using ToArc = Arc;
555 
556  using StateId = typename Arc::StateId;
557  using Weight = typename Arc::Weight;
558 
559  explicit ArcUniqueMapper(const Fst<Arc> &fst) : fst_(fst), i_(0) {}
560 
561  // Allows updating FST argument; pass only if changed.
563  const Fst<Arc> *fst = nullptr)
564  : fst_(fst ? *fst : mapper.fst_), i_(0) {}
565 
566  StateId Start() const { return fst_.Start(); }
567 
568  Weight Final(StateId state) const { return fst_.Final(state); }
569 
570  void SetState(StateId state) {
571  i_ = 0;
572  arcs_.clear();
573  arcs_.reserve(fst_.NumArcs(state));
574  for (ArcIterator<Fst<Arc>> aiter(fst_, state); !aiter.Done();
575  aiter.Next()) {
576  arcs_.push_back(aiter.Value());
577  }
578  // First sorts the exiting arcs by input label, output label and destination
579  // state and then uniques identical arcs.
580  std::sort(arcs_.begin(), arcs_.end(), comp_);
581  arcs_.erase(std::unique(arcs_.begin(), arcs_.end(), equal_), arcs_.end());
582  }
583 
584  bool Done() const { return i_ >= arcs_.size(); }
585 
586  const Arc &Value() const { return arcs_[i_]; }
587 
588  void Next() { ++i_; }
589 
591  return MAP_COPY_SYMBOLS;
592  }
593 
595  return MAP_COPY_SYMBOLS;
596  }
597 
598  uint64_t Properties(uint64_t props) const {
599  return props & kArcSortProperties & kDeleteArcsProperties;
600  }
601 
602  private:
603  struct Compare {
604  bool operator()(const Arc &x, const Arc &y) const {
605  if (x.ilabel < y.ilabel) return true;
606  if (x.ilabel > y.ilabel) return false;
607  if (x.olabel < y.olabel) return true;
608  if (x.olabel > y.olabel) return false;
609  if (x.nextstate < y.nextstate) return true;
610  if (x.nextstate > y.nextstate) return false;
611  return false;
612  }
613  };
614 
615  struct Equal {
616  bool operator()(const Arc &x, const Arc &y) const {
617  return (x.ilabel == y.ilabel && x.olabel == y.olabel &&
618  x.nextstate == y.nextstate && x.weight == y.weight);
619  }
620  };
621 
622  const Fst<Arc> &fst_;
623  Compare comp_;
624  Equal equal_;
625  std::vector<Arc> arcs_;
626  size_t i_; // Current arc position.
627 
628  ArcUniqueMapper &operator=(const ArcUniqueMapper &) = delete;
629 };
630 
631 // Useful aliases when using StdArc.
632 
634 
636 
637 } // namespace fst
638 
639 #endif // FST_STATE_MAP_H_
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: state-map.h:590
MapSymbolsAction
Definition: arc-map.h:65
ssize_t NumOutputEpsilons(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
Definition: expanded-fst.h:124
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: state-map.h:448
typename Arc::StateId StateId
Definition: state-map.h:556
constexpr uint64_t kArcSortProperties
Definition: properties.h:251
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: state-map.h:512
void InitArcIterator(StateId state, ArcIteratorData< B > *data) const override
Definition: state-map.h:386
typename Arc::StateId StateId
Definition: state-map.h:420
constexpr uint64_t kDeleteArcsProperties
Definition: properties.h:234
constexpr uint64_t kWeightInvariantProperties
Definition: properties.h:280
typename Arc::Weight Weight
Definition: state-map.h:216
virtual uint64_t Properties(uint64_t mask, bool test) const =0
ArcSumMapper(const Fst< Arc > &fst)
Definition: state-map.h:468
bool Done() const
Definition: state-map.h:438
void InitArcIterator(StateId state, ArcIteratorData< B > *data)
Definition: state-map.h:296
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: state-map.h:444
ErrorWeight Plus(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:61
ArcIterator(const StateMapFst< A, B, C > &fst, StateId state)
Definition: state-map.h:405
ArcUniqueMapper(const ArcUniqueMapper< Arc > &mapper, const Fst< Arc > *fst=nullptr)
Definition: state-map.h:562
typename Arc::Weight Weight
Definition: state-map.h:421
const Arc & Value() const
Definition: state-map.h:586
Arc::Weight Final(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
Definition: expanded-fst.h:107
IdentityStateMapper(const Fst< Arc > &fst)
Definition: state-map.h:423
constexpr uint64_t kError
Definition: properties.h:52
virtual void SetInputSymbols(const SymbolTable *isyms)=0
StateId Start() const
Definition: state-map.h:566
uint64_t Properties(uint64_t mask) const override
Definition: state-map.h:303
SetType
Definition: set-weight.h:59
virtual void SetStart(StateId)=0
void SetState(StateId state)
Definition: state-map.h:478
ssize_t NumArcs(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
Definition: expanded-fst.h:113
typename Arc::StateId StateId
Definition: state-map.h:215
StateMapFst(const Fst< A > &fst, C *mapper)
Definition: state-map.h:371
typename Arc::StateId StateId
Definition: state-map.h:189
constexpr int kNoStateId
Definition: fst.h:195
void SetState(StateId state)
Definition: state-map.h:570
StateId Start() const
Definition: state-map.h:474
StateMapFst(const Fst< A > &fst, C *mapper, const StateMapFstOptions &opts)
Definition: state-map.h:365
IdentityStateMapper(const IdentityStateMapper< Arc > &mapper, const Fst< Arc > *fst=nullptr)
Definition: state-map.h:426
ArcSumMapper(const ArcSumMapper< Arc > &mapper, const Fst< Arc > *fst=nullptr)
Definition: state-map.h:471
StateMapStateIteratorBase(std::unique_ptr< StateIteratorBase< A >> base)
Definition: state-map.h:191
Weight Final(StateId state)
Definition: state-map.h:267
StateMapFst * Copy(bool safe=false) const override
Definition: state-map.h:378
uint64_t Properties(uint64_t props) const
Definition: state-map.h:452
ssize_t NumInputEpsilons(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
Definition: expanded-fst.h:118
ArcUniqueMapper(const Fst< Arc > &fst)
Definition: state-map.h:559
bool Done() const
Definition: state-map.h:584
constexpr uint64_t kCopyProperties
Definition: properties.h:163
virtual void SetProperties(uint64_t props, uint64_t mask)=0
typename FirstCacheStore< VectorCacheStore< CacheState< Arc > > >::State State
Definition: cache.h:673
StateId nstates
Definition: fst.h:383
typename Arc::Weight Weight
Definition: state-map.h:466
virtual void DeleteArcs(StateId, size_t)=0
size_t NumOutputEpsilons(StateId state)
Definition: state-map.h:282
virtual StateId Start() const =0
StateMapFstImpl(const StateMapFstImpl< A, B, C > &impl)
Definition: state-map.h:250
StateMapFst(const Fst< A > &fst, const C &mapper)
Definition: state-map.h:368
std::unique_ptr< StateIteratorBase< Arc > > base
Definition: fst.h:381
size_t NumArcs(StateId state)
Definition: state-map.h:272
const Arc & Value() const
Definition: state-map.h:504
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: state-map.h:508
const Fst< A > * GetFst() const
Definition: state-map.h:319
bool Equal(const Fst< Arc > &fst1, const Fst< Arc > &fst2, WeightEqual weight_equal, uint8_t etype=kEqualFsts)
Definition: equal.h:63
void StateMap(MutableFst< A > *fst, C *mapper)
Definition: state-map.h:103
StateId Value() const final
Definition: state-map.h:196
StateId Start() const
Definition: state-map.h:430
typename Arc::StateId StateId
Definition: state-map.h:465
void Expand(StateId state)
Definition: state-map.h:311
Weight Final(StateId state) const
Definition: state-map.h:432
virtual std::optional< StateId > NumStatesIfKnown() const
Definition: fst.h:227
uint64_t Properties(uint64_t props) const
Definition: state-map.h:516
bool Done() const final
Definition: state-map.h:194
bool Done() const
Definition: state-map.h:502
constexpr uint64_t kFstProperties
Definition: properties.h:326
virtual void AddArc(StateId, const Arc &)=0
typename internal::StateMapFstImpl< Arc, Arc, ArcSortMapper< Arc, Compare > >::Arc Arc
Definition: fst.h:204
StateMapFstImpl(const Fst< A > &fst, const C &mapper, const StateMapFstOptions &opts)
Definition: state-map.h:233
virtual const SymbolTable * InputSymbols() const =0
size_t NumInputEpsilons(StateId state)
Definition: state-map.h:277
const Arc & Value() const
Definition: state-map.h:440
virtual StateId AddState()=0
void InitStateIterator(StateIteratorData< B > *data) const override
Definition: state-map.h:382
virtual void ReserveStates(size_t)
Definition: mutable-fst.h:101
Weight Final(StateId state) const
Definition: state-map.h:476
virtual void SetFinal(StateId s, Weight weight=Weight::One())=0
virtual void DeleteStates(const std::vector< StateId > &)=0
typename Arc::Weight Weight
Definition: state-map.h:557
void InitStateIterator(StateIteratorData< B > *datb) const
Definition: state-map.h:287
virtual void SetOutputSymbols(const SymbolTable *osyms)=0
void Expand(const Fst< Arc > &ifst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &parens, const std::vector< typename Arc::Label > &assignments, MutableFst< Arc > *ofst, const MPdtExpandOptions &opts)
Definition: expand.h:324
StateMapFst(const StateMapFst &fst, bool safe=false)
Definition: state-map.h:375
StateMapFstImpl(const Fst< A > &fst, C *mapper, const StateMapFstOptions &opts)
Definition: state-map.h:242
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: state-map.h:594
StateMapFst(const Fst< A > &fst, const C &mapper, const StateMapFstOptions &opts)
Definition: state-map.h:361
uint64_t Properties(uint64_t props) const
Definition: state-map.h:598
Weight Final(StateId state) const
Definition: state-map.h:568
void SetState(StateId state)
Definition: state-map.h:434
uint64_t Properties() const override
Definition: state-map.h:301
virtual const SymbolTable * OutputSymbols() const =0