kongdeqiang
6 天以前 28ccfbfc51068a663a80764e14074df5edf2b5ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// See www.openfst.org for extensive documentation on this weighted
// finite-state transducer library.
//
// Classes for storing filter state in various algorithms like composition.
 
#ifndef FST_FILTER_STATE_H_
#define FST_FILTER_STATE_H_
 
#include <forward_list>
#include <utility>
 
#include <fst/fst-decl.h>  // For optional argument declarations
#include <fst/fst.h>
#include <fst/matcher.h>
 
 
namespace fst {
 
// The filter state interface represents the state of a (e.g., composition)
// filter.
//
// class FilterState {
//  public:
//   // Required constructors.
//
//   FilterState();
//
//   FilterState(const FilterState &fs);
//
//   // An invalid filter state.
//   static const FilterState NoState();
//
//   // Maps state to integer for hashing.
//   size_t Hash() const;
//
//   // Equality of filter states.
//   bool operator==(const FilterState &fs) const;
//
//   // Inequality of filter states.
//   bool operator!=(const FilterState &fs) const;
//
//   // Assignment to filter states.
//   FilterState &operator=(const FilterState& fs);
// };
 
// Filter state that is a signed integral type.
template <typename T>
class IntegerFilterState {
 public:
  IntegerFilterState() : state_(kNoStateId) {}
 
  explicit IntegerFilterState(T s) : state_(s) {}
 
  static const IntegerFilterState NoState() { return IntegerFilterState(); }
 
  size_t Hash() const { return static_cast<size_t>(state_); }
 
  bool operator==(const IntegerFilterState &fs) const {
    return state_ == fs.state_;
  }
 
  bool operator!=(const IntegerFilterState &fs) const {
    return state_ != fs.state_;
  }
 
  T GetState() const { return state_; }
 
  void SetState(T state) { state_ = state; }
 
 private:
  T state_;
};
 
using CharFilterState = IntegerFilterState<signed char>;
using ShortFilterState = IntegerFilterState<short>;  // NOLINT
using IntFilterState = IntegerFilterState<int>;
 
// Filter state that is a weight (class).
template <class W>
class WeightFilterState {
 public:
  WeightFilterState() : weight_(W::Zero()) {}
 
  explicit WeightFilterState(W weight) : weight_(std::move(weight)) {}
 
  static const WeightFilterState NoState() { return WeightFilterState(); }
 
  size_t Hash() const { return weight_.Hash(); }
 
  bool operator==(const WeightFilterState &fs) const {
    return weight_ == fs.weight_;
  }
 
  bool operator!=(const WeightFilterState &fs) const {
    return weight_ != fs.weight_;
  }
 
  W GetWeight() const { return weight_; }
 
  void SetWeight(W weight) { weight_ = std::move(weight); }
 
 private:
  W weight_;
};
 
// Filter state is a list of signed integer types T. Order matters
// for equality.
template <typename T>
class ListFilterState {
 public:
  ListFilterState() {}
 
  explicit ListFilterState(T s) { list_.push_front(s); }
 
  static const ListFilterState NoState() { return ListFilterState(kNoStateId); }
 
  size_t Hash() const {
    size_t h = 0;
    for (const auto &elem : list_) h ^= h << 1 ^ elem;
    return h;
  }
 
  bool operator==(const ListFilterState &fs) const { return list_ == fs.list_; }
 
  bool operator!=(const ListFilterState &fs) const { return list_ != fs.list_; }
 
  const std::forward_list<T> &GetState() const { return list_; }
 
  std::forward_list<T> *GetMutableState() { return &list_; }
 
  void SetState(const std::forward_list<T> &state) { list_ = state; }
 
 private:
  std::forward_list<T> list_;
};
 
// Filter state that is the combination of two filter states.
template <class FS1, class FS2>
class PairFilterState {
 public:
  PairFilterState() : fs1_(FS1::NoState()), fs2_(FS2::NoState()) {}
 
  PairFilterState(const FS1 &fs1, const FS2 &fs2) : fs1_(fs1), fs2_(fs2) {}
 
  static const PairFilterState NoState() { return PairFilterState(); }
 
  size_t Hash() const {
    const auto h1 = fs1_.Hash();
    static constexpr auto lshift = 5;
    static constexpr auto rshift = CHAR_BIT * sizeof(size_t) - 5;
    return h1 << lshift ^ h1 >> rshift ^ fs2_.Hash();
  }
 
  bool operator==(const PairFilterState &fs) const {
    return fs1_ == fs.fs1_ && fs2_ == fs.fs2_;
  }
 
  bool operator!=(const PairFilterState &fs) const {
    return fs1_ != fs.fs1_ || fs2_ != fs.fs2_;
  }
 
  const FS1 &GetState1() const { return fs1_; }
 
  const FS2 &GetState2() const { return fs2_; }
 
  void SetState(const FS1 &fs1, const FS2 &fs2) {
    fs1_ = fs1;
    fs2_ = fs2;
  }
 
 private:
  FS1 fs1_;
  FS2 fs2_;
};
 
// Single non-blocking filter state.
class TrivialFilterState {
 public:
  explicit TrivialFilterState(bool state = false) : state_(state) {}
 
  static const TrivialFilterState NoState() { return TrivialFilterState(); }
 
  size_t Hash() const { return 0; }
 
  bool operator==(const TrivialFilterState &fs) const {
    return state_ == fs.state_;
  }
 
  bool operator!=(const TrivialFilterState &fs) const {
    return state_ != fs.state_;
  }
 
 private:
  bool state_;
};
 
}  // namespace fst
 
#endif  // FST_FILTER_STATE_H_