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