FST  openfst-1.8.2
OpenFst Library
statesort.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 // Function to sort states of an FST.
19 
20 #ifndef FST_STATESORT_H_
21 #define FST_STATESORT_H_
22 
23 #include <algorithm>
24 #include <vector>
25 
26 #include <fst/log.h>
27 
28 #include <fst/mutable-fst.h>
29 
30 
31 namespace fst {
32 
33 // Sorts the input states of an FST. order[i] gives the the state ID after
34 // sorting that corresponds to the state ID i before sorting; it must
35 // therefore be a permutation of the input FST's states ID sequence.
36 template <class Arc>
38  const std::vector<typename Arc::StateId> &order) {
39  using StateId = typename Arc::StateId;
40  using Weight = typename Arc::Weight;
41  if (order.size() != fst->NumStates()) {
42  FSTERROR() << "StateSort: Bad order vector size: " << order.size();
44  return;
45  }
46  if (fst->Start() == kNoStateId) return;
47  const auto props = fst->Properties(kStateSortProperties, false);
48  std::vector<bool> done(order.size(), false);
49  std::vector<Arc> arcsa;
50  std::vector<Arc> arcsb;
51  fst->SetStart(order[fst->Start()]);
52  for (StateIterator<MutableFst<Arc>> siter(*fst); !siter.Done();
53  siter.Next()) {
54  auto s1 = siter.Value();
55  StateId s2;
56  if (done[s1]) continue;
57  auto final1 = fst->Final(s1);
58  auto final2 = Weight::Zero();
59  arcsa.clear();
60  for (ArcIterator<MutableFst<Arc>> aiter(*fst, s1); !aiter.Done();
61  aiter.Next()) {
62  arcsa.push_back(aiter.Value());
63  }
64  for (; !done[s1]; s1 = s2, final1 = final2, std::swap(arcsa, arcsb)) {
65  s2 = order[s1];
66  if (!done[s2]) {
67  final2 = fst->Final(s2);
68  arcsb.clear();
69  for (ArcIterator<MutableFst<Arc>> aiter(*fst, s2); !aiter.Done();
70  aiter.Next()) {
71  arcsb.push_back(aiter.Value());
72  }
73  }
74  fst->SetFinal(s2, final1);
75  fst->DeleteArcs(s2);
76  for (auto arc : arcsa) { // NOLINT(performance-for-range-copy)
77  arc.nextstate = order[arc.nextstate];
78  fst->AddArc(s2, arc);
79  }
80  done[s1] = true;
81  }
82  }
83  fst->SetProperties(props, kFstProperties);
84 }
85 
86 } // namespace fst
87 
88 #endif // FST_STATESORT_H_
constexpr uint64_t kStateSortProperties
Definition: properties.h:240
virtual uint64_t Properties(uint64_t mask, bool test) const =0
constexpr uint64_t kError
Definition: properties.h:51
virtual Weight Final(StateId) const =0
virtual void SetStart(StateId)=0
constexpr int kNoStateId
Definition: fst.h:202
#define FSTERROR()
Definition: util.h:53
virtual void SetProperties(uint64_t props, uint64_t mask)=0
virtual void DeleteArcs(StateId, size_t)=0
virtual StateId Start() const =0
constexpr uint64_t kFstProperties
Definition: properties.h:325
virtual void AddArc(StateId, const Arc &)=0
virtual void SetFinal(StateId s, Weight weight=Weight::One())=0
virtual StateId NumStates() const =0
bool StateSort(std::vector< IntervalSet< Label >> *interval_sets, const std::vector< StateId > &order)