FST  openfst-1.8.3
OpenFst Library
shortest-path.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 #ifndef FST_SCRIPT_SHORTEST_PATH_H_
19 #define FST_SCRIPT_SHORTEST_PATH_H_
20 
21 #include <cstdint>
22 #include <memory>
23 #include <tuple>
24 #include <vector>
25 
26 #include <fst/log.h>
27 #include <fst/arcfilter.h>
28 #include <fst/fst.h>
29 #include <fst/mutable-fst.h>
30 #include <fst/properties.h>
31 #include <fst/queue.h>
32 #include <fst/shortest-path.h>
33 #include <fst/util.h>
34 #include <fst/weight.h>
36 #include <fst/script/fst-class.h>
39 
40 namespace fst {
41 namespace script {
42 
43 // Slightly simplified interface: `has_distance` and `first_path` are disabled.
44 
46  const int32_t nshortest;
47  const bool unique;
49  const int64_t state_threshold;
50 
51  ShortestPathOptions(QueueType queue_type, int32_t nshortest, bool unique,
52  float delta, const WeightClass &weight_threshold,
53  int64_t state_threshold = kNoStateId)
55  delta),
56  nshortest(nshortest),
57  unique(unique),
58  weight_threshold(weight_threshold),
59  state_threshold(state_threshold) {}
60 };
61 
62 namespace internal {
63 
64 // Code to implement switching on queue types.
65 
66 template <class Arc, class Queue>
67 void ShortestPath(const Fst<Arc> &ifst, MutableFst<Arc> *ofst,
68  std::vector<typename Arc::Weight> *distance,
69  const ShortestPathOptions &opts) {
70  using ArcFilter = AnyArcFilter<Arc>;
71  using Weight = typename Arc::Weight;
72  if constexpr (IsPath<Weight>::value) {
73  const std::unique_ptr<Queue> queue(
76  queue.get(), ArcFilter(), opts.nshortest, opts.unique,
77  /* has_distance=*/false, opts.delta, /* first_path=*/false,
78  *opts.weight_threshold.GetWeight<Weight>(), opts.state_threshold);
79  ShortestPath(ifst, ofst, distance, sopts);
80  } else {
81  FSTERROR() << "ShortestPath: Weight needs to have the path property: "
82  << Arc::Weight::Type();
83  ofst->SetProperties(kError, kError);
84  }
85 }
86 
87 template <class Arc>
88 void ShortestPath(const Fst<Arc> &ifst, MutableFst<Arc> *ofst,
89  const ShortestPathOptions &opts) {
90  using StateId = typename Arc::StateId;
91  using Weight = typename Arc::Weight;
92  std::vector<Weight> distance;
93  switch (opts.queue_type) {
94  case AUTO_QUEUE: {
95  ShortestPath<Arc, AutoQueue<StateId>>(ifst, ofst, &distance, opts);
96  return;
97  }
98  case FIFO_QUEUE: {
99  ShortestPath<Arc, FifoQueue<StateId>>(ifst, ofst, &distance, opts);
100  return;
101  }
102  case LIFO_QUEUE: {
103  ShortestPath<Arc, LifoQueue<StateId>>(ifst, ofst, &distance, opts);
104  return;
105  }
106  case SHORTEST_FIRST_QUEUE: {
107  if constexpr (IsIdempotent<Weight>::value) {
108  ShortestPath<Arc, NaturalShortestFirstQueue<StateId, Weight>>(
109  ifst, ofst, &distance, opts);
110  } else {
111  FSTERROR() << "ShortestPath: Bad queue type SHORTEST_FIRST_QUEUE for"
112  << " non-idempotent Weight " << Weight::Type();
113  ofst->SetProperties(kError, kError);
114  }
115  return;
116  }
117  case STATE_ORDER_QUEUE: {
118  ShortestPath<Arc, StateOrderQueue<StateId>>(ifst, ofst, &distance, opts);
119  return;
120  }
121  case TOP_ORDER_QUEUE: {
122  ShortestPath<Arc, TopOrderQueue<StateId>>(ifst, ofst, &distance, opts);
123  return;
124  }
125  default: {
126  FSTERROR() << "ShortestPath: Unknown queue type: " << opts.queue_type;
127  ofst->SetProperties(kError, kError);
128  return;
129  }
130  }
131 }
132 
133 } // namespace internal
134 
135 using FstShortestPathArgs = std::tuple<const FstClass &, MutableFstClass *,
137 
138 template <class Arc>
140  const Fst<Arc> &ifst = *std::get<0>(*args).GetFst<Arc>();
141  MutableFst<Arc> *ofst = std::get<1>(*args)->GetMutableFst<Arc>();
142  const ShortestPathOptions &opts = std::get<2>(*args);
143  internal::ShortestPath(ifst, ofst, opts);
144 }
145 
146 void ShortestPath(const FstClass &ifst, MutableFstClass *ofst,
147  const ShortestPathOptions &opts);
148 
149 } // namespace script
150 } // namespace fst
151 
152 #endif // FST_SCRIPT_SHORTEST_PATH_H_
ShortestPathOptions(QueueType queue_type, int32_t nshortest, bool unique, float delta, const WeightClass &weight_threshold, int64_t state_threshold=kNoStateId)
Definition: shortest-path.h:51
QueueType
Definition: queue.h:76
constexpr uint64_t kError
Definition: properties.h:52
void ShortestPath(const FstClass &ifst, const std::vector< std::pair< int64_t, int64_t >> &parens, MutableFstClass *ofst, const PdtShortestPathOptions &opts)
Definition: pdtscript.cc:109
std::bool_constant<(W::Properties()&kIdempotent)!=0 > IsIdempotent
Definition: weight.h:159
constexpr int kNoStateId
Definition: fst.h:196
#define FSTERROR()
Definition: util.h:56
const WeightClass & weight_threshold
Definition: shortest-path.h:48
virtual void SetProperties(uint64_t props, uint64_t mask)=0
void ShortestPath(const Fst< Arc > &ifst, MutableFst< Arc > *ofst, std::vector< typename Arc::Weight > *distance, const ShortestPathOptions &opts)
Definition: shortest-path.h:67
const W * GetWeight() const
Definition: weight-class.h:144
std::tuple< const FstClass &, MutableFstClass *, const ShortestPathOptions & > FstShortestPathArgs
std::bool_constant<(W::Properties()&kPath)!=0 > IsPath
Definition: weight.h:162