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