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