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