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