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