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