Guest User

Untitled

a guest
Sep 14th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 3.07 KB | None | 0 0
  1. diff --git a/src/include/fst/minimize.h b/src/include/fst/minimize.h
  2. index 7e59f9c..69518c9 100644
  3. --- a/src/include/fst/minimize.h
  4. +++ b/src/include/fst/minimize.h
  5. @@ -181,9 +181,7 @@ class CyclicMinimizer {
  6.      }
  7.    };
  8.  
  9. -  using ArcIterQueue =
  10. -      std::priority_queue<RevArcIterPtr, std::vector<RevArcIterPtr>,
  11. -                          ArcIterCompare>;
  12. +  using ArcIterQueue = std::vector<RevArcIterPtr>;
  13.  
  14.   private:
  15.    // Prepartitions the space into equivalence classes. We ensure that final and
  16. @@ -241,30 +239,26 @@ class CyclicMinimizer {
  17.      P_.Initialize(Tr_.NumStates() - 1);
  18.      // Prepares initial partition.
  19.      PrePartition(fst);
  20. -    // Allocates arc iterator queue.
  21. -    aiter_queue_ = std::make_unique<ArcIterQueue>();
  22.    }
  23.    // Partitions all classes with destination C.
  24.    void Split(ClassId C) {
  25. +
  26.      // Prepares priority queue: opens arc iterator for each state in C, and
  27.      // inserts into priority queue.
  28.      for (PartitionIterator<StateId> siter(P_, C); !siter.Done(); siter.Next()) {
  29.        const auto s = siter.Value();
  30.        if (Tr_.NumArcs(s + 1)) {
  31. -        aiter_queue_->push(std::make_unique<RevArcIter>(Tr_, s + 1));
  32. +        aiter_queue_.push_back(std::make_unique<RevArcIter>(Tr_, s + 1));
  33.        }
  34.      }
  35. +    std::make_heap(aiter_queue_.begin(), aiter_queue_.end(), ArcIterCompare{});
  36.      // Now pops arc iterator from queue, splits entering equivalence class, and
  37.      // re-inserts updated iterator into queue.
  38.      Label prev_label = -1;
  39. -    while (!aiter_queue_->empty()) {
  40. -      // NB: There is no way to "move" out of a std::priority_queue given that
  41. -      // the `top` accessor is a const ref. We const-cast to move out the
  42. -      // unique_ptr out of the priority queue. This is fine and doesn't cause an
  43. -      // issue with the invariants of the pqueue since we immediately pop after.
  44. -      RevArcIterPtr aiter =
  45. -          std::move(const_cast<RevArcIterPtr &>(aiter_queue_->top()));
  46. -      aiter_queue_->pop();
  47. +    while (!aiter_queue_.empty()) {
  48. +      std::pop_heap(aiter_queue_.begin(), aiter_queue_.end(), ArcIterCompare{});
  49. +      RevArcIterPtr aiter = std::move(aiter_queue_.back());
  50. +      aiter_queue_.pop_back();
  51.        if (aiter->Done()) continue;
  52.        const auto &arc = aiter->Value();
  53.        auto from_state = aiter->Value().nextstate - 1;
  54. @@ -274,7 +268,10 @@ class CyclicMinimizer {
  55.        if (P_.ClassSize(from_class) > 1) P_.SplitOn(from_state);
  56.        prev_label = from_label;
  57.        aiter->Next();
  58. -      if (!aiter->Done()) aiter_queue_->push(std::move(aiter));
  59. +      if (!aiter->Done()) {
  60. +        aiter_queue_.push_back(std::move(aiter));
  61. +        std::push_heap(aiter_queue_.begin(), aiter_queue_.end(), ArcIterCompare{});
  62. +      }
  63.      }
  64.      P_.FinalizeSplit(&L_);
  65.    }
  66. @@ -298,7 +295,7 @@ class CyclicMinimizer {
  67.    VectorFst<RevArc> Tr_;
  68.    // Priority queue of open arc iterators for all states in the splitter
  69.    // equivalence class.
  70. -  std::unique_ptr<ArcIterQueue> aiter_queue_;
  71. +  ArcIterQueue aiter_queue_;
  72.  };
  73.  
  74.  // Computes equivalence classes for acyclic FST.
Advertisement
Add Comment
Please, Sign In to add comment