FST  openfst-1.8.3
OpenFst Library
tuple-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 // Tuple weight set operation definitions.
19 
20 #ifndef FST_TUPLE_WEIGHT_H_
21 #define FST_TUPLE_WEIGHT_H_
22 
23 #include <algorithm>
24 #include <array>
25 #include <cstddef>
26 #include <cstdint>
27 #include <functional>
28 #include <istream>
29 #include <ostream>
30 #include <string>
31 #include <vector>
32 
33 #include <fst/flags.h>
34 #include <fst/log.h>
35 #include <fst/weight.h>
36 
37 namespace fst {
38 
39 // n-tuple weight, element of the n-th Cartesian power of W.
40 template <class W, size_t n>
41 class TupleWeight {
42  public:
44 
45  using Weight = W;
46  using Index = size_t;
47 
48  template <class Iterator>
49  TupleWeight(Iterator begin, Iterator end) {
50  std::copy(begin, end, values_.begin());
51  }
52 
53  explicit TupleWeight(const W &weight = W::Zero()) { values_.fill(weight); }
54 
55  // Initialize component `index` to `weight`; initialize all other components
56  // to `default_weight`
57  TupleWeight(Index index, const W &weight, const W &default_weight)
58  : TupleWeight(default_weight) {
59  values_[index] = weight;
60  }
61 
62  static const TupleWeight<W, n> &Zero() {
63  static const TupleWeight<W, n> zero(W::Zero());
64  return zero;
65  }
66 
67  static const TupleWeight<W, n> &One() {
68  static const TupleWeight<W, n> one(W::One());
69  return one;
70  }
71 
72  static const TupleWeight<W, n> &NoWeight() {
73  static const TupleWeight<W, n> no_weight(W::NoWeight());
74  return no_weight;
75  }
76 
77  constexpr static size_t Length() { return n; }
78 
79  std::istream &Read(std::istream &istrm) {
80  for (size_t i = 0; i < n; ++i) values_[i].Read(istrm);
81  return istrm;
82  }
83 
84  std::ostream &Write(std::ostream &ostrm) const {
85  for (size_t i = 0; i < n; ++i) values_[i].Write(ostrm);
86  return ostrm;
87  }
88 
89  bool Member() const {
90  return std::all_of(values_.begin(), values_.end(), std::mem_fn(&W::Member));
91  }
92 
93  size_t Hash() const {
94  uint64_t hash = 0;
95  for (size_t i = 0; i < n; ++i) hash = 5 * hash + values_[i].Hash();
96  return size_t(hash);
97  }
98 
99  TupleWeight<W, n> Quantize(float delta = kDelta) const {
100  TupleWeight<W, n> weight;
101  for (size_t i = 0; i < n; ++i) {
102  weight.values_[i] = values_[i].Quantize(delta);
103  }
104  return weight;
105  }
106 
109  for (size_t i = 0; i < n; ++i) w.values_[i] = values_[i].Reverse();
110  return w;
111  }
112 
113  const W &Value(size_t i) const { return values_[i]; }
114 
115  void SetValue(size_t i, const W &w) { values_[i] = w; }
116 
117  private:
118  std::array<W, n> values_;
119 };
120 
121 template <class W, size_t n>
122 inline bool operator==(const TupleWeight<W, n> &w1,
123  const TupleWeight<W, n> &w2) {
124  for (size_t i = 0; i < n; ++i) {
125  if (w1.Value(i) != w2.Value(i)) return false;
126  }
127  return true;
128 }
129 
130 template <class W, size_t n>
131 inline bool operator!=(const TupleWeight<W, n> &w1,
132  const TupleWeight<W, n> &w2) {
133  for (size_t i = 0; i < n; ++i) {
134  if (w1.Value(i) != w2.Value(i)) return true;
135  }
136  return false;
137 }
138 
139 template <class W, size_t n>
140 inline bool ApproxEqual(const TupleWeight<W, n> &w1,
141  const TupleWeight<W, n> &w2, float delta = kDelta) {
142  for (size_t i = 0; i < n; ++i) {
143  if (!ApproxEqual(w1.Value(i), w2.Value(i), delta)) return false;
144  }
145  return true;
146 }
147 
148 template <class W, size_t n>
149 inline std::ostream &operator<<(std::ostream &strm,
150  const TupleWeight<W, n> &w) {
151  CompositeWeightWriter writer(strm);
152  writer.WriteBegin();
153  for (size_t i = 0; i < n; ++i) writer.WriteElement(w.Value(i));
154  writer.WriteEnd();
155  return strm;
156 }
157 
158 template <class W, size_t n>
159 inline std::istream &operator>>(std::istream &strm, TupleWeight<W, n> &w) {
160  CompositeWeightReader reader(strm);
161  reader.ReadBegin();
162  W v;
163  // Reads first n-1 elements.
164  static_assert(n > 0, "Size must be positive.");
165  for (size_t i = 0; i < n - 1; ++i) {
166  reader.ReadElement(&v);
167  w.SetValue(i, v);
168  }
169  // Reads n-th element.
170  reader.ReadElement(&v, true);
171  w.SetValue(n - 1, v);
172  reader.ReadEnd();
173  return strm;
174 }
175 
176 } // namespace fst
177 
178 #endif // FST_TUPLE_WEIGHT_H_
void SetValue(size_t i, const W &w)
Definition: tuple-weight.h:115
size_t Hash() const
Definition: tuple-weight.h:93
TupleWeight(Index index, const W &weight, const W &default_weight)
Definition: tuple-weight.h:57
ReverseWeight Reverse() const
Definition: tuple-weight.h:107
static const TupleWeight< W, n > & NoWeight()
Definition: tuple-weight.h:72
const W & Value(size_t i) const
Definition: tuple-weight.h:113
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
TupleWeight(Iterator begin, Iterator end)
Definition: tuple-weight.h:49
std::ostream & operator<<(std::ostream &strm, const ErrorWeight &)
Definition: error-weight.h:71
std::ostream & Write(std::ostream &ostrm) const
Definition: tuple-weight.h:84
static const TupleWeight< W, n > & One()
Definition: tuple-weight.h:67
bool Member() const
Definition: tuple-weight.h:89
bool ReadElement(T *comp, bool last=false)
Definition: weight.h:367
bool operator==(const ErrorWeight &, const ErrorWeight &)
Definition: error-weight.h:51
static const TupleWeight< W, n > & Zero()
Definition: tuple-weight.h:62
TupleWeight(const W &weight=W::Zero())
Definition: tuple-weight.h:53
static constexpr size_t Length()
Definition: tuple-weight.h:77
void WriteElement(const T &comp)
Definition: weight.h:317
std::istream & Read(std::istream &istrm)
Definition: tuple-weight.h:79
constexpr float kDelta
Definition: weight.h:133
bool ApproxEqual(const ErrorWeight &, const ErrorWeight &, float)
Definition: error-weight.h:58
TupleWeight< W, n > Quantize(float delta=kDelta) const
Definition: tuple-weight.h:99