// product-weight.h // 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. // // Copyright 2005-2010 Google, Inc. // Author: riley@google.com (Michael Riley) // // \file // Product weight set and associated semiring operation definitions. #ifndef FST_LIB_PRODUCT_WEIGHT_H__ #define FST_LIB_PRODUCT_WEIGHT_H__ #include #include #include #include namespace fst { // Product semiring: W1 * W2 template class ProductWeight : public PairWeight { public: using PairWeight::Zero; using PairWeight::One; using PairWeight::NoWeight; using PairWeight::Quantize; using PairWeight::Reverse; typedef ProductWeight ReverseWeight; ProductWeight() {} ProductWeight(const PairWeight& w) : PairWeight(w) {} ProductWeight(W1 w1, W2 w2) : PairWeight(w1, w2) {} static const ProductWeight &Zero() { static const ProductWeight zero(PairWeight::Zero()); return zero; } static const ProductWeight &One() { static const ProductWeight one(PairWeight::One()); return one; } static const ProductWeight &NoWeight() { static const ProductWeight no_weight( PairWeight::NoWeight()); return no_weight; } static const string &Type() { static const string type = W1::Type() + "_X_" + W2::Type(); return type; } static uint64 Properties() { uint64 props1 = W1::Properties(); uint64 props2 = W2::Properties(); return props1 & props2 & (kLeftSemiring | kRightSemiring | kCommutative | kIdempotent); } ProductWeight Quantize(float delta = kDelta) const { return PairWeight::Quantize(delta); } ReverseWeight Reverse() const { return PairWeight::Reverse(); } }; template inline ProductWeight Plus(const ProductWeight &w, const ProductWeight &v) { return ProductWeight(Plus(w.Value1(), v.Value1()), Plus(w.Value2(), v.Value2())); } template inline ProductWeight Times(const ProductWeight &w, const ProductWeight &v) { return ProductWeight(Times(w.Value1(), v.Value1()), Times(w.Value2(), v.Value2())); } template inline ProductWeight Divide(const ProductWeight &w, const ProductWeight &v, DivideType typ = DIVIDE_ANY) { return ProductWeight(Divide(w.Value1(), v.Value1(), typ), Divide(w.Value2(), v.Value2(), typ)); } } // namespace fst #endif // FST_LIB_PRODUCT_WEIGHT_H__