FST  openfst-1.8.3
OpenFst Library
filter-state.h
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Classes for storing filter state in various algorithms like composition.
19 
20 #ifndef FST_FILTER_STATE_H_
21 #define FST_FILTER_STATE_H_
22 
23 #include <climits>
24 #include <cstddef>
25 #include <forward_list>
26 #include <utility>
27 
28 #include <fst/fst-decl.h> // For optional argument declarations
29 #include <fst/fst.h>
30 #include <fst/matcher.h>
31 
32 namespace fst {
33 
34 // The filter state interface represents the state of a (e.g., composition)
35 // filter.
36 //
37 // class FilterState {
38 // public:
39 // // Required constructors.
40 //
41 // FilterState();
42 //
43 // FilterState(const FilterState &fs);
44 //
45 // // An invalid filter state.
46 // static const FilterState NoState();
47 //
48 // // Maps state to integer for hashing.
49 // size_t Hash() const;
50 //
51 // // Equality of filter states.
52 // bool operator==(const FilterState &fs) const;
53 //
54 // // Inequality of filter states.
55 // bool operator!=(const FilterState &fs) const;
56 //
57 // // Assignment to filter states.
58 // FilterState &operator=(const FilterState& fs);
59 // };
60 
61 // Filter state that is a signed integral type.
62 template <typename T>
64  public:
66 
67  explicit IntegerFilterState(T s) : state_(s) {}
68 
69  static const IntegerFilterState NoState() { return IntegerFilterState(); }
70 
71  size_t Hash() const { return static_cast<size_t>(state_); }
72 
73  bool operator==(const IntegerFilterState &fs) const {
74  return state_ == fs.state_;
75  }
76 
77  bool operator!=(const IntegerFilterState &fs) const {
78  return state_ != fs.state_;
79  }
80 
81  T GetState() const { return state_; }
82 
83  private:
84  T state_;
85 };
86 
90 
91 // Filter state that is a weight (class).
92 template <class W>
94  public:
95  WeightFilterState() : weight_(W::Zero()) {}
96 
97  explicit WeightFilterState(W weight) : weight_(std::move(weight)) {}
98 
99  static const WeightFilterState NoState() { return WeightFilterState(); }
100 
101  size_t Hash() const { return weight_.Hash(); }
102 
103  bool operator==(const WeightFilterState &fs) const {
104  return weight_ == fs.weight_;
105  }
106 
107  bool operator!=(const WeightFilterState &fs) const {
108  return weight_ != fs.weight_;
109  }
110 
111  W GetWeight() const { return weight_; }
112 
113  private:
114  W weight_;
115 };
116 
117 // Filter state is a list of signed integer types T. Order matters
118 // for equality.
119 template <typename T>
121  public:
122  ListFilterState() = default;
123 
124  explicit ListFilterState(T s) { list_.push_front(s); }
125 
126  static const ListFilterState NoState() { return ListFilterState(kNoStateId); }
127 
128  size_t Hash() const {
129  size_t h = 0;
130  for (const auto &elem : list_) h ^= h << 1 ^ elem;
131  return h;
132  }
133 
134  bool operator==(const ListFilterState &fs) const { return list_ == fs.list_; }
135 
136  bool operator!=(const ListFilterState &fs) const { return list_ != fs.list_; }
137 
138  const std::forward_list<T> &GetState() const { return list_; }
139 
140  std::forward_list<T> *GetMutableState() { return &list_; }
141 
142  private:
143  std::forward_list<T> list_;
144 };
145 
146 // Filter state that is the combination of two filter states.
147 template <class FS1, class FS2>
149  public:
150  using FilterState1 = FS1;
151  using FilterState2 = FS2;
152 
153  PairFilterState() : fs1_(FS1::NoState()), fs2_(FS2::NoState()) {}
154 
155  PairFilterState(const FilterState1 &fs1, const FilterState2 &fs2)
156  : fs1_(fs1), fs2_(fs2) {}
157 
158  static const PairFilterState NoState() { return PairFilterState(); }
159 
160  size_t Hash() const {
161  const auto h1 = fs1_.Hash();
162  static constexpr auto lshift = 5;
163  static constexpr auto rshift = CHAR_BIT * sizeof(size_t) - 5;
164  return h1 << lshift ^ h1 >> rshift ^ fs2_.Hash();
165  }
166 
167  bool operator==(const PairFilterState &fs) const {
168  return fs1_ == fs.fs1_ && fs2_ == fs.fs2_;
169  }
170 
171  bool operator!=(const PairFilterState &fs) const {
172  return fs1_ != fs.fs1_ || fs2_ != fs.fs2_;
173  }
174 
175  const FilterState1 &GetState1() const { return fs1_; }
176 
177  const FilterState2 &GetState2() const { return fs2_; }
178 
179  private:
180  FilterState1 fs1_;
181  FilterState2 fs2_;
182 };
183 
184 // Single non-blocking filter state.
186  public:
187  explicit TrivialFilterState(bool state = false) : state_(state) {}
188 
189  static const TrivialFilterState NoState() { return TrivialFilterState(); }
190 
191  size_t Hash() const { return 0; }
192 
193  bool operator==(const TrivialFilterState &fs) const {
194  return state_ == fs.state_;
195  }
196 
197  bool operator!=(const TrivialFilterState &fs) const {
198  return state_ != fs.state_;
199  }
200 
201  private:
202  bool state_;
203 };
204 
205 } // namespace fst
206 
207 #endif // FST_FILTER_STATE_H_
bool operator==(const TrivialFilterState &fs) const
Definition: filter-state.h:193
bool operator!=(const IntegerFilterState &fs) const
Definition: filter-state.h:77
static const IntegerFilterState NoState()
Definition: filter-state.h:69
bool operator==(const IntegerFilterState &fs) const
Definition: filter-state.h:73
size_t Hash() const
Definition: filter-state.h:191
bool operator!=(const TrivialFilterState &fs) const
Definition: filter-state.h:197
bool operator!=(const ListFilterState &fs) const
Definition: filter-state.h:136
size_t Hash() const
Definition: filter-state.h:71
TrivialFilterState(bool state=false)
Definition: filter-state.h:187
bool operator==(const ListFilterState &fs) const
Definition: filter-state.h:134
constexpr int kNoStateId
Definition: fst.h:196
bool operator!=(const PairFilterState &fs) const
Definition: filter-state.h:171
static const PairFilterState NoState()
Definition: filter-state.h:158
static const ListFilterState NoState()
Definition: filter-state.h:126
static const TrivialFilterState NoState()
Definition: filter-state.h:189
bool operator==(const PairFilterState &fs) const
Definition: filter-state.h:167
bool operator!=(const WeightFilterState &fs) const
Definition: filter-state.h:107
bool operator==(const WeightFilterState &fs) const
Definition: filter-state.h:103
PairFilterState(const FilterState1 &fs1, const FilterState2 &fs2)
Definition: filter-state.h:155
size_t Hash() const
Definition: filter-state.h:101
size_t Hash() const
Definition: filter-state.h:128
static const WeightFilterState NoState()
Definition: filter-state.h:99
const FilterState1 & GetState1() const
Definition: filter-state.h:175
WeightFilterState(W weight)
Definition: filter-state.h:97
const FilterState2 & GetState2() const
Definition: filter-state.h:177
size_t Hash() const
Definition: filter-state.h:160
std::forward_list< T > * GetMutableState()
Definition: filter-state.h:140
const std::forward_list< T > & GetState() const
Definition: filter-state.h:138