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