FST  openfst-1.8.2.post1
OpenFst Library
weight-class.cc
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 
19 
20 #include <string_view>
21 
22 namespace fst {
23 namespace script {
24 
28 
29 WeightClass::WeightClass(std::string_view weight_type,
30  std::string_view weight_str) {
31  static const auto *reg = WeightClassRegister::GetRegister();
32  const auto stw = reg->GetEntry(weight_type);
33  if (!stw) {
34  FSTERROR() << "WeightClass: Unknown weight type: " << weight_type;
35  impl_.reset();
36  return;
37  }
38  impl_ = stw(weight_str);
39 }
40 
41 WeightClass WeightClass::Zero(std::string_view weight_type) {
42  return WeightClass(weight_type, __ZERO__);
43 }
44 
45 WeightClass WeightClass::One(std::string_view weight_type) {
46  return WeightClass(weight_type, __ONE__);
47 }
48 
49 WeightClass WeightClass::NoWeight(std::string_view weight_type) {
50  return WeightClass(weight_type, __NOWEIGHT__);
51 }
52 
54  const WeightClass &rhs,
55  std::string_view op_name) {
56  if (lhs.Type() != rhs.Type()) {
57  FSTERROR() << op_name << ": Weights with non-matching types: " << lhs.Type()
58  << " and " << rhs.Type();
59  return false;
60  }
61  return true;
62 }
63 
64 bool operator==(const WeightClass &lhs, const WeightClass &rhs) {
65  const auto *lhs_impl = lhs.GetImpl();
66  const auto *rhs_impl = rhs.GetImpl();
67  if (!(lhs_impl && rhs_impl &&
68  WeightClass::WeightTypesMatch(lhs, rhs, "operator=="))) {
69  return false;
70  }
71  return *lhs_impl == *rhs_impl;
72 }
73 
74 bool operator!=(const WeightClass &lhs, const WeightClass &rhs) {
75  return !(lhs == rhs);
76 }
77 
78 WeightClass Plus(const WeightClass &lhs, const WeightClass &rhs) {
79  const auto *rhs_impl = rhs.GetImpl();
80  if (!(lhs.GetImpl() && rhs_impl &&
81  WeightClass::WeightTypesMatch(lhs, rhs, "Plus"))) {
82  return WeightClass();
83  }
84  WeightClass result(lhs);
85  result.GetImpl()->PlusEq(*rhs_impl);
86  return result;
87 }
88 
89 WeightClass Times(const WeightClass &lhs, const WeightClass &rhs) {
90  const auto *rhs_impl = rhs.GetImpl();
91  if (!(lhs.GetImpl() && rhs_impl &&
92  WeightClass::WeightTypesMatch(lhs, rhs, "Times"))) {
93  return WeightClass();
94  }
95  WeightClass result(lhs);
96  result.GetImpl()->TimesEq(*rhs_impl);
97  return result;
98 }
99 
100 WeightClass Divide(const WeightClass &lhs, const WeightClass &rhs) {
101  const auto *rhs_impl = rhs.GetImpl();
102  if (!(lhs.GetImpl() && rhs_impl &&
103  WeightClass::WeightTypesMatch(lhs, rhs, "Divide"))) {
104  return WeightClass();
105  }
106  WeightClass result(lhs);
107  result.GetImpl()->DivideEq(*rhs_impl);
108  return result;
109 }
110 
111 WeightClass Power(const WeightClass &weight, size_t n) {
112  if (!weight.GetImpl()) return WeightClass();
113  WeightClass result(weight);
114  result.GetImpl()->PowerEq(n);
115  return result;
116 }
117 
118 std::ostream &operator<<(std::ostream &ostrm, const WeightClass &weight) {
119  weight.impl_->Print(&ostrm);
120  return ostrm;
121 }
122 
123 } // namespace script
124 } // namespace fst
friend WeightClass Plus(const WeightClass &lhs, const WeightClass &rhs)
Definition: weight-class.cc:78
W Weight
Definition: arc.h:46
friend bool operator==(const WeightClass &lhs, const WeightClass &rhs)
Definition: weight-class.cc:64
static constexpr std::string_view __ONE__
Definition: weight-class.h:133
virtual WeightImplBase & PowerEq(size_t n)=0
const std::string & Type() const
Definition: weight-class.h:154
#define FSTERROR()
Definition: util.h:53
static constexpr std::string_view __NOWEIGHT__
Definition: weight-class.h:134
friend std::ostream & operator<<(std::ostream &o, const WeightClass &c)
virtual WeightImplBase & DivideEq(const WeightImplBase &other)=0
virtual WeightImplBase & PlusEq(const WeightImplBase &other)=0
REGISTER_FST_WEIGHT(StdArc::Weight)
friend WeightClass Power(const WeightClass &w, size_t n)
static bool WeightTypesMatch(const WeightClass &lhs, const WeightClass &rhs, std::string_view op_name)
Definition: weight-class.cc:53
static WeightClass Zero(std::string_view weight_type)
Definition: weight-class.cc:41
static WeightClass NoWeight(std::string_view weight_type)
Definition: weight-class.cc:49
virtual WeightImplBase & TimesEq(const WeightImplBase &other)=0
static WeightClass One(std::string_view weight_type)
Definition: weight-class.cc:45
bool operator!=(const WeightClass &lhs, const WeightClass &rhs)
Definition: weight-class.cc:74
friend WeightClass Times(const WeightClass &lhs, const WeightClass &rhs)
Definition: weight-class.cc:89
friend WeightClass Divide(const WeightClass &lhs, const WeightClass &rhs)
static constexpr std::string_view __ZERO__
Definition: weight-class.h:132