FST  openfst-1.8.3
OpenFst Library
invert.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 // Functions and classes to invert an FST.
19 
20 #ifndef FST_INVERT_H_
21 #define FST_INVERT_H_
22 
23 #include <cstdint>
24 #include <memory>
25 
26 #include <fst/arc-map.h>
27 #include <fst/arc.h>
28 #include <fst/cache.h>
29 #include <fst/float-weight.h>
30 #include <fst/fst.h>
31 #include <fst/impl-to-fst.h>
32 #include <fst/mutable-fst.h>
33 #include <fst/properties.h>
34 #include <fst/symbol-table.h>
35 
36 namespace fst {
37 
38 // Mapper to implement inversion of an arc.
39 template <class A>
40 struct InvertMapper {
41  using FromArc = A;
42  using ToArc = A;
43 
44  InvertMapper() = default;
45 
46  ToArc operator()(const FromArc &arc) const {
47  return ToArc(arc.olabel, arc.ilabel, arc.weight, arc.nextstate);
48  }
49 
50  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
51 
53  return MAP_CLEAR_SYMBOLS;
54  }
55 
57  return MAP_CLEAR_SYMBOLS;
58  }
59 
60  uint64_t Properties(uint64_t props) const { return InvertProperties(props); }
61 };
62 
63 // Inverts the transduction corresponding to an FST by exchanging the
64 // FST's input and output labels.
65 //
66 // Complexity:
67 //
68 // Time: O(V + E)
69 // Space: O(1)
70 //
71 // where V is the number of states and E is the number of arcs.
72 template <class Arc>
73 inline void Invert(const Fst<Arc> &ifst, MutableFst<Arc> *ofst) {
74  std::unique_ptr<SymbolTable> input(
75  ifst.InputSymbols() ? ifst.InputSymbols()->Copy() : nullptr);
76  std::unique_ptr<SymbolTable> output(
77  ifst.OutputSymbols() ? ifst.OutputSymbols()->Copy() : nullptr);
78  ArcMap(ifst, ofst, InvertMapper<Arc>());
79  ofst->SetInputSymbols(output.get());
80  ofst->SetOutputSymbols(input.get());
81 }
82 
83 // Destructive variant of the above.
84 template <class Arc>
85 inline void Invert(MutableFst<Arc> *fst) {
86  std::unique_ptr<SymbolTable> input(
87  fst->InputSymbols() ? fst->InputSymbols()->Copy() : nullptr);
88  std::unique_ptr<SymbolTable> output(
89  fst->OutputSymbols() ? fst->OutputSymbols()->Copy() : nullptr);
90  ArcMap(fst, InvertMapper<Arc>());
91  fst->SetInputSymbols(output.get());
92  fst->SetOutputSymbols(input.get());
93 }
94 
95 // Inverts the transduction corresponding to an FST by exchanging the
96 // FST's input and output labels. This version is a delayed FST.
97 //
98 // Complexity:
99 //
100 // Time: O(v + e)
101 // Space: O(1)
102 //
103 // where v is the number of states visited and e is the number of arcs visited.
104 // Constant time and to visit an input state or arc is assumed and exclusive of
105 // caching.
106 template <class A>
107 class InvertFst : public ArcMapFst<A, A, InvertMapper<A>> {
108  public:
109  using Arc = A;
110 
113 
114  explicit InvertFst(const Fst<Arc> &fst) : ArcMapFst<Arc, Arc, Mapper>(fst) {
115  GetMutableImpl()->SetOutputSymbols(fst.InputSymbols());
116  GetMutableImpl()->SetInputSymbols(fst.OutputSymbols());
117  }
118 
119  // See Fst<>::Copy() for doc.
120  InvertFst(const InvertFst &fst, bool safe = false)
121  : ArcMapFst<Arc, Arc, Mapper>(fst, safe) {}
122 
123  // Get a copy of this InvertFst. See Fst<>::Copy() for further doc.
124  InvertFst *Copy(bool safe = false) const override {
125  return new InvertFst(*this, safe);
126  }
127 
128  private:
130 };
131 
132 // Specialization for InvertFst.
133 template <class Arc>
135  : public StateIterator<ArcMapFst<Arc, Arc, InvertMapper<Arc>>> {
136  public:
139 };
140 
141 // Specialization for InvertFst.
142 template <class Arc>
144  : public ArcIterator<ArcMapFst<Arc, Arc, InvertMapper<Arc>>> {
145  public:
146  using StateId = typename Arc::StateId;
147 
149  : ArcIterator<ArcMapFst<Arc, Arc, InvertMapper<Arc>>>(fst, s) {}
150 };
151 
152 // Useful alias when using StdArc.
154 
155 } // namespace fst
156 
157 #endif // FST_INVERT_H_
typename Impl::Arc Arc
Definition: impl-to-fst.h:43
MapSymbolsAction
Definition: arc-map.h:65
void ArcMap(MutableFst< A > *fst, C *mapper)
Definition: arc-map.h:120
ToArc operator()(const FromArc &arc) const
Definition: invert.h:46
void Invert(const Fst< Arc > &ifst, MutableFst< Arc > *ofst)
Definition: invert.h:73
typename ArcMapFst< Arc, Arc, InvertMapper< Arc > >::Arc Arc
Definition: fst.h:517
StateIterator(const InvertFst< Arc > &fst)
Definition: invert.h:137
InvertFst(const InvertFst &fst, bool safe=false)
Definition: invert.h:120
uint64_t Properties(uint64_t props) const
Definition: invert.h:60
typename Arc::StateId StateId
Definition: invert.h:146
virtual SymbolTable * Copy() const
Definition: symbol-table.h:411
const SymbolTable * InputSymbols() const override=0
virtual void SetInputSymbols(const SymbolTable *isyms)=0
InvertMapper()=default
typename ArcMapFst< Arc, Arc, InvertMapper< Arc > >::Arc Arc
Definition: fst.h:408
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: invert.h:52
InvertFst(const Fst< Arc > &fst)
Definition: invert.h:114
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: invert.h:56
const SymbolTable * OutputSymbols() const override=0
MapFinalAction
Definition: arc-map.h:50
InvertFst * Copy(bool safe=false) const override
Definition: invert.h:124
constexpr MapFinalAction FinalAction() const
Definition: invert.h:50
virtual const SymbolTable * InputSymbols() const =0
uint64_t InvertProperties(uint64_t inprops)
Definition: properties.cc:169
virtual void SetOutputSymbols(const SymbolTable *osyms)=0
ArcIterator(const InvertFst< Arc > &fst, StateId s)
Definition: invert.h:148
virtual const SymbolTable * OutputSymbols() const =0