FST  openfst-1.8.3
OpenFst Library
compactors.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 #ifndef FST_TEST_COMPACTORS_H_
16 #define FST_TEST_COMPACTORS_H_
17 
18 // See www.openfst.org for extensive documentation on this weighted
19 // finite-state transducer library.
20 //
21 // Compactors for use in tests. See compact-fst.h.
22 
23 #include <sys/types.h>
24 
25 #include <cstddef>
26 #include <cstdint>
27 #include <istream>
28 #include <memory>
29 #include <ostream>
30 #include <string>
31 #include <type_traits>
32 
33 #include <fst/arc.h>
34 #include <fst/expanded-fst.h>
35 #include <fst/fst.h>
36 #include <fst/properties.h>
37 #include <fst/util.h>
38 #include <fst/vector-fst.h>
39 
40 namespace fst {
41 
42 // A user-defined compactor for test FST.
43 // Stores all Arc components as a tuple.
44 template <class A>
46  public:
47  using Arc = A;
48  using Label = typename A::Label;
49  using StateId = typename A::StateId;
50  using Weight = typename A::Weight;
51  // We use ArcTpl, which is trivially copyable if Weight is.
52  static_assert(std::is_trivially_copyable_v<Weight>,
53  "Weight must be trivially copyable.");
55  static_assert(std::is_trivially_copyable_v<Element>,
56  "ArcTpl should be trivially copyable; someone broke it.");
57 
58  Element Compact(StateId s, const A &arc) const {
59  return Element(arc.ilabel, arc.olabel, arc.weight, arc.nextstate);
60  }
61 
62  Arc Expand(StateId s, const Element &e, uint32_t f = kArcValueFlags) const {
63  return Arc(e.ilabel, e.olabel, e.weight, e.nextstate);
64  }
65 
66  ssize_t Size() const { return -1; }
67 
68  uint64_t Properties() const { return 0ULL; }
69 
70  bool Compatible(const Fst<A> &fst) const { return true; }
71 
72  static const std::string &Type() {
73  static const std::string *const type =
74  new std::string("trival_arc_compactor_" + Arc::Type());
75  return *type;
76  }
77 
78  bool Write(std::ostream &strm) const { return true; }
79 
80  static TrivialArcCompactor *Read(std::istream &strm) {
81  return new TrivialArcCompactor;
82  }
83 };
84 
85 // A user-defined arc compactor for test FST.
86 // Doesn't actually do any compacting, but exercises the Compactor interface.
87 template <class A>
89  public:
90  using Arc = A;
91  using StateId = typename Arc::StateId;
92  using Weight = typename Arc::Weight;
93 
94  // Any empty FST is OK.
95  TrivialCompactor() : fst_(new VectorFst<Arc>) {}
96 
97  // Constructor from the Fst to be compacted. If compactor is present,
98  // only optional state should be copied from it.
99  explicit TrivialCompactor(const Fst<Arc> &fst,
100  std::shared_ptr<TrivialCompactor> = nullptr)
101  : fst_(fst.Copy()) {}
102 
103  // Copy constructor. Must make a thread-safe copy suitable for use by
104  // by Fst::Copy(/*safe=*/true).
106  : fst_(compactor.fst_->Copy(/*safe=*/true)) {}
107 
108  StateId Start() const { return fst_->Start(); }
109  StateId NumStates() const { return CountStates(*fst_); }
110  size_t NumArcs() const { return CountArcs(*fst_); }
111 
112  // Accessor class for state attributes.
113  class State {
114  public:
115  State() = default;
117  : c_(c),
118  s_(s),
119  i_(std::make_unique<ArcIterator<Fst<Arc>>>(*c->fst_, s)) {}
120  StateId GetStateId() const { return s_; }
121  Weight Final() const { return c_->fst_->Final(s_); }
122  size_t NumArcs() const { return c_->fst_->NumArcs(s_); }
123  Arc GetArc(size_t i, uint32_t f) const {
124  i_->Seek(i);
125  return i_->Value();
126  }
127 
128  private:
129  const TrivialCompactor *c_ = nullptr;
130  StateId s_ = kNoStateId;
131  std::unique_ptr<ArcIterator<Fst<Arc>>> i_;
132  };
133 
134  void SetState(StateId s, State *state) { *state = State(this, s); }
135 
136  template <typename Arc>
137  bool IsCompatible(const Fst<Arc> &fst) const {
138  return std::is_same_v<Arc, A>;
139  }
140 
141  uint64_t Properties(uint64_t props) const { return props; }
142 
143  static const std::string &Type() {
144  static const std::string *const type =
145  new std::string("trivial_compactor_" + Arc::Type());
146  return *type;
147  }
148 
149  bool Error() const { return fst_->Properties(kError, /*test=*/false); }
150 
151  bool Write(std::ostream &strm, const FstWriteOptions &opts) const {
152  WriteType(strm, Type());
153  // Write as a VectorFst.
154  return VectorFst<Arc>::WriteFst(*fst_, strm, opts);
155  }
156 
157  static TrivialCompactor *Read(std::istream &strm, FstReadOptions opts,
158  const FstHeader &hdr) {
159  std::string type;
160  ReadType(strm, &type);
161  if (type != Type()) return nullptr;
162  opts.header = nullptr;
163  auto fst = fst::WrapUnique(VectorFst<Arc>::Read(strm, opts));
164  if (fst == nullptr) return nullptr;
165  return new TrivialCompactor(*fst);
166  }
167 
168  private:
169  std::unique_ptr<Fst<Arc>> fst_;
170 };
171 
172 } // namespace fst
173 
174 #endif // FST_TEST_COMPACTORS_H_
constexpr uint8_t kArcValueFlags
Definition: fst.h:453
const FstHeader * header
Definition: fst.h:74
uint64_t Properties() const
Definition: compactors.h:68
typename A::Weight Weight
Definition: compactors.h:50
bool Compatible(const Fst< A > &fst) const
Definition: compactors.h:70
StateId GetStateId() const
Definition: compactors.h:120
Label ilabel
Definition: arc.h:50
constexpr uint64_t kError
Definition: properties.h:52
static TrivialCompactor * Read(std::istream &strm, FstReadOptions opts, const FstHeader &hdr)
Definition: compactors.h:157
static TrivialArcCompactor * Read(std::istream &strm)
Definition: compactors.h:80
ArcTpl< Weight > Element
Definition: compactors.h:54
void SetState(StateId s, State *state)
Definition: compactors.h:134
std::unique_ptr< T > WrapUnique(T *ptr)
Definition: compat.h:132
bool IsCompatible(const Fst< Arc > &fst) const
Definition: compactors.h:137
size_t CountArcs(const F &fst)
Definition: expanded-fst.h:203
Arc Expand(StateId s, const Element &e, uint32_t f=kArcValueFlags) const
Definition: compactors.h:62
constexpr int kNoStateId
Definition: fst.h:196
typename Arc::StateId StateId
Definition: compactors.h:91
Arc GetArc(size_t i, uint32_t f) const
Definition: compactors.h:123
State(const TrivialCompactor *c, StateId s)
Definition: compactors.h:116
StateId NumStates() const
Definition: compactors.h:109
typename A::StateId StateId
Definition: compactors.h:49
std::ostream & WriteType(std::ostream &strm, const T t)
Definition: util.h:228
Element Compact(StateId s, const A &arc) const
Definition: compactors.h:58
Label olabel
Definition: arc.h:51
Weight weight
Definition: arc.h:52
TrivialCompactor(const TrivialCompactor &compactor)
Definition: compactors.h:105
typename Arc::Weight Weight
Definition: compactors.h:92
bool Write(std::ostream &strm) const
Definition: compactors.h:78
bool Write(std::ostream &strm, const FstWriteOptions &opts) const
Definition: compactors.h:151
Arc::StateId CountStates(const Fst< Arc > &fst)
Definition: expanded-fst.h:179
StateId Start() const
Definition: compactors.h:108
typename A::Label Label
Definition: compactors.h:48
static const std::string & Type()
Definition: compactors.h:72
uint64_t Properties(uint64_t props) const
Definition: compactors.h:141
StateId nextstate
Definition: arc.h:53
std::istream & ReadType(std::istream &strm, T *t)
Definition: util.h:80
size_t NumArcs() const
Definition: compactors.h:110
TrivialCompactor(const Fst< Arc > &fst, std::shared_ptr< TrivialCompactor >=nullptr)
Definition: compactors.h:99
static bool WriteFst(const FST &fst, std::ostream &strm, const FstWriteOptions &opts)
Definition: vector-fst.h:632
ssize_t Size() const
Definition: compactors.h:66
bool Error() const
Definition: compactors.h:149
static const std::string & Type()
Definition: compactors.h:143