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