FST  openfst-1.8.3
OpenFst Library
arcsort.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 sort arcs in an FST.
19 
20 #ifndef FST_ARCSORT_H_
21 #define FST_ARCSORT_H_
22 
23 #include <sys/types.h>
24 
25 #include <algorithm>
26 #include <cstddef>
27 #include <cstdint>
28 #include <string>
29 #include <tuple>
30 #include <vector>
31 
32 #include <fst/arc-map.h>
33 #include <fst/arc.h>
34 #include <fst/cache.h>
35 #include <fst/fst.h>
36 #include <fst/mutable-fst.h>
37 #include <fst/properties.h>
38 #include <fst/state-map.h>
39 
40 namespace fst {
41 
42 template <class Arc, class Compare>
44  public:
45  using FromArc = Arc;
46  using ToArc = Arc;
47 
48  using StateId = typename Arc::StateId;
49  using Weight = typename Arc::Weight;
50 
51  constexpr ArcSortMapper(const Fst<Arc> &fst, const Compare &comp)
52  : fst_(fst), comp_(comp), i_(0) {}
53 
54  // Allows updating Fst argument; pass only if changed.
56  const Fst<Arc> *fst = nullptr)
57  : fst_(fst ? *fst : mapper.fst_), comp_(mapper.comp_), i_(0) {}
58 
59  StateId Start() { return fst_.Start(); }
60 
61  Weight Final(StateId s) const { return fst_.Final(s); }
62 
63  void SetState(StateId s) {
64  i_ = 0;
65  arcs_.clear();
66  arcs_.reserve(fst_.NumArcs(s));
67  for (ArcIterator<Fst<Arc>> aiter(fst_, s); !aiter.Done(); aiter.Next()) {
68  arcs_.push_back(aiter.Value());
69  }
70  std::stable_sort(arcs_.begin(), arcs_.end(), comp_);
71  }
72 
73  bool Done() const { return i_ >= arcs_.size(); }
74 
75  const Arc &Value() const { return arcs_[i_]; }
76 
77  void Next() { ++i_; }
78 
80 
82 
83  uint64_t Properties(uint64_t props) const { return comp_.Properties(props); }
84 
85  private:
86  const Fst<Arc> &fst_;
87  const Compare &comp_;
88  std::vector<Arc> arcs_;
89  ssize_t i_; // current arc position
90 
91  ArcSortMapper &operator=(const ArcSortMapper &) = delete;
92 };
93 
94 // Sorts the arcs in an FST according to function object 'comp' of type Compare.
95 // This version modifies its input. Comparison function objects ILabelCompare
96 // and OLabelCompare are provided by the library. In general, Compare must meet
97 // the requirements for a comparison function object (e.g., similar to those
98 // used by std::sort). It must also have a member Properties(uint64_t) that
99 // specifies the known properties of the sorted FST; it takes as argument the
100 // input FST's known properties before the sort.
101 //
102 // Complexity:
103 //
104 // - Time: O(v d log d)
105 // - Space: O(d)
106 //
107 // where v = # of states and d = maximum out-degree.
108 template <class Arc, class Compare>
109 void ArcSort(MutableFst<Arc> *fst, Compare comp) {
110  ArcSortMapper<Arc, Compare> mapper(*fst, comp);
111  StateMap(fst, mapper);
112 }
113 
115 
116 // Sorts the arcs in an FST according to function object 'comp' of type Compare.
117 // This version is a delayed FST. Comparsion function objects ILabelCompare and
118 // OLabelCompare are provided by the library. In general, Compare must meet the
119 // requirements for a comparision function object (e.g., similar to those
120 // used by std::sort). It must also have a member Properties(uint64_t) that
121 // specifies the known properties of the sorted FST; it takes as argument the
122 // input FST's known properties.
123 //
124 // Complexity:
125 //
126 // - Time: O(v d log d)
127 // - Space: O(d)
128 //
129 // where v = # of states visited, d = maximum out-degree of states visited.
130 // Constant time and space to visit an input state is assumed and exclusive of
131 // caching.
132 template <class Arc, class Compare>
133 class ArcSortFst : public StateMapFst<Arc, Arc, ArcSortMapper<Arc, Compare>> {
135 
136  public:
137  using StateId = typename Arc::StateId;
139 
140  ArcSortFst(const Fst<Arc> &fst, const Compare &comp)
141  : StateMapFst<Arc, Arc, Mapper>(fst,
142  ArcSortMapper<Arc, Compare>(fst, comp)) {}
143 
144  ArcSortFst(const Fst<Arc> &fst, const Compare &comp,
145  const ArcSortFstOptions &opts)
146  : StateMapFst<Arc, Arc, Mapper>(fst, Mapper(fst, comp), opts) {}
147 
148  // See Fst<>::Copy() for doc.
149  ArcSortFst(const ArcSortFst &fst, bool safe = false)
150  : StateMapFst<Arc, Arc, Mapper>(fst, safe) {}
151 
152  // Gets a copy of this ArcSortFst. See Fst<>::Copy() for further doc.
153  ArcSortFst *Copy(bool safe = false) const override {
154  return new ArcSortFst(*this, safe);
155  }
156 
157  size_t NumArcs(StateId s) const override {
158  return GetImpl()->GetFst()->NumArcs(s);
159  }
160 
161  size_t NumInputEpsilons(StateId s) const override {
162  return GetImpl()->GetFst()->NumInputEpsilons(s);
163  }
164 
165  size_t NumOutputEpsilons(StateId s) const override {
166  return GetImpl()->GetFst()->NumOutputEpsilons(s);
167  }
168 };
169 
170 // Specialization for ArcSortFst.
171 template <class Arc, class Compare>
172 class StateIterator<ArcSortFst<Arc, Compare>>
173  : public StateIterator<StateMapFst<Arc, Arc, ArcSortMapper<Arc, Compare>>> {
174  public:
176  : StateIterator<StateMapFst<Arc, Arc, ArcSortMapper<Arc, Compare>>>(fst) {
177  }
178 };
179 
180 // Specialization for ArcSortFst.
181 template <class Arc, class Compare>
182 class ArcIterator<ArcSortFst<Arc, Compare>>
183  : public ArcIterator<StateMapFst<Arc, Arc, ArcSortMapper<Arc, Compare>>> {
184  public:
185  ArcIterator(const ArcSortFst<Arc, Compare> &fst, typename Arc::StateId s)
186  : ArcIterator<StateMapFst<Arc, Arc, ArcSortMapper<Arc, Compare>>>(fst,
187  s) {}
188 };
189 
190 // Compare class for comparing input labels of arcs.
191 template <class Arc>
193  public:
194  constexpr ILabelCompare() = default;
195 
196  constexpr bool operator()(const Arc &lhs, const Arc &rhs) const {
197  return std::forward_as_tuple(lhs.ilabel, lhs.olabel) <
198  std::forward_as_tuple(rhs.ilabel, rhs.olabel);
199  }
200 
201  constexpr uint64_t Properties(uint64_t props) const {
202  return (props & kArcSortProperties) | kILabelSorted |
203  (props & kAcceptor ? kOLabelSorted : 0);
204  }
205 };
206 
207 // Compare class for comparing output labels of arcs.
208 template <class Arc>
210  public:
211  constexpr OLabelCompare() = default;
212 
213  constexpr bool operator()(const Arc &lhs, const Arc &rhs) const {
214  return std::forward_as_tuple(lhs.olabel, lhs.ilabel) <
215  std::forward_as_tuple(rhs.olabel, rhs.ilabel);
216  }
217 
218  constexpr uint64_t Properties(uint64_t props) const {
219  return (props & kArcSortProperties) | kOLabelSorted |
220  (props & kAcceptor ? kILabelSorted : 0);
221  }
222 };
223 
224 // Useful aliases when using StdArc.
225 
226 template <class Compare>
228 
230 
232 
233 } // namespace fst
234 
235 #endif // FST_ARCSORT_H_
MapSymbolsAction
Definition: arc-map.h:65
ArcIterator(const ArcSortFst< Arc, Compare > &fst, typename Arc::StateId s)
Definition: arcsort.h:185
StateId Start()
Definition: arcsort.h:59
bool Done() const
Definition: arcsort.h:73
constexpr uint64_t kArcSortProperties
Definition: properties.h:251
typename StateMapFst< Arc, Arc, ArcSortMapper< Arc, Compare > >::Arc Arc
Definition: fst.h:517
typename Arc::StateId StateId
Definition: arcsort.h:48
virtual size_t NumArcs(StateId) const =0
constexpr uint64_t Properties(uint64_t props) const
Definition: arcsort.h:218
const Arc & Value() const
Definition: arcsort.h:75
void SetState(StateId s)
Definition: arcsort.h:63
constexpr ArcSortMapper(const Fst< Arc > &fst, const Compare &comp)
Definition: arcsort.h:51
ArcSortFst(const ArcSortFst &fst, bool safe=false)
Definition: arcsort.h:149
size_t NumInputEpsilons(StateId s) const override
Definition: arcsort.h:161
virtual Weight Final(StateId) const =0
MapSymbolsAction InputSymbolsAction() const
Definition: arcsort.h:79
constexpr uint64_t Properties(uint64_t props) const
Definition: arcsort.h:201
typename StateMapFst< Arc, Arc, ArcSortMapper< Arc, Compare > >::Arc Arc
Definition: fst.h:408
StateIterator(const ArcSortFst< Arc, Compare > &fst)
Definition: arcsort.h:175
ArcSortMapper(const ArcSortMapper< Arc, Compare > &mapper, const Fst< Arc > *fst=nullptr)
Definition: arcsort.h:55
MapSymbolsAction OutputSymbolsAction() const
Definition: arcsort.h:81
void ArcSort(MutableFst< Arc > *fst, Compare comp)
Definition: arcsort.h:109
constexpr uint64_t kOLabelSorted
Definition: properties.h:99
ArcSortFst * Copy(bool safe=false) const override
Definition: arcsort.h:153
constexpr bool operator()(const Arc &lhs, const Arc &rhs) const
Definition: arcsort.h:196
virtual StateId Start() const =0
typename Arc::Weight Weight
Definition: arcsort.h:49
void StateMap(MutableFst< A > *fst, C *mapper)
Definition: state-map.h:103
size_t NumArcs(StateId s) const override
Definition: arcsort.h:157
ArcSortFst(const Fst< Arc > &fst, const Compare &comp)
Definition: arcsort.h:140
ArcSortFst(const Fst< Arc > &fst, const Compare &comp, const ArcSortFstOptions &opts)
Definition: arcsort.h:144
constexpr uint64_t kILabelSorted
Definition: properties.h:94
Weight Final(StateId s) const
Definition: arcsort.h:61
typename internal::StateMapFstImpl< Arc, Arc, ArcSortMapper< Arc, Compare > >::Arc Arc
Definition: fst.h:205
uint64_t Properties(uint64_t props) const
Definition: arcsort.h:83
typename Arc::StateId StateId
Definition: arcsort.h:137
constexpr bool operator()(const Arc &lhs, const Arc &rhs) const
Definition: arcsort.h:213
size_t NumOutputEpsilons(StateId s) const override
Definition: arcsort.h:165
constexpr uint64_t kAcceptor
Definition: properties.h:64