FST  openfst-1.8.3
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>> {
349  public:
350  friend class ArcIterator<StateMapFst<A, B, C>>;
351 
352  using Arc = B;
353  using StateId = typename Arc::StateId;
354  using Weight = typename Arc::Weight;
356  using State = typename Store::State;
358 
359  StateMapFst(const Fst<A> &fst, const C &mapper,
360  const StateMapFstOptions &opts)
361  : ImplToFst<Impl>(std::make_shared<Impl>(fst, mapper, opts)) {}
362 
363  StateMapFst(const Fst<A> &fst, C *mapper, const StateMapFstOptions &opts)
364  : ImplToFst<Impl>(std::make_shared<Impl>(fst, mapper, opts)) {}
365 
366  StateMapFst(const Fst<A> &fst, const C &mapper)
367  : ImplToFst<Impl>(
368  std::make_shared<Impl>(fst, mapper, StateMapFstOptions())) {}
369 
370  StateMapFst(const Fst<A> &fst, C *mapper)
371  : ImplToFst<Impl>(
372  std::make_shared<Impl>(fst, mapper, StateMapFstOptions())) {}
373 
374  // See Fst<>::Copy() for doc.
375  StateMapFst(const StateMapFst &fst, bool safe = false)
376  : ImplToFst<Impl>(fst, safe) {}
377 
378  // Get a copy of this StateMapFst. See Fst<>::Copy() for further doc.
379  StateMapFst *Copy(bool safe = false) const override {
380  return new StateMapFst(*this, safe);
381  }
382 
383  void InitStateIterator(StateIteratorData<B> *data) const override {
384  GetImpl()->InitStateIterator(data);
385  }
386 
387  void InitArcIterator(StateId state, ArcIteratorData<B> *data) const override {
388  GetMutableImpl()->InitArcIterator(state, data);
389  }
390 
391  protected:
394 
395  private:
396  StateMapFst &operator=(const StateMapFst &) = delete;
397 };
398 
399 // Specialization for StateMapFst.
400 template <class A, class B, class C>
401 class ArcIterator<StateMapFst<A, B, C>>
402  : public CacheArcIterator<StateMapFst<A, B, C>> {
403  public:
404  using StateId = typename A::StateId;
405 
407  : CacheArcIterator<StateMapFst<A, B, C>>(fst.GetMutableImpl(), state) {
408  if (!fst.GetImpl()->HasArcs(state)) fst.GetMutableImpl()->Expand(state);
409  }
410 };
411 
412 // Utility mappers.
413 
414 // Mapper that returns its input.
415 template <class Arc>
417  public:
418  using FromArc = Arc;
419  using ToArc = Arc;
420 
421  using StateId = typename Arc::StateId;
422  using Weight = typename Arc::Weight;
423 
424  explicit IdentityStateMapper(const Fst<Arc> &fst) : fst_(fst) {}
425 
426  // Allows updating FST argument; pass only if changed.
428  const Fst<Arc> *fst = nullptr)
429  : fst_(fst ? *fst : mapper.fst_) {}
430 
431  StateId Start() const { return fst_.Start(); }
432 
433  Weight Final(StateId state) const { return fst_.Final(state); }
434 
435  void SetState(StateId state) {
436  aiter_ = std::make_unique<ArcIterator<Fst<Arc>>>(fst_, state);
437  }
438 
439  bool Done() const { return aiter_->Done(); }
440 
441  const Arc &Value() const { return aiter_->Value(); }
442 
443  void Next() { aiter_->Next(); }
444 
446  return MAP_COPY_SYMBOLS;
447  }
448 
450  return MAP_COPY_SYMBOLS;
451  }
452 
453  uint64_t Properties(uint64_t props) const { return props; }
454 
455  private:
456  const Fst<Arc> &fst_;
457  std::unique_ptr<ArcIterator<Fst<Arc>>> aiter_;
458 };
459 
460 template <class Arc>
462  public:
463  using FromArc = Arc;
464  using ToArc = Arc;
465 
466  using StateId = typename Arc::StateId;
467  using Weight = typename Arc::Weight;
468 
469  explicit ArcSumMapper(const Fst<Arc> &fst) : fst_(fst), i_(0) {}
470 
471  // Allows updating FST argument; pass only if changed.
472  ArcSumMapper(const ArcSumMapper<Arc> &mapper, const Fst<Arc> *fst = nullptr)
473  : fst_(fst ? *fst : mapper.fst_), i_(0) {}
474 
475  StateId Start() const { return fst_.Start(); }
476 
477  Weight Final(StateId state) const { return fst_.Final(state); }
478 
479  void SetState(StateId state) {
480  i_ = 0;
481  arcs_.clear();
482  arcs_.reserve(fst_.NumArcs(state));
483  for (ArcIterator<Fst<Arc>> aiter(fst_, state); !aiter.Done();
484  aiter.Next()) {
485  arcs_.push_back(aiter.Value());
486  }
487  // First sorts the exiting arcs by input label, output label and destination
488  // state and then sums weights of arcs with the same input label, output
489  // label, and destination state.
490  std::sort(arcs_.begin(), arcs_.end(), comp_);
491  size_t narcs = 0;
492  for (const auto &arc : arcs_) {
493  if (narcs > 0 && equal_(arc, arcs_[narcs - 1])) {
494  arcs_[narcs - 1].weight = Plus(arcs_[narcs - 1].weight, arc.weight);
495  } else {
496  arcs_[narcs] = arc;
497  ++narcs;
498  }
499  }
500  arcs_.resize(narcs);
501  }
502 
503  bool Done() const { return i_ >= arcs_.size(); }
504 
505  const Arc &Value() const { return arcs_[i_]; }
506 
507  void Next() { ++i_; }
508 
510  return MAP_COPY_SYMBOLS;
511  }
512 
514  return MAP_COPY_SYMBOLS;
515  }
516 
517  uint64_t Properties(uint64_t props) const {
518  return props & kArcSortProperties & kDeleteArcsProperties &
520  }
521 
522  private:
523  struct Compare {
524  bool operator()(const Arc &x, const Arc &y) const {
525  if (x.ilabel < y.ilabel) return true;
526  if (x.ilabel > y.ilabel) return false;
527  if (x.olabel < y.olabel) return true;
528  if (x.olabel > y.olabel) return false;
529  if (x.nextstate < y.nextstate) return true;
530  if (x.nextstate > y.nextstate) return false;
531  return false;
532  }
533  };
534 
535  struct Equal {
536  bool operator()(const Arc &x, const Arc &y) const {
537  return (x.ilabel == y.ilabel && x.olabel == y.olabel &&
538  x.nextstate == y.nextstate);
539  }
540  };
541 
542  const Fst<Arc> &fst_;
543  Compare comp_;
544  Equal equal_;
545  std::vector<Arc> arcs_;
546  ssize_t i_; // Current arc position.
547 
548  ArcSumMapper &operator=(const ArcSumMapper &) = delete;
549 };
550 
551 template <class Arc>
553  public:
554  using FromArc = Arc;
555  using ToArc = Arc;
556 
557  using StateId = typename Arc::StateId;
558  using Weight = typename Arc::Weight;
559 
560  explicit ArcUniqueMapper(const Fst<Arc> &fst) : fst_(fst), i_(0) {}
561 
562  // Allows updating FST argument; pass only if changed.
564  const Fst<Arc> *fst = nullptr)
565  : fst_(fst ? *fst : mapper.fst_), i_(0) {}
566 
567  StateId Start() const { return fst_.Start(); }
568 
569  Weight Final(StateId state) const { return fst_.Final(state); }
570 
571  void SetState(StateId state) {
572  i_ = 0;
573  arcs_.clear();
574  arcs_.reserve(fst_.NumArcs(state));
575  for (ArcIterator<Fst<Arc>> aiter(fst_, state); !aiter.Done();
576  aiter.Next()) {
577  arcs_.push_back(aiter.Value());
578  }
579  // First sorts the exiting arcs by input label, output label and destination
580  // state and then uniques identical arcs.
581  std::sort(arcs_.begin(), arcs_.end(), comp_);
582  arcs_.erase(std::unique(arcs_.begin(), arcs_.end(), equal_), arcs_.end());
583  }
584 
585  bool Done() const { return i_ >= arcs_.size(); }
586 
587  const Arc &Value() const { return arcs_[i_]; }
588 
589  void Next() { ++i_; }
590 
592  return MAP_COPY_SYMBOLS;
593  }
594 
596  return MAP_COPY_SYMBOLS;
597  }
598 
599  uint64_t Properties(uint64_t props) const {
600  return props & kArcSortProperties & kDeleteArcsProperties;
601  }
602 
603  private:
604  struct Compare {
605  bool operator()(const Arc &x, const Arc &y) const {
606  if (x.ilabel < y.ilabel) return true;
607  if (x.ilabel > y.ilabel) return false;
608  if (x.olabel < y.olabel) return true;
609  if (x.olabel > y.olabel) return false;
610  if (x.nextstate < y.nextstate) return true;
611  if (x.nextstate > y.nextstate) return false;
612  return false;
613  }
614  };
615 
616  struct Equal {
617  bool operator()(const Arc &x, const Arc &y) const {
618  return (x.ilabel == y.ilabel && x.olabel == y.olabel &&
619  x.nextstate == y.nextstate && x.weight == y.weight);
620  }
621  };
622 
623  const Fst<Arc> &fst_;
624  Compare comp_;
625  Equal equal_;
626  std::vector<Arc> arcs_;
627  size_t i_; // Current arc position.
628 
629  ArcUniqueMapper &operator=(const ArcUniqueMapper &) = delete;
630 };
631 
632 // Useful aliases when using StdArc.
633 
635 
637 
638 } // namespace fst
639 
640 #endif // FST_STATE_MAP_H_
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: state-map.h:591
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:449
typename Arc::StateId StateId
Definition: state-map.h:557
constexpr uint64_t kArcSortProperties
Definition: properties.h:251
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: state-map.h:513
void InitArcIterator(StateId state, ArcIteratorData< B > *data) const override
Definition: state-map.h:387
typename Arc::StateId StateId
Definition: state-map.h:421
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:469
bool Done() const
Definition: state-map.h:439
void InitArcIterator(StateId state, ArcIteratorData< B > *data)
Definition: state-map.h:296
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: state-map.h:445
ErrorWeight Plus(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:61
ArcIterator(const StateMapFst< A, B, C > &fst, StateId state)
Definition: state-map.h:406
ArcUniqueMapper(const ArcUniqueMapper< Arc > &mapper, const Fst< Arc > *fst=nullptr)
Definition: state-map.h:563
typename Arc::Weight Weight
Definition: state-map.h:422
const Arc & Value() const
Definition: state-map.h:587
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:424
constexpr uint64_t kError
Definition: properties.h:52
virtual void SetInputSymbols(const SymbolTable *isyms)=0
StateId Start() const
Definition: state-map.h:567
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:479
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:370
typename Arc::StateId StateId
Definition: state-map.h:189
constexpr int kNoStateId
Definition: fst.h:196
void SetState(StateId state)
Definition: state-map.h:571
StateId Start() const
Definition: state-map.h:475
StateMapFst(const Fst< A > &fst, C *mapper, const StateMapFstOptions &opts)
Definition: state-map.h:363
IdentityStateMapper(const IdentityStateMapper< Arc > &mapper, const Fst< Arc > *fst=nullptr)
Definition: state-map.h:427
ArcSumMapper(const ArcSumMapper< Arc > &mapper, const Fst< Arc > *fst=nullptr)
Definition: state-map.h:472
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:379
uint64_t Properties(uint64_t props) const
Definition: state-map.h:453
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:560
bool Done() const
Definition: state-map.h:585
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:384
typename Arc::Weight Weight
Definition: state-map.h:467
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:366
std::unique_ptr< StateIteratorBase< Arc > > base
Definition: fst.h:382
size_t NumArcs(StateId state)
Definition: state-map.h:272
const Arc & Value() const
Definition: state-map.h:505
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: state-map.h:509
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:431
typename Arc::StateId StateId
Definition: state-map.h:466
void Expand(StateId state)
Definition: state-map.h:311
Weight Final(StateId state) const
Definition: state-map.h:433
virtual std::optional< StateId > NumStatesIfKnown() const
Definition: fst.h:228
uint64_t Properties(uint64_t props) const
Definition: state-map.h:517
bool Done() const final
Definition: state-map.h:194
bool Done() const
Definition: state-map.h:503
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:205
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:441
virtual StateId AddState()=0
void InitStateIterator(StateIteratorData< B > *data) const override
Definition: state-map.h:383
virtual void ReserveStates(size_t)
Definition: mutable-fst.h:101
Weight Final(StateId state) const
Definition: state-map.h:477
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:558
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:323
StateMapFst(const StateMapFst &fst, bool safe=false)
Definition: state-map.h:375
internal::StateMapFstImpl< A, B, C > * GetMutableImpl() const
Definition: impl-to-fst.h:125
StateMapFstImpl(const Fst< A > &fst, C *mapper, const StateMapFstOptions &opts)
Definition: state-map.h:242
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: state-map.h:595
StateMapFst(const Fst< A > &fst, const C &mapper, const StateMapFstOptions &opts)
Definition: state-map.h:359
uint64_t Properties(uint64_t props) const
Definition: state-map.h:599
Weight Final(StateId state) const
Definition: state-map.h:569
const internal::StateMapFstImpl< A, B, C > * GetImpl() const
Definition: impl-to-fst.h:123
void SetState(StateId state)
Definition: state-map.h:435
uint64_t Properties() const override
Definition: state-map.h:301
virtual const SymbolTable * OutputSymbols() const =0