FST  openfst-1.7.2
OpenFst Library
pair-weight.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Pair weight templated base class for weight classes that contain two weights
5 // (e.g. Product, Lexicographic).
6 
7 #ifndef FST_PAIR_WEIGHT_H_
8 #define FST_PAIR_WEIGHT_H_
9 
10 #include <climits>
11 #include <stack>
12 #include <string>
13 #include <utility>
14 
15 #include <fst/flags.h>
16 #include <fst/log.h>
17 
18 #include <fst/weight.h>
19 
20 
21 namespace fst {
22 
23 template <class W1, class W2>
24 class PairWeight {
25  public:
26  using ReverseWeight =
28 
30 
31  PairWeight(W1 w1, W2 w2) : value1_(std::move(w1)), value2_(std::move(w2)) {}
32 
33  static const PairWeight<W1, W2> &Zero() {
34  static const PairWeight zero(W1::Zero(), W2::Zero());
35  return zero;
36  }
37 
38  static const PairWeight<W1, W2> &One() {
39  static const PairWeight one(W1::One(), W2::One());
40  return one;
41  }
42 
43  static const PairWeight<W1, W2> &NoWeight() {
44  static const PairWeight no_weight(W1::NoWeight(), W2::NoWeight());
45  return no_weight;
46  }
47 
48  std::istream &Read(std::istream &strm) {
49  value1_.Read(strm);
50  return value2_.Read(strm);
51  }
52 
53  std::ostream &Write(std::ostream &strm) const {
54  value1_.Write(strm);
55  return value2_.Write(strm);
56  }
57 
58  bool Member() const { return value1_.Member() && value2_.Member(); }
59 
60  size_t Hash() const {
61  const auto h1 = value1_.Hash();
62  const auto h2 = value2_.Hash();
63  static constexpr int lshift = 5;
64  static constexpr int rshift = CHAR_BIT * sizeof(size_t) - 5;
65  return h1 << lshift ^ h1 >> rshift ^ h2;
66  }
67 
68  PairWeight<W1, W2> Quantize(float delta = kDelta) const {
69  return PairWeight<W1, W2>(value1_.Quantize(delta), value2_.Quantize(delta));
70  }
71 
73  return ReverseWeight(value1_.Reverse(), value2_.Reverse());
74  }
75 
76  const W1 &Value1() const { return value1_; }
77 
78  const W2 &Value2() const { return value2_; }
79 
80  void SetValue1(const W1 &weight) { value1_ = weight; }
81 
82  void SetValue2(const W2 &weight) { value2_ = weight; }
83 
84  private:
85  W1 value1_;
86  W2 value2_;
87 };
88 
89 template <class W1, class W2>
90 inline bool operator==(const PairWeight<W1, W2> &w1,
91  const PairWeight<W1, W2> &w2) {
92  return w1.Value1() == w2.Value1() && w1.Value2() == w2.Value2();
93 }
94 
95 template <class W1, class W2>
96 inline bool operator!=(const PairWeight<W1, W2> &w1,
97  const PairWeight<W1, W2> &w2) {
98  return w1.Value1() != w2.Value1() || w1.Value2() != w2.Value2();
99 }
100 
101 template <class W1, class W2>
102 inline bool ApproxEqual(const PairWeight<W1, W2> &w1,
103  const PairWeight<W1, W2> &w2, float delta = kDelta) {
104  return ApproxEqual(w1.Value1(), w2.Value1(), delta) &&
105  ApproxEqual(w1.Value2(), w2.Value2(), delta);
106 }
107 
108 template <class W1, class W2>
109 inline std::ostream &operator<<(std::ostream &strm,
110  const PairWeight<W1, W2> &weight) {
111  CompositeWeightWriter writer(strm);
112  writer.WriteBegin();
113  writer.WriteElement(weight.Value1());
114  writer.WriteElement(weight.Value2());
115  writer.WriteEnd();
116  return strm;
117 }
118 
119 template <class W1, class W2>
120 inline std::istream &operator>>(std::istream &strm,
121  PairWeight<W1, W2> &weight) {
122  CompositeWeightReader reader(strm);
123  reader.ReadBegin();
124  W1 w1;
125  reader.ReadElement(&w1);
126  weight.SetValue1(w1);
127  W2 w2;
128  reader.ReadElement(&w2, true);
129  weight.SetValue2(w2);
130  reader.ReadEnd();
131  return strm;
132 }
133 
134 // This function object returns weights by calling the underlying generators
135 // and forming a pair. This is intended primarily for testing.
136 template <class W1, class W2>
137 class WeightGenerate<PairWeight<W1, W2>> {
138  public:
142 
143  explicit WeightGenerate(bool allow_zero = true)
144  : generate1_(allow_zero), generate2_(allow_zero) {}
145 
146  Weight operator()() const { return Weight(generate1_(), generate2_()); }
147 
148  private:
149  Generate1 generate1_;
150  Generate2 generate2_;
151 };
152 
153 } // namespace fst
154 
155 #endif // FST_PAIR_WEIGHT_H_
static const PairWeight< W1, W2 > & NoWeight()
Definition: pair-weight.h:43
ReverseWeight Reverse() const
Definition: pair-weight.h:72
static const PairWeight< W1, W2 > & One()
Definition: pair-weight.h:38
bool Member() const
Definition: pair-weight.h:58
const W2 & Value2() const
Definition: pair-weight.h:78
PairWeight< typename W1::ReverseWeight, typename W2::ReverseWeight > ReverseWeight
Definition: pair-weight.h:27
void SetValue2(const W2 &weight)
Definition: pair-weight.h:82
static const PairWeight< W1, W2 > & Zero()
Definition: pair-weight.h:33
std::ostream & Write(std::ostream &strm) const
Definition: pair-weight.h:53
std::istream & operator>>(std::istream &strm, FloatWeightTpl< T > &w)
Definition: float-weight.h:160
bool operator==(const PdtStateTuple< S, K > &x, const PdtStateTuple< S, K > &y)
Definition: pdt.h:133
PairWeight(W1 w1, W2 w2)
Definition: pair-weight.h:31
std::ostream & operator<<(std::ostream &strm, const FloatWeightTpl< T > &w)
Definition: float-weight.h:146
constexpr bool operator!=(const FloatWeightTpl< T > &w1, const FloatWeightTpl< T > &w2)
Definition: float-weight.h:119
size_t Hash() const
Definition: pair-weight.h:60
bool ReadElement(T *comp, bool last=false)
Definition: weight.h:349
constexpr bool ApproxEqual(const FloatWeightTpl< T > &w1, const FloatWeightTpl< T > &w2, float delta=kDelta)
Definition: float-weight.h:140
PairWeight< W1, W2 > Quantize(float delta=kDelta) const
Definition: pair-weight.h:68
std::istream & Read(std::istream &strm)
Definition: pair-weight.h:48
void WriteElement(const T &comp)
Definition: weight.h:299
constexpr float kDelta
Definition: weight.h:109
void SetValue1(const W1 &weight)
Definition: pair-weight.h:80
const W1 & Value1() const
Definition: pair-weight.h:76