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