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