// Copyright 2005-2024 Google LLC // // Licensed under the Apache License, Version 2.0 (the 'License'); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an 'AS IS' BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // See www.openfst.org for extensive documentation on this weighted // finite-state transducer library. #ifndef FST_SCRIPT_SHORTEST_DISTANCE_H_ #define FST_SCRIPT_SHORTEST_DISTANCE_H_ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace fst { namespace script { struct ShortestDistanceOptions { const QueueType queue_type; const ArcFilterType arc_filter_type; const int64_t source; const float delta; ShortestDistanceOptions(QueueType queue_type, ArcFilterType arc_filter_type, int64_t source, float delta) : queue_type(queue_type), arc_filter_type(arc_filter_type), source(source), delta(delta) {} }; namespace internal { // Code to implement switching on queue and arc filter types. template struct QueueConstructor { using Weight = typename Arc::Weight; static std::unique_ptr Construct(const Fst &, const std::vector *) { return std::make_unique(); } }; // Specializations to support queues with different constructors. template struct QueueConstructor, ArcFilter> { using StateId = typename Arc::StateId; using Weight = typename Arc::Weight; // template static std::unique_ptr> Construct( const Fst &fst, const std::vector *distance) { return std::make_unique>(fst, distance, ArcFilter()); } }; template struct QueueConstructor< Arc, NaturalShortestFirstQueue, ArcFilter> { using StateId = typename Arc::StateId; using Weight = typename Arc::Weight; static std::unique_ptr> Construct( const Fst &, const std::vector *distance) { return std::make_unique>( *distance); } }; template struct QueueConstructor, ArcFilter> { using StateId = typename Arc::StateId; using Weight = typename Arc::Weight; static std::unique_ptr> Construct( const Fst &fst, const std::vector *) { return std::make_unique>(fst, ArcFilter()); } }; template void ShortestDistance(const Fst &fst, std::vector *distance, const ShortestDistanceOptions &opts) { std::unique_ptr queue( QueueConstructor::Construct(fst, distance)); const fst::ShortestDistanceOptions sopts( queue.get(), ArcFilter(), opts.source, opts.delta); ShortestDistance(fst, distance, sopts); } template void ShortestDistance(const Fst &fst, std::vector *distance, const ShortestDistanceOptions &opts) { switch (opts.arc_filter_type) { case ArcFilterType::ANY: { ShortestDistance>(fst, distance, opts); return; } case ArcFilterType::EPSILON: { ShortestDistance>(fst, distance, opts); return; } case ArcFilterType::INPUT_EPSILON: { ShortestDistance>(fst, distance, opts); return; } case ArcFilterType::OUTPUT_EPSILON: { ShortestDistance>(fst, distance, opts); return; } default: { FSTERROR() << "ShortestDistance: Unknown arc filter type: " << static_cast>( opts.arc_filter_type); distance->clear(); distance->resize(1, Arc::Weight::NoWeight()); return; } } } } // namespace internal using FstShortestDistanceArgs1 = std::tuple *, const ShortestDistanceOptions &>; template void ShortestDistance(FstShortestDistanceArgs1 *args) { using StateId = typename Arc::StateId; using Weight = typename Arc::Weight; const Fst &fst = *std::get<0>(*args).GetFst(); const auto &opts = std::get<2>(*args); std::vector typed_distance; switch (opts.queue_type) { case AUTO_QUEUE: { internal::ShortestDistance>(fst, &typed_distance, opts); break; } case FIFO_QUEUE: { internal::ShortestDistance>(fst, &typed_distance, opts); break; } case LIFO_QUEUE: { internal::ShortestDistance>(fst, &typed_distance, opts); break; } case SHORTEST_FIRST_QUEUE: { if constexpr (IsIdempotent::value) { internal::ShortestDistance>( fst, &typed_distance, opts); } else { FSTERROR() << "ShortestDistance: Bad queue type SHORTEST_FIRST_QUEUE" << " for non-idempotent Weight " << Weight::Type(); } break; } case STATE_ORDER_QUEUE: { internal::ShortestDistance>( fst, &typed_distance, opts); break; } case TOP_ORDER_QUEUE: { internal::ShortestDistance>( fst, &typed_distance, opts); break; } default: { FSTERROR() << "ShortestDistance: Unknown queue type: " << opts.queue_type; typed_distance.clear(); typed_distance.resize(1, Arc::Weight::NoWeight()); break; } } internal::CopyWeights(typed_distance, std::get<1>(*args)); } using FstShortestDistanceArgs2 = std::tuple *, bool, double>; template void ShortestDistance(FstShortestDistanceArgs2 *args) { using Weight = typename Arc::Weight; const Fst &fst = *std::get<0>(*args).GetFst(); std::vector typed_distance; ShortestDistance(fst, &typed_distance, std::get<2>(*args), std::get<3>(*args)); internal::CopyWeights(typed_distance, std::get<1>(*args)); } using FstShortestDistanceInnerArgs3 = std::tuple; using FstShortestDistanceArgs3 = WithReturnValue; template void ShortestDistance(FstShortestDistanceArgs3 *args) { const Fst &fst = *std::get<0>(args->args).GetFst(); args->retval = WeightClass(ShortestDistance(fst, std::get<1>(args->args))); } void ShortestDistance(const FstClass &fst, std::vector *distance, const ShortestDistanceOptions &opts); void ShortestDistance(const FstClass &ifst, std::vector *distance, bool reverse = false, double delta = fst::kShortestDelta); WeightClass ShortestDistance(const FstClass &ifst, double delta = fst::kShortestDelta); } // namespace script } // namespace fst #endif // FST_SCRIPT_SHORTEST_DISTANCE_H_