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