FST  openfst-1.7.1
OpenFst Library
weight-class.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 
5 
6 namespace fst {
7 namespace script {
8 
12 
13 WeightClass::WeightClass(const string &weight_type, const string &weight_str) {
15  StrToWeightImplBaseT stw = reg->GetEntry(weight_type);
16  if (!stw) {
17  FSTERROR() << "Unknown weight type: " << weight_type;
18  impl_.reset();
19  return;
20  }
21  impl_.reset(stw(weight_str, "WeightClass", 0));
22 }
23 
24 WeightClass WeightClass::Zero(const string &weight_type) {
25  return WeightClass(weight_type, __ZERO__);
26 }
27 
28 WeightClass WeightClass::One(const string &weight_type) {
29  return WeightClass(weight_type, __ONE__);
30 }
31 
32 WeightClass WeightClass::NoWeight(const string &weight_type) {
33  return WeightClass(weight_type, __NOWEIGHT__);
34 }
35 
37  const string &op_name) const {
38  if (Type() != other.Type()) {
39  FSTERROR() << "Weights with non-matching types passed to " << op_name
40  << ": " << Type() << " and " << other.Type();
41  return false;
42  }
43  return true;
44 }
45 
46 bool operator==(const WeightClass &lhs, const WeightClass &rhs) {
47  const auto *lhs_impl = lhs.GetImpl();
48  const auto *rhs_impl = rhs.GetImpl();
49  if (!(lhs_impl && rhs_impl && lhs.WeightTypesMatch(rhs, "operator=="))) {
50  return false;
51  }
52  return *lhs_impl == *rhs_impl;
53 }
54 
55 bool operator!=(const WeightClass &lhs, const WeightClass &rhs) {
56  return !(lhs == rhs);
57 }
58 
59 WeightClass Plus(const WeightClass &lhs, const WeightClass &rhs) {
60  const auto *rhs_impl = rhs.GetImpl();
61  if (!(lhs.GetImpl() && rhs_impl && lhs.WeightTypesMatch(rhs, "Plus"))) {
62  return WeightClass();
63  }
64  WeightClass result(lhs);
65  result.GetImpl()->PlusEq(*rhs_impl);
66  return result;
67 }
68 
69 WeightClass Times(const WeightClass &lhs, const WeightClass &rhs) {
70  const auto *rhs_impl = rhs.GetImpl();
71  if (!(lhs.GetImpl() && rhs_impl && lhs.WeightTypesMatch(rhs, "Plus"))) {
72  return WeightClass();
73  }
74  WeightClass result(lhs);
75  result.GetImpl()->TimesEq(*rhs_impl);
76  return result;
77 }
78 
79 WeightClass Divide(const WeightClass &lhs, const WeightClass &rhs) {
80  const auto *rhs_impl = rhs.GetImpl();
81  if (!(lhs.GetImpl() && rhs_impl && lhs.WeightTypesMatch(rhs, "Divide"))) {
82  return WeightClass();
83  }
84  WeightClass result(lhs);
85  result.GetImpl()->DivideEq(*rhs_impl);
86  return result;
87 }
88 
89 WeightClass Power(const WeightClass &weight, size_t n) {
90  if (!weight.GetImpl()) return WeightClass();
91  WeightClass result(weight);
92  result.GetImpl()->PowerEq(n);
93  return result;
94 }
95 
96 std::ostream &operator<<(std::ostream &ostrm, const WeightClass &weight) {
97  weight.impl_->Print(&ostrm);
98  return ostrm;
99 }
100 
101 } // namespace script
102 } // namespace fst
friend WeightClass Plus(const WeightClass &lhs, const WeightClass &rhs)
Definition: weight-class.cc:59
static constexpr const char * __ZERO__
Definition: weight-class.h:120
W Weight
Definition: arc.h:30
friend bool operator==(const WeightClass &lhs, const WeightClass &rhs)
Definition: weight-class.cc:46
virtual WeightImplBase & PowerEq(size_t n)=0
bool WeightTypesMatch(const WeightClass &other, const string &op_name) const
Definition: weight-class.cc:36
static constexpr const char * __ONE__
Definition: weight-class.h:124
static constexpr const char * __NOWEIGHT__
Definition: weight-class.h:128
#define FSTERROR()
Definition: util.h:35
friend std::ostream & operator<<(std::ostream &o, const WeightClass &c)
Definition: weight-class.cc:96
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)
Definition: weight-class.cc:89
WeightImplBase *(*)(const string &str, const string &src, size_t nline) StrToWeightImplBaseT
Definition: weight-class.h:192
EntryType GetEntry(const KeyType &key) const
static WeightClass NoWeight(const string &weight_type)
Definition: weight-class.cc:32
virtual WeightImplBase & TimesEq(const WeightImplBase &other)=0
static WeightClass Zero(const string &weight_type)
Definition: weight-class.cc:24
const string & Type() const
Definition: weight-class.h:144
bool operator!=(const WeightClass &lhs, const WeightClass &rhs)
Definition: weight-class.cc:55
static WeightClass One(const string &weight_type)
Definition: weight-class.cc:28
friend WeightClass Times(const WeightClass &lhs, const WeightClass &rhs)
Definition: weight-class.cc:69
friend WeightClass Divide(const WeightClass &lhs, const WeightClass &rhs)
Definition: weight-class.cc:79