You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.8 KiB

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