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